canvas绘制字体动画特效

<!doctype HTML>  
<html>  
    <meta charset="utf8"/>  
    <title>canvas test</title>  
    <meta name="description" content="">  
    <meta name="HandheldFriendly" content="True">  
    <meta name="MobileOptimized" content="320">  
    <meta name="format-detection" content="telephone=no">  
    <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1, maximum-scale=1, user-scalable=no" />  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
  
    <body>  
        <canvas id="first">不支持</canvas>  
        <canvas id="second">不支持</canvas>  
        <script>  
            var canvas = document.getElementById('first'),  
            canvas2 = document.getElementById('second');  
  
            canvas.width = 400;  
            canvas.height = 200;  
  
            canvas2.width = 400;  
            canvas2.height = 200;  
  
            var ctx = canvas.getContext("2d"),  
                ctx2 = canvas2.getContext("2d");  
  
            //order maters a lot, it's related to the measureText  
  
            ctx.font = ctx2.font = "normal bold 2em sans-serif";  
  
            ctx.fillStyle = ctx2.fillStyle= "#c01c1d";  
  
            var message = "HelloWorld";  
  
            var metrics = ctx.measureText(message);  
  
            console.log(metrics);  
  
            var xPosition = (canvas.width - metrics.width) / 2;  
  
            console.log(xPosition);  
  
            var styles = ["#f2d","#ff2","#2f3","#2f7","#fff","#333","#aaa","#eee","#3e3","#bbb"];  
  
  
            var yPosition = canvas.height /2 ;  
  
            //setInterval(function () {  
            // requestAnimationFrame(function () {  
            //  var idx = Math.floor(Math.random() * 10);  
  
            //  ctx.fillStyle = styles[idx];  
  
            //  ctx.fillText(message,xPosition,yPosition);  
            // });  
            //},16)  
  
            ctx.fillText(message,10,yPosition);  
  
            ctx2.fillText(message,10,yPosition);  
              
            var text = [],marginLeft = 10,_m,_obj,_sepWidthSum = 0;  
  
            for(var i = 0 ; i < message.length; i++){  
                //_m = ctx.measureText(message[i]);  
  
                _m = ctx.measureText(message.substr(0,i));  
  
                //ctx.fillText(message[i],marginLeft,yPosition);  
  
                _obj = {  
                    text : message[i],  
                    marginLeft : marginLeft + _m.width  
                }  
  
                //marginLeft += _m.width;  
                //_sepWidthSum += _m.width;   
  
                text.push(_obj);  
  
            }  
  
            //console.log(metrics.width - _sepWidthSum);  
  
              
  
            // var index = 0;  
  
            // function draw () {  
  
            //  setTimeout(function () {  
                      
            //      if(index  < text.length){  
            //          ctx.fillStyle = "#333";  
            //          ctx.fillText(text[index].text,text[index].marginLeft,yPosition);  
  
            //          if(index){  
            //              ctx.fillStyle = "#f2d";  
            //              ctx.fillText(text[index - 1].text,text[index -1].marginLeft,yPosition);  
            //          }  
                          
            //          index++;  
            //          if(index = text.length){  
            //              // setTimeout(function () {  
            //              // ctx.fillStyle = "#f2d";  
            //              // ctx.fillText(text[index - 1].text,text[index -1].marginLeft,yPosition);  
            //              // },30);  
            //              // index = 0;  
            //              // draw();  
            //          }else{  
            //              draw();  
            //          }  
            //          //draw();  
            //      }  
            //  },30);  
  
  
            // }  
  
            var TIMER = 70;  
  
            function draw(index) {  
  
                setTimeout(function (index) {  
                    return function () {  
  
                        //console.log(index);  
  
                        if(index  < text.length){  
                            //requestAnimationFrame(function () {  
                                ctx.fillStyle = "#ffac00";  
                                ctx.fillText(text[index].text,text[index].marginLeft,yPosition);  
                            //});  
                              
  
                            if(index){  
                                //requestAnimationFrame(function() {  
                                    ctx.fillStyle = "#fff";  
                                    ctx.fillText(text[index - 1].text,text[index -1].marginLeft,yPosition);  
                                    ctx.fillStyle = "#c01c1d";  
                                    ctx.fillText(text[index - 1].text,text[index -1].marginLeft,yPosition);  
  
                                //});  
                            }else{  
                                //requestAnimationFrame(function () {  
                                    // ctx.fillStyle = "#fff";  
                                    // ctx.fillText(text[text.length - 1].text,text[text.length - 1].marginLeft,yPosition);  
                                    // ctx.fillStyle = "#c01c1d";  
                                    // ctx.fillText(text[text.length - 1].text,text[text.length - 1].marginLeft,yPosition);  
                                //});  
  
                            }  
                              
                            index++;  
                            if(index == text.length){  
                                setTimeout(function () {  
                                    ctx.fillStyle = "#fff";  
                                    ctx.fillText(text[text.length - 1].text,text[text.length - 1].marginLeft,yPosition);  
                                    ctx.fillStyle = "#c01c1d";  
                                    ctx.fillText(text[text.length - 1].text,text[text.length - 1].marginLeft,yPosition);  
                                },TIMER);  
  
                                setTimeout(function () {  
                                    draw(0);  
                                },1000);  
                                  
                                // setTimeout(function () {  
                                // ctx.fillStyle = "#f2d";  
                                // ctx.fillText(text[index - 1].text,text[index -1].marginLeft,yPosition);  
                                // },30);  
                                // index = 0;  
                                // draw();  
                            }else{  
                                draw(index);  
                            }  
                            //draw();  
                        }  
                    }  
                }(index),TIMER - Math.floor(Math.sin(index) * 20));  
  
  
            }  
  
            draw(0);  
  
            // setInterval(function () {  
            //  //清空  
            //  index = 0;  
            //  draw();  
            // },400);  
  
  
  
            //ctx.fillText(text[2].text,text[2].marginLeft,yPosition);  
  
  
  
  
  
              
              
  
  
  
        </script>  
    </body>  
  
</html>  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值