简体   繁体   English

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

[英]Memory leak while re-rendering using raphael.js

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. 我使用raphael.js将形状渲染到画布,但问题是内存泄漏并且页面在一段时间后崩溃。 Can some one help me how it can be handled ? 有人可以帮助我如何处理它? I am using underscore.js to handle the loop while removing still no luck. 我使用underscore.js来处理循环,同时删除仍然没有运气。 I tried changing the library to svg.js but the problem was worse. 我尝试将库更改为svg.js,但问题更严重。 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 Wall.json - > https://jsonblob.com/54f47ff7e4b0ae1ed0b1fcf2

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

It was my problem only. 这只是我的问题。 I forgot to clear my array of objects in window variable 我忘了清除窗口变量中的对象数组

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

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