我修改了一個頁面,可以將圖像拖放到畫布上。它做我想要的一切,除了一個。我已經嘗試了多種方法(包括腳本,例如Kinetic和Raphael,我仍然認爲這可能是路線),但已經結束:如何將圖像拖放到HTML5 Canvas後拖動?
一旦圖像被丟棄,我無法將其拖動到帆布到一個新的位置。
function drag(e)
{
//store the position of the mouse relativly to the image position
e.dataTransfer.setData("mouse_position_x",e.clientX - e.target.offsetLeft);
e.dataTransfer.setData("mouse_position_y",e.clientY - e.target.offsetTop );
e.dataTransfer.setData("image_id",e.target.id);
}
function drop(e)
{
e.preventDefault();
var image = document.getElementById(e.dataTransfer.getData("image_id"));
var mouse_position_x = e.dataTransfer.getData("mouse_position_x");
var mouse_position_y = e.dataTransfer.getData("mouse_position_y");
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// the image is drawn on the canvas at the position of the mouse when we lifted the mouse button
ctx.drawImage(image , e.clientX - canvas.offsetLeft - mouse_position_x , e.clientY - canvas.offsetTop - mouse_position_y);
}
function convertCanvasToImage() {
var canvas = document.getElementById('canvas');
var image_src = canvas.toDataURL("image/png");
window.open(image_src);
}
這裏是一個的jsfiddle,我作爲我最初的起點 - http://fiddle.jshell.net/gael/GF96n/4/(拖動的jsfiddle標誌到畫布上,然後嘗試將其移動)。我已經在我的工作頁面中添加了CSS,標籤,內容等。我不想失去的功能是能夠將單個圖像多次(克隆)拖到畫布上。
有關如何創建此功能的任何想法/示例/指針?
我做了同樣的功能。爲此,您需要在拖動的位置一次又一次地繪製圖像。 – MJQ