var _canvas = document.getElementById("chessBoard"); function drawChessBoard(canvas) { let ctx = canvas.getContext("2d"); let w = canvas.width; let h = canvas.height; ctx.fillStyle = '#c0c0c0'; for (let r = 0; r < 8; r++) { for (let c = 0; c < 8; c++) { if ((r + c) % 2) ctx.fillRect(w/8*r, h/8*c, w/8, h/8); } } } drawChessBoard(_canvas);