gpt4 book ai didi

javascript - “Text on the canvas movable by dragging text to their desired position?”

转载 作者:行者123 更新时间:2023-11-30 08:46:43 26 4
gpt4 key购买 nike

在线程不成功后,我再次寻求帮助。如何让用户通过将文本拖动到所需位置来在 Canvas 上添加一些文本?比方说,他们以某种方式在某处输入文本,它出现在 Canvas 上,然后用户可以将该文本拖动到所需位置。我应该使用哪种技术?有没有人可以快速编写代码?

最佳答案

下面是如何在 html Canvas 上拖动文本的概述:

  • 创建一个文本对象,其中包含有关该词的信息(文本、x、y、宽度、高度)。
  • 创建一个数组来保存所有文本对象
  • 在鼠标按下时, HitTest 数组中的每个文本对象以查看用户是否在文本上按下鼠标
  • 在 mousemove 上,将所选文本移动用户拖动鼠标的距离

演示:http://jsfiddle.net/m1erickson/tLvMK/

这里是注释代码:

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
body{ background-color: ivory; }
#canvas{border:1px solid red;}
</style>

<script>
$(function(){

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

// variables used to get mouse position on the canvas
var $canvas=$("#canvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var scrollX=$canvas.scrollLeft();
var scrollY=$canvas.scrollTop();

// variables to save last mouse position
// used to see how far the user dragged the mouse
// and then move the text by that distance
var startX;
var startY;

// some text objects
var texts=[];

// some test texts
texts.push({text:"Hello",x:20,y:20});
texts.push({text:"World",x:20,y:70});

// calculate width of each text for hit-testing purposes
ctx.font="16px verdana";
for(var i=0;i<texts.length;i++){
var text=texts[i];
text.width=ctx.measureText(text.text).width;
text.height=16;
}

// this var will hold the index of the selected text
var selectedText=-1;

// START: draw all texts to the canvas
draw();

// clear the canvas draw all texts
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
for(var i=0;i<texts.length;i++){
var text=texts[i];
ctx.fillText(text.text,text.x,text.y);
}
}

// test if x,y is inside the bounding box of texts[textIndex]
function textHittest(x,y,textIndex){
var text=texts[textIndex];
return(x>=text.x &&
x<=text.x+text.width &&
y>=text.y-text.height &&
y<=text.y);
}

// handle mousedown events
// iterate through texts[] and see if the user
// mousedown'ed on one of them
// If yes, set the selectedText to the index of that text
function handleMouseDown(e){
e.preventDefault();
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);

// Put your mousedown stuff here
for(var i=0;i<texts.length;i++){
if(textHittest(startX,startY,i)){
selectedText=i;
}
}
}

// done dragging
function handleMouseUp(e){
e.preventDefault();
selectedText=-1;
}

// also done dragging
function handleMouseOut(e){
e.preventDefault();
selectedText=-1;
}

// handle mousemove events
// calc how far the mouse has been dragged since
// the last mousemove event and move the selected text
// by that distance
function handleMouseMove(e){
if(selectedText<0){return;}
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);

// Put your mousemove stuff here
var dx=mouseX-startX;
var dy=mouseY-startY;
startX=mouseX;
startY=mouseY;

var text=texts[selectedText];
text.x+=dx;
text.y+=dy;
draw();
}

// listen for mouse events
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});

}); // end $(function(){});
</script>

</head>

<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

关于javascript - “Text on the canvas movable by dragging text to their desired position?”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21411550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com