3

画像をキャンバスにドラッグ アンド ドロップできるページを修正しました。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、タブ、コンテンツなどを追加しました。失いたくない機能は、単一の画像をキャンバスに複数回ドラッグ (クローン) する機能です。

この機能を作成する方法に関するアイデア/例/ポインタはありますか?

4

2 に答える 2

6

画像をすぐにキャンバスに描画するのではなく、コードにいくつかの変更を加える必要があります。ドロップされたすべての画像を追跡する必要があります。imagesOnCanvasドロップされたすべての画像で埋められます。

var imagesOnCanvas = [];

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');

    imagesOnCanvas.push({
      context: ctx,  
      image: image,  
      x:e.clientX - canvas.offsetLeft - mouse_position_x,
      y:e.clientY - canvas.offsetTop - mouse_position_y,
      width: image.offsetWidth,
      height: image.offsetHeight
    });

}

imagesOnCanvasまた、すべての画像を調べて順番に描画するアニメーション ループも必要です。requestAnimationFrameこれを達成するために使用されます。

function renderScene() {
    requestAnimationFrame(renderScene);

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0,0,
        canvas.width,
        canvas.height
    );


    for(var x = 0,len = imagesOnCanvas.length; x < len; x++) {
        var obj = imagesOnCanvas[x];
        obj.context.drawImage(obj.image,obj.x,obj.y);

    }
}

requestAnimationFrame(renderScene);

mousedown次に、キャンバスでイベントを監視する必要があります。イベントが画像で発生した場合は、startMoveアクションを呼び出すことができます

canvas.onmousedown = function(e) {
    var downX = e.offsetX,downY = e.offsetY;

    // scan images on canvas to determine if event hit an object
    for(var x = 0,len = imagesOnCanvas.length; x < len; x++) {
        var obj = imagesOnCanvas[x];
        if(!isPointInRange(downX,downY,obj)) {
            continue;
        }

        startMove(obj,downX,downY);
        break;
    }

}

isPointInRange画像オブジェクトでマウス イベントが発生した場合、関数は true を返します。

function isPointInRange(x,y,obj) {
    return !(x < obj.x ||
        x > obj.x + obj.width ||
        y < obj.y ||
        y > obj.y + obj.height);
}

「移動モード」がアクティブになると、オブジェクトの x/y 座標が変更され、新しいマウス位置が反映されます。

function startMove(obj,downX,downY) {
    var canvas = document.getElementById('canvas');

    var origX = obj.x, origY = obj.y;
    canvas.onmousemove = function(e) {
        var moveX = e.offsetX, moveY = e.offsetY;
        var diffX = moveX-downX, diffY = moveY-downY;


        obj.x = origX+diffX;
        obj.y = origY+diffY;
    }

    canvas.onmouseup = function() {
        // stop moving
        canvas.onmousemove = function(){};
    }
}

ここでの作業例:

http://jsfiddle.net/XU2a3/41/

于 2012-11-27T08:42:20.940 に答える