繁体   English   中英

HTML5 Canvas文字围绕圆圈动画

[英]HTML5 Canvas Text Animating around circle

更新了代码有什么问题? 我知道它不会旋转,但是为什么文本会很乱。

有谁知道我为什么要撕掉头发试图解决这个问题

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

    }

在Canvas中,不是很确定。 但是,如果可以使用SVG,它会变得微不足道吗?

是的,有可能。

这是一个可以建立的简单方法(我现在就这样做了,因此可以肯定地可以通过各种方式对其进行优化和调整)。

  • 它使用两个对象,一个用于文本本身,一个用于每个字符。
  • 字符串在文本对象的构造函数中拆分为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方法:

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

如前所述,这里有足够的优化空间。 最昂贵的零件是:

  • 文本渲染(首先将文本渲染为图像)
  • 使用保存/恢复对每个字符进行本地旋转
  • 小事

我将其保留为OP的练习:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM