Raphael.JS:如何在raphael纸上拖放图像dom元素

时间:2015-09-06 10:26:34

标签: javascript jquery html5 drag-and-drop raphael

我有带有图像集的html工具箱。我想将它们拖放到raphael画布上。使用jquery ui或html5我想我可以拖放,但是当它被丢弃时如何使工具箱html元素(图像)raphael对象?

我需要将html elem传递给raphael的函数:

var r = Raphael(0, 0, 800, 800);

raiseMeWhenDropped(html_elem, e) {
    // create image on canvas area when dropped from toolbox
    r.image(html_elem.src, e.mouseXdropped, e.mouseYdropped, 30, 30);
}

1 个答案:

答案 0 :(得分:1)

最简单的方法可能是使用HTML拖放功能,并使您的功能成为handler drop事件的drop event。您可以将canvas添加到paper的{​​{1}}元素中。像这样举例如:

var r = Raphael(0, 60, 800, 800);
var canvas = r.canvas;
var dragImage = document.getElementById('test');

dragImage.addEventListener('dragstart', function (e) {
    dragEvent = e;
});

canvas.addEventListener('dragover', function (e) {
    e.preventDefault();
});

canvas.addEventListener('drop', function (e) {
    e.preventDefault();
    r.image(dragEvent.target.src, e.offsetX - dragEvent.offsetX, e.offsetY - dragEvent.offsetY, dragEvent.target.clientWidth, dragEvent.target.clientHeight);
})
svg
{
  background-color: #ddd;
}

img
{
  cursor: pointer;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>

<img draggable="true" id="test" src="https://www.gravatar.com/avatar/a4d924e65b84f1572ea3598437aa5559?s=48&d=identicon&r=PG&f=1" /><span>You can drop the image on the grey zone</span>