绕三角形画布旋转圆形

3
我想在canvas中围绕三角形旋转一个圆。我有之前的代码,但是现在圆在中间,一个矩形在旋转,我希望圆也能旋转并且在中间放置一个三角形。有人可以帮忙吗?
这是我目前拥有的JS代码:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cx = 100;
var cy = 100;
var rectWidth = 15;
var rectHeight = 10;
var rotation = 0;
requestAnimationFrame(animate);

function animate() {
  requestAnimationFrame(animate);
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();
  ctx.arc(cx, cy, 10, 0, Math.PI * 2);
  ctx.closePath();
  ctx.fill();
  ctx.save();
  ctx.translate(cx, cy);
  ctx.rotate(rotation);
  ctx.strokeRect(-rectWidth / 2 + 20, -rectHeight / 2, rectWidth, rectHeight);
  ctx.restore();

  rotation += Math.PI / 180;
}
<canvas id="canvas"></canvas>

3个回答

3

我已经编辑了你的代码,绘制了所需的形状,并添加了注释来描述下面片段中正在进行的操作。

var canvas = document.body.appendChild(document.createElement("canvas"));
var ctx = canvas.getContext("2d");
var cx = 100;
var cy = 100;
var rotation = 0;
requestAnimationFrame(animate);
function animate() {
    //Clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    //Draw center figure
    /*
    ctx.beginPath();
    ctx.arc(cx, cy, 10, 0, Math.PI * 2);
    ctx.closePath();
    ctx.fill();
    */
    ctx.beginPath();
    ctx.moveTo(cx - 10, cy - 10);
    ctx.lineTo(cx, cy + 10);
    ctx.lineTo(cx + 10, cy - 10);
    ctx.closePath();
    ctx.fill();
    //Rotate canvas
    ctx.save();
    ctx.translate(cx, cy);
    ctx.rotate(rotation);
    //Draw rotating object
    /*ctx.strokeRect(-rectWidth / 2 + 20, -rectHeight / 2, rectWidth, rectHeight);*/
    ctx.beginPath();
    ctx.arc(20, 0, 5, 0, Math.PI * 2);
    ctx.closePath();
    ctx.fill();
    //Rotate canvas back
    ctx.restore();
    //Save rotation
    rotation += Math.PI / 180;
    //Request next frame
    requestAnimationFrame(animate);
}

看起来您在HTML Canvas操作方面缺乏经验,因此我想推荐一些MDN官方Canvas教程

如果您有更多问题,请随时在未来开启具有更多代码特定问题的新问题。


2

这里有一种不使用ctx.translatectx.rotate移动对象的替代方法。

我们可以使用Math.sinMath.cos来在圆形或椭圆形运动中移动。
一旦你理解了这种方法,你就为许多可能性打开了大门,例如你可以使旋转相对于其他对象。

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var rotation = 0;
setInterval(animate, 10);

function animate(rx, ry, speed) { 
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  draw(120, 80, 1)
  draw(240, 80, 10/3)
}

function draw(rx, ry, speed) {    
  var x = Math.cos(rotation) * 50 + rx
  var y = Math.sin(rotation) * 50 + ry
  
  ctx.beginPath()
  ctx.arc(x, y, 20, 0, Math.PI * 2);
  ctx.stroke();
  
  for (var i = 1; i < 8; i++) {
    x += Math.sin(rotation * i/speed) * 20
    y += Math.cos(rotation * i/speed) * 20/i
    ctx.beginPath()
    ctx.arc(x, y, 8/i, 0, Math.PI * 2);
    ctx.stroke();
  }
  rotation += Math.PI / 180;
}
<canvas id="canvas" height=170 width=400></canvas>


是的,对于以圆形方式移动对象,三角函数可能正是所需的。但一些基本的解释可能会大大改善这个答案(这样读者实际上可以“理解这种方法”)。此外,请注意你所说的话。你设置弧形方法的x和y值只相当于translate(x, y); arc(0,0..。因此,你可以用这两个参数做的所有事情实际上都可以通过简单的平移来完成。只有使用三角函数才能更难实现的一件事... - Kaiido
一个可能的解决方案是改变形状的比例和倾斜,就像rotate()函数一样。而且,一旦你掌握了矩阵变换,让对象相对于彼此移动也不难。 - Kaiido

0

请尝试以下操作:

<code>    
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cx=100;
var cy=100;
var rectWidth=15;
var rectHeight=10;
var rotation=0;
requestAnimationFrame(animate);
function animate(){
    requestAnimationFrame(animate);
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.beginPath();

    var radius = 8;        
    ctx.moveTo(cx - radius, cy + radius);        
    ctx.lineTo(cx, cy - radius);        
    ctx.lineTo(cx + radius , cy + radius);        
    ctx.lineTo(cx - radius, cy + radius);        
    ctx.fill();

    ctx.save();
    ctx.translate(cx,cy);
    ctx.rotate(rotation);
    ctx.strokeRect(-rectWidth/2+20,-rectHeight/2,rectWidth,rectHeight);
    ctx.restore(); 
    rotation+=Math.PI/180;
}
</code>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接