title: Display image in canvas with Javascript?
body: <p>I want to be able to click on a button on my page and load an image into a canvas at some X,Y coordinates?</p>
  
  <p>The following code is what I have below. I would like the image to be in either image/photo.jpg or in the same directory but preferably in a subdirectory of the main page.</p>
  
  <p>**Question: How to make a JPG show up in a canvas with the click of a button on the web page?</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;script&gt;
      function draw(){  
          var ctx = document.getElementById("myCanvas").getContext("2d");  
          var img = new Image():  
        //  img.src = "2c.jpg";  
          img.src = "/images/2c.jpg";  
  
          ctx.drawImage(img,0,0);  
      }
  
  
  &lt;/script&gt;
  
  
  &lt;body background="Black"&gt;
  
  &lt;div align="center"&gt;
      &lt;button type="button" onclick="draw()"&gt;Show Image on Canvas&lt;/button&gt; 
  
      &lt;canvas id="myCanvas" width="900" height="400" style="border:2px solid #d3d3d3;"&gt;
      Your browser does not support the HTML5 canvas tag.
      &lt;/canvas&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
      var c=document.getElementById("myCanvas");
      var ctx=c.getContext("2d");
      ctx.font="20px Arial";
  
      ctx.fillText("Royal Flush     $",500,50);
      ctx.fillText("Striaght Flush  $",500,80);
      ctx.fillText("Flush           $",500,110);
      ctx.fillText("Four of a Kind  $",500,140);
      ctx.fillText("Full House      $",500,170);
      ctx.fillText("Three of a Kind $",500,200);
      ctx.fillText("Two Pair        $",500,230);
      ctx.fillText("Pair of ACES    $",500,260);
  
  
      ctx.rect(495,10,270,350);
      ctx.stroke();
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>March 6th, 2014 Code:</p>
  
  <p>How is the following code not working. Do you have to have an ID tag on Canvas. The page will display but for some reason the image will not when the button is clicked. The image is in the same directory that my index.html file is in.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;style type="text/css"&gt;
      canvas{
         border: 5px solid black;
      }
  &lt;/style&gt;
  
  &lt;/html&gt;
      &lt;button id="galaxy"&gt;Add image #1&lt;/button&gt;
      &lt;button id="circles"&gt;Add image #2&lt;/button&gt;&lt;span&gt;&lt;/span&gt;
      &lt;canvas width="500" height="500"&gt;&lt;/canvas&gt;
  
  &lt;script&gt;
    var Images = {};
  
  function loadImages(list){
      var total = 0;
      document.querySelector("span").innerText = "...Loading...";
      for(var i = 0; i &lt; list.length; i++){
          var img = new Image();
          Images[list[i].name] = img;
          img.onload = function(){
              total++;
              if(total == list.length){
                  document.querySelector("span").innerText = "...Loaded.";
              }
          };
          img.src = list[i].url;
      }
  }
  
  function drawImage(img){
      var ctx = document.querySelector("canvas").getContext("2d");
      ctx.drawImage(Images[img], 0, 0, 50, 50);
  }
  
  loadImages([{
      name: "2c.jpg",
      url: "mp.jpg"
  },{
      name: "mp.jpg",
      url: "mp.jpg"
  }]);
  
  document.querySelector("#galaxy").addEventListener("click", function(){
      drawImage("galaxy");
  });
  
  document.querySelector("#circles").addEventListener("click", function(){
      drawImage("weirdCircles");
  });
  
  &lt;/script&gt;   
  
  &lt;/html&gt;
  </code></pre>