Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda