Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
 <head>
 <style>
     #myCanvas1 {
         border: 1px solid black;
     }
 </style>
 </head>
<body onload="init();">   
   <canvas id="myCanvas1" width="300" height=200>Your browser does not support the canvas tag.</canvas>
</body>
</html>
 
var canvas, ctx, grdFrenchFlag;
function init() {
  // Good practice 1: set global vars  canvas, ctx, gradients, etc here
   canvas = document.querySelector('#myCanvas1');
   ctx = canvas.getContext('2d');
  drawCheckboard(5);
}
function setGradient(x, y, width, height) {
    grdFrenchFlag = ctx.createLinearGradient(x, y, width, height);
              
  grdFrenchFlag.addColorStop(0, "blue"); 
  grdFrenchFlag.addColorStop(0.5, "white");
  grdFrenchFlag.addColorStop(1, "red"); 
  ctx.fillStyle = grdFrenchFlag;
}
// n = number of cells per row/column
function drawCheckboard(n) {
  
  var l = canvas.width;
  var h = canvas.height;
  var cellWidth = l / n;
  var cellHeight = h / n;
  
  for(i = 0; i < n; i+=2) {
    for(j = 0; j < n; j++) {
      var x = cellWidth*(i+j%2);
      var y = cellHeight*j;
      setGradient(x, y, x+cellWidth, y+cellHeight);
      ctx.fillRect(x, y, cellWidth, cellHeight); 
    }  
  }
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
micbuffapro
0viewers