$(function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
// get the offset position of the container
var $canvas = $("#canvas");
var Offset = $canvas.offset();
var offsetX = Offset.left;
var offsetY = Offset.top;
// select all .tool's
var $tools = $(".tool");
// make all .tool's draggable
$tools.draggable({
helper: 'clone',
revert: 'invalid'
});
// assign each .tool its index in $tools
$tools.each(function (index, element) {
$(this).data("toolsIndex", index);
});
// make the canvas a dropzone
$canvas.droppable({
drop: dragDrop,
});
// handle a drop into the canvas
function dragDrop(e, ui) {
// get the drop point (be sure to adjust for border)
var x = parseInt(ui.offset.left - offsetX);
var y = parseInt(ui.offset.top - offsetY);
// get the drop payload (here the payload is the $tools index)
var theIndex = ui.draggable.data("toolsIndex");
// drawImage at the drop point using the dropped image
ctx.drawImage($tools[theIndex], x, y, 32, 32);
}
});
我尝试了很多东西,但是失败了。该代码允许我将多个图像拖放到画布元素上。我需要做的是增加拖放图像后再次拖放的可能性。我知道画布必须每次重绘,但我不知道如何。在将图像放到画布上后允许拖动
任何人都可以解决这个问题吗?
是什么意思哟* '再次拖动图像' *?你的意思是在画布上开始拖动一次? –
我拖动的图像不在画布元素中。我从画布外面(实际上是手风琴)拖放到画布元素上。所以,一旦他们进入画布,我不能再移动它们,我需要它们可以拖动,改变它们的位置(为了应用程序的灵活性)。 –
你的意思是说,一旦你放弃它们,它们将从手风琴中移除,但是你希望它们仍然存在,所以你可以再次拖动它们? –