如何在放入HTML5 Canvas后拖动图像?

Bob*_*seb 3 html5 canvas

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

}
Run Code Online (Sandbox Code Playgroud)

这是一个JSFiddle,我用它作为我的初始起点 - http://fiddle.jshell.net/gael/GF96n/4/(将JSFiddle徽标拖到画布上然后尝试移动它).我已经将CSS,标签,内容等添加到我几乎工作的页面中.我不想丢失的功能是能够将单个图像多次(克隆)拖动到画布上.

关于如何创建此功能的任何想法/示例/指针?

los*_*rce 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
    });

}
Run Code Online (Sandbox Code Playgroud)

您还需要一个动画循环,它将遍历所有图像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);
Run Code Online (Sandbox Code Playgroud)

接下来,您将必须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;
    }

}
Run Code Online (Sandbox Code Playgroud)

isPointInRange如果鼠标事件发生在图像对象上,则该函数返回true

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

一旦"移动模式"激活,对象的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(){};
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的工作示例:

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