gpt4 book ai didi

javascript - 在 Canvas 中渲染粒子,提高 FireFox FPS

转载 作者:行者123 更新时间:2023-12-03 09:26:13 26 4
gpt4 key购买 nike

我试图在 Firefox 中将 FPS 提高到 30 以上。我真的不知道我还可以做些什么来改进它。有什么建议吗?

Chrome 和 Opera 的帧速率徘徊在 60 fps 左右,而 FF 则停留在 10-20 之间,因此,它会导致我页面的其余部分出现延迟问题。

http://jsfiddle.net/7vv2tur7/

window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ) {
window.setTimeout(callback, 1000 / 60);
};
})();


(function() {

particleCanvas();

con = particle.getContext('2d');
pxs = [];
for(var i = 0; i < 25; i++) {
pxs[i] = new Circle();
pxs[i].reset();
}

requestAnimFrame(paintParticles);

window.onresize = function(event) {
particleCanvas();
};

})();

function Circle() {

// settings
this.s = {

ttl:10000,

// speeds
xmax:4,
ymax:4,

// max size
size:1,

rt:4,

xdrift:60,
ydrift:60,

opacity: 0.3,
};


this.reset = function() {

// randomise positioning for each particle
this.x = particle.width * Math.random();
this.y = particle.height * Math.random();

// size
this.r = ((this.s.size-1)*Math.random()) + 1;


this.dx = (Math.random()*this.s.xmax) * (Math.random() < 0.5 ? -1 : 1);
this.dy = (Math.random()*this.s.ymax) * (Math.random() < 0.5 ? -1 : 1);
this.hl = this.s.ttl / 4 * (this.r/this.s.size);
this.rt = Math.random()*this.hl;
this.s.rt = Math.random() +1;
this.stop = Math.random();
this.s.xdrift *= Math.random() * (Math.random() < 0.5 ? -1 : 1);
this.s.ydrift *= Math.random() * (Math.random() < 0.5 ? -1 : 1);
};

this.fade = function() {
this.rt += 5 + this.s.rt;
};

this.draw = function() {

if(this.rt >= this.hl) this.reset();
var newo = 1 - (this.rt/this.hl);



con.globalAlpha = this.s.opacity;


con.beginPath();
con.arc(this.x,this.y,this.r,0,Math.PI*2,true);
con.closePath();
var cr = this.r*newo;

g = con.createRadialGradient(this.x, this.y, 0, this.x, this.y, (cr <= 0 ? 1 : 5));
g.addColorStop(0.0, 'rgba(255,255,255,' + newo + ')');
g.addColorStop(this.stop, 'rgba(255,255,255,' + 0.5 * newo + ')');
g.addColorStop(1.0, 'rgba(255,255,255, 0)');


con.fillStyle = g;
con.fill();
};

this.move = function() {
this.x += (this.rt/this.hl)*this.dx;
this.y += (this.rt/this.hl)*this.dy;
if(this.x > particle.width || this.x < 0) this.dx *= -1;
if(this.y > particle.height || this.y < 0) this.dy *= -1;
};

this.getX = function() { return this.x; };
this.getY = function() { return this.y; };
}

function particleCanvas() {
particle.width = document.querySelector('.start').offsetWidth / 2;
particle.height = document.querySelector('.start').offsetHeight / 2;
}

function paintParticles() {

requestAnimFrame(paintParticles);

con.clearRect ( 0 , 0 , particle.width, particle.height );

for(var i = 0; i < pxs.length; i++) {
pxs[i].fade();
pxs[i].move();
pxs[i].draw();
}

var thisFrameFPS = 1000 / ((now=new Date()) - lastUpdate);
fps += (thisFrameFPS - fps) / fpsFilter;
lastUpdate = now;

}

var fps = 0, now,
lastUpdate = (new Date())*1 - 1, fpsFilter = 50;



setInterval(function(){
document.querySelector('.fps').innerHTML = fps.toFixed(1) + ' fps';
}, 1000);

最佳答案

性能 killer 是在每个动画帧期间为每个粒子创建渐变。

您可以将昂贵的gradient换成相对便宜的globalAlpha

这是使用 globalAlpha 而不是渐变的重构。我让您调整 globalAlpha 以匹配您的效果,但这种重构在 Firefox 上要快得多(我的机器上为 59+)。

代码中还可以进行其他优化,但使用渐变是性能 killer ......

window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ) {
window.setTimeout(callback, 1000 / 60);
};
})();


(function() {

particleCanvas();

con = particle.getContext('2d');
pxs = [];
for(var i = 0; i < 25; i++) {
pxs[i] = new Circle();
pxs[i].reset();
}

requestAnimFrame(paintParticles);

window.onresize = function(event) {
particleCanvas();
};

})();

function Circle() {

// settings
this.s = {

ttl:10000,

// speeds
xmax:4,
ymax:4,

// max size
size:1,

rt:4,

xdrift:60,
ydrift:60,

opacity: 0.7,
};


this.reset = function() {

// randomise positioning for each particle
this.x = particle.width * Math.random();
this.y = particle.height * Math.random();

// size
this.r = ((this.s.size-1)*Math.random()) + 1;


this.dx = (Math.random()*this.s.xmax) * (Math.random() < 0.5 ? -1 : 1);
this.dy = (Math.random()*this.s.ymax) * (Math.random() < 0.5 ? -1 : 1);
this.hl = this.s.ttl / 4 * (this.r/this.s.size);
this.rt = Math.random()*this.hl;
this.s.rt = Math.random() +1;
this.stop = Math.random();
this.s.xdrift *= Math.random() * (Math.random() < 0.5 ? -1 : 1);
this.s.ydrift *= Math.random() * (Math.random() < 0.5 ? -1 : 1);

this.s.opacity=0.70;
};

this.fade = function() {
this.rt += 5 + this.s.rt;
this.s.opacity-=.005;
};

this.draw = function() {

if(this.rt >= this.hl) this.reset();
var newo = 1 - (this.rt/this.hl);

con.globalAlpha = this.s.opacity;

con.beginPath();
con.arc(this.x,this.y,this.r,0,Math.PI*2,true);
con.closePath();
var cr = this.r*newo;
con.fill();
};

this.move = function() {
this.x += (this.rt/this.hl)*this.dx;
this.y += (this.rt/this.hl)*this.dy;
if(this.x > particle.width || this.x < 0) this.dx *= -1;
if(this.y > particle.height || this.y < 0) this.dy *= -1;
};

this.getX = function() { return this.x; };
this.getY = function() { return this.y; };
}

function particleCanvas() {
particle.width = document.querySelector('body').offsetWidth / 2;
particle.height = document.querySelector('body').offsetHeight / 2;
}

function paintParticles() {

requestAnimFrame(paintParticles);

con.clearRect ( 0 , 0 , particle.width, particle.height );

con.fillStyle = 'white';
for(var i = 0; i < pxs.length; i++) {
pxs[i].fade();
pxs[i].move();
pxs[i].draw();
}

var thisFrameFPS = 1000 / ((now=new Date()) - lastUpdate);
fps += (thisFrameFPS - fps) / fpsFilter;
lastUpdate = now;

}

var fps = 0, now,
lastUpdate = (new Date())*1 - 1, fpsFilter = 50;



setInterval(function(){
document.querySelector('.fps').innerHTML = fps.toFixed(1) + ' fps';
}, 1000);
html,
body {height:100%;width:100%;display:block;margin:0;padding:0;background:black}

* {box-sizing:border-box}

span {
position:absolute;
top:50px;
left:50px;
font-size:28px;
color:white;
font-family:mono;
}
canvas {width:100%;height:100%} </style>
<canvas id="particle"></canvas>
<span class="fps"></span>

关于javascript - 在 Canvas 中渲染粒子,提高 FireFox FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658613/

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