Last active
June 1, 2016 10:37
-
-
Save oreillyross/451a7c321436e34f987e99f54e42ea0b to your computer and use it in GitHub Desktop.
Draw a chessboard on canvas object using new HTML5 api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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