HTML5 Game - Drawing Rectangles in a Canvas

Description

Drawing Rectangles in a Canvas

Demo

ResultView the demo in separate window

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Canvas Demo</title> 
        <style> 
canvas { /*from  ww w.j  a v  a 2  s. co  m*/
  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> 
// Get the context we will be using for drawing. 
let myCanvas = document.getElementById('myCanvas'); 
let myContext = myCanvas.getContext('2d'); 
  
// Set a stroke style and stroke a rectangle. 
myContext.strokeStyle = 'green'; 
myContext.strokeRect(30, 30, 50, 100); 
  
// Set a fill style and fill a rectangle. 
myContext.fillStyle = 'rgba(200, 100, 75, 0.5)'; 
myContext.fillRect(20, 20, 50, 50); 
  
// Clear a rectangle. 
myContext.clearRect(25, 25, 25, 25); 
      </script> 
    </body> 
</html>

Related Topic