Note that there are some explanatory texts on larger screens.

plurals
  1. POSeamless drawing in multiple canvas
    primarykey
    data
    text
    <p>I'm trying to make a simple drawing tool using JS and Canvas element. My problem is that I would like to have several canvases and user should be able to draw one line through all of them. Here's a little page I did:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var act = null; var context = null; var draw = false; var c = false; function boot() { $('.can') .mouseenter(function(){ act = this; context = act.getContext('2d'); // console.log(this); }) .mouseleave(function(){ act = null; context = null; // console.log('out'); }) .mousedown(function(){ draw = true; }) .mouseup(function(){ draw = false; }) .mousemove(function(ev){ // console.log(act); if (ev.layerX || ev.layerX == 0) { // Firefox x = ev.layerX; y = ev.layerY; } else if (ev.offsetX || ev.offsetX == 0) { // Opera x = ev.offsetX; y = ev.offsetY; } if(draw &amp;&amp; context != null) if (!c) { context.beginPath(); context.moveTo(x, y); c = true; } else { context.lineTo(x, y); context.stroke(); } }); } $(document).ready(boot); &lt;/script&gt; &lt;style&gt; .can {border: 1px solid blue; display:block; float:left; margin:0;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas class="can" id="c2" width="200" height="200"&gt;&lt;/canvas&gt; &lt;canvas class="can" id="c1" width="200" height="200"&gt;&lt;/canvas&gt; &lt;canvas class="can" id="c3" width="200" height="200"&gt;&lt;/canvas&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And it partially works: I can draw only in the first canvas. I debugged it and i got really confused, because the context changes as expected and drawing is enabled only in the first canvas.</p> <p>Any ideas what's the cause of such behavior? </p>
    singulars
    1. This table or related slice is empty.
    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