连续拖放导致挂起

Posted

技术标签:

【中文标题】连续拖放导致挂起【英文标题】:continuous drag drop causes hangs 【发布时间】:2015-01-27 19:13:50 【问题描述】:

见http://liveweave.com/GknZjq

当我在 5-6 次拖放后有时在移动设备中继续连续拖动对象时,我看到有时对象不会恢复原状并且我以后无法使用。

基本上我在画布上制作了两个对象的组,所以有时最多可以有两个图像不超过, 另见图片

为什么会发生我该如何预防是请解决

(function () 
    var canvas = new fabric.Canvas('canvas');
    var canvas_el = document.getElementById('canvas');
    var canvas1 = new fabric.Canvas('canvas1');

    var group;
    fabric.Image.fromURL('img/blank.png', function (img) 
        var img1 = img.set(
            left: 0,
            top: 0
        );
        fabric.Image.fromURL('img/blank.png', function (img) 
            var img2 = img.set(
                left: 0,
                top: 0
            );
            group = new fabric.Group([img1, img2], 
                left: 0,
                top: 0
            );
            canvas.add(group)
        );
    );


    fabric.Image.fromURL('img/blank.png', function (img) 
        var img1 = img.set(
            left: 0,
            top: 0
        );
        fabric.Image.fromURL('img/blank.png', function (img) 
            var img2 = img.set(
                left: 0,
                top: 0
            );
            group1 = new fabric.Group([img1, img2], 
                left: 0,
                top: 0
            );
            canvas1.add(group1)
        );
    );



    $(document).ready(function () 



        /* Define drag and drop zones */
        var $drop = $('#canvas-drop-area,#canvas-drop-area1'),
            $gallery = $('td > #image-list li'),
            $draggedImage=null;

        /* Define the draggable properties */
        $gallery.draggable(
             helper: 'clone',
            start: function (e) 
            $draggedImage=event.target;
                $drop.css(
                    'display': 'block'
                )
            ,
            stop: function () 
                $(this).find('img').css(
                   /* 'opacity': 0.4 */
                );
                $drop.css(
                    'display': 'none'
                );
                $draggedImage=null;
            ,
            revert: true
        );

        /* Define the events for droppable properties */
        $drop.droppable(
            over: function (event, ui) 
                $(this).addClass('active');
            ,
            drop: function (event, ui) 
                var image =$draggedImage&& $draggedImage.src;
                console.log($draggedImage.alt);
                img_to_canvas(image,$draggedImage.alt,$(event.target).is("#canvas-drop-area")?1:2);
            ,
            out: function (event, ui) 
                $(this).removeClass('active');
            ,
            deactivate: function (event, ui) 
                $(this).removeClass('active');
            
        );

    );


    var img_to_canvas = function(image,sendfront,checkcanvas) 
        var img = new Image();
        img.src = image;
        if(checkcanvas =='1')
            if(sendfront=='top')
                fabric.util.loadImage(img.src, function (img) 
                    group.item(0).setElement(img);
                        canvas.renderAll();
                ); 
            else
                fabric.util.loadImage(img.src, function (img) 
                    group.item(1).setElement(img);
                        canvas.renderAll();
                ); 
            
            canvas.calcOffset();  
        else
            if(sendfront=='top')
                fabric.util.loadImage(img.src, function (img) 
                    group1.item(0).setElement(img);
                        canvas1.renderAll();
                );         
            else
                fabric.util.loadImage(img.src, function (img) 
                    group1.item(1).setElement(img);
                        canvas1.renderAll();
                ); 
            
            canvas1.calcOffset();       
        

    


)();

【问题讨论】:

【参考方案1】:

改变

$drop.droppable(
    over: function (event, ui) 
        $(this).addClass('active');
    ,
    drop: function (event, ui) 
        var image =$draggedImage&& $draggedImage.src;

$drop.droppable(
        over: function(event, ui) 
            $(this).addClass('active');
        ,
        drop: function(event, ui) 
            $draggedImage = ui.draggable.find("img").get(0);

【讨论】:

以上是关于连续拖放导致挂起的主要内容,如果未能解决你的问题,请参考以下文章

使用 javascript 进行 HTML5 拖放 DOM 操作

Swing 拖放导致内存泄漏

在 wxPython 中跨面板拖放图像

使用 IStream 拖放虚拟文件

禁用文本拖放

JavaFX ImageView旋转更改拖放