Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda