<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Janelle Ojanola</title>
    <description>The latest articles on DEV Community by Janelle Ojanola (@j-oj).</description>
    <link>https://dev.to/j-oj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3072315%2F79a4c2e3-d0aa-44a9-94d1-9257ee619ddb.jpg</url>
      <title>DEV Community: Janelle Ojanola</title>
      <link>https://dev.to/j-oj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/j-oj"/>
    <language>en</language>
    <item>
      <title>How to Turn Code Into Art and Games with p5.js</title>
      <dc:creator>Janelle Ojanola</dc:creator>
      <pubDate>Sat, 26 Apr 2025 11:01:40 +0000</pubDate>
      <link>https://dev.to/up_min_sparcs/how-to-turn-code-into-art-and-games-with-p5js-1n7j</link>
      <guid>https://dev.to/up_min_sparcs/how-to-turn-code-into-art-and-games-with-p5js-1n7j</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;What possibilities emerge at the intersection of art and programming?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Artists express themselves using mediums like paint, pencils, film, clay, and even sound. But one medium you might not expect? Code!&lt;/p&gt;

&lt;p&gt;In this article, co-authored by &lt;a class="mentioned-user" href="https://dev.to/krarneas"&gt;@krarneas&lt;/a&gt;, we will explore the world of creative coding with a popular tool called p5.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Creative Coding?
&lt;/h2&gt;

&lt;p&gt;Rather than aiming to be purely functional, creative coding focuses more on the expressive and artistic outcome, just like art. It basically combines programming and creativity to produce visuals, sounds, and interactive experiences. There are countless possibilities of what creative coding can produce.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sample Works Made with Creative Coding
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52y3nq8nxg14cno3vrk0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52y3nq8nxg14cno3vrk0.png" alt="Southern Chile 2 by Andrew Bryant"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a href="https://www.artsyshark.com/2021/05/26/what-is-creative-coding/" rel="noopener noreferrer"&gt;Southern Chile 2 by Andrew Bryant&lt;/a&gt;



&lt;blockquote&gt;
&lt;p&gt;The random black squares in the image weren't drawn by the artist. Instead, they were generated randomly by the program!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's another example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkee4e27twyvnuaxiqfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkee4e27twyvnuaxiqfl.png" alt="Mushroom Spore"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a href="https://thehobbyhopper.substack.com/p/creative-coding" rel="noopener noreferrer"&gt;Mushroom Spore Print by The Hobby Hopper&lt;/a&gt;



&lt;blockquote&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkzpmn174urd84vax4dy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkkzpmn174urd84vax4dy.jpg" alt="Node Garden"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a href="https://www.deconbatch.com/2022/05/ex-nodegarden01.html" rel="noopener noreferrer"&gt;Node Garden by deconbatch&lt;/a&gt;



&lt;blockquote&gt;
&lt;p&gt;Quite mesmerizing, right?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that you know what creative coding is, let’s dive into one of the most popular and beginner-friendly tools for creative coding, p5.js.  &lt;/p&gt;




&lt;h3&gt;
  
  
  What is p5.js?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://p5js.org/about/" rel="noopener noreferrer"&gt;p5.js&lt;/a&gt; is an open-source JavaScript library with beginner-friendly tools for creating beautiful, interactive art.&lt;/p&gt;

&lt;p&gt;It’s based on a language called &lt;a href="https://processing.org/" rel="noopener noreferrer"&gt;Processing&lt;/a&gt; (also known as Processing 5 or P5). Processing is a Java-based sketchbook designed to help artists and designers learn programming in the context of visual art. Eventually, p5.js was developed, using JavaScript to make creative coding more accessible on the web. It supports modern web development tools and plugins, making it the perfect choice for developing interactive experiences directly in the browser.  &lt;/p&gt;

&lt;h3&gt;
  
  
  What is it used for?
&lt;/h3&gt;

&lt;p&gt;p5.js is used to create interactive experiences on the web. Here are a few examples of the things you can do with it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generative Art&lt;/li&gt;
&lt;li&gt;Animations&lt;/li&gt;
&lt;li&gt;Audio Visualization &lt;/li&gt;
&lt;li&gt;Image Manipulation&lt;/li&gt;
&lt;li&gt;Game Development
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;In this article, we will mainly focus on writing codes on the p5.js web editor. But, you can go to this &lt;a href="https://p5js.org/download/" rel="noopener noreferrer"&gt;link&lt;/a&gt; to download the library file if you want to develop locally.  &lt;/p&gt;

&lt;h4&gt;
  
  
  The Web Editor
&lt;/h4&gt;

&lt;p&gt;Click &lt;a href="https://editor.p5js.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to open the web editor. You can code here and run the program to see the result instantly, but it’s important to note that &lt;strong&gt;in order to save your sketches, you have to make an account&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzwkrstj4j0m9pnacb1m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzwkrstj4j0m9pnacb1m.png" alt="p5.js web editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip: You can choose between Light, Dark, or High Contrast for the theme. You can find this under the General Settings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you first open the editor, you will see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break it down to understand how it works:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setup()&lt;/strong&gt;&lt;br&gt;
The setup function runs only once at the beginning. It’s where you set initial configurations like the canvas size. In the code above, we’re creating a canvas of size 400x400 pixels using &lt;code&gt;createCanvas(width, height)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;draw()&lt;/strong&gt;&lt;br&gt;
The draw function runs over and over, updating the canvas every time it’s called. Its default rate is 60 FPS (frames per second), which means it updates the canvas 60 times per second. This loop continues until the program is stopped.&lt;/p&gt;

&lt;p&gt;Inside the draw function, you can see &lt;code&gt;background(220)&lt;/code&gt;. This turns the color of your canvas into light gray. The number you pass to this function represents a grayscale value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 for black&lt;/li&gt;
&lt;li&gt;255 for white&lt;/li&gt;
&lt;li&gt;Any number in between gives a shade of gray&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Building your first sketch with simple shapes
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Let's start with a simple line first.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(400, 400);
}

function draw(){
  line(0, 200, 300, 200); // x1, y1, x2, y2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/j-oj/embed/vEEZBGy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The top-left corner of the canvas is the origin (0,0) in the p5.js coordinate system&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Draw a circle using this code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(400, 400); // Set canvas size
  background(220);  // Set background color
}

function draw() {
  ellipse(200, 200, 100, 100);  // Circle at position (200, 200) 100px size
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/j-oj/embed/emmzQNP?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Change its position or resize it by adjusting the values in the parameters!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Try making a triangle.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(400, 400);
  background(240);
}

function draw(){
  fill(0 , 255, 0);   // fill with green
  triangle(200, 100, 100, 300, 300, 300); // top point, bottom-left, bottom-right
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/j-oj/embed/RNNgwje?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We colored the shape using &lt;code&gt;fill()!&lt;/code&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Make a Drawing Pad
&lt;/h4&gt;

&lt;p&gt;Let’s turn your mouse into a brush!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(800, 400); //Set canvas size
  background(255); // White background
}

function draw() {

  if (mouseIsPressed) { // If mouse button is pressed
    stroke(0);      // Set stroke color to black
    strokeWeight(4);    // Set line thickness to 4px
    line(pmouseX, pmouseY, mouseX, mouseY); // Draw line from previous to current mouse position
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/j-oj/embed/YPPNjpg?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Try this: Change the &lt;code&gt;stroke(0)&lt;/code&gt; to another color like &lt;code&gt;stroke(‘red’)&lt;/code&gt; or change &lt;code&gt;strokeWeight(4)&lt;/code&gt; to &lt;code&gt;strokeWeight(10)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Building a Basic Shooter Game
&lt;/h4&gt;

&lt;p&gt;In this example, we’ll create a simple interactive game mechanic where a player can shoot bullets and move left or right.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let playerX;      // Player's x position
let bullets = []; // Array to store bullets

function setup() {
  createCanvas(700, 500);  // Set canvas size
  playerX = width / 2;     // Position player to center
}

function draw() {
  background(0);  // Black background

  // Draw player
  fill(255);  // Fill with white
  rect(playerX, 400, 40, 20);  // Draw rectangle (player)

  // Draw bullets
  for (let i = 0; i &amp;lt; bullets.length; i++) {
    bullets[i].y -= 5;                        // Move bullet up
    rect(bullets[i].x, bullets[i].y, 5, 10);
  }

  // Remove bullets offscreen
  bullets = bullets.filter(b =&amp;gt; b.y &amp;gt; 0);
}

function keyPressed() {
  // Move player left
  if (keyCode === LEFT_ARROW) {
    playerX -= 20;
  } 
  // Move player right
  else if (keyCode === RIGHT_ARROW) {
    playerX += 20;
  } 
  // Shoot bullets when spacebar is pressed
  else if (key === ' ') {
    bullets.push({ x: playerX + 17.5, y: 350 });
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/j-oj/embed/MYYJBpv?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can keep expanding the code to create objects to shoot at and even add some logic to turn it into a fully functional game!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Game Development
&lt;/h2&gt;

&lt;p&gt;At this point, you've already created the foundation of a simple game. But creating a fully developed game starts with understanding the basics of p5.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lines and shapes&lt;/strong&gt;&lt;br&gt;
Draw characters, objects, and environments using functions like &lt;code&gt;line()&lt;/code&gt;, &lt;code&gt;ellipse()&lt;/code&gt;, and &lt;code&gt;rect()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Animation&lt;/strong&gt;&lt;br&gt;
Animate objects by updating visuals frame by frame. This is achieved by the &lt;code&gt;draw()&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interaction&lt;/strong&gt;&lt;br&gt;
Make the game responsive through mouse and keyboard inputs like &lt;code&gt;mouseX&lt;/code&gt;, &lt;code&gt;mouseY&lt;/code&gt;, and &lt;code&gt;keyPressed()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Media&lt;/strong&gt;&lt;br&gt;
Enhance the game experience by adding media assets like images and audios using functions like &lt;code&gt;loadImage()&lt;/code&gt; and &lt;code&gt;playSound()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For the complete list of predefined functions, visit the &lt;a href="https://p5js.org/reference/" rel="noopener noreferrer"&gt;p5.js reference documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Sample Game
&lt;/h3&gt;

&lt;p&gt;Wanna see what you can do by combining the basics? &lt;br&gt;
&lt;a href="https://editor.p5js.org/j-oj/sketches/y2YfR14pj" rel="noopener noreferrer"&gt;Check out this game we made!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu6b0i1hmiro99xfgx6ec.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu6b0i1hmiro99xfgx6ec.png" alt="Our Game"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Where to Go From Here
&lt;/h2&gt;

&lt;p&gt;One of the best things about p5.js is its welcoming and inclusive community. As their &lt;a href="https://p5js.org/about/" rel="noopener noreferrer"&gt;official site&lt;/a&gt; says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“We are all learners. We share and value many kinds of knowledge. We do not assume knowledge or imply that somebody should already know. We maintain a beginner’s mind, and believe that newcomers are just as valuable as experts.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So don’t be afraid to explore. Keep experimenting and see where your creativity takes you. &lt;strong&gt;Most importantly, have fun!&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some amazing resources to help you get started:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=HerCR8bw_GE&amp;amp;list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA&amp;amp;pp=0gcJCV8EOCosWNin" rel="noopener noreferrer"&gt;p5.js Video Tutorials by The Coding Train&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/playlist?list=PL0beHPVMklwgMz4Z-mNp4_udo9mjBk7pn" rel="noopener noreferrer"&gt;Creative Coding for Beginners Playlist by Patt Vira&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tynker.com/blog/exploring-the-power-of-p5-js-a-beginners-guide/" rel="noopener noreferrer"&gt;Exploring the Power of p5.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://p5js.org/tutorials/" rel="noopener noreferrer"&gt;p5.js Tutorials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/learn/learn-p5js" rel="noopener noreferrer"&gt;Learn p5.js Course by Codecademy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for reading this article! We hope you're curious enough to build something great with p5.js &amp;lt;3&lt;/p&gt;

&lt;p&gt;Special thanks to &lt;a class="mentioned-user" href="https://dev.to/rakunnn"&gt;@rakunnn&lt;/a&gt; for his helpful feedback.&lt;br&gt;&lt;br&gt;
Happy coding!&lt;/p&gt;

</description>
      <category>creativecoding</category>
      <category>p5js</category>
      <category>gamedev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
