$(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);
}
});
我嘗試了很多東西,但是失敗了。該代碼允許我將多個圖像拖放到畫布元素上。我需要做的是增加拖放圖像後再次拖放的可能性。我知道畫布必須每次重繪,但我不知道如何。在將圖像放到畫布上後允許拖動
任何人都可以解決這個問題嗎?
是什麼意思喲* '再次拖動圖像' *?你的意思是在畫布上開始拖動一次? –
我拖動的圖像不在畫布元素中。我從畫布外面(實際上是手風琴)拖放到畫布元素上。所以,一旦他們進入畫布,我不能再移動它們,我需要它們可以拖動,改變它們的位置(爲了應用程序的靈活性)。 –
你的意思是說,一旦你放棄它們,它們將從手風琴中移除,但是你希望它們仍然存在,所以你可以再次拖動它們? –