Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to zoom into canvas, it means you have to <strong>redraw</strong> it with zoom.</p> <p>So instead of drawing pixels on click right onto canvas, which is made of pure pixels... You need to first create some representation of your grid of pixels.</p> <pre><code>var gridOfPixels = []; </code></pre> <p>Let's say you are ok with static size for now. Make it 8x8 pixels. At start you want to initialize your array:</p> <pre><code>for (var i=0; i &lt; 8*8; i++) gridOfPixels[i] = 0; </code></pre> <p>So the grid canvas is ready, now we need to draw it.</p> <pre><code>function renderGrid() { for (var y=0; y &lt; 8; y++) for (var x=0; x &lt; 8; x++) renderPixel( x, y, gridOfPixels[x+y*8] ); } </code></pre> <p>You already know how to renderPixel - calculate the rectangle position (posX = x*pixWidth, posY*pixHeight), where pixWidth is canvasWidth/8, etc.. Now you draw all your pixels, using the third parameter for the color.</p> <p>To finish, you have to connect onclick to put a pixel on grid, and then call renderGrid so the user sees the change.</p> <pre><code>$('#my-canvas').click(function(e) { var x = ...; var y = ...; // calculate the position of pixels from mouse position inside canvas // dont forget to check that x,y are in the 0-7 range // dont forget to convert x,y to whole number using parseInt() gridOfPixels[x+y*8] = 1; renderGrid(); // update the grid canvas }); </code></pre> <p>Now, every time you resize the canvas or change some variables, the original canvas content will be saved in your grid, and you can renderGrid() any time you need to. You could even do it in realtime, animating the color of the pixels, etc..</p> <p>Have fun. :)</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.
    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.
    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