【发布时间】:2016-07-13 19:36:55
【问题描述】:
我是使用画布的新手。我的挑战是通过弧将对象从一个静态坐标移动到另一个。我使用了来自 https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations 的太阳系代码示例,但是在浏览器中按 f5 后,我的对象仍保留在当前位置。我想在按 f5 后将对象返回到起点,并在对象到达最终位置时停止移动对象观点。
function draw() {
var ctx = document.getElementById('myCanvas').getContext('2d');
ctx.globalCompositeOperation = 'destination-over';
ctx.clearRect(0,0,220,220); // clear canvas
ctx.save();
ctx.translate(110,110); //relative for canvas center;
//element
var time = new Date();
ctx.rotate( - ((2*Math.PI)/8)*time.getSeconds() - ((2*Math.PI)/8000)*time.getMilliseconds() );
ctx.translate(65,0);//moving radius
ctx.beginPath();//draw the arc
ctx.arc(10, 10, 6, 0, 2 * Math.PI, false);
//ctx.restore();
ctx.lineWidth = 10;
ctx.fillStyle = '#1e88e5';
ctx.fill();
ctx.strokeStyle = '#ffffff';
ctx.stroke();
// ctx.lineWidth = 5;
ctx.stroke();
ctx.restore();
window.requestAnimationFrame(draw);}window.requestAnimationFrame(draw);
【问题讨论】:
标签: javascript html canvas