• <tfoot id='oInfS'></tfoot>

      <bdo id='oInfS'></bdo><ul id='oInfS'></ul>

  • <small id='oInfS'></small><noframes id='oInfS'>

    1. <legend id='oInfS'><style id='oInfS'><dir id='oInfS'><q id='oInfS'></q></dir></style></legend>
    2. <i id='oInfS'><tr id='oInfS'><dt id='oInfS'><q id='oInfS'><span id='oInfS'><b id='oInfS'><form id='oInfS'><ins id='oInfS'></ins><ul id='oInfS'></ul><sub id='oInfS'></sub></form><legend id='oInfS'></legend><bdo id='oInfS'><pre id='oInfS'><center id='oInfS'></center></pre></bdo></b><th id='oInfS'></th></span></q></dt></tr></i><div id='oInfS'><tfoot id='oInfS'></tfoot><dl id='oInfS'><fieldset id='oInfS'></fieldset></dl></div>

      1. HTML5 Canvas 文本围绕圆圈制作动画

        时间:2023-06-19

              <bdo id='qoh6P'></bdo><ul id='qoh6P'></ul>

                  <tbody id='qoh6P'></tbody>
                <legend id='qoh6P'><style id='qoh6P'><dir id='qoh6P'><q id='qoh6P'></q></dir></style></legend>
                <i id='qoh6P'><tr id='qoh6P'><dt id='qoh6P'><q id='qoh6P'><span id='qoh6P'><b id='qoh6P'><form id='qoh6P'><ins id='qoh6P'></ins><ul id='qoh6P'></ul><sub id='qoh6P'></sub></form><legend id='qoh6P'></legend><bdo id='qoh6P'><pre id='qoh6P'><center id='qoh6P'></center></pre></bdo></b><th id='qoh6P'></th></span></q></dt></tr></i><div id='qoh6P'><tfoot id='qoh6P'></tfoot><dl id='qoh6P'><fieldset id='qoh6P'></fieldset></dl></div>
                <tfoot id='qoh6P'></tfoot>

                <small id='qoh6P'></small><noframes id='qoh6P'>

                1. 本文介绍了HTML5 Canvas 文本围绕圆圈制作动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  更新了代码有什么问题?我知道它不旋转,但为什么文字是扭曲的.

                  Updated what is wrong with the code? I know it doesnt rotate but why is the text screwy.

                  有谁知道为什么我正在努力解决这个问题

                  Does anyone know why I am tearing my hair out trying to figure this out

                  function showCircularNameRotating(string, startAngle, endAngle){
                      //context.clearRect(0, 0, canvas.width, canvas.height);
                      context.font = '32pt Sans-Serif';
                      context.fillStyle = '#1826B0';
                      circle = {
                          x: canvas.width/2,
                          y: canvas.height/2,
                          radius: 200
                      };
                  
                      var radius = circle.radius,
                          angleDecrement = (startAngle - endAngle/string.length-1),
                          angle = parseFloat(startAngle),
                          index = 0,
                          character;
                  
                      context.save();
                      while(index <string.length){
                      character = string.charAt(index);
                      context.save();
                      context.beginPath();
                      context.translate(circle.x + Math.cos(angle) * radius,
                                        circle.y - Math.sin(angle) * radius);
                      context.rotate(Math.PI/2 - angle);
                  
                      context.fillText(character, 0,0);
                      context.strokeText(character,0,0);
                      angle -= angleDecrement;
                      index++;
                      context.restore();
                      }
                      context.restore();
                  
                      }
                  

                  推荐答案

                  是的,有可能.

                  这是一个简单的方法,您可以在此基础上进行构建(我现在就制作了它,因此肯定可以通过各种方式对其进行优化和调整).

                  Here is a simple approach which you can build upon (I made it right now so it can certainly be optimized and tweaked in various ways).

                  • 这使用了两个对象,一个用于文本本身,一个用于每个字符.
                  • 字符串在文本对象的构造函数中被拆分为 char 对象
                  • 画布旋转
                  • 每个字符都以圆形图案相对于彼此绘制

                  文本对象

                  function Text(ctx, cx, cy, txt, font, radius) {
                  
                      this.radius = radius;               // expose so we can alter it live
                      
                      ctx.textBaseline = 'bottom';        // use base of char for rotation
                      ctx.textAlign = 'center';           // center char around pivot
                      ctx.font = font;
                      
                      var charsSplit = txt.split(''),     // split string to chars
                          chars = [],                     // holds Char objects (see below)
                          scale = 0.01,                   // scales the space between the chars
                          step = 0.05,                    // speed in steps
                          i = 0, ch;
                      
                      for(; ch = charsSplit[i++];)       // create Char objects for each char
                          chars.push(new Char(ctx, ch));
                      
                      // render the chars
                      this.render = function() {
                          
                          var i = 0, ch, w = 0;
                          
                          ctx.translate(cx, cy);         // rotate the canvas creates the movement
                          ctx.rotate(-step);
                          ctx.translate(-cx, -cy);
                          
                          for(; ch = chars[i++];) {      // calc each char's position
                              ch.x = cx + this.radius * Math.cos(w);
                              ch.y = cy + this.radius * Math.sin(w);
                  
                              ctx.save();                // locally rotate the char
                              ctx.translate(ch.x, ch.y);
                              ctx.rotate(w + 0.5 * Math.PI);
                              ctx.translate(-ch.x, -ch.y);
                              ctx.fillText(ch.char, ch.x, ch.y);
                              ctx.restore();
                  
                              w += ch.width * scale;
                          }
                      };
                  }
                  

                  Char 对象

                  function Char(ctx, ch) {
                      this.char = ch;                    // current char
                      this.width = ctx.measureText('W').width;  // width of char or widest char
                      this.x = 0;                        // logistics
                      this.y = 0;
                  }
                  

                  现在我们要做的就是创建一个Text对象,然后循环调用render方法:

                  Now all we need to do is to create a Text object and then call the render method in a loop:

                  var text = new Text(ctx, cx, cy, 'CIRCULAR TEXT', '32px sans-serif', 170);
                  
                  (function loop() {
                      ctx.clearRect(0, 0, w, h);
                      text.render();
                      requestAnimationFrame(loop);
                  })();
                  

                  如前所述,这里有很大的优化空间.最昂贵的部分是:

                  As said, there is plenty of room for optimizations here. The most expensive parts are:

                  • 文字渲染(先将文字渲染成图片)
                  • 使用保存/恢复对每个字符进行局部旋转
                  • 小事

                  不过,我会把它作为 OP 的练习 :)

                  I'll leave that as an exercise for OP though :)

                  这篇关于HTML5 Canvas 文本围绕圆圈制作动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Javascript 根据 HTML 中的输入文本字段更改文本元素? 下一篇:用 html 绘制数据

                  相关文章

                2. <i id='APlpY'><tr id='APlpY'><dt id='APlpY'><q id='APlpY'><span id='APlpY'><b id='APlpY'><form id='APlpY'><ins id='APlpY'></ins><ul id='APlpY'></ul><sub id='APlpY'></sub></form><legend id='APlpY'></legend><bdo id='APlpY'><pre id='APlpY'><center id='APlpY'></center></pre></bdo></b><th id='APlpY'></th></span></q></dt></tr></i><div id='APlpY'><tfoot id='APlpY'></tfoot><dl id='APlpY'><fieldset id='APlpY'></fieldset></dl></div>
                  <legend id='APlpY'><style id='APlpY'><dir id='APlpY'><q id='APlpY'></q></dir></style></legend>
                  1. <tfoot id='APlpY'></tfoot>

                    1. <small id='APlpY'></small><noframes id='APlpY'>

                      • <bdo id='APlpY'></bdo><ul id='APlpY'></ul>