Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you “move” an object, what you really need to do is erase the object and redraw it</p> <p>First code a function that will redraw the rect at a specified x,y</p> <pre><code>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(); } </code></pre> <p>Then handle mousedown events and call the draw function</p> <p>This example uses jquery for cross-browser compatibility, but you can always recode using native javascript.</p> <pre><code>// 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); } </code></pre> <p>Here is code and a Fiddle: <a href="http://jsfiddle.net/m1erickson/GHSG4/">http://jsfiddle.net/m1erickson/GHSG4/</a></p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /&gt; &lt;!-- reset css --&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt; body{ background-color: ivory; } canvas{border:1px solid red;} &lt;/style&gt; &lt;script&gt; $(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(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Click to redraw the rect at the mouse position&lt;/p&gt; &lt;p id="downlog"&gt;Down&lt;/p&gt; &lt;canvas id="canvas" width=300 height=300&gt;&lt;/canvas&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload