Here is code, info, etc for Math. Code. A R T. p5.js module of Recreating the Past at MIT.
I've been doing creative and/or visual coding + math stuffs since 2015 when I started working with Ira Winder in City Science at the MIT Media Lab. I have primarily done Processing and p5.js.
I post (almost) daily sketches made wtih p5.js on my Twitter.
This is a baseline p5.js module/workshop as part of Zach Lieberman's "Recreating the Past" Course at MIT.
p5.js is a free Javascript library for creative coding. It is super accesible, free, and open source!
You have 3 main options for creating:
sketch.js
, do not edit the other files. index.html
is the file for the browser that you would open to view the results of the sketch.Every p5.js sketch has a setup()
and draw()
loop. Setup runs once. Draw runs consistently and just keeps looping. If you don't want to have a looping drawing, just leave your draw()
loop empty and put all your code in setup()
In general, once you have that structure, you can kind of do whatever you want. The key things to remember in general are:
background(255, 50)
this makes a semi transparent background that gives your code a "ghost" appearance. 255 would be a white background like Example 1.
noise()
function so you don't have to write matrices to get noise! Reference here.
tau
(2*pi). Example 5 shows an example of tau being used to create weird, noisy curves. Example 4 shows some cool sin/cos curves.
map(value, start1, stop1, start2, stop2)
is a function that allows you to linearly interpolate between value ranges. Used for color + coordinates. lerpColor(startColor, endColor, percent)
lets you do this a bit easier for colors.
createVector()
a lot of my sketches are vector based and what's great about creating a vector is you can do vector math like adding forces and velocities. Reference here.
curveVertex()
allows you to build more complex geometries overall. Refence here. Example 4 also has an example of this.
blendMode()
is a super easy way to start playing with color and pixel blending without writing any code. Reference for all the modes is here . I personally love using EXCLUSION to get a glitchy look.
The p5.js website really is the best place to get help/documentation. In particular they have a learn page with more code explanations and also a documentation/reference page for quick lookups and an examples page.
There are a lot of amazing people in the space that do tutorials and content for this work and code alongs and have lots of tutorials. Daniel Shiffman is a great person to look at for these.
There is also just an amazing community around this space, which I encourage people to look into. There are creative coding clubs and scenes all over the world, and a lot of them are welcoming to everyone and newcomers!
All of the code of the examples on this site is in this repo, including a blank p5.js sketch template here
I am, in general, trying to get better at documenting things and making more tutorials and tutorial like materials, so feel free to check back on my Twitter as I post things.
Feel free to copy/paste this code below and play with it in an online editor if you want. This is just to illustrate that it doesn't take a lot of code to make some beautiful.
//Set up 2 global vars
var r=200; //radius
var s=0.002; //speed
function setup() {
createCanvas(600, 600);
background(255)
}
function draw() {
background(255, 150);
translate(width/2, height/2);
let n=360;
for (let i=0; i<n; i++) {
let t1= i*2*PI/n;
let t2= s*t1;
let x1= r*cos(t1);
let y1= r*sin(t1);
let x2= r*cos(t2);
let y2= r*sin(t2);
stroke(i%255, 50)
strokeWeight(0.2)
noFill();
line(x1, y1, x2,y2);
}
s+=0.002;
fill(0);
}