function setup() {
createCanvas(400, 400); // Sets up a 400x400 pixel canvas
background(255); // Sets the background color to white
}
function draw() {
ellipse(200, 200, 150, 150); // Draws a circle at the center of the canvas
}
In this example:
By placing the ellipse() function inside draw(), I ensure that it’s called after the p5.js library has been initialized, preventing the scope-related error.
Today, I learned how to draw shapes using code, specifically how to create a face using circles. The primary function for drawing circles in p5.js is ellipse(). To draw a circle, I need to specify four parameters:
For example, to draw a circle centered in the canvas, I can use:
ellipse(200, 200, 150, 150);
This places the center of the ellipse at (200, 200) with a width and height of 150 pixels, resulting in a perfect circle.
By adjusting the x and y values, I can move the ellipse around the canvas. Increasing the x value moves it to the right, while increasing the y value moves it down. This coordinate system allows precise placement of shapes, which is essential for creating complex drawings.