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

134
Views
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 answers
Answer question

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 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!