【问题标题】:Javascript draw text animationJavascript绘制文本动画
【发布时间】:2020-04-16 17:24:47
【问题描述】:

我是网络开发的新手。在定义的事件之后,我想要在 JavaScript 上为文本设置动画。 找到了一个绘制线条动画的示例,但该方法不适用于弧(how to animate drawing lines on canvas 示例:http://jsfiddle.net/m1erickson/7faRQ/. 滚动并找到了一个方法(http://jsfiddle.net/loktar/uhVj6/4/),但只有十几个字母。在多行动画后,不准确性会升级,但动画会缩短,我找不到控制这些因素的解决方案。假设字母“P”是:

var l = 10 //factor of scale

function line010(current) {
     context.beginPath();
     context.moveTo(x-5*l, y-3/2*l);
     context.lineTo(x-5*l, y-3/2*l-4*l*current);     
     context.stroke();
     curPerc++;
     if (curPerc < endPerc) {
         requestAnimationFrame(function () {
             line010(curPerc / 100)
         });
     }
 }
 function line011(current) {
     context.beginPath();
     context.moveTo(x-5*l, y-11/2*l);
     context.lineTo(x-5*l+3/2*l*current, y-11/2*l);     
     context.stroke();
     curPerc++;
     if (curPerc < endPerc) {
         requestAnimationFrame(function () {
             line011(curPerc / 100)
         });
     }
 } 
 function arc012(current) {
     context.beginPath();
     context.arc(x-7/2*l, y-9/2*l, l, -1/2*Math.PI, Math.PI*current-1/2*Math.PI);
     context.stroke();
     curPerc++;
     if (curPerc < endPerc) {
         requestAnimationFrame(function () {
             arc012(curPerc / 100)
         });
     }
 }
  function line013(current) {
     context.beginPath();
     context.moveTo(x-7/2*l, y-7/2*l);
     context.lineTo(x-7/2*l-l*current, y-7/2*l);     
     context.stroke();
     curPerc++;
     if (curPerc < endPerc) {
         requestAnimationFrame(function () {
             line013(curPerc / 100)
         });
     }
 }

要指出的任何错误或其他更方便的方法来创建一组统一的字母,并在函数调用时为文本设置动画?我可以在其中插入文本并编译函数, 例如。

function draw("insertText")
{
A();
B();
C();
-
-
-
!();
&();
-
-
-
}(1000);

onload.draw(Thanks for your help!);

在画布上显示动画“感谢您的帮助!”

编辑:视觉输出如下图所示(单个字母/四行与十几个字母/五十行):

enter image description here

enter image description here

【问题讨论】:

    标签: javascript html animation canvas drawing


    【解决方案1】:

    实际上在 cmets 中有一个解决方案:How can I animate the drawing of text on a web page? 给出了一个很好的方法。但是更多地关注创建符号和动画几何轨迹。

    http://jsfiddle.net/8hwzqnrc/9/

    <canvas width=630>
        <div class="txtStyle">STROKE-ON CANVAS</div>
    </canvas>
    
    <script>
    var ctx = document.querySelector("canvas").getContext("2d")
        dashLen = 220, dashOffset = dashLen, speed = 10,
        txt = "STROKE-ON CANVAS", x = 30, i = 0;
    
    ctx.font = "50px Comic Sans MS, cursive, TSCu_Comic, sans-serif"; 
    ctx.lineWidth = 5; ctx.lineJoin = "round"; ctx.globalAlpha = 2/3;
    ctx.strokeStyle = ctx.fillStyle = "#1f2f90";
    
    (function loop() {
      ctx.clearRect(x, 0, 60, 150);
      ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]); // create a long dash mask
      dashOffset -= speed;                                         // reduce dash length
      ctx.strokeText(txt[i], x, 90);                               // stroke letter
    
      if (dashOffset > 0) requestAnimationFrame(loop);             // animate
      else {
        ctx.fillText(txt[i], x, 90);                               // fill final letter
        dashOffset = dashLen;                                      // prep next char
        x += ctx.measureText(txt[i++]).width + ctx.lineWidth * Math.random();
        ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random());        // random y-delta
        ctx.rotate(Math.random() * 0.005);                         // random rotation
        if (i < txt.length) requestAnimationFrame(loop);
      }
    })();
    </script>
    

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 2015-04-25
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2015-12-14
      • 2018-08-11
      相关资源
      最近更新 更多