Javascript 绘制 Canvas 内存泄漏

标签 javascript memory canvas memory-leaks draw

这个脚本在所有浏览器中无休止地占用内存。我不明白为什么!

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var particles = [], amount = 5;
var x = 100; var y = 100;
var W, H;
var p, gradient;  

//dimensions
if(window.innerHeight){
    W = window.innerWidth, H = window.innerHeight;
}else{
    W = document.documentElement.clientWidth, H = document.documentElement.clientHeight;
}
canvas.width = W, canvas.height = H;


//array voor de meerdere particles
for(var i=0;i<amount;i++){
    particles.push(new create_particle());
}

function create_particle(){
    //random positie op canvas
    this.x = Math.random()*W;
    this.y = Math.random()*H;

    //random snelheid
    this.vx = Math.random()*20-10;
    this.vy = Math.random()*20-10;

    //Random kleur
    var r = Math.random()*255>>0;
    var g = Math.random()*255>>0;
    var b = Math.random()*255>>0;
    this.color = "rgba("+r+", "+g+", "+b+", 0.5)";
    this.radius = Math.random()*20+20;

}

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

function draw(){
    canvas.width = canvas.width;
    canvas.height = canvas.height;
    //achtergrond tekenen
    //ctx.globalCompositeOperation = "source-over";
    //ctx.fillStyle = "rgba(0,0,0,0.5)";
    ctx.fillRect(0, 0, W, H);
    //ctx.globalCompositeOperation = "lighter";

    //teken cirkel
    for(var t=0; t<particles.length;t++){
        p = particles[t];

        //gradient
        gradient = ctx.createRadialGradient(p.x,p.y,0,p.x,p.y,p.radius);
        gradient.addColorStop(0,"white");
        gradient.addColorStop(0.4,"white");
        gradient.addColorStop(0.4,p.color);
        gradient.addColorStop(1,"black");
        ctx.beginPath();
        ctx.fillStyle = gradient;
        ctx.arc(p.x,p.y,p.radius,Math.PI*2,false)
        ctx.fill();

        //beweeg
        p.x+=p.vx;
        p.y+=p.vy;

        //canvas boundery detect
        if(p.x < -50)p.x = W+50;
        if(p.y < -50)p.y=H+50;
        if(p.x > W+50)p.x = -50;
        if(p.y > H+50)p.y = -50;
    }
}

(function animloop(){
    canvas.width = canvas.width;
    canvas.height = canvas.height;
    requestAnimFrame(animloop);
    draw();
})();


//resize?
function resizeCanvas(){ 
    canvas.height = W; 
    canvas.width = H;
    ctx.fillRect(0, 0, W, H);
}
if(window.addEventListener){
     window.addEventListener('resize', resizeCanvas, false);
}else{
     window.attachEvent('resize', resizeCanvas);
}

我试图更改一些代码(这也是最终版本)但它仍然泄漏。如果您使用此脚本并观察“taskmanager”或浏览器内存检查,您会发现它缓慢且不断地消耗内存。

编辑:在添加 canvas.height 解决方案并移动一些声明后,脚本仍然泄漏!我必须说,Firefox 看起来比 Chrome 更容易泄漏!

最佳答案

你有:

   canvas.width = canvas.width;
    canvas.height = canvas.height;

我猜这与 clearRect 的作用相同...但一定要试试这个:

function draw(){

   ctx.clearRect ( 0 , 0 , canvas.width , canvas.height );

   /* draw */
}

看看是否有任何变化。

关于Javascript 绘制 Canvas 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12158937/

相关文章:

javascript - 将 blob 转换为 base64

java - Android服务类变量

c++ - 从指针 vector 的 vector 中释放内存

javascript - 拉斐尔JS : arrow's end and start have different color than the path itself

java - 如何在javaFX中使用阴影填充颜色?

android - 在 API 级别 28 中找不到 Canvas 变量

javascript - JS : enabling export/import on client side (ES6 or using babel)?

javascript - 如何在页面加载时使用 Featherlight 打开 DOM 元素?

asp.net - 如何 trim ClientValidationFunction 中的字符串

java - for 循环中的内存管理和速度