1

我有一个用户可以添加字幕的应用程序,我唯一的问题是我在拖放多个文本时遇到问题。对于通常的 mousedown、mousemove、mouseup 事件,我只能拖放一个文本,我希望能够拖放多个文本,但是我对这个问题没有明确的方法。任何帮助将非常感激。

更新:当我试图拖动两个文本时,我的代码搞砸了,但无论如何我都会发布它。

谢谢

<html>
<body>
<canvas id = 'canvas'></canvas>
<textarea id = 'topCaption'></textarea>
<textarea id = 'bottomCaption'></textarea>
<script type = 'text/javascript'>


window.addEventListener('load',initCanvas,false);

function initCanvas(e)
{
canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
canvas.height = 500;
canvas.width = 500;
mouse = {x:0,y:0};
dragging = false;
topCap = document.getElementById('topCaption');
bottomCap = document.getElementById('bottomCaption');
topX = 100; //top x position
topY = 100; //top y position
botX = 300; //bottom x position
botY = 300; //bottom y position
canvas.addEventListener('mousemove',MouseMove,false);
canvas.addEventListener('mouseup',MouseUp,false);
canvas.addEventListener('mousedown',MouseDown,false);
window.addEventListener('keyup',KeyUp,false);
return setInterval(keyup,10)
}
function clear()
{
context.clearRect(0,0,canvas.width,canvas.height);
}

function text(Caption,x,y)
{
context.fillStyle = '#000';
context.font = '45px Impact';        //'bold 45px impact';
context.textAlign = 'center';
context.lineCap = 'round';
context.lineJoin = 'round';
context.fill();
context.stroke();
context.fillText(Caption,x,y);
};




function MouseMove(event){
mouse.x = event.pageX - canvas.offsetLeft;
mouse.y = event.pageY - canvas.offsetTop;
if(dragging)
{
context.lineTo(mouse.x,mouse.y);
}
}



function MouseDown(event)
{
dragging = true;
setInterval(function(){
topX = mouse.x;
topY = mouse.y;
botX = mouse.x;
botY = mouse.y;
},10)
}

function MouseUp(event)
{
if(dragging)
{
dragging = false;
}
}

function KeyUp(event)
{
clear();
text(topCap.value.toUpperCase(),topX,topY);
text(bottomCap.value.toUpperCase(),botX,botY);

}



</script>
</body>
</html>
4

1 回答 1

1

听起来你通过监听鼠标事件来理解基本的拖动,所以这里是一个拖动多个项目的方法的概要:

监听 mousedown、mouseup 和 mousemove。

如果您在文本边界框内获得 mousedown+mouseup 且中间有 <10px 的 mousemove,则“选择”此文本(可能将其引用添加到“选定”数组)

如果您在鼠标按下后跟随 10+ 像素的鼠标移动,则它是“拖动”(移动“选定”数组中的所有文本)。

于 2013-07-25T17:48:48.880 回答