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

174
Views
Issues scaling a js canvas pattern

I need a large rectangle filled with a checkerboard pattern and it must be able to scale. When I draw every square one-by-one, the final result is what I want, but it is a really inefficient way to draw once I have thousands of squares to create every frame. To counter this, I decided to create a pattern canvas and just fill the entire region with a simple pattern.

Here is an example of both methods: https://jsfiddle.net/Stephen_Witwick/zpunh195/150/

The black checkerboard pattern on the left is the one created with the pattern canvas and the red checkerboard is created by drawing each individual square.

This is the code that works but is slow:

//unitWidth and unitHeight are the dimensions of each square. Since the checkerboard uses squares, they are the same value
//boxWidthUnits and boxHeightUnits are the dimensions of the checkerboard by number of squares

for(let i = 0; i < boxWidthUnits/2; i++){
    for(let j = 0; j < boxHeightUnits/2; j++){
        ctx.fillRect(2*i*unitWidth,2*j*unitHeight,unitWidth,unitHeight);
        ctx.fillRect(2*(i+1/2)*unitWidth,2*(j+1/2)*unitHeight,unitWidth,unitHeight)
    }
}

Is there an way for me to get the result from the red checkerboard without it being so slow?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is because most of the time the unitWidth/height is a fraction of a pixel but the pattern size must be a round number (because it is an image).

for example when n=15 the unit size is 10.375 but the patternCanvas size is round to 20x20. in this case each pattern is missing 0.75px * 10 patterns that fit in the square it is a total of 7.5px.

it is imperceptible to the eye but if you save the canvas image and use an image editor you can see that the pattern is almost always missing a portion to the right/bottom

about 4 years ago · Juan Pablo Isaza Report
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!