如何将项目(如图像或其他画布中的其他对象)拖放到由fabricjs管理的画布中?我已经找到了许多如何在canvas中移动项目的例子,但是我想把项目从外部元素拖放到画布中。
答案 0 :(得分:17)
既然你问过一个例子而我还没有尝试过,那么这就是:
<div id="images">
<img draggable="true" src="http://i.imgur.com/8rmMZI3.jpg" width="250" height="250"></img>
<img draggable="true" src="http://i.imgur.com/q9aLMza.png" width="252" height="295"></img>
<img draggable="true" src="http://i.imgur.com/wMU4SFn.jpg" width="238" height="319"></img>
</div>
<div id="canvas-container">
<canvas id="canvas" width="800" height="600"></canvas>
</div>
Fabric.canvas
实例首先我们想要画布,当然:
var canvas = new fabric.Canvas('c');
不确定这是否必要,因为您拥有画布这一事实使浏览器很可能也具有拖放功能。如果您使用它,可以使用Modernizr:
这样做if (Modernizr.draganddrop) {
// Browser supports HTML5 DnD.
// Bind the event listeners for the image elements
// Bind the event listeners for the canvas
} else {
// Replace with a fallback to a library solution.
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}
同样,与下面的源文章不同,源元素和目标元素是不同的(在文章的例子中,你只是在同一个父容器中移动div
),所以我没注意到一些这些事件适用于被拖动的元素,但大多数事件都绑定到要删除的元素。
注意:我知道这在技术上是一个关于Fabric.js的问题,但在使用Fabric.js向<canvas>
添加对象的上下文中,它实际上是一个关于拖放的问题。这就是为什么我现在要更深入地了解DnD的东西。
对于<img>
dragstart
(我在这里添加了一个类以降低不透明度)dragend
(并在此处删除了该课程) #canvas-container
:
dragenter
(添加了一个类,为画布容器提供了漂亮的虚线)dragover
:您可以在此处设置event.dataTransfer.dropEffect
属性以显示其中一种本机游标类型。这里的默认值为'move'
,但我将其设置为'copy'
,因为我实际上并没有删除<img>
元素(事实上你可以使用它,例如创建几个dragleave
3}})。drop
(此处删除虚线)fabric.Image
:此事件的处理程序创建并添加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.");
}
对象(请参阅小提琴)。{{1}}
来源:
答案 1 :(得分:16)
我经历过@natchiketa的小提琴,并解决了问题,只是检查这个小提琴..
http://jsfiddle.net/Ahammadalipk/w8kkc/185/
window.onload = function () {
var canvas = new fabric.Canvas('canvas');
/*
NOTE: the start and end handlers are events for the <img> elements; the rest are bound to
the canvas container.
*/
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();
}
e.dataTransfer.dropEffect = 'copy';
return false;
}
function handleDragEnter(e) {
this.classList.add('over');
}
function handleDragLeave(e) {
this.classList.remove('over');
}
function handleDrop(e) {
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
var img = document.querySelector('#images img.img_dragging');
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
});
newImage.hasControls = newImage.hasBorders = false;
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');
});
}
if (Modernizr.draganddrop) {
var images = document.querySelectorAll('#images img');
[].forEach.call(images, function (img) {
img.addEventListener('dragstart', handleDragStart, false);
img.addEventListener('dragend', handleDragEnd, false);
});
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 {
alert("This browser doesn't support the HTML5 Drag and Drop API.");
}
}
由于
答案 2 :(得分:3)
嗯,这个问题很老了^^
我已经更新了小提琴,不会在Firefox中也有效。
function handleDrop(e) {
// this / e.target is current target element.
e.preventDefault(); //I've altert this line for FireFox