简体   繁体   English

Javascript绘制画布内存泄漏

[英]Javascript draw canvas memory leak

This script is endlessly taking memory in all the browsers.这个脚本在所有浏览器中无休止地占用内存。 I can't see why!我不明白为什么!

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);
}

I tried to change some code around (this is also final version) but still it leaks.我试图更改一些代码(这也是最终版本)但它仍然泄漏。 If you use this script and watch 'taskmanager' or in-browser's memory check you see that it slowly and constantly eats memory.如果您使用此脚本并查看“任务管理器”或浏览器中的内存检查,您会发现它缓慢且不断地占用内存。

EDIT: after adding in the canvas.height solution and moving some declaring's around, the script still leaks!编辑:在添加 canvas.height 解决方案并移动一些声明后,脚本仍然泄漏! I must say that it looks like Firefox leaks harder then Chrome!我必须说,看起来 Firefox 比 Chrome 更容易泄漏!

You have:你有:

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

I'm guessing this does the same as clearRect... but to be sure try this too:我猜这和 clearRect 一样......但一定要试试这个:

function draw(){

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

   /* draw */
}

See if any thing changes.看看有没有什么变化。

Try cleaning the canvas before starting another set of drawing.在开始另一组绘图之前尝试清洁画布。 You can clear it by setting it's width and height again.您可以通过再次设置它的宽度和高度来清除它。

Here is some orientative code:这是一些定向代码:

function draw() {
   canvas.width = canvas.width;
   canvas.height = canvas.height;

   /* draw */
}

my guess is your create_particle function could be leaking but im not sure as how, one idea would be to create an interal object instead of using this我的猜测是你的 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,
  };
}

Im not sure if this is the issue, but it seems to be the only thing I can really think of that kinda looks odd,我不确定这是否是问题所在,但这似乎是我唯一能真正想到的事情,看起来有点奇怪,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM