Explore the Power of p5.js: A Beginner’s Guide

Last Updated: June 19, 2023 1:32 pm
Explore the Power of p5.js: A Beginner’s Guide

In the world of creative coding, p5.js has emerged as a popular tool that combines the portability of JavaScript with the visual-arts focus of Processing. Whether you’re an aspiring artist, a seasoned programmer, or simply curious about the intersection of code and art, p5.js offers a gateway to create captivating visuals, interactive experiences, and engaging animations. In this blog post, we will delve into the world of p5.js, covering tutorials, examples, and even explore its comparison with Processing. Additionally, we’ll touch upon Tynker’s Intro to Programming and Art course, which utilizes p5.js as a creative platform.

What Is p5.js?

p5.js is a JavaScript library that simplifies creative coding, making it accessible to a wide range of people. It provides a simple yet powerful framework for creating interactive graphics, animations, and even games. Inspired by the Processing language, p5.js inherits its simplicity and user-friendly approach, while leveraging the vast capabilities of JavaScript.

What is the difference between JavaScript and p5.js?

JavaScript is a general-purpose programming language with a broad range of applications, while p5.js is a JavaScript library specialized for creative coding, graphics, and interactive projects, offering a simplified and user-friendly approach to web development. The choice between the two depends on the specific needs and expertise of the developer or creator.

p5.js Tutorials

If you’re new to p5.js, tutorials are a great way to kickstart your journey. There are numerous online resources available that cater to different skill levels and interests. Here are a few notable p5.js tutorial platforms to get you started:

  • The p5.js Official Website: The official p5.js website offers a comprehensive set of tutorials, documentation, and examples. It covers everything from basic shapes and colors to more advanced topics like interactivity and data visualization.
  • Coding Train: Run by Daniel Shiffman, a well-known figure in the creative coding community, Coding Train offers an extensive collection of P5.js tutorials on YouTube. Shiffman’s engaging teaching style makes complex concepts accessible to beginners and advanced learners alike.
  • Creative Coding with p5.js: Authored by the creators of p5.js, Lauren McCarthy, Casey Reas, and Ben Fry, this short but sweet book provides a thorough introduction to p5.js.
  • Intro to Programming and Art: Tynker cool course designed for high-schoolers. Using JavaScript and p5.js code, draw on a digital canvas, create Snapchat-style photo filters, make amazing games, and much much more! You’ll also find more p5.js tutorials in your Tynker Student Dashboard.
  • OpenProcessing: A community for creative coders to share their work and connect.

What is p5.js used for?

To truly grasp the potential of p5.js, exploring real-life examples can be inspiring. Here are some areas where p5.js finds applications:

  • Generative Art: p5.js allows you to generate stunning visuals using algorithms and mathematical concepts. From mesmerizing patterns to abstract compositions, the possibilities are limitless.

  • Interactive Experiences: With its interactivity features, p5.js enables you to create engaging experiences that respond to user input. Think: Animated art that follows the user’s mouse or responds to the microphone. From interactive animations to games, you can design immersive digital worlds.

  • Data Visualization: p5.js can be used to visualize complex datasets and present information in a visually appealing and intuitive manner. Whether it’s graphs, charts, or maps, p5.js provides the tools to communicate data effectively.
  • Image Processing and Photo Filters: Turn your digital photographs into interactive, Pointillist masterpieces! Or achieve a comic-book effect using a Halftones filter. You can even use MediaPipe and p5.js together to create amazing webcam effects!

p5.js Coding Examples

Find some free examples in your Tynker Student Dashboard. Login at tynker.com, then go to Projects > Create Project > p5.js. Inside you’ll see categories you can select from. If you’re an absolute beginner, start with Basic Drawing. Or try some of the short examples below!

Basic Drawing. Put on your cartoonist’s hat and make some original comics and shapes. Learn how to draw shapes, lines, then learn about irregular shapes and design your own Origami Animal. 

Art Style. Take inspiration from famous artists, then recreate their own work in the same style—using code!

Math Art. Make intricate patterns and colors! You’ll have a blast learning how to make animations and interactive art projects. 

Basic Animation

This animation moves an ellipse across the canvas.

function setup() {
createCanvas(400, 400);
}

function draw() {
background(220);

// Move a shape across the canvas
     ellipse(frameCount % width, height / 2, 50, 50);
}

Interactive Drawing

The ellipse seems to follow your mouse!

function setup() {
createCanvas(400, 400);
}

function draw() {
background(220);

// Draw a shape at the mouse position
ellipse(mouseX, mouseY, 50, 50);
}


How does p5.js work?

p5.js is a creative coding framework that simplifies the process of developing interactive and visual web applications. It operates through a straightforward structure consisting of two primary functions, setup() and draw(), making it accessible even to those with limited programming experience. Here’s a breakdown of how p5.js works:

Setup and Draw Functions:

  • The setup() function initializes program settings and variables, typically used for defining canvas size and configuration.
  • The draw() function is called continuously, allowing for the creation of animations and updates to visual content.

Canvas Handling:

  • p5.js automatically generates an HTML5 canvas element, where all graphic elements are drawn.
  • You can customize the canvas size, position, and background color using functions like createCanvas() and background().

Drawing and Interaction:

  • p5.js provides a range of functions for drawing shapes, lines, and text, simplifying the creation of visuals.
  • User interactions are easily managed through built-in event handling functions like mousePressed() and keyPressed(), enhancing interactivity.

Additionally, p5.js includes features like image manipulation, sound integration, and integration with external libraries, expanding its creative potential. After developing a project, creators can export it as a web page or share it online, enabling them to showcase their interactive and visual creations with a global audience. Overall, p5.js empowers artists, designers, educators, and beginners to engage in creative coding, offering a user-friendly approach to web development for a wide range of applications.

Can you make Games with p5.js?

Yes, you can create games with p5.js. p5.js is a JavaScript library that is particularly well-suited for creative coding, including game development. It provides a simple and intuitive way to work with graphics, animations, and interactivity, making it popular among artists, designers, and programmers who want to create interactive visual experiences, including games.

What are Processing and p5.js?

Processing and p5.js share a common goal of making creative coding accessible to a wide audience, but they differ in their implementation. Processing is stand-alone software with its own integrated development environment (IDE), while p5.js is a JavaScript library that can be used within any web browser.

p5.js inherits many of Processing’s features while leveraging the web’s capabilities, making it more accessible and versatile for online projects.

How to Make Your First Generative Art with p5.js?

Tynker, a popular online learning platform, offers an Introduction to Art and Programming course that combines programming and art using p5.js. This course introduces learners to coding concepts through interactive lessons, creative projects, and engaging challenges.

By leveraging P5.js, Tynker’s course provides an intuitive way for learners to explore the world of programming and art simultaneously using creative coding. This course was created by Professor Zoe Wood from Cal Poly University. Professor Wood’s original course, CSC 123 Introduction to Computing – Computational Art, was designed as a college freshman course for non-CS majors. She shares Tynker’s vision of making Computer Science accessible for everyone and spent the majority of her career inspiring students to use code as a medium to express themselves. In this captivating course, students will learn how to use code to model their own masterpieces, while exploring the art styles of painters such as Monet, Picasso, and Warhol.

Check out Tynker’s Curriculum and learn more about inspiring the next generation to change the world through code.

Related Searches:

coding class

creative coding js

programming fundamentals javascript

javascript skills

learn javascript games

javascript hack

Image by vectorjuice on Freepik

About Teri Llach

Teri Llach is Head of Marketing for Tynker. Teri is an experienced growth marketer with a highly successful track record in B2B and B2C, deep experience with start-ups, brands, and all functional areas of marketing. Teri lives in Palo Alto, CA.