Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文本旋转HTML5画布_Javascript_Html_Canvas_Html5 Canvas - Fatal编程技术网

Javascript 文本旋转HTML5画布

Javascript 文本旋转HTML5画布,javascript,html,canvas,html5-canvas,Javascript,Html,Canvas,Html5 Canvas,我想在画布上添加一些文本,并像风车一样将文本旋转成一个圆圈 你会怎么做。我只想要一个连续的循环,文本永远不会停止旋转 function showCircularNameRotating(string, startAngle, endAngle){ //context.clearRect(0, 0, canvas.width, canvas.height); circle = { x: canvas.width/2, y: canvas.height/2, radius:

我想在画布上添加一些文本,并像风车一样将文本旋转成一个圆圈

你会怎么做。我只想要一个连续的循环,文本永远不会停止旋转

function showCircularNameRotating(string, startAngle, endAngle){
//context.clearRect(0, 0, canvas.width, canvas.height);
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 = stringcharAt(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(charater, 0,0);
context.strokeText(character,0,0);
angle -= angleDecrement;
index++;
context.restore();
}
context.restore();

}

drawCircularText("Clockwise", Math.PI*2, Math.PI/8);
函数showCircularNameRotating(字符串、星形缠结、端角){
//clearRect(0,0,canvas.width,canvas.height);
圆圈={
x:canvas.width/2,
y:canvas.height/2,
半径:200
};
var半径=圆半径,
角度减量=(startAngle-endAngle)/string.length-1),
角度=浮点数(startAngle),
指数=0,
性格
context.save();

而(index旋转文本的简单代码可能如下所示-根据需要采用:

var ctx = canvas.getContext('2d'),
    w = canvas.width,
    h = canvas.height,
    cx = w * 0.5,                  // center of canvas
    cy = h * 0.5,
    angleStep = 0.1,
    txt = 'PINWHEEL TEXT';

ctx.font = 'bold 30px sans-serif'; // set font
ctx.textAlign = 'center';          // align text in center
ctx.textBaseline = 'middle';

(function rotate() {
    ctx.clearRect(0, 0, w, h);     // clear canvas
    ctx.translate(cx, cy);         // translate to center of rotation
    ctx.rotate(angleStep);         // rotate (accumulative)
    ctx.translate(-cx, -cy);       // translate back
    ctx.fillText(txt, cx, cy);     // draw text at rotated center

    requestAnimationFrame(rotate); // loop
})();
更新

它的通用函数(将画布和上下文保持在外部):

现在就打电话:

rotateText(ctx, 'MyText', '32px myfont', 0.1);


是否可以将此代码放在另一个函数中?@user3380698当然可以,只需将大部分代码包装在一个调用中,这样您就可以设置示例步骤。画布和上下文也不需要在其中。我用一个示例更新了答案(未测试).酷!现在开始制作你提供的动画。你如何制作文本并将其环绕一个圆圈,然后制作动画使其环绕一个圆圈移动。我不知道该怎么做this@user3380698这里有一个解决方案:)您在这里发布了大量代码。您能告诉我们您尝试的代码出了什么问题吗?
rotateText(ctx, 'MyText', '32px myfont', 0.1);
rotateText(ctx, 'MyText', '32px myfont', 0.1, centerX, centerY);