-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Description
Most appropriate sub-area of p5.js?
- Accessibility (Web Accessibility)
- Build tools and processes
- Color
- Core/Environment/Rendering
- Data
- DOM
- Events
- Friendly error system
- Image
- IO (Input/Output)
- Localization
- Math
- Unit Testing
- Typography
- Utilities
- WebGL
- Other (specify if possible)
Details about the bug:
- p5.js version: v1.2.0 Dec 19, 2020
- Web browser and version: 85.0
- Operating System: Windows 10
- Steps to reproduce this:
Hello, p5 newbie here. I'm trying to draw on top of an image and erase only what I've drawn and not the image. The code I made works fine, but there seems to be weird interaction between erase() and Graphic.
In order to test that erase() function is working normally on canvas, the code I tried is this:
var eraseMode = false;
function setup(){
createCanvas(300, 300);
}
function draw() {
if(keyIsPressed && key == 'e'){
eraseMode = true;
} else {
eraseMode = false;
};
}
function mousePressed(){
if (eraseMode) {
erase();
rect(mouseX, mouseY, 20, 20);
} else {
noErase();
fill(255, 0, 0);
ellipse(mouseX, mouseY, 20, 20);
}
}Basically, when I just click on canvas, a little red ellipse is drawn. When I hold e and click, I erase stuff on canvas with a rect shape. It works fine, even if erase() is the first thing that is called.
It's a different story when I bring in a Graphic. The code is below;
var img;
var pg;
var eraseMode = false;
function preload() {
img = loadImage("./imgs/some_image.png");
}
function setup(){
createCanvas(300, 300);
pg = createGraphics(300, 300);
}
function mousePressed(){
if (eraseMode) {
pg.erase();
pg.rect(mouseX, mouseY, 20, 20);
} else {
pg.noErase();
pg.fill(255, 0, 0);
pg.ellipse(mouseX, mouseY, 20, 20);
}
}
function draw() {
console.log('working')
image(img, 0, 0, 300, 300);
image(pg, 0, 0);
if (keyIsPressed && key == 'e') {
eraseMode = true;
} else {
eraseMode = false;
};
}At first it seems fine - you draw something on Graphic, and then you erase it. But then when erasing is the first thing you do before you draw anything, it throws Uncaught TypeError: this._accessibleOutputs is undefined.
Now, when you try it with the code I provided, even though it throws the error, it will continue to work as if there's no problem. However, before I created the eraseMode variable, I had pg.erase() inside the if (keyIsPressed) statement, and the error stopped everything from working. The draw() function would simply stop working.
To summarize, erase() is working differently on Graphic than it does on Canvas. I don't know if this is an expected behaviour, as I haven't been able to find any documentation saying so.
Can someone confirm;
- whether this is an expected/unexpected behaviour,
- whether the usage of
erase()is wrong in the code above (if it is, can you guide me to the documentation that clearly describes how erase() works? The reference documentation is not very clear on this matter), - whether this behavior occurs on a specific environment (I like to think I'm using run-of-the-mill setup),
- whether there are distinct differences between Canvas and Graphic (and is there any documentation saying so)?
I realize I may be asking a lot of things here, but I want to learn more p5.js stuff and I'd appreciate it greatly if someone could clarify what's happening here.
Thank you very much for your time!!