0

Am using raphael.js to render shapes to a canvas, but the problem is that there is a memory leak and the page crashes after some time. Can some one help me how it can be handled ? I am using underscore.js to handle the loop while removing still no luck. I tried changing the library to svg.js but the problem was worse. Thanks in advance, the code is as follows:

var paper = new Raphael(document.getElementById('visualizerContainer'), 545, 545);
paper.canvas.style.backgroundColor = '#000';
Raphael.fn.line = function(startX, startY, endX, endY){
    return this.path('M' + startX + ' ' + startY + ' L' + endX + ' ' + endY);
};
var flag=0;
window.particleObject={
    "id":"",
    "obj":[]
}
function addParticles(){
    if(flag) removeParticles(particleObject);
for(var i=0;i<5000;i++){

    var x=Math.floor((Math.random() * 628) + 1);
    var y=Math.floor((Math.random() * 571) + 1);
    var circleName =  "var circle"+i;
    circleName = paper.circle(x, y, 1);
    //var fillColor='#'+ ('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6);
    circleName.attr("fill", "#0F0");
    circleName.attr("stroke", "#ff");
    particleObject.id=i;
    particleObject.obj.push(circleName);

}
    flag=1;
}
function removeParticles(particleObject){

    _.map(particleObject.obj, function(o) {o.remove(); });
}
$.getJSON('assets/data/jp_Wall.json', function(data) {
    $.each(data.features, function(id, obj) {
        var wall = paper.line(obj.x1, obj.y1, obj.x2, obj.y2).attr({stroke:'red',fill:'red',"stroke-width": 3});
    });
});
$.getJSON('assets/data/jp_ImpossibleSpace.json', function(data) {
    $.each(data.features, function(id, obj) {
        var width=(obj.x2-obj.x1);
        var height=(obj.y2-obj.y1);
        var top_left_x=obj.x1;
        var top_left_y=obj.y1;
        var rectangle = paper.rect(top_left_x, top_left_y, width, height).attr({stroke:'blue',"stroke-width": 3, "stroke-dasharray": "."});
    });
});
addParticles();
setInterval(addParticles, 500);

Wall.json-->https://jsonblob.com/54f47ff7e4b0ae1ed0b1fcf2

jp_ImpossibleSpace.json-->https://jsonblob.com/54f48114e4b0ae1ed0b1fd04

JDZ
  • 1
  • 2
  • Hard to track without a working fiddle. Personally I'd probably approach it slightly differently. Maybe I would create an array of 5000 hidden particles at first and leave this fixed. Then when looping I would change the x,y and visibility of the object. As you aren't creating/deleting objects within a timeinterval it may help. – Ian Mar 03 '15 at 11:19
  • Cant help about changing the flow and I read that the same thing would happen even then, is there any way I could solve this? – JDZ Mar 03 '15 at 11:39
  • Where did you read that the same thing would happen ? You don't need to change the flow. Just don't create the objects every time, change their attributes. – Ian Mar 03 '15 at 11:56
  • the number varies in time so i have to destroy the old number of objects and create new ones. – JDZ Mar 03 '15 at 12:44

1 Answers1

0

It was my problem only. I forgot to clear my array of objects in window variable

JDZ
  • 1
  • 2