【发布时间】:2018-03-24 10:35:59
【问题描述】:
大家! 我有一个挑战。我需要在 Canvas 上绘制一个迷你太阳系。 在这个系统中,有 3 个圆(月球、太阳和地球)。 根据任务,月球必须围绕地球转,地球也必须围绕太阳转。
这些是旋转函数:
var drawSun = function(centerX, centerY, radius){
centerX = canvas.width / 2;
centerY = canvas.height / 2;
radius = 150;
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.lineWidth = 5;
context.strokeStyle = 'yellow';
context.stroke();
}
var drawEarth = function(){
var circle = {centerX:canvas.width/4, centerY:canvas.height/4, radius:50, angle:0}
var earth = {x:0, y:0,speed:1};
earth.x = circle.centerX + Math.cos(circle.angle) * circle.radius;
earth.y = circle.centerY + Math.sin(circle.angle) * circle.radius;
circle.angle += earth.speed;
context.lineWidth = 5;
context.strokeStyle = 'blue';
context.stroke();
context.beginPath();
context.arc(earth.x, earth.y, 15,0, Math.PI*2, true);
context.closePath();
}
var drawMoon = function(){
var circle = {centerX:canvas.width / 4, centerY:canvas.height / 4, radius:5, angle:0}
var moon= {x:0, y:0,speed:1};
moon.x = circle.centerX + Math.cos(circle.angle) * circle.radius;
moon.y = circle.centerY + Math.sin(circle.angle) * circle.radius;
circle.angle += moon.speed;
context.lineWidth = 5;
context.strokeStyle = 'white';
context.stroke();
context.beginPath();
context.arc(moon.x, moon.y, 15,0, Math.PI*2, true);
context.closePath();
}
var drawPlanets = function(){
drawSun();
drawEarth();
drawMoon();
}
但是,说实话,行星根本不动。它们只是存在(但只有太阳和月亮),它们的位置是静态的。
我的错误在哪里?
【问题讨论】:
标签: javascript html canvas