Book
Submodule 16.2: Basics of p5.js
Submodule 16.2: Basics of p5.js
Completion requirements
View
- setup() and draw() functions
- createCanvas
- Primitives Shapes
Primitives Shapes - rect()
rect()
The rect() function takes the same 4 arguments with the ellipse. Thus we have rect (x,y,w,h).
Let's see an example of a code that uses the rect() function to create a simulation of a 3d shape:
function setup() {
// create canvas
createCanvas(800,500);
}
function draw () {
// set background color
background('orange');
for (dim = 400; dim>10; dim = dim-10 ) {
rectMode(CENTER);
rect(width/2,height/2,dim,dim);
}
}
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex826.js
- Open the file
ex812.html
in your editor and save it asex826.html
- In the
ex826.html
file, update the link toex826.js
from ex816.js - Go to the
index.html
file and create, underModule 8
, alink
to theex826.html
file with the title "Primitives Shapes - rect".
Modify the ex826.js
file in order to create a cube using rects and lines.. You can see here an example.
function setup() {
// create canvas
createCanvas(800,500);
}
function draw () {
// set background color
background('orange');
noFill();
// create the cube
rect(300,200,100,100);
rect(350,250,100,100);
line (400,200,450,250);
line (300,200,350,250);
line (400,300,450,350);
line (300,300,350,350);
}
Do a Git commit with the message "Primitives Shapes - rect".
- See more about the rect() function