gpt4 book ai didi

javascript - requestAnimFrame 在 Chrome 中为 60FPS,在 FF 中为 1FPS

转载 作者:行者123 更新时间:2023-11-28 09:48:53 26 4
gpt4 key购买 nike

我最近使用 requestAnimFrame 创建了一个 Canvas 动画,并且对 Chrome 中的结果非常满意,但在 FF 中,看起来正在运行幻灯片。我不知道是什么导致了这个问题。

此外,当我将粒子数量从 500 减少到 <50 时,FF 中的动画很平滑,但仍达不到 60FPS。这是我的代码:

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

var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
W = window.innerWidth,
H = window.innerHeight,
circles = [];

canvas.width = W;
canvas.height = H;

//Random Circles creator
function create() {

//Place the circles at the center

this.x = W/2;
this.y = H/2;


//Random radius between 2 and 6
this.radius = 2 + Math.random()*3;

//Random velocities
this.vx = -10 + Math.random()*20;
this.vy = -10 + Math.random()*20;

//Random colors
this.r = Math.round(Math.random())*255;
this.g = Math.round(Math.random())*255;
this.b = Math.round(Math.random())*255;
}

for (var i = 0; i < 500; i++) {
circles.push(new create());
}

function draw() {

//Fill canvas with black color
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "rgba(0,0,0,0.15)";
ctx.fillRect(0, 0, W, H);

//Fill the canvas with circles
for(var j = 0; j < circles.length; j++){
var c = circles[j];

//Create the circles
ctx.beginPath();
ctx.globalCompositeOperation = "lighter";
ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, false);
ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 0.5)";
ctx.fill();

c.x += c.vx;
c.y += c.vy;
c.radius -= .05;

if(c.radius < 0)
circles[j] = new create();
}
}

function animate() {
requestAnimFrame(animate);
draw();
}

animate();

here's the live demo 。我在这里做错了什么吗?

最佳答案

以下是在没有 globalCompositeOperation 引用的情况下它的工作原理:

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

var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
W = window.innerWidth,
H = window.innerHeight,
circles = [];

canvas.width = W;
canvas.height = H;

//Random Circles creator
function create() {

//Place the circles at the center

this.x = W/2;
this.y = H/2;


//Random radius between 2 and 6
this.radius = 2 + Math.random()*3;

//Random velocities
this.vx = -10 + Math.random()*20;
this.vy = -10 + Math.random()*20;

//Random colors
this.r = Math.round(Math.random())*255;
this.g = Math.round(Math.random())*255;
this.b = Math.round(Math.random())*255;
}

for (var i = 0; i < 500; i++) {
circles.push(new create());
}

function draw() {

//Fill canvas with black color
ctx.fillStyle = "rgba(0,0,0,0.15)";
ctx.fillRect(0, 0, W, H);

//Fill the canvas with circles
for(var j = 0; j < circles.length; j++){
var c = circles[j];

//Create the circles
ctx.beginPath();
ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, false);
ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 0.5)";
ctx.fill();

c.x += c.vx;
c.y += c.vy;
c.radius -= .05;

if(c.radius < 0)
circles[j] = new create();
}
}

function animate() {
requestAnimFrame(animate);
draw();
}

animate();
<canvas id="canvas"></canvas>

关于javascript - requestAnimFrame 在 Chrome 中为 60FPS,在 FF 中为 1FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367067/

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