Lecture 4 (17 Feb)
You can jump to any slide with the menu to the (bottom) left.
Did you miss a slide or want to revisit? Open the narration tab while studying to get an explanation of difficult slides.

fill() and stroke() affects all subsequent shapespush() and pop() allow us to save and restore the current drawing statepush() and pop() will not affect the drawing state outside of that blockrect(), we can only get rectangles on straight anglestranslate()translate(x, y) moves the origin to the specified (x, y) positionrotate()rotate(angle) rotates the coordinate system by the specified angleangleMode(DEGREES) first if you want to use degreesClass Assignment: Flower!
translate(), rotate(), and a for-loop to draw multiple petalsframeCount?rect() and ellipse() are examples of functions that draw shapes on the canvasbeginShape() and endShape(CLOSE)vertex(x, y)bezierVertex()bezierVertex(x1, y1, x2, y2, x3, y3) where (x1, y1) and (x2, y2) are the control points and (x3, y3) is the final destination anchor pointrect() and ellipse() are examples of built-in functions, but we can also create our own custom functions!(a, b, c) are the parameters of the function. These are essentially variables for within the function’s scope1, 2, and 3 as arguments to the function, which will be assigned to the parameters a, b, and c respectively when the function is executedreturn keywordClass Assignment: Flower forest
mouseX and mouseY to make our sketches interactivepmouseX and pmouseY to get the previous mouse positionsetup() is a built-in function that happens when the program startsmousePressed() is called whenever the mouse is pressedmousePressed() function to specify what should happen when the mouse is pressedmousePressed(): called when the mouse button is pressedmouseReleased(): called when the mouse button is releasedmouseClicked(): called when the mouse button is clickedmouseMoved(): called when the mouse is movedmouseDragged(): called when the mouse is dragged (moved while down)mouseWheel(): called when the mouse wheel is scrolled.
return false; to prevent page scrollingmouseWheel() event has an optional event parameter that contains information about the scroll amount and direction
event.deltaIn this example we ease a circle towards the mouse position. The circle will smoothly follow the mouse around the canvas.
mouseIsPressed variabletrue if the mouse is currently being pressed, and false otherwisemouseButton contains the value of the mouse button that is currently being pressed (LEFT, RIGHT, or CENTER)Class Assignment: Mousing around
keyPressed(): called when a key is pressedkeyReleased(): called when a key is releasedkeyTyped(): called when a key is typed (pressed and released)key and keyCode, two variables that contain information about the key that was pressedkey contains the value of the key that was pressed as a string. For example, if you press the ‘a’ key, key will be ‘a’keyCode contains the numeric code of the key that was pressed. For example, the ‘a’ key has a keyCode of 65
SPACE, ENTER, and LEFT_ARROW.function keyPressed() {
if (key == 'a') {
// Do something when the 'a' key is pressed
} else if (keyCode == 32) { // We could have also used SPACE instead of 32
// Do something when the space bar (keyCode 32) is pressed
} else if (keyCode == LEFT_ARROW) {
// Do something when the left arrow key is pressed
}
}key for letter keys and keyCode for special keyskeyIsDown() functiontrue if that key is currently being pressed, and false otherwiseClass Assignment: Alphabet
key variable on the canvas
Creative Programming 2026 Lecture 4 - Ties Robroek - IT University of Copenhagen