1

我正在编写一些在画布上沿弧线弯曲文本的代码,我让顶部尽可能接近,但我还需要在底部添加一些向上弯曲的文本。我搞不定。任何帮助是极大的赞赏。

另外,这是我正在研究的小提琴的链接:这里

var
        text = 'Hello world, Im just JS',
        len = text.length,
        // The coverage of the circle
        angle = Math.PI * .7,
        centerX = 275,
        centerY = 250,
        radius = 200,
        context = document.getElementById('canvas').getContext('2d'),
        n = 0;

    // Format the text
    context.font = '40px Arial';
    context.textAlign = 'center';
    context.fillStyle = 'black';
    context.strokeStyle = 'blue';
    context.lineWidth = 2;

    // Save the current state
    context.save();

    // Move our pointer
    context.translate(centerX, centerY);

    // Rotate
    context.rotate(-1 * angle / 2);
    context.rotate(-1 * (angle / len) / 2);

    // Loop over the string
    for(; n < len; n += 1) {
        context.rotate(angle / len);
        context.save();
        context.translate(0, -1 * radius);

        context.fillText(text[n], 0, 0);
        context.strokeText(text[n], 0, 0);

        context.restore();
    };

    // Restore the canvas state
    context.restore();
4

1 回答 1

1

好的,我设法做到了。是两个非常小的变化。

它涉及在循环中反转翻译并反转输入字符串。完美的

这是工作代码。(注意两个小变化)这里是一个链接

var
        text = 'Hello world, Im just JS'.split('').reverse().join(''),
        len = text.length,
        // The coverage of the circle
        angle = Math.PI * .7,
        centerX = 275,
        centerY = 250,
        radius = 200,
        context = document.getElementById('canvas').getContext('2d'),
        n = 0;

    // Format the text
    context.font = '40px Arial';
    context.textAlign = 'center';
    context.fillStyle = 'black';
    context.strokeStyle = 'blue';
    context.lineWidth = 2;

    // Save the current state
    context.save();

    // Move our pointer
    context.translate(centerX, centerY);

    // Rotate
    context.rotate(-1 * angle / 2);
    context.rotate(-1 * (angle / len) / 2);

    // Loop over the string
    for(; n < len; n += 1) {
        context.rotate(angle / len);
        context.save();
        context.translate(0, -(-1 * radius));

        context.fillText(text[n], 0, 0);
        context.strokeText(text[n], 0, 0);

        context.restore();
    };

    // Restore the canvas state
    context.restore();
于 2013-01-29T13:04:52.520 回答