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

248
Views
Drawing tiles on canvas has blurry gaps

Example: https://jsfiddle.net/jm1y9c0L/1/

Code:

const context = document.getElementById("canvas").getContext("2d");
const scale = document.getElementById("scale");
const translate = document.getElementById("translate");
scale.value = 17.78598548284369;
translate.value = 10.02842190048295;
function draw() {
    context.fillStyle="#f00";
    context.fillRect(0,0,1,1);
    context.fillRect(1,0,1,1);
}
function update() {
    const s = Number(scale.value);
    const t = Number(translate.value);
    context.clearRect(0,0,100,100);
    context.save();
    context.translate(t,t);
    context.scale(s,s);
    draw();
    context.restore();
}
update();

Question: How do I draw tiles on canvas in a scaled context on canvas without blurry gaps?

A few things to note: If I don't change the background to black, things look ok: https://jsfiddle.net/jm1y9c0L/2/

I think if I draw all tiles on a buffer and draw the buffer on the canvas scaled, it would work. But that has many issues, one being it potentially uses a lot of memory if I'm scaling a very large buffer smaller.

Edit: To explain my goal better, I want to draw two rectangles next to each other with its boundaries being a fraction. So if I draw two rectangles, one in green and one in red with a black background. I want to see the boundary being half green, half red, but not black at all.

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

0

It is because you are using floats and not integers, canvas cant draw 0.78598548284369 of a pixel, I would recommend putting Math.floor() around your scale in the scale() function:

context.scale(Math.floor(s),Math.floor(s));

Hope this helps :D

about 4 years ago · Juan Pablo Isaza Report

0

You can't do subpixel rendering in a browser (at least you can't expect consistent results). In your case, the end of one rectangle isn't necesarily the start of the other, since there may or may not be a pixel-wide gap which comes from rounding.

You might even notice some weird behaviour, such as getting different results in different screen positions, different browsers, when resizing, etc.

The solution is to calculate the value of each pixel yourself, not let the browser do it for you.

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!