canvas艺术字-扩展-闪烁效果

本文介绍了如何使用HTML5 Canvas创建艺术字并实现闪烁效果。通过结合路径绘制、颜色变化和定时器,使得文字在页面上产生动态的闪烁动画,为网页增添视觉吸引力。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>canvas艺术字-扩展</title>
    <style>
        html{height: 100%;overflow: hidden;}
        body{height: 100%;margin: 0;}
        #test{
            background: #000;
        }
    </style>
</head>
<body>
    <canvas id="test"></canvas>
    <script>
        const cvs=document.getElementById('test');
        //canvas充满窗口
        cvs.width=window.innerWidth;
        cvs.height=window.innerHeight;
        //画笔
        const  ctx=cvs.getContext('2d');
    
        //颜色数组
        const colors=['red','yellow'];
    
        /*文字内容*/
        const text='学习最高尚';
    
        /*文字位置*/
        const [x,y]=[600,600];
    
        //字体属性
        ctx.font='bold 220px arial';
    
        function draw(){
            //保存上下文对象的状态
            ctx.save();
            //设置描边样式
            ctx.strokeStyle=colors[0];
            //设置描边宽度
            ctx.lineWidth=3;
            //虚线
            ctx.setLineDash([8]);
            //以描边的方式显示路径
            ctx.strokeText(text,x,y);
            //第二部分虚线
            ctx.lineDashOffset=8;
            ctx.strokeStyle=colors[1];
            //光晕
            ctx.shadowColor='orange';
            //多画几遍光晕
            for(let i=25;i>3;i-=2){
                ctx.shadowBlur=i;
                ctx.strokeText(text,x,y);
            }
            //将上下文对象的状态恢复到上一次保存时的状态
            ctx.restore();
        }
    
        draw();
    
        setInterval(function(){
            ctx.clearRect(0,0,cvs.width,cvs.height);
            draw();
            colors.reverse();
        },500)
    </script>   
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值