xxxxxxxxxx
// SPIROGRAPH
// this p5 sketch uses simple transformations to create a Spirograph-like effect with interlocking circles (called sines).
// press the spacebar to switch between tracing and showing the underlying geometry.
//
// your tasks:
// (1) tweak the code to change the simulation so that it draws something you like.
// hint: you can change the underlying system, the way it gets traced when you hit the space bar,
// or both. try to change *both*. :)
// (2) use p5.sound or tone.js to make the simulation MAKE SOUND.
// hint: the websites for p5.sound and tone.js have lots of examples.
// try and adapt them.
var NUMSINES = 10; // how many of these things can we do at once?
var sines = new Array(NUMSINES); // an array to hold all the current angles
var oscs = new Array(NUMSINES);
var rad; // an initial radius value for the central sine
var i; // a counter variable
// play with these to get a sense of what's going on:
var fund = 0.02; // the speed of the central sine
var ratio = 2; // what multiplier for speed is each additional sine?
var alpha = 50; // how opaque is the tracing system
var trace = false; // are we tracing?
function setup()
{
createCanvas(windowWidth, windowHeight);
rad = height/2; // compute radius for central circle
background(0); // clear the screen
for (i = 0; i<sines.length; i++)
{
sines[i] = PI; // start EVERYBODY facing NORTH
oscs[i] = new p5.Oscillator('sine');
oscs[i].amp(0.5);
oscs[i].freq(50*(i+1));
oscs[i].start();
}
}
function draw()
{
if (!trace) {
background(0); // clear screen if showing geometry
stroke(random(255), random(255), random(255), random(80, 200));
strokeWeight(20);
fill(random(255), random(255), random(255), 80);
}
// MAIN ACTION
push(); // start a transformation matrix
translate(width/2, height/2); // move to middle of screen
for (i = 0; i<sines.length; i++) // go through all the sines
{
var erad = 0; // radius for small "point" within circle... this is the 'pen' when tracing
// setup for tracing
if (trace) {
stroke(random(255), random(255), 255*(float(i)/sines.length), 80); // choose color
fill(255, 255, 255, alpha/2);
erad = 1.5*(1.0-float(i)/sines.length); // pen width will be related to which sine
}
var radius = rad/(i+1); // radius for circle itself
rotate(sines[i/2]); // rotate circle
if (!trace) ellipse(0, 0, radius/2, radius/2); // if we're simulating, draw the sine
push(); // go up one level
translate(0, radius/2); // move to sine edge
if (!trace){
ellipse(radius, 0, 3, 3); // draw a little circle
ellipse(radius, 0, 6, 6);
ellipse(radius, 0, 9, 9);
}
if (trace){
ellipse(0, 0, erad, erad); // draw with erad if tracing
ellipse(0, 0, erad, erad);
ellipse(0, 0, erad, erad);
}
pop(); // go down one level
translate(0, radius/3); // move into position for next sine
oscs[i].amp((sines[i]/TWO_PI), 0.05);
sines[i] = (sines[i]+(fund+(fund*i*ratio)))%TWO_PI; // update angle based on fundamental
}
pop(); // pop down final transformation
}
function keyReleased()
{
if (key==' ') {
trace = !trace;
background(0);
}
}