我正在尝试使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);
最佳答案
性能杀手正在为每个动画帧中的每个粒子创建渐变。
您可以将昂贵的gradient
换成相对便宜的globalAlpha
。
这是一个使用globalAlpha而不是gradient的重构。我把它留给您来调整globalAlpha以匹配您的效果,但是在Firefox(我的计算机上为59+)上,这种重构要快得多。
您的代码中可能还有其他优化,但是使用渐变是性能的杀手...……
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/