Html HTML5 Canvas 将对象移动到鼠标单击位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16755991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 Canvas moving object to mouse click position
提问by Brian
Basically I want to move an object from point A ( 10x,10y ) to a position on the canvas where the mouse has been clicked ( 255x,34y ).
基本上我想将一个对象从 A 点( 10x,10y )移动到画布上鼠标被点击的位置( 255x,34y )。
I'm currently using a method, but it goes from ++X then ++Y to coordinates; up then right.
我目前正在使用一种方法,但它从 ++X 然后 ++Y 到坐标;向上然后正确。
I want it to go straight to position, likes an animation on a path. Slowing going from point A to Point B.
我希望它直接定位,喜欢路径上的动画。从 A 点到 B 点减速。
回答by markE
When you “move” an object, what you really need to do is erase the object and redraw it
当您“移动”一个对象时,您真正需要做的是擦除该对象并重新绘制它
First code a function that will redraw the rect at a specified x,y
首先编写一个函数,该函数将在指定的 x,y 处重绘矩形
function draw(x,y){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.fillStyle="skyblue";
ctx.strokeStyle="gray";
ctx.rect(x,y,30,20);
ctx.fill();
ctx.stroke();
}
Then handle mousedown events and call the draw function
然后处理mousedown事件并调用draw函数
This example uses jquery for cross-browser compatibility, but you can always recode using native javascript.
此示例使用 jquery 来实现跨浏览器兼容性,但您始终可以使用本机 javascript 重新编码。
// listen for all mousedown events on the canvas
$("#canvas").mousedown(function(e){handleMouseDown(e);});
// handle the mousedown event
function handleMouseDown(e){
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
$("#downlog").html("Down: "+ mouseX + " / " + mouseY);
// Put your mousedown stuff here
draw(mouseX,mouseY);
}
Here is code and a Fiddle: http://jsfiddle.net/m1erickson/GHSG4/
这是代码和小提琴:http: //jsfiddle.net/m1erickson/GHSG4/
<!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");
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
function draw(x,y){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.beginPath();
ctx.fillStyle="skyblue";
ctx.strokeStyle="gray";
ctx.rect(x,y,30,20);
ctx.fill();
ctx.stroke();
}
function handleMouseDown(e){
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
$("#downlog").html("Down: "+ mouseX + " / " + mouseY);
// Put your mousedown stuff here
draw(mouseX,mouseY);
}
$("#canvas").mousedown(function(e){handleMouseDown(e);});
// start the rect at [10,10]
draw(10,10);
}); // end $(function(){});
</script>
</head>
<body>
<p>Click to redraw the rect at the mouse position</p>
<p id="downlog">Down</p>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>