1

最近キャンバス アニメーションを作成しrequestAnimFrame、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();

そして、これがライブデモです。ここで何か間違ったことをしていますか?

4

1 に答える 1

0

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>

于 2017-09-28T06:42:00.683 に答える