gpt4 book ai didi

javascript - Canvas 标签拱形文本

转载 作者:行者123 更新时间:2023-11-28 02:28:08 24 4
gpt4 key购买 nike

我正在编写一些代码,这些代码沿着 Canvas 上的弧线弯曲文本,我的顶部工作得尽可能接近我需要的位置,但我还需要在底部添加一些向上弯曲的文本。我无法解决。非常感谢任何帮助。

另外,这里是我正在制作的 fiddle 的链接:here

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

最佳答案

好的,我成功了。有两个非常小的变化。

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

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

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

关于javascript - Canvas 标签拱形文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com