Creative Coding

Creative Coding

Topics

Sine and cosine

Lecture notes

Coding Advice

Code for humans too

image
image

Take small steps,…

image

so that you can tell which change broke the code

image

When your program becomes too complicated, take a break to make it do the same thing more simply instead of making it do more

image

Optimize your workflow

Make this faster!
Make this faster!

Day 3.1 – Functions and iteration

Day 3.2 – Arranging items, sin and cos

This material has been moved to 🎨Creative Coding with p5.js

Day 4.1 – Functions, Scope, Shadowing

Scope and Shadowing

Day 4.2 – random(), sin(), and noise()

Code
From
From random() to noise() to sin()

Day 5.1 – Objects

Code

Day 5.2 – Classes && OOP

Code

Naming Conventions

Class naming conventions:

  • Uppercase: Dog not dog
  • Singular: Dog not Dogs

Method naming conventions:

  • Don't include the class name: Dog.speak() not Dog.dogSpeak() or Dog.speakDog()
  • CamelCase: Dog.rollOver() not Dog.roll_over(). (Unless you are modifying a sketch that already uses the snake_case convention. Consistency within a file is more important than consistency with a style guide.)
image
image

Day 5.R – OOP continued

Old-Style Class Definitions

These are three ways of defining classes. Use the first (modern class definition). You may see the other two in older examples and tutorials, or code written by people who learned JavaScript from older tutorials.

These ways of defining a class all do the same thing*. They each define a class that is instantiated using let ball1 = new Ball(100, 150, "red") . Each instance has properties x, y, radius, and color,. The class has methods draw() and move().

Modern class definition – use this

class Ball {
	constructor(x, y, c) {
		this.x = x;
		this.y = y;
		this.radius = 40;
		this.color = c;
	}

	draw() {
		fill(this.color);
		circle(this.x, this.y, this.radius);
	}

	move(targetX, targetY) {
		this.x = lerp(this.x, targetX, 0.1);
		this.y = lerp(this.y, targetY, 0.1);
	}
}

let b1 = new Ball(10, 20, "red");
\b1.draw();
The class keyword was added to JavaScript in 2015. Code that is older than this, or code that copies patterns from code that is older than this, uses one of the other two class definition patterns instead.

Old-style: Functions

function Ball(x, y, c) {
	this.x = x;
	this.y = y;
	this.radius = 40;
	this.color = c;

	this.draw = function() {
		fill(this.color);
		circle(this.x, this.y, this.radius);
	}

	this.move = function(targetX, targetY) {
		this.x = lerp(this.x, targetX, 0.1);
		this.y = lerp(this.y, targetY, 0.1);
	}
}

let b1 = new Ball(10, 20, "red");
b1.draw();
Old-style class definition, that uses functions as property values.

Old-style: Prototype chain

function Ball(x, y, c) {
	this.x = x;
	this.y = y;
	this.radius = 40;
	this.color = c;
}

Ball.prototype.draw = function() {
	fill(this.color);
	circle(this.x, this.y, this.radius);
}

Ball.prototype.move = function(targetX, targetY) {
		this.x = lerp(this.x, targetX, 0.1);
		this.y = lerp(this.y, targetY, 0.1);
	}
}

let b1 = new Ball(10, 20, "red");
b1.draw();
Old-style definition using the prototype chain.

* There are subtle differences between the modern class definition, the function-based class definition, and the prototype class definition. These differences are unlikely to matter to you in practice.

Day 6.1

Code

https://glitch.com/edit/?#!/cclab-arrays?path=sketch.js%3A1%3A0

Glitch

Arrays

You should be able to do the following things to an Array:

  • Create a new Array. For example, create an Array that contains the three items "Beijing", "Shanghai", and "Amherst".
  • Get the nth element from an Array. For example, if cities has the value ["Beijing", "Shanghai", "Amherst"], retrieve the second element cities[1] (which is "Shanghai").
  • Use for(;;) and for (let…of…) to iterate over the elements of an Array.
  • Replace an element within an Array. For example, if cities has the value ["Beijing", "Shanghai", "Amherst"], modify it so that its value is ["Beijing", "Shanghai", "Xi'an"]
  • Add an element to the end of an Array. For example, if cities has the value above, modify it so that its value is ["Beijing", "Shanghai", "Amherst", "Xi'an"].

By the time of the midterm, you should also be able to:

  • Find the position of an element within an Array. For example, if cities has the value ["Beijing", "Shanghai", "Amherst", "Xi'an"], find the value 2 such that cities[2] === "Amherst".
  • Find the first element in an Array that satisfies a certain property. For example, given the property "starts with a vowel", find "Amherst".
  • Find all the elements within an Array that satisfy a certain property. For example, given the property "begins with the letter 'X'" and the Array ue ["Beijing", "Shanghai", "Xiamen", "Xi'an"], find the elements "["Xiamen", "Xi'an"].

(There will not be a midterm exam. This timeframe is guidance for a pace of learning. There may be additional quizzes that cover this material, even after we are done with "Array week".)

Array Resources

Day 6.2

Course roadmap; domains

Computation

Variables

Values, Types, and Expressions

Functions

Control Flow (if, while)

Iteration (repetition)

Array

Object

Object-Oriented Programming (classes and instances)

Workflow, Practices, & Techniques

Debugging

Iterative Development

Exploratory Programming

Bottom Up vs. Top Down

Refactoring to Functions, Classes

Project Planning & Management

Learning from Mistakes

Edit-Run-Debug-Modify Cycle

Layers

Sequencing

Tools

OpenProcessing

JavaScript Console

Glitch

Visual Studio Code

Domains

Creative Coding

Shapes

Colors

Lines and Curves

Transformations

Sound (Audio)

Images

Text and Fonts

Data (JSON)

HTML & CSS

WebGL (3D)

WebCam and PoseNet

Computational Biology (not covered)

Codons

Nucleotides

Sequences

Genes

What's Left (The Big Picture)

  • Computation – patterns of use
  • Workflow – how to debug
  • Tools – formatting, refactoring, Glitch, Visual Studio Code
  • Domain concepts (in orange above)
  • More projects
  • Practice practice practice!

Workflow Tip

Setting up a coding environment – have everything at your fingertips. I currently use a combination of Notion, and browser bookmarks, for this. Previously I've used Google docs, and HTML documents.

image
image
image
image

Code – Freezing Animations

Three ways of freezing an animation (of keeping draw() from being called again and again):

function setup() {
	createCanvas(windowWidth, windowHeight);
	background(100);
	// drawing code goes here
}
Put everything inside of setup(). The drawing only happens once; there is no animation or interaction.
function setup() {
	createCanvas(windowWidth, windowHeight);
}

function draw() {
	background(100);
	// drawing code goes here
	noLoop();
}

function mousePressed() {
	loop();
}
draw() is called once, and then stops. Each time the mouse is pressed, draw() is called one more time.
function setup() {
	createCanvas(windowWidth, windowHeight);
	noLoop();
}

function draw() {
	background(100);
	// drawing code goes here
}

function mousePressed() {
	loop();
}
draw() is called once, and then stops. The first time the mouse is pressed, draw() starts running again; it is called 30 times per second for as long as the sketch is running.

Code – Two Array Iteration Methods

Two ways of iterating over all the items in an Array:

let teams = [
	"Adagio e Lieto",
	"Type Your Music",
	"Interactive Music Visualization",
	"Animal Farm",
	"Smash",
	"May The Force Be With You",
	"Interactive Game",
	"Oasis in the Distance",
];

function setup() {
	createCanvas(windowWidth, windowHeight);
	for (let i = 0; i < teams.length; i++) {
		let team = teams[i];
		console.info(team);
	}
}
let teams = [
	"Adagio e Lieto",
	"Type Your Music",
	"Interactive Music Visualization",
	"Animal Farm",
	"Smash",
	"May The Force Be With You",
	"Interactive Game",
	"Oasis in the Distance",
];

function setup() {
	createCanvas(windowWidth, windowHeight);
	for (let team of teams) {
		console.info(team);
	}
}

Code – Shuffling Team Names

Code: Shuffle Team Names

Code – Arrays of Objects

Code: Arrays of Objects

Code – closeToMouse() (review of booleans and return values)

Several ways of writing closeToMouse():

class Ball {
	constructor() {
		// …
	}
	closeToMouse() {
		let d = dist(this.x, this.y, mouseX, mouseY);
		if (d < this.size) {
			return true;
		} else {
			return false;
		}
	}
	draw() {
		// …
	}
}
	closeToMouse() {
		let d = dist(this.x, this.y, mouseX, mouseY);
		return d < this.size * 2;
	}
	closeToMouse() {
		return dist(this.x, this.y, mouseX, mouseY) < this.size * 2;
	}

If P5.js didn't define dist():

	closeToMouse() {
		let dx = this.x - mouseX;
		let dy = this.y - mouseY;
		let d = sqrt(sq(dx) + sq(dy));
		return d < this.size * 2;
	}

Code – variable scope (review)

Review: scope

Code – Optional Arguments with Default Values

This is optional material. You do not need to understand or use this in this course.

Optional arguments

Day 6.R – Array.select and Particles

let balls;

function setup() {
	createCanvas(windowWidth, windowHeight);
	balls = [];
	for (let i = 0; i < 20; i++) {
		let ball = new Ball(random(width), random(height));
		balls.push(ball);
	}
}

function draw() {
	background(200); // + 50 * cos(frameCount / 25));

	// draw the balls
	for (let ball of balls) {
		ball.draw();
	}
}

function mousePressed() {
	let ball = new Ball(mouseX, mouseY);
	balls.push(ball);
}

class Ball {
	constructor(x, y) {
		this.x = x;
		this.y = y;
		this.size = 80;
	}

	draw() {
		circle(this.x, this.y, this.size);
	}

	closeToMouse() {
		let d = dist(this.x, this.y, mouseX, mouseY);
		return d < this.size * 2;
	}
}

Related Pages

CClab Example Codes