如何为转到类似于游戏http://www.agar.io的特定坐标对的圆圈设置动画效果?我已经尝试过jquery animate()函数,但它的速度很慢,因为我希望圆圈移动到的坐标不断更新。
答案 0 :(得分:1)
agar.io仅使用核心画布功能。
以下示例显示光标后面的球。
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.canvas.width = 500;
ctx.canvas.height = 500;
var V2 = function(x, y) {
this.x = x;
this.y = y;
};
var cursor = new V2(0, 0);
var ballSize = 20;
var ball = new V2(0, 0);
ctx.DrawGrid = function(size) {
this.fillStyle = "black";
for (var x = 0; x != 25; x++)
for (var y = 0; y != 25; y++)
this.strokeRect(x * size, y * size, size, size);
}
var main = setInterval(function() {
ctx.clearRect(0, 0, 5000, 5000);
if (cursor.x > ball.x - ballSize)
ball.x += 3;
if (cursor.y > ball.y - ballSize)
ball.y += 3;
if (cursor.x < ball.x + ballSize)
ball.x -= 3;
if (cursor.y < ball.y + ballSize)
ball.y -= 3;
if (ball.x < ballSize)
ball.x = ballSize;
if (ball.y < ballSize)
ball.y = ballSize;
if (ball.x > ctx.canvas.width - ballSize)
ball.x = ctx.canvas.width - ballSize;
if (ball.y > ctx.canvas.height - ballSize)
ball.y = ctx.canvas.height - ballSize;
ctx.DrawGrid(50);
ctx.beginPath();
ctx.arc(ball.x, ball.y, ballSize, 0, 2 * Math.PI);
ctx.fill();
}, 33);
document.onmousemove = function(e) {
cursor.x = e.pageX;
cursor.y = e.pageY;
}
&#13;
html,
body {
margin: 0px;
}
&#13;
<canvas id="canvas"></canvas>
&#13;