使用raphael.js重新渲染时内存泄漏

时间:2015-03-03 10:28:08

标签: javascript jquery svg raphael svg.js

使用 raphael.js 将形状渲染到画布,但问题是内存泄漏并且页面在一段时间后崩溃。有人可以帮助我如何处理它?我使用 underscore.js 来处理循环,同时删除仍然没有运气。我尝试将库更改为 svg.js ,但问题更严重。在此先感谢,代码如下:

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 - &GT; https://jsonblob.com/54f47ff7e4b0ae1ed0b1fcf2

jp_ImpossibleSpace.json - &GT; https://jsonblob.com/54f48114e4b0ae1ed0b1fd04

1 个答案:

答案 0 :(得分:0)

这只是我的问题。我忘了在窗口变量

中清除我的对象数组