HTML5画布移动对象到鼠标点击位置

4
基本上,我想将一个物体从点A(10x,10y)移动到画布上鼠标点击的位置(255x,34y)。
目前我正在使用一种方法,但它是从++X然后++Y到坐标; 向上然后向右。
我希望它能直接到达目标位置,就像路径上的动画一样。缓慢地从点A到点B。
1个回答

15

当你“移动”一个对象时,你实际上需要做的是擦除该对象并重新绘制它。

首先编写一个函数,在指定的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();
}

然后处理mousedown事件并调用draw函数。

本示例使用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);
}

这里有代码和 Fiddle(在线代码编辑器):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>

我可能应该提一下,我需要让对象像动画一样移动。缓慢地接近坐标。 - Brian
1
好的,所以修改绘制函数,使用requestAnimationFrame逐渐将对象移向目标。您需要在起点和终点之间插值线上的点。这是一个关于requestAnimationFrame的参考链接:http://cookbooks.adobe.com/post_HTML5_requestAnimationFrame_simple_example-19747.html,这是一个关于插值线上的点的参考链接:http://snipplr.com/view/47206/。 - markE
以上网站似乎在奇怪地加载。但如果可以的话,你能否把这个放入播放中?我是新手。看着"Interpolate Points on a Line",那似乎就是我要找的东西。Adobe动画部分已经搞定了。我在setintervaul上有脚本。就像我说的,我有移动物体,但它们是按照X然后Y的基础移动的。我需要X和Y同时运行。就像一条直线从点A到点B。不是先向上,然后向右到目的地。 - Brian
这里有一个演示对象动画的 JSFiddle:http://jsfiddle.net/m1erickson/GHSG4/ - markE
不错,但是有可能吗?原始位置(x0,y0),当用户点击点A(x100,y100)时,然后在对象移动且当前位置为(x50,y50)时再次点击点B(150,150),然后使用此位置(50,50)移动到(150,150)而不是100,100移动到150,150吗? - Swee Hong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接