【问题标题】:Javascript draw canvas memory leakJavascript绘制画布内存泄漏
【发布时间】:2012-08-22 22:36:27
【问题描述】:

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

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 更容易泄漏!

【问题讨论】:

  • 不要使用任务管理器。使用 Firefox 和 about:memory 选项卡,您可以获得准确的内存使用报告。
  • 只是一个参考,因为即使在那里,它也不仅仅是可见的......但感谢您的提示:)
  • 嗨。除了“梯度存在内存泄漏”之外,你找到答案了吗?
  • 不,我还没有解决方案:(
  • createRadialGradient 对我来说也有内存泄漏,在 Firefox 77.0b9 中,但在 Chrome 或 Edge 中没有。全部赢 7。

标签: javascript memory canvas memory-leaks draw


【解决方案1】:

你有:

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

我猜这与 clearRect 的作用相同...但请务必尝试一下:

function draw(){

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

   /* draw */
}

看看有没有什么变化。

【讨论】:

    【解决方案2】:

    在开始另一组绘图之前尝试清洁画布。您可以通过重新设置它的宽度和高度来清除它。

    这是一些定向代码:

    function draw() {
       canvas.width = canvas.width;
       canvas.height = canvas.height;
    
       /* draw */
    }
    

    【讨论】:

    • 当我直接尝试这个时,我的第一个结论是,这不是解决方案。 :(
    【解决方案3】:

    我的猜测是您的 create_particle 函数可能会泄漏,但我不确定如何泄漏,一个想法是创建一个内部对象而不是使用 this

    function create_particle(){
      return {
        x: Math.random()*W,
        y: Math.random()*H,
        vx: Math.random()*20-10,
        vy: Math.random()*20-10,
        r: Math.random()*255>>0,
        g: Math.random()*255>>0,
        b: Math.random()*255>>0,
        color: "rgba("+r+", "+g+", "+b+", 0.5)",
        radius: Math.random()*20+20,
      };
    }
    

    我不确定这是否是问题所在,但这似乎是我唯一能真正想到的东西,看起来有点奇怪,

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多