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 - point()
point()
The point function takes 2 arguments which determine its x and y position. Thus we have point(x,y).
The function draws a point with dimensions of 1 pixel.
Let's see an example of a code that uses the point() function to create points across the canvas:
let xPos;
let yPos;
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('orange');
}
function draw () {
for (xPos = 1; xPos<width; xPos = xPos +10) {
for (yPos = 1; yPos<height; yPos = yPos+10) {
point (xPos,yPos);
}
}
}
Exercise
- Open your Visual Studio editor and the
p5yourNamefolder. - Open the file
ex812.jsin your editor and save it asex825.js - Open the file
ex812.htmlin your editor and save it asex825.html - In the
ex825.htmlfile, update the link toex825.jsfrom ex812.js - Go to the
index.htmlfile and create, underModule 8, alinkto theex825.htmlfile with the title "Primitives Shapes - point".
Modify the ex825.jsfile in order to create a diagonal line of points, going from the up left edge to the right down. You can see here an example.
let xPos;
function setup() {
// create canvas
createCanvas(800,500);
// set background color
background('orange');
}
function draw () {
// use a for loop and create points with a distance of 30px
for (xPos = 1; xPos<width; xPos = xPos +30) {
// in order to create a diagonal line of points going from up left to down right
// you will have to use the appropiate geometry to define the y coordinate
point (xPos,5/8*xPos);
}
}
Do a Git commit with the message "Primitives Shapes - point".
- See more about the point() function