Introduction to creative coding with p5.js
Tags: javascript, webdev, codepen, computerscience
Published: 2020-10-06 12:10:18


*"Creating a process which makes the process"*<br>
Creative coding is addictive, if you really tune into it! It has immense possibilities, and that is why I decided to write this article, to introduce all of you to this wonderful genre of coding with a Javascript library called **p5.js**, where you can communicate to your audience more artistically than ever before!
# What is creative coding?
In my terms, creative coding is just making some art, pattern, background, models, sketches, data visualization, animation, etc with the help of code, in a sort of iterative way to express something. It is about creating something expressive rather than functional, with code. Creative coding is a discipline which is an intersection of technology, art and design.<br>
Didn't get it? Let me show you some examples. 

![Image 1](https://res.cloudinary.com/codepenrishav/image/upload/v1601969117/DEV/ccss_image_ibvwo9.jpg)
![Image 2](https://res.cloudinary.com/codepenrishav/image/upload/v1601969124/DEV/180827_Sphere3D_frame-000125_ofqmze.png)
![Image 3](https://res.cloudinary.com/codepenrishav/image/upload/v1601969076/DEV/unnamed_dl8nf3.jpg)

You must be thinking right now : "Whattt? These art forms are made from 'code'? I can't believe it! ". That's what I initially thought, but once you get into this beautiful practice, you'll start to understand how nature itself is just a bunch of repetitive processes which ultimately makes this world, which is kinda what we do in coding, right? We create an algorithm, to solve our problems. The only thing that changes in creative coding is that we create algorithms for expressing something, in the form of art.
# p5.js
You could do creative coding with C++ too! But it's not meant for that, that's why we have frameworks specifically for creative coding. [Processing](https://processing.org/) is the most famous platform for this, and it uses Java, but it has other variants for other languages as well, and [p5.js](https://p5js.org/) is the platform we use for creative coding on the web. It obviously, uses Javascript, so it is very easy for web developers to get started with this, which was the primary reason it was made by [Lauren McCarthy](https://lauren-mccarthy.com/). So let's just get started. <br>
For a quickstart, just open the [p5.js web editor](https://editor.p5js.org/). Now that you have a p5 editor, let's get into some code. So, there are **two functions** that are most important in p5:
- **setup()**
- **draw()**

So setup() is an inbuilt function which runs only once, at the start of the program, and draw() is also an inbuilt function which runs on loop, which means it runs infinitely by default. Let's understand them individually.

**1. function setup()**
This is the function in which we put all the things that we want to do initially and only once. It basically sets up the environment and things like screen size, background color, for our **"sketch"** : which is what we call our program files in p5.js. A very generic setup function looks like this: <br>
```
function setup(){
 createCanvas(500,500);
 background(120);
}
```

As we can see, we have a function <code>createCanvas()</code>, which creates a canvas of 500x500 units, and a <code>background()</code> function, which sets up the color for our canvas. If we provide a single argument, it will be a value from 0 to 255, 0 being black and 255 white. If we provide three arguments, it will use RGB color notation, each value ranging from 0 to 255. 

**2. function draw()**
This is where most of the action happens. Here we use various functions, loops, and self made algorithms to make our final artwork. You can work with sound, data, webcams, sensors, and other things to interpolate in your sketch to make it interactive, but that is out of scope of this article. So if we wanna draw a line, circle, rectangle or any shape, we have functions for that:
```
function draw(){
  fill(233,11,44); // color to fill the shapes
  stroke(1); // stroke color
  strokeWeight(2);
  line(20,20,160,60); // line from (20,20) to (160,60)
  fill(23,44,223); 
  rect(170,170,120,70); // rectangle on (170,170) with 120x70 size
  fill(223,212,44);
  ellipse(120,130,100,100); // ellipse on (120,130) with height 100 and width 100, which makes it a circle
  noLoop();
}
```
So the code above will produce something like this: 
![Sketch](https://res.cloudinary.com/codepenrishav/image/upload/v1601980157/DEV/a_1_ozmhdz.jpg)

The functions are self explanatory, and by the comments you might have understood what's going on. There was one function at the end called **noLoop()** , which actually does nothing but stops the draw() function from running infinitely. This is useful in scenarios where you might want to run <code>draw()</code> for a particular number of times.<br>
There are some **inbuilt** variables in p5 too, which can add complexity to our sketches. Some of them are **mouseX** and **mouseY**. They are variables used to store x and y co-ordinates of the mouse cursor.
Look at this sketch:
```
function setup() {
  createCanvas(400, 400);
  background(200);
}
function draw(){
  fill(233,11,44);
  stroke(1); 
  strokeWeight(2);
  makeShapes();
}
function makeShapes(){
  fill(random(200),random(200),random(200));
  ellipse(mouseX,mouseY,50,50);
  rect(mouseX,mouseY,120,70);
}
```
The result:
![Sketch](https://res.cloudinary.com/codepenrishav/image/upload/v1601982117/DEV/screen-capture_19_cp63sl.gif)
# The possibilities
As this is just to introduce you to p5.js, I can't go into complex sketches but I've laid down a path for you, and hopefully you'll create some beautiful things after venturing into the art of creative coding. For inspiration, here are some of my sketches :)

![BLOOMING SHAPES](https://res.cloudinary.com/codepenrishav/image/upload/v1601983456/DEV/screen-capture_20_dctgcb.gif)
![Alternate Dimensions](https://res.cloudinary.com/codepenrishav/image/upload/v1601983920/DEV/Screenshot_624_ldth5b.png)
![Triangles](https://res.cloudinary.com/codepenrishav/image/upload/v1601984167/DEV/Screenshot_627_hwttve.png)
![Oblivion](https://res.cloudinary.com/codepenrishav/image/upload/v1601984867/DEV/Screenshot_630_mqshnd.png)

If you've reached till here, thank you for reading! Hope it helped you in some way. If you're interested in CSS, I also write some interesting articles on my website [CSS Kenpai](https://www.csskenpai.com/).
Some sources for further reading:
- https://medium.com/better-programming/getting-started-with-creative-coding-16072ff7e778
- https://p5js.org
- https://www.creativebloq.com/features/6-ways-to-get-into-creative-coding