Javascript内存泄漏-Canvas HTML5 jQuery

Javascript memory leak - Canvas HTML5 jQuery

本文关键字:HTML5 jQuery -Canvas 泄漏 内存 Javascript      更新时间:2023-09-26

我正在构建一个主要使用画布的网站,但唯一涉及的画布是一条水平绘制的线,这条线大约有13000px长。

当用户滚动我的窗口,然后沿着m画布路径水平滚动时,示例。

我注意到在firefox(6.0.2版本)上,我的文档无法滚动。在我的控制台中,我收到了一些类似(NS_ERROR_OUT_of_MEMORY)的内容。

在谷歌上搜索后,我发现这可能是一个潜在的内存泄漏?这是如何工作的,是因为我编写代码的方式吗?还是浏览器/硬件问题?

我正在重新启动我的窗口调整等功能,我很好奇这是否有任何imapct?

// Initate the plugin
$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});
$(window).bind('resizeEnd', function() {
    $("#path").scrollPath({drawPath: true, wrapAround: false});
});
$("#path").scrollPath({drawPath: true, wrapAround: false}); 

        $(document).ready(init);
            $('.wrapper').css({'top' : '0px','left' : '0px'});
            $('.wrapper > div').css({'height' : + $(window).height() +'px'});
        function init() {

        // Set window height and width variables 
            var windowheight = $(window).height(); 
            var windowwidth = $(window).width(); 
            // Check monitor size and workot if incentives needs extra space etc 
            var bff = 4020 + (1993 - windowwidth);
            // Move divs into position 
            $('.culture').css('top', + - windowheight + 'px');
            $('.careerpath').css('top', + - windowheight + 'px');
            $('.training').css('top', + - windowheight + 'px');
            $('.apply').css('top' , + - windowheight + 'px');

            /* ========== DRAWING THE PATH AND INITIATING THE PLUGIN ============= */
            $.fn.scrollPath("getPath")
                // Move to 'start' element
                .moveTo(0, 0, {name: "div"})
                .lineTo(2400, 0, {name: "div1"})    
                .lineTo((bff-550), 0, {name: "div2"})
                .lineTo(bff, 0, {name: "div3"})
                .lineTo(bff, -windowheight, {name: "div4"}) 
                .lineTo((bff + 1993), -windowheight, {name: "div5"})
                .lineTo((bff + 1993 + 1837), -windowheight, {name: "div6"}) 
                .lineTo((bff + ((1993 + 1837 + 1795) - 325)), -windowheight, {name: "div7"})    
            // We're done with the path, let's initate the plugin on our wrapper element
            // Window resize function
            $(window).resize(function() {
                if(this.resizeTO) clearTimeout(this.resizeTO);
                this.resizeTO = setTimeout(function() {
                    $(this).trigger('resizeEnd');
                }, 500);
            });
            $(window).bind('resizeEnd', function() {
                $("#path").scrollPath({drawPath: true, wrapAround: false});
            });
            $("#path").scrollPath({drawPath: true, wrapAround: false});
        }

好的,现在我在谷歌上搜索了你使用的插件,我知道发生了什么。

http://joelb.me/scrollpath/

"线"实际上是一个形状,scrollPath正在为此生成一个漂亮的大画布。问题出在scrollPath的内部。它创建了太多的画布实例或泄漏了一些内容。

您应该更好地跟踪/记录错误,并将其报告给作者。

既然我们知道您的意思不是一条直线,那么从单个DOM元素创建路径的建议是无效的。我不知道你的trget到底是什么,但你可能可以用impress.js 来实现它

你做错了。这种做法只会带来痛苦。

我不认为你有漏洞,你只是有一个程序的内存占用。除了内存,您还将面临巨大的性能问题。2D画布受填充率(绘制的像素数)的影响很大。即使在速度很快的计算机上,绘制这么多像素也会非常慢。

因此,不要制作一个巨大的画布,然后在上面滚动窗口/视口。相反,制作一个小画布,只渲染更大事物的可见部分。