HTML5 Game - Loading an Image into a canvas Element

Description

Loading an Image into a canvas Element

Demo

ResultView the demo in separate window

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Canvas Demo</title> 
        <style> 
canvas { //w  w  w .ja  v  a 2s .  c  om
  border: 1px solid #000; 
} 
        </style> 
    </head> 
    <body> 
      <canvas id="myCanvas" width="200" height="200">Did You Know: Every time 
        you use a browser that doesn't support HTML5
      </canvas> 
      <script> 
let myCanvas = document.getElementById('myCanvas'); 
let myContext = myCanvas.getContext('2d'); 
  
let myImage = new Image(); 
myImage.src = 'http://java2s.com/style/download.png'; 
  
myImage.onload = function() { 
  myContext.drawImage(myImage, 0, 0); 
}; 
      </script> 
    </body> 
</html>

Related Topic