[英]Rotating an image so it looks like it is spinning
我试图连续旋转雪花的图像,以使它产生在图像中心旋转的错觉,而我为此非常挣扎,这里是一些代码。
我有一个循环,其中调用了snowball函数,但这并不重要,因为它不会连续旋转一次。 Snow,是我在代码前面引入的图像。
function drawSnowBall() {
context.beginPath();
for (var i = 0; i < mp; i++) {
var p = particles[i];
context.drawImage(Snow,p.x, p.y);
}
if (pause == false)
{
updateSnow();
}
}
var angle = 0;
function updateSnow() {
angle;
for (var i = 0; i < mp; i++) {
var p = particles[i];
//Updating X and Y coordinates
//We will add 1 to the cos function to prevent negative values which will lead flakes to move upwards
//Every particle has its own density which can be used to make the downward movement different for each flake
//Lets make it more random by adding in the radius
p.y += Math.cos(angle + p.d) + SnowSpeed + p.r / 2;
p.x += Math.sin(angle) * 2;
//Sending flakes back from the top when it exits
if (p.x > 800 + 5 || p.x < -5 || p.y > 700) {
if (i % 3 > 0) //66.67% of the flakes
{
particles[i] = {
x: Math.random() * 800,
y: -10,
r: p.r,
d: p.d
};
}
}
}
}
只需将setInterval与内部方法一起使用就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.