我修改了一个页面,可以将图像拖放到画布上。它做我想要的一切,除了一个。我已经尝试了多种方法(包括脚本,例如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