1

The p5.js line() function does not seem to be able to draw a true 1-pixel wide black line. (Or any other color.)

  • strokeWeight is 1 pixel, but the resulting line is 2 pixels wide.
  • color is black with no transparency, but the resulting color is gray.

blendMode() seems to have something to do with the funny results above. I tried REPLACE, but it didn't have the effect I expected. (Only the last shape seems to survive?)

How can I draw a 1-pixel wide line in p5.js with the "true" stroke color? Is this a limitation of the underlying canvas element?

A simple p5.js demo is included in the snippet below. This problem can also be seen in official p5.js examples:

function setup() {
  createCanvas(400, 100);
  
  background(200);
    
  stroke(0,0,0, 255); // Black color, no transparancy.
  
  strokeWeight(1);   // Default
  line(20, 20, 380, 20);
  
  strokeWeight(2);   // 2
  line(20, 40, 380, 40);
  
  strokeWeight(4);   // Thicker
  line(20, 60, 380, 60); 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>


Zoom in and verify pixels/colors with color picker: <input type=color>
Leftium
  • 16,497
  • 6
  • 64
  • 99
  • Not quite p5, but essentially the same concept: [Draw single pixel line in html5 canvas](https://stackoverflow.com/questions/9311428/draw-single-pixel-line-in-html5-canvas) – ggorlen Jul 01 '22 at 13:53
  • p5.js uses webgl to draw the shapes. In fact, a line primitive is rendered. Specify coordinates in the "center" of the fragments (pixels). Unfortunately, that depends on your system's DPI scale. – Rabbid76 Jul 01 '22 at 13:56
  • 2
    I think you have to use context.translate(.5,.5); for your canvas context – Pete Pearl Jul 01 '22 at 14:02
  • 2
    Oh, [translate()](https://p5js.org/reference/#/p5/translate) is much nicer than adding 0.5 to all my coordinates! @PetePearl – Leftium Jul 01 '22 at 14:05

0 Answers0