Drawing with p5.js

a drawing with colored hair and pencil on a paper and marker pen next to it

Note: this page has been created with the use of AI. Please take caution, and note that the content of this page does not necessarily reflect the opinion of Cratecode.

There's no denying that the world of programming can be a little dry at times. All those variables, loops, and functions can make for some pretty dull viewing. Until, that is, you stumble across the vibrant and colorful world of p5.js.

p5.js - The Artist's Paintbrush

p5.js is a JavaScript library that brings the power of interactivity and graphics to your web pages. With p5.js, you can create everything from simple geometric shapes to complex interactive graphics and animations, all with just a few lines of code.

To give you a taste of what this library can do, let's dive right into the basics.

Setup and Draw

In p5.js, every sketch (that's what p5.js calls your programs) starts with two special functions: setup() and draw().

function setup() { createCanvas(400, 400); } function draw() { background(220); }

The setup() function is run once, at the very beginning. It's where you set up your canvas and any initial settings you need. The draw() function, on the other hand, is run continuously. Anything you put in draw() will be re-drawn on your canvas 60 times per second!

Drawing Shapes

Now that we have our canvas, it's time to start drawing. Let's start with some basic shapes: a line, a rectangle, and a circle.

function draw() { background(220); line(50, 50, 350, 350); rect(100, 100, 200, 200); circle(200, 200, 100); }

The line() function draws a line from point (50, 50) to point (350, 350). The rect() function draws a rectangle at position (100, 100) with a width and height of 200. Similarly, the circle() function draws a circle centered at position (200, 200) with a radius of 100.

Adding Color

What's a painting without color? Luckily, p5.js makes it easy to add color to your sketches. You can change the color of your shapes using the fill() function and the color of your lines with the stroke() function.

function draw() { background(220); stroke(255, 0, 0); line(50, 50, 350, 350); fill(0, 255, 0); rect(100, 100, 200, 200); fill(0, 0, 255); circle(200, 200, 100); }

In the above example, the line is red, the rectangle is green, and the circle is blue. Colors in p5.js are defined using RGB (Red, Green, Blue) values.

Hey there! Want to learn more? Cratecode is an online learning platform that lets you forge your own path. Click here to check out a lesson: Drawing Basic Shapes (psst, it's free!).

FAQ

What is p5.js?

p5.js is a JavaScript library that makes it easy to create interactive graphics and animations in the browser. It provides a simple and consistent API for drawing shapes, manipulating colors, and handling user input.

How do I draw shapes in p5.js?

In p5.js, you can use the line(), rect(), and circle() functions to draw lines, rectangles, and circles, respectively. Each function takes in coordinates as arguments to determine where the shape should be drawn on the canvas.

How do I add color to my drawings in p5.js?

You can add color to your p5.js sketches using the fill() and stroke() functions. The fill() function changes the color of the interior of shapes, while the stroke() function changes the color of the lines. Both functions take RGB values as arguments.

What are the setup() and draw() functions in p5.js?

In p5.js, the setup() function is run once at the beginning of your sketch and is typically used for initial setup like creating the canvas. The draw() function, on the other hand, is run continuously. Anything you put in the draw() function is re-drawn on your canvas 60 times per second, making it ideal for animations.

Can I create interactive graphics with p5.js?

Absolutely! One of the main strengths of p5.js is its ability to create interactive graphics. You can easily capture and respond to user input, like mouse clicks or keyboard presses, to create truly interactive experiences.

Similar Articles