书写效果Javascript

书写效果Javascript,javascript,jquery,html,canvas,Javascript,Jquery,Html,Canvas,我正在使用此页()的代码。给定一个字符串,它会创建一个效果,一个字母一个字母地写入字符串 这是我正在使用的代码: function setup(txt, fill){ // get 2D context $('body').append("<canvas id = 'newWord' width='1000' height=800></canvas>"); $("#newWord").css("position","ab

我正在使用此页()的代码。给定一个字符串,它会创建一个效果,一个字母一个字母地写入字符串

这是我正在使用的代码:

function setup(txt, fill){
          // get 2D context
        $('body').append("<canvas id = 'newWord' width='1000' height=800></canvas>");
        $("#newWord").css("position","absolute");
        $("#newWord").css("top","300px");
        $("#newWord").css("left","700px");
        $("#newWord").css("overflow","visible");
        var ctx = document.querySelector("#newWord").getContext("2d"),

          // dash-length for off-range
            dashLen = 200,

        // we'll update this, initialize
            dashOffset = dashLen,

        // some arbitrary speed
            speed = 20,

        // the text we will draw
            //txt = "TEST",

        // start position for x and iterator
        x = 50, i = 0,z = 0; w = 0;

         // Comic Sans?? Let's make it useful for something ;) w/ fallbacks
        ctx.font = "80pt Impact"; 

      // thickness of the line
        ctx.lineWidth = 10; 


      // to avoid spikes we can join each line with a round joint
      //ctx.lineJoin = "round";

      // increase realism letting background (f.ex. paper) show through
      ctx.globalAlpha = 1;

      // some color, lets use a black pencil
        ctx.strokeStyle = "yellow";
        ctx.fillStyle = fill;

        (function loop() {
          // clear canvas for each frame
          ctx.clearRect(x, 0, '100%', '100%');

          // calculate and set current line-dash for this char
          ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]);

          // reduce length of off-dash
          dashOffset -= speed;

          // draw char to canvas with current dash-length
          ctx.strokeText(txt[i], x, 90);

          // char done? no, the loop
          if (dashOffset > 0) requestAnimationFrame(loop);

          else {

            // ok, outline done, lets fill its interior before next
            ctx.fillText(txt[i], x, 90);

            // reset line-dash length
            dashOffset = dashLen;

            // get x position to next char by measuring what we have drawn
            // notice we offset it a little by random to increase realism
            x += ctx.measureText(txt[i++]).width + ctx.lineWidth * Math.random();

            // lets use an absolute transform to randomize y-position a little
            ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random());

            // and just cause we can, rotate it a little too to make it even
            // more realistic
            ctx.rotate(Math.random() * 0.005);

            // if we still have chars left, loop animation again for this char
            if (i < txt.length) requestAnimationFrame(loop);
          }
        })();  // just to self-invoke the loop
};
功能设置(txt,填充){
//获取二维上下文
$('body')。追加(“”);
$(“#newWord”).css(“位置”、“绝对”);
$(“#newWord”).css(“top”,“300px”);
$(“#newWord”).css(“左”,“700px”);
$(“#newWord”).css(“溢出”、“可见”);
var ctx=document.querySelector(“#newWord”).getContext(“2d”),
//偏离范围的短划线长度
达什伦=200,
//我们会更新这个,初始化
dashOffset=dashLen,
//任意速度
速度=20,
//我们将要画的文本
//txt=“测试”,
//x和迭代器的起始位置
x=50,i=0,z=0;w=0;
//漫画无??让我们做点有用的事吧;)有退路
ctx.font=“80pt冲击”;
//线的厚度
ctx.线宽=10;
//为了避免尖刺,我们可以用一个圆接头连接每条线
//ctx.lineJoin=“圆形”;
//增加真实感,让背景(例如纸张)显示出来
ctx.globalAlpha=1;
//一些颜色,让我们用黑色铅笔
ctx.strokeStyle=“黄色”;
ctx.fillStyle=填充;
(函数循环(){
//清除每个帧的画布
clearRect(x,0,'100%,'100%);
//计算并设置此字符的当前划线
ctx.setLineDash([dashLen-dashOffset,dashOffset-speed]);
//缩短短划线的长度
dashOffset-=速度;
//使用当前划线长度将字符绘制到画布
strokeText(txt[i],x,90);
//完成了吗?不,循环
如果(dashOffset>0)请求动画帧(循环);
否则{
//好的,轮廓画好了,让我们在下一步之前填充它的内部
ctx.fillText(txt[i],x,90);
//重置线条长度
dashOffset=dashLen;
//通过测量我们所画的,得到下一个字符的x位置
//请注意,我们通过随机偏移它来增加真实感
x+=ctx.measureText(txt[i++]).width+ctx.lineWidth*Math.random();
//让我们使用绝对变换稍微随机化y位置
setTransform(1,0,0,1,0,3*Math.random());
//因为我们可以,把它旋转一点,使它均匀
//更现实
ctx.rotate(Math.random()*0.005);
//如果我们仍然有剩余的字符,则再次循环该字符的动画
如果(i
这很有效。但是我需要同时写这些信。我可以修改什么

谢谢

试试这段代码

function setup(txt, fill){
      // get 2D context
    $('body').append("<canvas id = 'newWord' width='1000' height=800></canvas>");
    $("#newWord").css("position","absolute");
    $("#newWord").css("top","300px");
    $("#newWord").css("left","700px");
    $("#newWord").css("overflow","visible");
    var ctx = document.querySelector("#newWord").getContext("2d"),

      // dash-length for off-range
        dashLen = 200,

    // we'll update this, initialize
        dashOffset = dashLen,

    // some arbitrary speed
        speed = 20,

    // the text we will draw
        //txt = "TEST",

    // start position for x and iterator
    x = 50, i = 0,z = 0; w = 0;

     // Comic Sans?? Let's make it useful for something ;) w/ fallbacks
    ctx.font = "80pt Impact"; 

  // thickness of the line
    ctx.lineWidth = 10; 


  // to avoid spikes we can join each line with a round joint
  //ctx.lineJoin = "round";

  // increase realism letting background (f.ex. paper) show through
  ctx.globalAlpha = 1;

  // some color, lets use a black pencil
    ctx.strokeStyle = "yellow";
    ctx.fillStyle = fill;

    (function loop() {
      // clear canvas for each frame
      ctx.clearRect(x, 0, '100%', '100%');

      // calculate and set current line-dash for this char
      ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]);

      // reduce length of off-dash
      dashOffset -= speed;

      // draw char to canvas with current dash-length
      ctx.strokeText(txt, x, 90);

      // char done? no, the loop
      if (dashOffset > 0) requestAnimationFrame(loop);

      else {

        // ok, outline done, lets fill its interior before next
        ctx.fillText(txt, x, 90);

        // reset line-dash length
        dashOffset = dashLen;

        // get x position to next char by measuring what we have drawn
        // notice we offset it a little by random to increase realism
        x += ctx.measureText(txt).width + ctx.lineWidth * Math.random();

        // lets use an absolrequestAnimationFrameute transform to randomize y-position a little
        ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random());

        // and just cause we can, rotate it a little too to make it even
        // more realistic
        ctx.rotate(Math.random() * 0.005);

        // if we still have chars left, loop animation again for this char
        if (i < 0){i++; requestAnimationFrame(loop);}
      }
    })();  // just to self-invoke the loop
 };
功能设置(txt,填充){
//获取二维上下文
$('body')。追加(“”);
$(“#newWord”).css(“位置”、“绝对”);
$(“#newWord”).css(“top”,“300px”);
$(“#newWord”).css(“左”,“700px”);
$(“#newWord”).css(“溢出”、“可见”);
var ctx=document.querySelector(“#newWord”).getContext(“2d”),
//偏离范围的短划线长度
达什伦=200,
//我们会更新这个,初始化
dashOffset=dashLen,
//任意速度
速度=20,
//我们将要画的文本
//txt=“测试”,
//x和迭代器的起始位置
x=50,i=0,z=0;w=0;
//漫画无??让我们做点有用的事吧;)有退路
ctx.font=“80pt冲击”;
//线的厚度
ctx.线宽=10;
//为了避免尖刺,我们可以用一个圆接头连接每条线
//ctx.lineJoin=“圆形”;
//增加真实感,让背景(例如纸张)显示出来
ctx.globalAlpha=1;
//一些颜色,让我们用黑色铅笔
ctx.strokeStyle=“黄色”;
ctx.fillStyle=填充;
(函数循环(){
//清除每个帧的画布
clearRect(x,0,'100%,'100%);
//计算并设置此字符的当前划线
ctx.setLineDash([dashLen-dashOffset,dashOffset-speed]);
//缩短短划线的长度
dashOffset-=速度;
//使用当前划线长度将字符绘制到画布
strokeText(txt,x,90);
//完成了吗?不,循环
如果(dashOffset>0)请求动画帧(循环);
否则{
//好的,轮廓画好了,让我们在下一步之前填充它的内部
ctx.fillText(txt,x,90);
//重置线条长度
dashOffset=dashLen;
//通过测量我们所画的,得到下一个字符的x位置
//请注意,我们通过随机偏移它来增加真实感
x+=ctx.measureText(txt).width+ctx.lineWidth*Math.random();
//让我们使用AbsoleRequestAnimationFrameUTE变换稍微随机化y位置
setTransform(1,0,0,1,0,3*Math.random());
//因为我们可以,把它旋转一点,使它均匀
//更现实
ctx.rotate(Math.random()*0.005);
//如果我们仍然有剩余的字符,则再次循环该字符的动画
如果(i<0){i++;请求动画帧(循环);}
}
})();//只是自我调用循环
};
试试这段代码

function setup(txt, fill){
      // get 2D context
    $('body').append("<canvas id = 'newWord' width='1000' height=800></canvas>");
    $("#newWord").css("position","absolute");
    $("#newWord").css("top","300px");
    $("#newWord").css("left","700px");
    $("#newWord").css("overflow","visible");
    var ctx = document.querySelector("#newWord").getContext("2d"),

      // dash-length for off-range
        dashLen = 200,

    // we'll update this, initialize
        dashOffset = dashLen,

    // some arbitrary speed
        speed = 20,

    // the text we will draw
        //txt = "TEST",

    // start position for x and iterator
    x = 50, i = 0,z = 0; w = 0;

     // Comic Sans?? Let's make it useful for something ;) w/ fallbacks
    ctx.font = "80pt Impact"; 

  // thickness of the line
    ctx.lineWidth = 10; 


  // to avoid spikes we can join each line with a round joint
  //ctx.lineJoin = "round";

  // increase realism letting background (f.ex. paper) show through
  ctx.globalAlpha = 1;

  // some color, lets use a black pencil
    ctx.strokeStyle = "yellow";
    ctx.fillStyle = fill;

    (function loop() {
      // clear canvas for each frame
      ctx.clearRect(x, 0, '100%', '100%');

      // calculate and set current line-dash for this char
      ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]);

      // reduce length of off-dash
      dashOffset -= speed;

      // draw char to canvas with current dash-length
      ctx.strokeText(txt, x, 90);

      // char done? no, the loop
      if (dashOffset > 0) requestAnimationFrame(loop);

      else {

        // ok, outline done, lets fill its interior before next
        ctx.fillText(txt, x, 90);

        // reset line-dash length
        dashOffset = dashLen;

        // get x position to next char by measuring what we have drawn
        // notice we offset it a little by random to increase realism
        x += ctx.measureText(txt).width + ctx.lineWidth * Math.random();

        // lets use an absolrequestAnimationFrameute transform to randomize y-position a little
        ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random());

        // and just cause we can, rotate it a little too to make it even
        // more realistic
        ctx.rotate(Math.random() * 0.005);

        // if we still have chars left, loop animation again for this char
        if (i < 0){i++; requestAnimationFrame(loop);}
      }
    })();  // just to self-invoke the loop
 };
功能设置(txt,填充){
//获取二维上下文
$('body')。追加(“”);
$(“#newWord”).css(“位置”、“绝对”);
$(“#newWord”).css(“top”,“300px”);
$(“#newWord”).css(“左”,“700px”);
$(“#newWord”).css(“溢出”、“可见”);
var ctx=document.querySelector(“#newWord”).getContext(“2d”),
//偏离范围的短划线长度
达什伦=200,
//我们会更新这个,初始化
dashOffset=dashLen,
//任意速度
速度=20,
//我们将要画的文本
//txt=“测试”,
//x和迭代器的起始位置
x=50,i=0,z=0;w=0;
//喜剧演员?让我们来做一个