我修改了一个页面,可以将图片拖放到画布上。它完成了我想要的所有功能,除了一个问题。我尝试了多种方法(包括脚本,如Kinetic和Raphael,我仍然认为这可能是正确的路线),但都陷入了困境:
一旦图片被放下,我无法将其在画布上拖动到新位置。
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、选项卡、内容等几乎可以工作的页面。我不想失去的功能是能够将单个图像多次拖动(克隆)到画布上的能力。你有什么创意/示例/指针来创建这个功能吗?