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

136
Visualizações
Unpredictable behaviour of HTML canvas

I observe weird behaviour of HTML canvas both in chrome and in firefox. For some reason there is one-pixel wide palid line when I fill rect over the smaller rect of different color after clipping.

Here is JS fiddle snippet: https://jsfiddle.net/srkgbxw1/7/

var my_canvas = document.getElementById('canvas'),
    context = my_canvas.getContext("2d");

context.translate(0.5, 0.5)

context.fillStyle = "orange";
context.fillRect(10,10,100,100);

context.beginPath();
context.rect(20,20,90,90);
context.clip();

context.fillStyle = "white";
context.fillRect(0,0,110,110);

context.fillStyle = "orange";
context.fillRect(0,0,190,190);

This is the result: enter image description here

Please help me figure out what the reason for the pallid line after third fillRect with orange?

UPD: original question's changed after I was pointed out I had made a stupid mistake in its first edition

UPD 2 I got the answer, this happens due to color interpolation at edges of clipping region because context was translated for half a pixel, which was done to get thin lines (recommended technique). To avoid interpolation, clipping region should be adjusted for half a pixel as well, then the pallid line disappears.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you look into the logs:

TypeError: context.setStrokeStyle is not a function

Therefore, when you comment it, everything after it is executed while when you uncomment it, it fails and everything after this line is not executed.

EDIT: For the second question issue:

The issue is equivalent to this, why the blue square does not fully overlap the red square:

context.translate(0.5, 0.5);

context.fillStyle = "red";
context.fillRect(10,10,100,100);
context.fillStyle = "blue";
context.fillRect(10,10,100,100);

Because of translate, you are now drawing in float pixels, everything moved by half a pixel. I suggest you read this for more information about how it is handled: fillRect() not overlapping exactly when float numbers are used

Removing context.translate(0.5, 0.5); removes the "palid line".

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