Creative Coding Lectures
Creative Coding Lectures
INTERACTIVITY
DYNAMIC AND GENERATIVE ART
ACCESSIBILITY
INTEGRATION OF TECHNOLOGY
Technical skills still matter and you will develop your code skills
from the foundations learned in CodeLab, but you will also use
code in unexpected ways, driven by curiosity and iteration rather
than finding a particular solution.
SA 1 - CREATIVE CODER CASE STUDY
SA 1: (LINK)
Each entry should link to a series of live versions of the code and include
screenshots and a suggested 300-word commentary.
INTRODUCTION TO P5.JS
PAUL RAND
FORMAT
How big is my canvas?
SHAPE
Basic square? Ellipse? Triangle?
Relationships POSITION
Where do we place it?
DIMENSION
what size should it have?
ROTATION
http://printingcode.runem Do I turn it?
adsen.com/lecture-form/
FORMAT
CANVAS
CANVAS
CANVAS
SHAPE
POSITION
DIMENSIONS
ROTATION
SYMBOLISM
playful
SYMBOLISM
heavy
SYMBOLISM
sharp
SYMBOLISM
lost
SYMBOLISM
dominant
ICE CREAM CONE / PIN
sharp
RUNE MADSEN
Tiny Artist 4 - 6
JONATHAN PUCKEY
Delaunay Raster
FIELD
Communion
FIELD
Communion
FORMAT
How big is my canvas?
SHAPE
Basic square? Ellipse? Triangle?
Relationships POSITION
Where do we place it?
DIMENSION
what size should it have?
ROTATION
http://printingcode.runem Do I turn it?
adsen.com/lecture-form/
FORMAT
createCanvas(), background()
Basic
SHAPE
point(), line(), rect(), ellipse(), triangle(), quad(), beginShape(),
endShape(), vertex(), bezierVertex(), curveVertex(), also covering
Relationships
ellipseMode(), rectMode(), fill(), noFill(), stroke(), noStroke(),
strokeWeight()
POSITION
(Code) rect( 10, 20, 10, 10), translate(), push(), pop()
DIMENSION
rect( 10, 20, 10, 10), scale()
ROTATION
rotate(PI), radians(180)
VARIABLES
var, width, height, display,Width, displayHeight
P5.JS VARIABLES
String
var word = “Hello world!”;
P5.JS Variables
int (integer) width = canvas width;
height = canvas height;
var wholeNum = 50;
float (decimal)
var decimalNum = 20.5;
FORMAT
createCanvas (150, 150)
createCanvas (width, height)
background (200);
background (color)
SHAPE/ POSITION/ DIMENSIONS
point ( 75, 75); line ( 10, 100, 100, 10);
point(x, y); line ( x1, y1, x2, y2);
SHAPE/ POSITION/ DIMENSIONS
rect(50, 25, 50,100); ellipse ( 75, 75, 100, 100);
rect( x, y, width, height); ellipse (x, y, width, height);
SHAPE/ POSITION/ DIMENSIONS
beginShape();
vertex( 0, 50);
bezierVertex(50, 25, 75, 50, 50, 50);
endShape();
vertex(x, y);
bezierVertex(x1, y1, x2, y2, x3, y3);
x1, y1 = first control point
x2, y2 = second control point
x3, y3 = anchor point
SHAPE/ POSITION/ DIMENSIONS
SHAPE/ POSITION/ DIMENSIONS
translate(width/2 - 150, 0);
beginShape();
vertex(0,100);
bezierVertex( 0, 100, 50, 0, 100, 100);
bezierVertex(100, 100, 150, 200, 200, 100);
bezierVertex(200, 100, 250, 0, 300, 100);
vertex(300, 300);
vertex( 0, 300);
vertex( 0, 100);
endShape();
SHAPE MODES
ellipseMode(CENTER); ellipseMode(CORNER);
ellipse (0, 0, 100, 100); ellipse (0, 0, 100, 100);
default
SHAPE MODES
rectMode(CENTER); rectMode(CORNER);
rect (0, 0, 100, 100); rect (0, 0, 100, 100);
default
SHAPE STYLES
fill(100); noFill();
rect( 25, 25, 100, 100); rect( 25, 25, 100, 100);
SHAPE STYLES
stroke(0); NoStroke();
rect( 25, 25, 100, 100); rect( 25, 25, 100, 100);
SHAPE STYLES
strokeWeight(5);
rect( 25, 25, 100, 100);
ACTIVITY
Create your own car using different shape modes
and styles.
push();
translate (50, 60);
triangle (25, 0, 50, 50, 0, 50);
pop();
rotate (degree);
PI = 180 degrees
TWO_PI = 360 degrees
HALF_PI = 90 degrees
Rotates around 0.0
ACTIVITY
Create your own car using different shapes modes
and styles.
IMAGE COLOR
tint(), noTint()
IMAGE METHODS
Image Sequence / Animation
IMAGE INPUT - loadImage
var img;
function preload() {
img = loadImage("tree.jpg");
}
function setup () {
createCanvas (400, 400);
background(0);
}
function draw() { // It continuously redraws everything on the canvas
background(0);
tint(255, 0, 0);
image(img, 0, 0, img.width/2, img.height/2);
noTint();
image(img, 0, img.height/2, img.width/2, img.height/2);
//These specify the width and height to which the image will be
scaled when drawn. It's drawing the image at half of its original
width and half of its original height.
}
IMAGE INPUT - Filters
var img;
function preload() {
img = loadImage("tree.jpg");
}
function setup () {
createCanvas (400, 400); // The setup() function sets up the
canvas with a size of 400x400 pixels
background(0); // (RGB value of (0, 0, 0))
}
function draw() { //It redraws the canvas continuously.
background(0);
image(img, 0, 0);
filter(BLUR, 10); // Applies a blur effect to the canvas and 10
specifies the level of blur
}
IMAGE INPUT - Filters
var img;
function preload() {
img = loadImage("tree.jpg");
}
function setup () {
createCanvas (400, 400);
background(0); //RGB VALUES
}
function draw() {
background(0);
image(img, 0, 0);
var v = map(mouseX, 0, width, 0, 5);//re-maps a number from one
range to another.
filter(BLUR, v); // v specifies the level of blur, which is dynamically
adjusted based on the horizontal position of the mouse.
}
IMAGE INPUT - Filters
var img;
function preload() {
img = loadImage(“ tree.jpg “);
}
function setup () {
createCanvas (400, 400);
background(0);
}
function draw() {
background(0);
image(img, 0, 0); //Draws the loaded image (img) onto
the canvas at the position (0, 0).
var v = map(mouseX, 0, width, 0, 1);
filter(THRESHOLD, v);
}
IMAGE INPUT - Filters
var img;
function preload() {
img = loadImage("tree.jpg");
}
function setup () {
createCanvas (400, 400);
background(0);
}
function draw() {
background(0);
image(img, 0, 0); //Draws the loaded image (img) onto
the canvas at the position (0, 0)
var v = map(mouseX, 0, width, 2, 20);
filter(POSTERIZE, v); //v specifies the number of color
levels used in the posterization
}
IMAGE INPUT - Get Image Pixel Data
var img, x, y;
function preload() {
img = loadImage("tree.jpg");
}
function setup() {
createCanvas (400, 400);
background(0);
noStroke();
}
function draw() {
background(0);
x = mouseX;
y = mouseY;
image( img, 0, 0);
var c = get(x, y); //Retrieves the color of the pixel at the mouse position (x, y)
on the canvas and stores it in the variable c.
fill(c); //Sets the fill color for shapes drawn on the canvas to the color sampled
from the image.
ellipse (x, y, 100, 100); //Draws an ellipse at the current mouse position (x, y)
with a diameter of 100 pixels, filled with the color sampled from the image.
}
IMAGE INPUT - Pointillism Effect
var img, x, y;
function preload() {
img = loadImage("tree.jpg");
}
function setup() {
createCanvas (400, 400);
background(0);
noStroke();
}
function draw() {
x = random(width);
y = random(height);
var c = img.get(x, y); //Retrieves the color of the pixel at the mouse
position (x, y) on the canvas
fill(c[0], c[1], c[2], 50); // array of values which is the red green blue
colors and pass in the opacity value for the 4th one.
ellipse (x, y, 30, 30);
}
IMAGE INPUT - Image Sequence
var numFrames = 6;
var frame = 0
var images = new Array(numFrames); //store multiple pieces of data of the same
type together
function preload() {
images[0] = loadImage("dove01.png");
images[1] = loadImage("dove02.png");
images[2] = loadImage("dove03.png");
images[3] = loadImage("dove04.png");
images[4] = loadImage("dove05.png");
images[5] = loadImage("dove06.png");
}
function setup() {
createCanvas(600, 400);
frameRate(15); //the speed
background(255);
}
function draw() {
background(255);
frame++;
if (frame == numFrames) frame = 0; //loop back to 0
image(images[frame], mouseX - 75, mouseY - 100);
}
ACTIVITY
Do similar work of image pixel data. Instead of an
ellipse, use another shape. Use your own image.
}
REPETITION - SIMPLE PATTERN
var circleSize = 80;
translate(circleSize/2, circleSize/2);
function setup() {
createCanvas(400, 400); // Create a canvas of size 400x400
translate(circleSize / 2, circleSize / 2); // Translate the origin
function draw() {
// Nothing needed here for static drawings
}
DECISION
DECISION - IF STATEMENTS
if (conditional statement) { if (conditional statement) {
…then do this; …then do this;
} else if (conditional statement) {
} …then do this;
} else if (conditional statement) {
…then do this;
} else if (conditional statement) {
…then do this;
} else if (conditional statement) {
if (conditional statement) {
…then do this;
…then do this; } else {
} else { …then do this;
}
…then do this;
}
DECISION - IF STATEMENTS
DECISION - ARITHMETIC SYNTAX
> greater than
< less than != not equal to
>= greater than or equal to || logical OR
<= less than or equal to && logical AND
== equal to ! logical NOT
DECISION - If Statement Examples
if (x == 8) { if (x > 3) { if (x <= 10) {
color = red; color = red; color = red;
} else { } else { } else {
color = black; color = black; color = black;
} } }
random(50, 100);
// Gives a float between 50 and 100
round(random(100));
// If you want a intrather than a float (which
returns a decimal point) you can use round(),
ceil() or floor() to round it to the closest, next
or previous whole number.
RANDOMIZATION - random(max)
function setup() {
varrectSize= random(100);
createCanvas(400, 400);
colorMode(HSB, 360, 100, 100);
varx = random(width -rectSize); vary =
random(height -rectSize);
background(random(360), 100, 100);
noStroke();
fill(random(360),
100, 100);
rect(x, y, rectSize, rectSize);
}
RANDOMIZATION - random(min,max)
function setup() {
varrectSize= random(20, 100)
createCanvas(400, 400);
colorMode(HSB, 360, 100, 100);
varx = random(width -rectSize);
vary = random(height -rectSize);
background(random(270, 360), 100, 100);
noStroke();
fill(random(90, 180), 100, 100);
rect(x, y, rectSize, rectSize);
}
RANDOMIZATION - color theory
varrectSize= 130;
function setup(){
varorigColor= random(180, 360); createCanvas(600,
400);
rectMode(CENTER);
background(255);
colorMode(HSB, 360, 100, 100);
noStroke();
fill(origColor, 80, 80);
rect(width/2 -rectSize/2, height/2, rectSize, rectSize);
fill((origColor+ 180) % 360, 80, 80);
rect(width/2 + rectSize/2, height/2, rectSize, rectSize);
}
RANDOMIZATION - Perlin Noise
RANDOMIZATION - Perlin Noise
RANDOMIZATION - Perlin Noise
RANDOMIZATION - Perlin Noise
RANDOMIZATION - Perlin Noise
function setup() {
createCanvas(600, 400);
background(255);
stroke(0);
noFill();
strokeWeight(3);
translate(0, height/2);
beginShape();
var noiseCount= 0;
for(var i= 0; i< width; i+= 1) {
var ranY= noise(noiseCount);
vertex(i, ranY* 100);
noiseCount+= 0.02;
}
endShape();
}
RANDOMIZATION - Perlin Noise Clouds
var noiseVal;
var noiseScale=0.005;
function setup() {
createCanvas(600, 400);
colorMode(HSB, 360, 100, 100);
//noprotect
for (var y = 0; y < height; y++) {
for (var x = 0; x < width; x++) {
noiseDetail(10, 0.5);
noiseVal= noise((x) * noiseScale, (y) * noiseScale);
stroke(150, 30, noiseVal*100);
point(x,y);
}
}
}
RDR - ACTIVITY
Create your own pattern
with the use of for loop and
if statements that shows
repetition , decision and
randomization
FUNCTIONS, CLASSES, &
TYPOGRAPHY
WEEK 5
}
Classes- Simple Custom Classes
function ClassName(var1, var2) {
this.function1() {
fill(0, 0, 0);
ellipse(var1, var2, 20, 20);
}
this.function2() {
fill(0, 0, 0);
rect(var1, var2, 10, 40);
}
}
Classes - Random Faces
function setup() {
createCanvas(600, 400);
background(255);
noStroke();
f1.head();
f1.features();
f2.head();
f2.features();
}
function Face(x, y, w, h, c) {
// Create multiple functions to use.
this.features = function() {
fill(0, c, 0);
ellipse(x - w/6, y - h/8, w/4, h/4);
ellipse(x + w/6, y - h/8, w/4, h/4);
ellipse(x, y + h/8, w/8, h/8);
ellipse(x, y + h/3, w/8, h/8);
}
this.head = function() {
rectMode(CENTER);
fill(c, 0, 0);
rect(x, y, w, h);
}
}
TYPEFACE - CLASSIFICATIONS
Serif - They are often perceived as more traditional
and formal.
TYPEFACE - CLASSIFICATIONS
San Serifs - without the decorative strokes, giving
them a cleaner and more modern look.
TYPEFACE - CLASSIFICATIONS
Scripts - Fonts that mimic handwriting or calligraphy.
They can vary greatly in style, from elegant to playful.
TYPEFACE - CLASSIFICATIONS
Dingbat or Specialty: consist of symbols or pictograms
rather than traditional alphanumeric characters.
COMPUTATIONAL TYPE
Rule-Based Typography
Creating a typeface from scratch using rules and
p a r a me t e r s .
Outline Based
S t a r t i n g wi t h a p r e - e x i s t i n g f o n t a n d mo d i f y i n g t h e
o u t l i n e o r i n t e r i o r b a s e d o n p a r a me t e r s .
COMPUTATIONAL TYPE
Rule-Based Typography
COMPUTATIONAL TYPE
Outline Based Typography
function setup() {
createCanvas(600, 400);
background(255); fill(0); textSize(48);
textAlign(CENTER);
text(word, width/2, height/2 - 30);
text(textWidth(word), width/2, height/2 + 30);
}
TYPOGRAPHY - SIMPLE TEXT
textSize(Number);
//size
textAlign(alignX, alignY);
// LEFT, CENTER, or RIGHT and TOP, BOTTOM, CENTER, or BASELINE
text(text, x, y);
// Pass in a string of text, x location and y location
TYPOGRAPHY - FONT
var word = "Hello world!";
var font1, font2, font3;
function preload() {
font1 = loadFont("fonts/BlackOpsOne-Regular.ttf");
font2 = loadFont("fonts/Comfortaa-Light.ttf");
font3 = loadFont("fonts/ShadowsIntoLight.ttf");
}
function setup() { createCanvas(600, 400);
background(255); fill(0); textFont(font3, 100);
textAlign(CENTER); text(word, width/2, height/2);
}
TYPOGRAPHY - RULE - BASED
var letters = [ [ 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1 ], //A
[ 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0 ], //B
[ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1 ] ]; //C
function preload() {
font = loadFont('ShadowsIntoLight.ttf'); }
var points;
function setup() {
createCanvas(300, 400);
stroke(0);
points = font.textToPoints('hello', 100, 100, 100, { sampleFactor: 0.15 });
background(255);
for (var i = 0; i < points.length; i++) {
var p = points[i];
ellipse(p.x, p.y, 3, 3);
}
}
ACTIVITY
Design the BATH SPA UNIVERSITY word using your
own font , random, and with different colors
(outline - based)
function draw() {
line(mouseX, mouseY, pmouseX, pmouseY);
}
Mapping
var angle;
function setup() {
createCanvas(600, 400);
background(0);
stroke(255);
strokeWeight(5);
}
function draw() {
background(0);
angle = map(mouseX, 0, width, 0, TWO_PI);
translate(width/2, height/2);
rotate(angle);
triangle(0, -95, 75, 65, -75, 65);
}
Hover
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
}
function draw() {
background(0);
fill(255);
if (mouseX < width/2) {
rect(0, 0, width/2, height);
} else {
rect(width/2, 0, width/2, height);
}
}
Boolean
var hoverBoolean = true;
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
}
function draw() {
background(0);
fill(255);
if (mouseX < width/2) hoverBoolean = true;
else hoverBoolean = false;
if (hoverBoolean == true) {
rect(0, 0, width/2, height);
} else if (hoverBoolean == false) {
rect(width/2, 0, width/2, height);
}
}
mousePressed
function setup() {
createCanvas(600, 400);
background(0);
stroke(255);
}
function draw() {
if (mouseIsPressed) {
translate(mouseX, mouseY);
for(var i = 0; i < 10; i++) {
strokeWeight(i);
point(0, i * -10);
}
}
}
constrain()
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
}
function draw() {
var x = constrain(mouseX, 100, 500);
var y = constrain(mouseY, 100, 300);
if (mouseIsPressed) {
fill(255, 0, 0); ellipse(x, y, 20, 20);
}
}
dist()
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
fill(255, 0, 0);
}
function draw() {
background(0);
var d = dist(width/2, height/2, mouseX, mouseY);
ellipse(width/2, height/2, d*2, d*2);
}
easing
var x = 0;
var easing = 0.07;
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
}
function draw() {
background(0);
fill(255);
var targetX = mouseX; x += (targetX - x) * easing;
ellipse(x, height/2, 40, 40);
}
Speed
var speed = 0;
var easing = 0.03;
var x, y;
function setup() {
createCanvas(600, 400);
background(0);
stroke(255);
}
function draw() {
var target = dist(mouseX, mouseY, pmouseX, pmouseY);
speed += (target - speed) * easing;
if(mouseIsPressed){
strokeWeight(speed/2);
line(mouseX, mouseY, pmouseX, pmouseY);
}
}
save()
var count = 1;
var c;
function setup() {
c = createCanvas(600, 400);
background(255);
noStroke();
}
function draw() {
if (mouseIsPressed) {
fill(255, 0, 0, 40);
ellipse(mouseX, mouseY, 30, 30);
}
}
function keyPressed() {
if (keyCode == 65) {
saveCanvas(c, 'canvasName' + count, 'jpg');
count++;
}
}
Button Class for GUI
var brush = 0;
var bx = 20;
var by = 20;
var bw = 50;
var bh = 20;
var by2 = by + bh + 10;
var b1, b2;
Button Class for GUI
function Interface(x, y, w, h, c1, c2, b, bSize) {
this.button = function() {
if((mouseX > x) && (mouseX < x + w) && (mouseY > y) && (mouseY < y + h) && mouseIsPressed) {
fill(c1);
brush = b;
} else {
fill(c2);
}
rect(x, y, w, h);
}
this.brush = function() {
if(((mouseX < x) || (mouseX > x + w) || (mouseY < y) || (mouseY > y + h)) && brush == b &&
mouseIsPressed) {
if (b == 0) fill(255, 0, 0, 50);
if (b == 1) fill(255, 50);
ellipse(mouseX, mouseY, bSize, bSize);
}
}
}
Button Class for GUI
function setup() {
createCanvas(600, 400);
background(0);
noStroke();
// Create a new interface class for each button and brush.
b1 = new Interface(bx, by, bw, bh, color(100, 0, 0), color(255, 0, 0), 0, 20);
b2 = new Interface(bx, by2, bw, bh, color(255), color(100), 1, 40);
} function draw() {
// Draw the button and brush to the canvas.
b1.button();
b1.brush();
b2.button();
b2.brush();
}
Button Class for GUI
ACTIVITY
Create your own mouse
interaction trails with different
colors that depends on your
preference and has the save
option.
Group Major ACTIVITY - (April 29)
Create an interactive art:
1 . It must display Welcome to Bath Spa
University
2 . Full Canvas Size: windowWidth,
windowHeight