Note that there are some explanatory texts on larger screens.

plurals
  1. POdrag and drop multiple images from one canvas to other
    primarykey
    data
    text
    <p>How do I drag and drop multiple images from one canvas to another? My manager has given me this task, it's been 3 days and I am not able to do this as I'm new to HTML5. I've searched Google but only got it working for one image. Please help me in this matter.</p> <p>This is what I got for one image:</p> <pre><code>&lt;pre&gt; &lt;script&gt; window.onload = function () { var canvas1 = document.getElementById("cvs1"); var canvas2 = document.getElementById("cvs2"); var context1 = canvas1.getContext('2d'); var context2 = canvas2.getContext('2d'); var imageXY = {x: 5, y: 5}; /** * This draws the image to the canvas */ function Draw () { //Clear both canvas first context1.clearRect(0,0,canvas1.width,canvas1.height); context2.clearRect(0,0,canvas2.width,canvas2.height); //Draw a red rectangle around the image if (state &amp;&amp; state.dragging) { state.canvas.getContext('2d').strokeStyle = 'red'; state.canvas.getContext('2d').strokeRect(imageXY.x - 2.5, imageXY.y - 2.5, state.image.width + 5, state.image.height + 5); } // Now draw the image state.canvas.getContext('2d').drawImage(state.image, imageXY.x, imageXY.y); } canvas2.onclick = canvas1.onclick = function (e) { if (state &amp;&amp; state.dragging) { state.dragging = false; Draw(); return; } var mouseXY = RGraph.getMouseXY(e); state.canvas = e.target; if ( mouseXY[0] &gt; imageXY.x &amp;&amp; mouseXY[0] &lt; (imageXY.x + state.image.width) &amp;&amp; mouseXY[1] &gt; imageXY.y &amp;&amp; mouseXY[1] &lt; (imageXY.y + state.image.height)) { state.dragging = true; state.originalMouseX = mouseXY[0]; state.originalMouseY = mouseXY[1]; state.offsetX = mouseXY[0] - imageXY.x; state.offsetY = mouseXY[1] - imageXY.y; } } canvas1.onmousemove = canvas2.onmousemove = function (e) { if (state.dragging) { state.canvas = e.target; var mouseXY = RGraph.getMouseXY(e); // Work how far the mouse has moved since the mousedon event was triggered var diffX = mouseXY[0] - state.originalMouseX; var diffY = mouseXY[1] - state.originalMouseY; imageXY.x = state.originalMouseX + diffX - state.offsetX; imageXY.y = state.originalMouseY + diffY - state.offsetY; Draw(); e.stopPropagation(); } } /** * Load the image on canvas1 initially and set the state up with some defaults */ state = {} state.dragging = false; state.canvas = document.getElementById("cvs1"); state.image = new Image(); state.image.src = 'images/logo.png'; state.offsetX = 0; state.offsetY = 0; state.image.onload = function () { Draw(); } } &lt;/script&gt; &lt;canvas id="cvs1" width="400" height="125" style="float: left"&gt;[No canvas support]&lt;/canvas&gt; &lt;canvas id="cvs2" width="400" height="125" style="float: left; margin-left: 100px"&gt;[No canvas support]&lt;/canvas&gt; &lt;/pre&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.
 

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