画像をキャンバスにドラッグ アンド ドロップできるページを修正しました。1つを除いて、私が望むすべてを行います。私は複数の方法を試しました (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、タブ、コンテンツなどを追加しました。失いたくない機能は、単一の画像をキャンバスに複数回ドラッグ (クローン) する機能です。
この機能を作成する方法に関するアイデア/例/ポインタはありますか?