首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript绘制文本动画

Javascript绘制文本动画
EN

Stack Overflow用户
提问于 2020-04-16 15:29:58
回答 1查看 115关注 0票数 0

我在web dev上是个新手。我想在JavaScript上的动画文本,在一个已定义的事件。我发现了一个绘制线条的动画示例,但该方法不适用于圆弧(示例:http://jsfiddle.net/m1erickson/7faRQ/。滚动浏览并找到了一种方法(http://jsfiddle.net/loktar/uhVj6/4/),但仅限于十几个字母。在一些线条动画之后,不精确度会升级,但动画会变短,我找不到一个解决方案来控制这些因素。假设字母'P‘是:

代码语言:javascript
复制
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)
         });
     }
 }

有什么错误要指出吗?或者用其他更方便的方法来创建一组统一的字母表,并在函数调用时对文本进行动画处理?在这里我可以插入文本,然后它会编译函数,例如。

代码语言:javascript
复制
function draw("insertText")
{
A();
B();
C();
-
-
-
!();
&();
-
-
-
}(1000);

onload.draw(Thanks for your help!);

在画布上显示了一个动画“谢谢你的帮助!”

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

enter image description here

enter image description here

EN

回答 1

Stack Overflow用户

发布于 2020-04-16 17:24:47

实际上,在评论中有一个解决方案:How can I animate the drawing of text on a web page?给出了一个很好的方法。而是更深入地研究如何创建符号和几何轨迹的动画。

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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61244863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档