Βιβλίο
Υποενότητα 9.2: Προηγμένες συναρτήσεις και μετασχηματισμοί στο p5
Υποενότητα 9.2: Προηγμένες συναρτήσεις και μετασχηματισμοί στο p5
- noLoop and loop
- redraw
- clear
- translate and rotate
- random
Συνάρτηση clear
Η clear() είναι μια συνάρτηση p5.js η οποία, όταν καλείται, καθαρίζει όλα όσα βρίσκονται μέσα στον καμβά.
Αυτή η συνάρτηση δεν διαγράφει πραγματικά τα στοιχεία. Τα καθαρίζει καθιστώντας τα διαφανή.
Παράδειγμα
Ακολουθεί ένα παράδειγμα κώδικα που καθαρίζει τον καμβά κάθε φορά που πατάτε το ποντίκι:
function setup() {
createCanvas(windowWidth, windowHeight);
// set background color in the setup so sketches will leave traces
background('white');
}
function draw() {
noStroke();
fill(0, 0, 0, 30);
// ellipse will change according to the mouseX and mouseY position
ellipse(mouseX, mouseY, 30, 30);
}
// clear the canvas every time the user presses the mouse
function mousePressed() {
clear();
}
Μπορείτε να δείτε το αποτέλεσμα του παραπάνω κώδικα here
Exercise
- Open your Visual Studio editor and the
p5yourNamefolder. - Open the file
ex812.jsin your editor and save it asex923.js - Open the file
ex812.htmlin your editor and save it asex923.html - In the
ex923.htmlfile, update the link toex923.jsfrom exersice812.js - Go to the
index.htmlfile and create, underModule 9, alinkto theex923.htmlfile with the title "clear".
Modify the ex923.jsfile to create a drawing program which will be draw ellipses in the mouseX and mouseY position every time the user presses the mouse. Clear the sketch every time the user presses a key using the information from here. You can see here an example.
function setup() {
createCanvas(windowWidth, windowHeight);
// set background color
background('white');
}
function draw() {
noStroke();
fill(0, 0, 0, 30);
if (mouseIsPressed) {
// ellipse will be drawn to the mouseX and mouseY position
ellipse(mouseX, mouseY, 30, 30);
}
}
// clear the canvas every time the user presses a key
function keyPressed() {
clear();
}
Κάντε μια Git commit με το μήνυμα "clear".
- Δείτε περισσότερα για την clear() function