Brian Mueller
- What is p5js?
- p5.js is a JavaScript library modeled after Processing, a Java library
- all JavaScript syntax rules apply
- How do I use p5js?
- It runs nicely in an HTML page. Just embed it like you would with normal javascript!
- What is p5js used for?
- Animations
- Games
- Mockups
- Much more!
- template: use tiny.cc/p5template to get started
- using offline? make sure you download the p5.js file
- you can also fork/clone this repo
- IDEs
- Atom (live preview)
- Cloud9 (cloud based, semi-live preview)
- Nitrous (requires using
python -m SimpleHTTPServer -p 3000
) - sandbox: jsbin.com
- native IDE: p5js > editor
function setup(){}
is called once on loadfunction draw(){}
is called 60 times per second- you can change this with
frameRate(#)
- you can change this with
createCanvas(w,h)
sets the size of your sketch (px)- optional: you can use
displayWidth
anddisplayHeight
(orwindowWidth
andwindowHeight
) - the top-left corner is
(0,0)
x
increases as you go righty
increases as you go down
- the 4 basic shapes
point(x,y)
line(x1,y1,x2,y2)
rect(x,y,w,h)
- draws from the top-left corner
- default "mode" is
rectMode(CORNER)
rectMode(CENTER)
draws from the centerrectMode(CORNERS)
uses diagonal corners:rect(x1,y1,x2,y2)
- see documentation for rounded edges
ellipse(x,y,w,h)
- draws from the center
- default "mode" is
ellipseMode(CENTER)
- can also use
ellipseMode(CORNER)
andellipseMode(CORNERS)
(similar torectMode()
)
- other shapes
triangle
,quad
rilateral, etc. (see documentation)
- color theory
- min =
0
(none of that color) - max =
255
(100% of that color) - three values represent RGB values, i.e.
(255,0,0)
means red (no green or blue) - one value represents grayscale (all RGB values are equal
(150) = (150,150,150)
0
= black,255
= white- color commands: the setting will be applied to all future shapes until the setting is changed again
fill(R,G,B,[A])
sets the interior color[A]
is an optional "alpha" (opacity) argument
stroke(R,G,B,[A])
sets the outline colorstrokeWeight(#)
sets the outline thickness (px)background(R,G,B)
sets the color of the background- use inside
function setup(){}
to run once - use inside
function draw(){}
to "wipe" the background for each frame
- use inside
- global variables must be declared outside of all functions
- their scope is visible inside & outside all functions
- local variables are declared inside a function
- their scope is limited to that specific function
- system variables are reserved keywords for values such as the
width
andheight
of the canvas
mouseX
andmouseY
are system variables that contain the coordinates of the user's mousefunction mousePressed(){}
is called when the user clicks the mousefunction keyPressed(){}
is called when the user presses a key
Read more in the documentation under Events (including mobile: touchX
, touchY
, etc).
if (condition){
// code
} else if (condition){
// code
}
else {
// default
}
while (condition) {
// code
}
do {
// code
} while (condition);
for (var i = 0; i < num; i++) {
// code
}
// defining your function
function myFunction(optional parameters){
// code
}
// calling your function
myFunction(arguments);
var arrayName = [item1, item2, ...];
arrayName[1]; // returns item2
- Random Circles
- Ghost
- Flower (relative position)
- Flower dilation
- Interaction & Loops
- Interaction & Graffiti
- Circle Orb
- Kuku Kube [mobile]
- Pong
- GitHub Pages Tutorial
- For mirroring your gh-pages and master branches