我不会依赖jQuery的animate
这个,因为你的情况比较特殊......相反,使用“游戏循环模式”:有一个游戏对象,它保持一组粒子,这些粒子被移动(碰撞......)和然后在常规的interv阿尔斯。
这里的一个基本结构:
function Particle(x, y) {
this.x = x;
this.y = y;
this.speed = 0; // in pixels per second
this.direction = 0; // in radians per second
}
Particle.prototype.move = function(d_time) {
this.x += Math.cos(this.direction) * this.speed;
this.y += Math.sin(this.direction) * this.speed;
}
Particle.prototype.draw = function() {
// either set the position of a DOM object belonging to this particle
// or draw to a canvas
}
function Game() {
this.particles = Array();
this.MS_PER_FRAME = 20; // in milliseconds
this.D_TIME = 1000.0/this.MS_PER_FRAME;
}
Game.prototype.tick = function() {
$.each(this.particles, function(_, particle) {
particle.move(this.D_TIME);
particle.draw();
})
}
Game.prototype.go = function() {
setInterval(this.tick, this.MS_PER_FRAME)
})
然后就可以操纵速度和粒子的方向,只要你喜欢,也许通过引入额外的部件d_speed
(加速度)和d_direction
左右。
你应该像随机变化的加速度那样获得路径。 –