2013-09-27 70 views
2

我正在使用fabric.js庫創建拖放情境板創建器。一切都很完美,除了在Firefox中將圖像拖放到畫布上時除外。它添加圖像,但重定向到img url。重定向後,當我點擊後面的btn時,它會回到上一頁,圖像在畫布上。Firefox拖放重定向停止JQUERY

我一直在尋找和閱讀幾個小時,嘗試各種各樣的東西。看來我的代碼中的某些東西阻止了「e.stopPropagation();」或「e.preventDefault()」無法正常工作。我似乎無法找到這個gremlin。

任何幫助將是偉大的。謝謝!

更新:增加了JSFiddle。
這裏是我的代碼的jsfiddle:
http://jsfiddle.net/bQxMu/

更新2:
修正了問題,這裏要說的是固定它的部分:

function handleDrop(e) { 
// this/e.target is current target element. 

/* 
    if (e.stopPropagation) { 
     e.stopPropagation(); // stops the browser from redirecting. 
    } 
*/ 

    e.stopPropagation(); // Stops some browsers from redirecting. 
    e.preventDefault(); // Stops some browsers from redirecting. 

    var img = document.querySelector('#images img.img_dragging'); 

    console.log('event: ', e); 

    var newImage = new fabric.Image(img, { 
     width: img.width, 
     height: img.height, 
    // Set the center of the new object based on the event coordinates relative 
    // to the canvas container. 
     left: e.layerX, 
     top: e.layerY 
    }); 
    canvas.add(newImage); 

    return false; 
} 

感謝所有幫助: )

window.onload=function(){ 

var canvas = new fabric.Canvas('canvas'); 

canvas.backgroundColor = '#ffffff'; 


function handleDragStart(e) { 
    [].forEach.call(images, function (img) { 
     img.classList.remove('img_dragging'); 
    }); 
    this.classList.add('img_dragging'); 

} 

function handleDragOver(e) { 
    if (e.preventDefault) { 
     e.preventDefault(); // Necessary. Allows us to drop. 

    } 

    e.dataTransfer.dropEffect = 'copy'; 

    return false; 
} 

function handleDragEnter(e) { 
    // this/e.target is the current hover target. 

    this.classList.add('over'); 

} 

function handleDragLeave(e) { 
    e.stopPropagation(); 

    this.classList.remove('over'); // this/e.target is previous target element. 

} 

function handleDrop(e) { 
    // this/e.target is current target element. 

    if (e.stopPropagation) { 
     e.stopPropagation(); // stops the browser from redirecting. 
    } 

    var img = document.querySelector('#images img.img_dragging'); 

    console.log('event: ', e); 

    var newImage = new fabric.Image(img, { 
     width: img.width, 
     height: img.height, 
     // Set the center of the new object based on the event coordinates relative 
     // to the canvas container. 
     left: e.layerX, 
     top: e.layerY 
    }); 
    canvas.add(newImage); 
    return false; 
} 

function handleDragEnd(e) { 
    // this/e.target is the source node. 
    [].forEach.call(images, function (img) { 
     img.classList.remove('img_dragging'); 

    }); 
} 

    var removeSelectedEl = document.getElementById('remove-selected'); 
    removeSelectedEl.onclick = function() { 
    var activeObject = canvas.getActiveObject(), 
     activeGroup = canvas.getActiveGroup(); 
    if (activeGroup) { 
     var objectsInGroup = activeGroup.getObjects(); 
     canvas.discardActiveGroup(); 
     objectsInGroup.forEach(function(object) { 
     canvas.remove(object); 
     }); 
    } 
    else if (activeObject) { 
     canvas.remove(activeObject); 
     $("#imageControl").fadeOut('slow'); 

    } 
    }; 

    var sendBackwardsEl = document.getElementById('send-backwards'); 
    sendBackwardsEl.onclick = function() { 
    var activeObject = canvas.getActiveObject(); 
    if (activeObject) { 
     canvas.sendBackwards(activeObject); 
    } 
    }; 

    var sendToBackEl = document.getElementById('send-to-back'); 
    sendToBackEl.onclick = function() { 
    var activeObject = canvas.getActiveObject(); 
    if (activeObject) { 
     canvas.sendToBack(activeObject); 
    } 
    }; 

    var bringForwardEl = document.getElementById('bring-forward'); 
    bringForwardEl.onclick = function() { 
    var activeObject = canvas.getActiveObject(); 
    if (activeObject) { 
     canvas.bringForward(activeObject); 
    } 
    }; 

    var bringToFrontEl = document.getElementById('bring-to-front'); 
    bringToFrontEl.onclick = function() { 
    var activeObject = canvas.getActiveObject(); 
    if (activeObject) { 
     canvas.bringToFront(activeObject); 
    } 
    }; 

document.getElementById('saveImg').onclick = function() { 
    if (!fabric.Canvas.supports('toDataURL')) { 
     alert('This browser doesn\'t provide means to serialize canvas to an image'); 
    } 
    else { 
    canvas.deactivateAll().renderAll(); 
     window.open(canvas.toDataURL('png')); 
    } 
    }; 



canvas.on('selection:cleared', function(options) { 
    var activeObject = canvas.getActiveObject(); 
     if (activeObject === null) { 
       $("#imageControl").fadeOut('slow'); 
      } 
}); 

canvas.on('object:selected', function(options) { 
    var activeObject = canvas.getActiveObject(); 
     if (options.target && activeObject !== null) { 
        $("#imageControl").show('slow'); 
       } 
}); 

canvas.on('selection:created', function(options) { 
     if (options.target) { 
        $("#imageControl").show('slow'); 
       } 
}); 


if (Modernizr.draganddrop) { 
    // Browser supports HTML5 DnD. 

    // Bind the event listeners for the image elements 
    var images = document.querySelectorAll('#images img'); 
    [].forEach.call(images, function (img) { 
     img.addEventListener('dragstart', handleDragStart, false); 
     img.addEventListener('dragend', handleDragEnd, false); 
    }); 
    // Bind the event listeners for the canvas 
    var canvasContainer = document.getElementById('canvas-container'); 
    canvasContainer.addEventListener('dragenter', handleDragEnter, false); 
    canvasContainer.addEventListener('dragover', handleDragOver, false); 
    canvasContainer.addEventListener('dragleave', handleDragLeave, false); 
    canvasContainer.addEventListener('drop', handleDrop, false); 

} else { 
    // Replace with a fallback to a library solution. 
    alert("This browser doesn't support the HTML5 Drag and Drop API."); 
} 

} 

HTML:

<div id="images"> 
<img draggable="true" src="http://i.imgur.com/q9aLMza.png" width="70" height="90"></img>  
</div> 

<div id="canvas-container-controls"> 
<div id="imageControlWrapper"> 
<div id="imageControl"> 
<button class= "imgBtn" id="saveImg">Save Canvas</button> 
<button class="imgBtn" id="remove-selected">Remove selected object/group</button> 
<button id="send-backwards" class="imgBtn">Send backwards</button> 
<button id="send-to-back" class="imgBtn">Send to back</button> 
<button id="bring-forward" class="imgBtn">Bring forwards</button> 
<button id="bring-to-front" class="imgBtn">Bring to front</button> 
</div> 
</div> 

<div id="canvas-container"> 
    <canvas id="canvas" width="400" height="400"></canvas> 
</div> 
</div> 
+0

如果添加了撥弄你的代碼這將是有益的。 –

+0

對不起,這是一個鏈接http://jsfiddle.net/bQxMu/ – JustAnotherDude

+0

下一次,如果你找出自己的問題,你可以隨時添加你的答案並將其標記爲已接受。 :) –

回答

6

在handleDrop添加e.preventDefault():http://jsfiddle.net/wN29y/

function handleDrop(e) { 
// this/e.target is current target element. 
e.preventDefault(); 
if (e.stopPropagation) { 
    e.stopPropagation(); // stops the browser from redirecting. 
} 

var img = document.querySelector('#images img.img_dragging'); 

console.log('event: ', e); 

var newImage = new fabric.Image(img, { 
    width: img.width, 
    height: img.height, 
    // Set the center of the new object based on the event coordinates relative 
    // to the canvas container. 
    left: e.layerX, 
    top: e.layerY 
}); 
canvas.add(newImage); 

return false; 
} 
+0

....我添加了您建議的代碼,但它仍然無法阻止重定向。 – JustAnotherDude

+0

抱歉沒有看到你的編輯最高。 –

+0

謝謝:)尋求幫助。 – JustAnotherDude