我用javascript制作了这个星场动画。我只是不确定让它从另一边循环相同动画的最好方法。我喜欢让星星从画布的一边滚动到另一边。
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
stars = [];
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getSpeed(depth) {
switch (depth / 2) {
case 0:
return 6;
case 1:
return 5;
case 2:
return 4;
case 3:
return 3;
case 4:
return 2;
case 5:
return 1;
}
}
var i;
for (i = 0; i < 2000; i++) {
var star = {
x: 0,
y: 0,
z: 0,
v: 0
};
star.x = getRandomInt(0, 500);
star.y = getRandomInt(0, 500);
star.z = getRandomInt(0, 5) * 2;
star.v = getSpeed(star.z);
stars.push(star);
}
function animation() {
//clears background
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
// populates space with stars
for (i = 0; i < 2000; i++) {
ctx.fillStyle = "rgb(" + (256 / stars[i].z) + "," + (256 / stars[i].z) + "," + (256 / stars[i].z) + ")";
ctx.fillRect(stars[i].x, stars[i].y, 2, 2);
stars[i].x += stars[i].v;
}
setTimeout(animation, 33);
}
animation();<canvas id="myCanvas"></canvas>
发布于 2017-04-02 08:00:45
当x值降到零以下时,只需重新设置它。
if (star.x < 0) star.x = canvasWidth;比较一下,我做了一些其他的改变。
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function StarField(ctx, numStars, width, height) {
var stars = new Array(numStars).fill(null).map(getStar);
function getStar() {
var z = getRandomInt(0, 5),
c = (256 / z).toString(16);
return {
x: getRandomInt(0, width),
y: getRandomInt(0, height),
z: z * 2,
v: 6 - z,
c: "#" + c + c + c
};
}
this.animate = function () {
var i, s;
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);
for (i = 0; i < numStars; i++) {
s = stars[i];
ctx.fillStyle = s.c;
ctx.fillRect(s.x, s.y, 2, 2);
s.x -= s.v;
if (s.x < 0) s.x = width;
}
setTimeout(this.animate.bind(this), 32);
};
}
var canvas = document.getElementById("myCanvas");
var s = new StarField(canvas.getContext("2d"), 2000, 500, 500);
s.animate();<canvas id="myCanvas"></canvas>
发布于 2017-04-02 06:58:27
倒转
stars[i].x -= stars[i].v;您可以永远循环,当i==1999时,用-1乘v,然后再将i设置为0。
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
stars = [];
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getSpeed(depth) {
switch (depth / 2) {
case 0:
return 6;
case 1:
return 5;
case 2:
return 4;
case 3:
return 3;
case 4:
return 2;
case 5:
return 1;
}
}
var i;
for (i = 0; i < 2000; i++) {
var star = {
x: 0,
y: 0,
z: 0,
v: 0
};
star.x = getRandomInt(0, 500);
star.y = getRandomInt(0, 500);
star.z = getRandomInt(0, 5) * 2;
star.v = getSpeed(star.z);
stars.push(star);
}
function animation() {
//clears background
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
// populates space with stars
for (i = 0; i < 2000; i++) {
ctx.fillStyle = "rgb(" + (256 / stars[i].z) + "," + (256 / stars[i].z) + "," + (256 / stars[i].z) + ")";
ctx.fillRect(stars[i].x, stars[i].y, 2, 2);
stars[i].x -= stars[i].v;
}
setTimeout(animation, 33);
}
animation();<canvas id="myCanvas"></canvas>
发布于 2017-04-02 07:04:56
您可以看到,通过添加新的属性d(方向),星星可以双向运动,当星星到达屏幕的末尾时,方向会发生变化。
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
stars = [];
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getSpeed( depth)
{
switch(depth/2)
{
case 0:
return 6;
case 1:
return 5;
case 2:
return 4;
case 3:
return 3;
case 4:
return 2;
case 5:
return 1;
}
}
var i;
for (i = 0; i < 2000; i++) {
var star= { x: 0, y: 0, z: 0, v: 0 };
star.x = getRandomInt(0, 500);
star.y = getRandomInt(0, 500);
star.z = getRandomInt(0, 5) * 2;
star.v = getSpeed(star.z);
star.d = 1;
stars.push(star);
}
function animation () {
//clears background
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 500, 500);
// populates space with stars
for (i = 0; i < 2000; i++) {
ctx.fillStyle = "rgb(" + (256 / stars[i].z) + "," + (256 / stars[i].z) + "," + (256 / stars[i].z) + ")";
ctx.fillRect(stars[i].x, stars[i].y, 2, 2);
if(stars[i].x>=500)stars[i].d=-1;
if(stars[i].x<=0)stars[i].d=1;
stars[i].x += stars[i].v * stars[i].d;
}
setTimeout(animation, 33);
}
animation ();
</script>
</body>
</html>
https://stackoverflow.com/questions/43165377
复制相似问题