Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Making a chessboard pattern with canvas

Attempting to make a chessboard with JavaScript using canvas, but the result is not what I am expecting.

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
//ctx.fillRect(0, 0, canvas.width, canvas.height);

class Square {
    constructor(space, color, size) {
        ...
    }

    draw(x, y) {
        ctx.fillStyle = this.color
        ctx.fillRect(x, y, this.size, this.size)
    }
}

class Game {
    constructor() {
        this.squares = createBoard()
    }

    createPattern(length, size) {
        for (let i = 0; i < length; i++) {
            var xOffset = size * (i + 1)
            let color = ''

            for (let j = 0; j < length; j++) {
                var yOffset = size * i
                if (i % 2 === 0 && j % 2 === 0) {
                    color = '#FFE9C4'
                } else {
                    color = '#FFD58F'
    
                    if (i % 2 != 0 && j % 2 != 0) {
                        color = '#FFE9C4'
                    }
                }

                const square = new Square(this.squares[(i*8) + j], color, 80)
                square.draw(xOffset, yOffset)
            }
        }
    }
}

...
game.createPattern(8, 80)

The resulting board does not display a square at the expected intervals. Unsure where I misstepped.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!