Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>You were very close with your code. Here’s some adjustments that fill in the gaps.</strong></p> <p>[Edited to show a single canvas solution]</p> <p>To avoid drawing “a mound of lines” when the user drags their new line, you <em>must</em> clear the canvas during each mousemove and draw just their latest dragging line. </p> <p>Clearing the canvas will also clear any lines previously drawn by the user, so you must redraw the previous lines with every drag. To do that, you must store enough info about each line to redraw it. </p> <p>For each line you will need the starting and ending points (x1/y1 and x2/y2). You can put these starting and ending points in an object and store the line-objects in an array:</p> <pre><code>// an array to store previous lines var storedLines=[]; // to store a new line storedLines.push( { x1:10, y1:20, x2:50, y2:35 } ); </code></pre> <p>This function redraws all the previously drawn lines</p> <pre><code> function redrawStoredLines(){ ctx.clearRect(0,0,canvas.width,canvas.height); if(storedLines.length==0){ return; } // redraw each stored line for(var i=0;i&lt;storedLines.length;i++){ ctx.beginPath(); ctx.moveTo(storedLines[i].x1,storedLines[i].y1); ctx.lineTo(storedLines[i].x2,storedLines[i].y2); ctx.stroke(); } } </code></pre> <p>Now displaying the user’s dragging line is much easier like this:</p> <pre><code>function handleMouseMove(e){ if(!isDown){ return; } redrawStoredLines(); var mouseX=parseInt(e.clientX-offsetX); var mouseY=parseInt(e.clientY-offsetY); // draw the current line ctx.beginPath(); ctx.moveTo(startX,startY); ctx.lineTo(mouseX,mouseY); ctx.stroke() } </code></pre> <p>Here is code and a Fiddle: <a href="http://jsfiddle.net/m1erickson/NnZ7a/" rel="nofollow">http://jsfiddle.net/m1erickson/NnZ7a/</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; padding:10px; } 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; var storedLines=[]; var startX=0; var startY=0; var isDown; ctx.strokeStyle="orange"; ctx.lineWidth=3; $("#canvas").mousedown(function(e){handleMouseDown(e);}); $("#canvas").mousemove(function(e){handleMouseMove(e);}); $("#canvas").mouseup(function(e){handleMouseUp(e);}); $("#clear").click(function(){ storedLines.length=0; redrawStoredLines(); }); function handleMouseDown(e){ var mouseX=parseInt(e.clientX-offsetX); var mouseY=parseInt(e.clientY-offsetY); isDown=true; startX=mouseX; startY=mouseY; } function handleMouseMove(e){ if(!isDown){ return; } redrawStoredLines(); var mouseX=parseInt(e.clientX-offsetX); var mouseY=parseInt(e.clientY-offsetY); // draw the current line ctx.beginPath(); ctx.moveTo(startX,startY); ctx.lineTo(mouseX,mouseY); ctx.stroke() } function handleMouseUp(e){ isDown=false; var mouseX=parseInt(e.clientX-offsetX); var mouseY=parseInt(e.clientY-offsetY); storedLines.push({x1:startX, y1:startY, x2:mouseX, y2:mouseY}); redrawStoredLines(); } function redrawStoredLines(){ ctx.clearRect(0,0,canvas.width,canvas.height); if(storedLines.length==0){ return; } // redraw each stored line for(var i=0;i&lt;storedLines.length;i++){ ctx.beginPath(); ctx.moveTo(storedLines[i].x1,storedLines[i].y1); ctx.lineTo(storedLines[i].x2,storedLines[i].y2); ctx.stroke(); } } }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Drag to draw lines&lt;/p&gt; &lt;canvas id="canvas" width=300 height=300&gt;&lt;/canvas&gt;&lt;br/&gt; &lt;button id="clear"&gt;Clear Canvas&lt;/button&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