繁体   English   中英

围绕三角形画布旋转圆

[英]Rotate circle around triangle 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> 

我已经编辑了您的代码以绘制所需的形状,并添加了注释来描述我在以下代码段中所做的工作。

 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教程

如果您还有其他问题,请随时打开新问题,以解决更多特定于代码的问题。

这是不使用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> 

请尝试以下操作:

<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>

暂无
暂无

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

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