Javascript 使用 html5 在画布内移动文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11450030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:51:22  来源:igfitidea点击:

Moving text inside canvas using html5

javascripthtmlcanvas

提问by Victor

I am new to html5 development could anyone tell me how to make text to move one side to other horizontally inside canvas..

我是 html5 开发的新手,谁能告诉我如何使文本在画布内水平移动一侧到另一侧..

回答by Garrett Vlieger

Here's an example of how to animate text back and forth across the screen:

下面是如何在屏幕上来回设置文本动画的示例:

<html>
   <head>
   <title>HTML 5 Animated Text</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        var context;
        var text = "";
        var textDirection ="";

        $(function()
        {
            context = document.getElementById("cvs").getContext("2d");            
            setInterval("animate()", 30);

            textDirection ="right";
            textXpos = 5;
            text = "Animation!";    
        });  

        function animate() {            
            // Clear screen
            context.clearRect(0, 0, 500, 500);
            context.globalAlpha = 1;
            context.fillStyle = '#fff';
            context.fillRect(0, 0, 500, 500);    

            var metrics = context.measureText(text);
            var textWidth = metrics.width;

            if (textDirection == "right") {
                textXpos += 10;

                if (textXpos > 500 - textWidth) {
                    textDirection = "left";
                }
            }
            else {
                textXpos -= 10;

                if (textXpos < 10) {
                    textDirection = "right";
                }                    
            }

            context.font = '20px _sans';
            context.fillStyle = '#FF0000';
            context.textBaseline = 'top';
            context.fillText  ( text, textXpos, 180);    
          }    
          </script>
      </head>
      <body> 
         <div id="page">
            <canvas id="cvs" width="500" height="500">
               Your browser does not support the HTML 5 Canvas. 
            </canvas>
         </div>
      </body>
   </html>

In action: http://jsfiddle.net/bS79G/

在行动:http: //jsfiddle.net/bS79G/

回答by Hirendra Sisodiya

You can also use the canvas store(), translate() and restore() methods to animate the text. suppose if you want to move the text from right side to left side, then you can use the following code snippet:

您还可以使用画布 store()、translate() 和 restore() 方法为文本设置动画。假设如果要将文本从右侧移动到左侧,则可以使用以下代码片段:

Refer site: http://www.authorcode.com/text-animation-in-html5/

参考网站:http: //www.authorcode.com/text-animation-in-html5/

var can, ctx, step, steps = 0, delay = 20;

var can,ctx,step,steps = 0,delay = 20;

    function init() {
        can = document.getElementById("MyCanvas1");
        ctx = can.getContext("2d");
        ctx.fillStyle = "blue";
        ctx.font = "20pt Verdana";
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        step = 0;
        steps = can.height + 50;
        RunTextRightToLeft();
    }

    function RunTextRightToLeft() {
        step++;
        ctx.clearRect(0, 0, can.width, can.height);
        ctx.save();
        ctx.translate(can.width / 2, step);
        ctx.fillText("Welcome", 0, 0);
        ctx.restore();
        if (step == steps)
            step = 0;
        if (step < steps)
            var t = setTimeout('RunTextRightToLeft()', delay);
    }