HTML5 Draggable setDragImage不适用于Chrome上的画布

时间:2017-05-04 18:12:20

标签: javascript html5 google-chrome html5-canvas html5-draggable

我正在尝试使用canvas作为本机HTML5拖放API中的拖动图像。

问题是它适用于Firefox但不适用于Chrome(58),我无法查明问题。

代码:https://jsfiddle.net/196urLwd/5/

<div id="thing" draggable="true">DRAG ME</div>
function onDragStart(event){

    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');

    canvas.width = 100;
    canvas.height = 20;

    context.fillStyle = '#333333';
    context.fillRect(0, 0, canvas.width, canvas.height);

    context.fillStyle = '#999999';
    context.font = 'bold 13px Arial';
    context.fillText('DRAGGING...', 5, 15);

    event.dataTransfer.setData('text', 'lorem ipsum');
    event.dataTransfer.effectAllowed = 'copy';    
    event.dataTransfer.setDragImage(canvas, -15, 9);

};

var theThing = document.getElementById('thing');
theThing.addEventListener('dragstart', onDragStart, false);

我做错了什么?

1 个答案:

答案 0 :(得分:3)

Chrome似乎要求画布位于dom

document.body.append(canvas);

然后用一些css隐藏它

canvas{
  position:absolute;
  left:-100%;
}

https://jsfiddle.net/196urLwd/6/