0

I am quite new in web dev. I want animate text on JavaScript, after a defined event. Found am example animating a line drawing, but the method does not work on arcs (how to animate drawing lines on canvas example: http://jsfiddle.net/m1erickson/7faRQ/. Scrolled through and found a way (http://jsfiddle.net/loktar/uhVj6/4/), but only to extent of dozen letters. After a number of lines animated the inaccuracy escalate, yet the animation shortens and I can't find a solution to control these factors. Lets say the letter 'P' would be:

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)
         });
     }
 }

Any error to point out or other more convenient method to create a set of uniform alphabets, and animate a text on function calling? Where I can insert text and it compile the function, eg.

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

onload.draw(Thanks for your help!);

Shows an animation on canvas drawing 'Thanks for your help!'

EDIT: the visual output is seen in the following pictures(single letter/four lines vs. dozen letters/fifty lines):

enter image description here

enter image description here

1 Answers1

0

Actually there is a solution here in comments: How can I animate the drawing of text on a web page? giving one good approach. But looking more into creating the symbols and animating the geometric trajectory.

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>