如何将图像拖放到 HTML5 Canvas 上?

2024-02-05

我修改了一个页面,可以将图像拖放到画布上。它可以做我想要的一切,除了一个。我尝试了多种方法(包括脚本,例如 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/ http://fiddle.jshell.net/gael/GF96n/4/(将 JSFiddle 徽标拖到画布上,然后尝试移动它)。此后,我将 CSS、选项卡、内容等添加到我几乎可以工作的页面中。我不想失去的功能是将单个图像多次拖动(克隆)到画布上的功能。

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


您需要对代码进行一些更改,而不是立即将图像绘制到画布上,您需要跟踪所有丢弃的图像。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;
    }

}

The 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/ http://jsfiddle.net/XU2a3/41/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将图像拖放到 HTML5 Canvas 上? 的相关文章

随机推荐