Skip to content

Instantly share code, notes, and snippets.

@oreillyross
Last active June 1, 2016 10:37
Show Gist options
  • Save oreillyross/451a7c321436e34f987e99f54e42ea0b to your computer and use it in GitHub Desktop.
Save oreillyross/451a7c321436e34f987e99f54e42ea0b to your computer and use it in GitHub Desktop.
Draw a chessboard on canvas object using new HTML5 api
// n = number of cells per row/column
function drawCheckboard(n) {
grdFrenchFlag = ctx.createLinearGradient(0, 0, 300, 200);
grdFrenchFlag.addColorStop(0, "blue");
grdFrenchFlag.addColorStop(0.5, "white");
grdFrenchFlag.addColorStop(1, "red");
ctx.fillStyle = grdFrenchFlag;
var l = canvas.width;
var h = canvas.height;
var cellWidth = l / n;
var cellHeight = h / n;
for(i = 0; i < n; i++) {
for(j = i % 2; j < n; j++) {
ctx.fillRect(cellWidth*i, cellHeight*j, cellWidth, cellHeight);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment