Currently, I am using p5.js to draw pixels on canvas with a pencil tool. However, I have encountered an issue where the pixel does not appear centered when the size of the pencil is set to 1 or smaller. It seems to be offset towards the left or right.
Upon analyzing our code, I made an interesting observation. It seems that when the mouse's x and y positions are close to .5, the drawn pixel appears centered; but in other cases, it appears slightly off-center. To address this issue, I plan to enhance our rendering logic by ensuring the pixel density is set to 1 to avoid any scaling problems. Additionally, I will explore adjusting the pixel position by introducing a correction factor, potentially subtracting 0.5 from both x and y coordinates. It's important to review any transformations or scaling operations in our code that may affect the precise positioning of pixels.
I have also noticed that P5.image.set method does not accept floating-point numbers; instead, it rounds to the nearest integer. Are there any workarounds for this limitation, or have I overlooked something in the documentation?
Here you can find the code. And here is the link to the deployed application.
To replicate the issue, follow these steps:
- Open the provided link
- Zoom in (using mouse press and wheel)
- Select the pencil tool
- Click on the canvas with your mouse
Thank you in advance for your assistance. Feel free to leave any comments if you need further clarification.
Below is an excerpt of the 'mousepressed' method in which we set the pixel values within a loop:
mousePressed(mouseX, mouseY) {
if (this.menuSelection === 'drawPencil') {
this.mapImage.loadPixels()
let size = 1
let y = mouseY
for (let i = mouseX - size / 2; i < mouseX + size / 2; i++) {
for (let j = y - size / 2; j < y + size / 2; j++) {
this.mapImage.set(i, j, this.p5.color(255))
//console.log('i,j', i, j)
}
}
this.mapImage.updatePixels()
return
}
},