gpt4 book ai didi

javascript绘制粒子

转载 作者:行者123 更新时间:2023-11-30 06:34:55 24 4
gpt4 key购买 nike

我如何在浏览器中绘制大约 50000 个粒子然后停止,我知道如何创建无休止的粒子动画,但是我如何创建一个一旦绘制完粒子就只是停止。

编辑

所以本质上我想为粒子的绘制计时,但是当我攻击计时器时,它没有得到改变,因为动画没有停止。

var scene = new Scene(),
particles = [],
len = 40000,
height = document.body.offsetHeight,
width = document.body.offsetWidth;

function Particle() {
this.x = 0;
this.y = 0;
this.size = 0;
this.depth = 0;
this.vy = 0;
}
Particle.prototype = {
constructor: Particle,
update: function (width, height) {
if (this.y > height) {
this.y = 1 - this.size;
}
this.y += this.vy;
}
};
for (var i = 0; i < len; i++) {
var particle = new Particle();
particle.x = Math.random() * width;
particle.y = Math.random() * height;
particle.depth = Math.random() * 10 | 0;
particle.size = (particle.depth + 1) / 8;
particle.vy = (particle.depth * .25) + 1 / Math.random();
particles.push(particle);
}

function falling_particles(scene) {
for (var i = 0, l = particles.length; i < l; i++) {
var particle = particles[i];
for (var w = 0; w < particle.size; w++) {
for (var h = 0; h < particle.size; h++) {
var pData = (~~(particle.x + w) + (~~(particle.y + h) * scene.width)) * 4;
scene.idata.data[pData] = 255;
scene.idata.data[pData + 1] = 255;
scene.idata.data[pData + 2] = 255;
scene.idata.data[pData + 3] = 255;
}
}
particle.update(scene.width, scene.height);
}
return scene.idata;
}
scene.setup(document.getElementById('canvas'), falling_particles, width, height, !0);
scene.animate();
window.onresize = function () {
height = scene.height = scene.canvas.height = document.body.offsetHeight;
width = scene.width = scene.canvas.width = document.body.offsetWidth;
};

此处链接:http://jsfiddle.net/MdSP4/

最佳答案

我不确定,你到底想做什么,但如果你添加

setTimeout(function(){
scene.paused = true;
},1000);

然后所有绘图将在一秒钟后停止。

关于javascript绘制粒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com