Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery drag drop hangs or gets stuck for android browser

see below link http://liveweave.com/JckSgC

when I try to drag drop items item gets stuck somewhere on mobile screen without being dropped to droppable area,

and when I click somewhere and screen it starts working (this basically happened once I open page for first time)

what could be the issue and how do I resolve it?

below is code

js code

(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();       
        }

    }


})();
like image 735
mydeve Avatar asked Nov 10 '22 23:11

mydeve


1 Answers

If you are using JQuery try using the following javascript library called touchpunch and make sure you include it after you include JQuery.

http://touchpunch.furf.com/

I remember having a lot of issues with a drag and drop application while testing on mobile devices until I started using touchpunch.

like image 141
Larry Lane Avatar answered Nov 15 '22 04:11

Larry Lane