Workshop › Chess › Step 2 of 48
A chess board is 8 × 8 squares of 56 pixels. We name the sizes, then draw the top row: 8 squares side by side,
light and dark in turn, using a for loop.
```js
const SQ = 56 // one square
const LEFT = 16
const TOP = 56 // room for the messages
for (let c = 0; c < 8; c++) {
const x = LEFT + c * SQ
const y = TOP
ctx.fillStyle = c % 2 === 0 ? '#e7d8b8' : '#b58863'
ctx.fillRect(x, y, SQ, SQ)
}
```SQ is the size of a square; LEFT and TOP leave a margin (the top one will hold messages).for (let c = 0; c < 8; c++) counts c from 0 to 7: the column.x = LEFT + c * SQ moves each square 56 pixels further right.c % 2 is the remainder after dividing by 2: 0 for even columns, 1 for odd ones. a ? b : c picks b if a is
true, else c: so the colors alternate.const ctx = ... leave an empty line and write the three sizes.draw, after the background, leave an empty line and write the loop. Press Run.Skills: Drawing on a canvas, Loops
← A dark table All 64 squares →