title: JavaScript put Image data on top of canvas
body: <p>I have the following code which creates a rectangle as shown in the image below:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
    &lt;canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;"&gt;
      Your browser does not support the HTML5 canvas tag.
    &lt;/canvas&gt;
    &lt;p id='one'&gt;&lt;/p&gt;
  
    &lt;script&gt;
  
      var c = document.getElementById("myCanvas");
      var ctx = c.getContext("2d");
      ctx.fillStyle = 'black';
      ctx.fillRect(20, 20, 40, 40)
      var imgData = ctx.createImageData(100, 100);
      var i;
      for (i = 0; i &lt; imgData.data.length; i += 16) {
        imgData.data[i + 0] = 255;
        imgData.data[i + 1] = 0;
        imgData.data[i + 2] = 0;
        imgData.data[i + 3] = 255;
      }
      // ctx.putImageData(imgData, 10, 10);
  
    &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5zbvB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5zbvB.png" alt="enter image description here"></a></p>
  
  <p><br><hr></p>
  
  <p>When I make sure that the image data is added by not commenting <code>ctx.putImageData</code> , I get only the image data like the picture shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/tuWMU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tuWMU.png" alt="enter image description here"></a></p>
  
  <p><br><hr></p>
  
  <p>How do I make sure that the image is created on top of the black rectangle in the sense that in the white spaces between the red lines?<br>
  I want to be able to see the black rectangle. I tried changing the alpha channel but that did not do anything. </p>
  
  <p>Thanks in advance :).</p>