p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Drawn pixel using pencil on canvas is not centred

Open nilesh9836 opened this issue 1 year ago • 3 comments

Topic

I am drawing pixels using a pencil on canvas using p5.js. The issue is that when the pencil size is 1 and canvas is Zoomed IN in that scenario, its position is not centered. It's coming left sometime right not centered. I've noticed an interesting observation in our code. It appears that when the mouse's x and y positions are close to .5, the drawn pixel appears centered, So I have query that I want to know how image.set() method work internally , like did it accept Floating value too?

nilesh9836 avatar Dec 21 '23 07:12 nilesh9836

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

welcome[bot] avatar Dec 21 '23 07:12 welcome[bot]

Hey @nilesh9836 can you share some code or screenshot for better understanding.Are you talking about following thing.

let img;
let zoom = 2;

function setup() {
createCanvas(400, 400);
img = createImage(400, 400);
}

function draw() {
background(220);

let px = floor(mouseX / zoom);
let py = floor(mouseY / zoom);

img.set(px, py, color(255, 0, 0));

img.updatePixels();

image(img, 0, 0, width * zoom, height * zoom);
}

Vishal2002 avatar Jan 04 '24 16:01 Vishal2002

Yes @Vishal2002 here is the link for my sample app.

nilesh9836 avatar Jan 23 '24 08:01 nilesh9836

@nilesh9836 I'm not quite sure what you mean here and if possible, a screenshot will likely be very helpful. There are subpixel drawing in native HTML canvas and by extension in p5.js and its behaviour may or may not be what you want, if the idea is to avoid subpixel drawing, rounding or flooring the numerical value passed to positional arguments can help.

limzykenneth avatar Mar 01 '24 11:03 limzykenneth