sketch
var rAngle = 30;var rotation = 0;var maxShapes = 8;
var shape = [];
function setup() {
createCanvas(480, 480);
background(250);
createShapes();
}
function draw() {
background(250);
stroke(200);
var xRot = map(mouseX, 0, width, 0, TWO_PI);
var my = map(mouseY, 0, height, 50, height/2);
var r = constrain(my, 100, height/2 - 75);
addText();
push(); translate(width/2, height/2);
for(var s = 0; s < shape.length; s++) {
drawSmallShapes(s, height/2 - 25);
}
rotate(xRot);
circle(0, 0, 2 * r);
for(var s = 0; s < shape.length; s++) {
rotation = 0;
if(shape[s].on == true){
for(var i = 0; i < 4; i++) {
rotate(radians(rotation));
rotation = rAngle;
drawCurves(shape[s].verts, r, shape[s].color);
}
}
}
pop();}
function mousePressed() {
var mx = map(mouseX, 0, width, -240, 240);
var my = map(mouseY, 0, height, -240, 240);
for(var i = 0; i < shape.length; i++) {
var d = dist(shape[i].px, shape[i].py, mx, my);
if(d <= shape[i].radius){
if(shape[i].on == true) {
shape[i].on = false;
} else {
shape[i].on = true;
}
}
}
}
function addText() {
fill(shape[0].colorB);
strokeWeight(.1);
textAlign(CENTER, CENTER);
textSize(20);
text("Hypocycloid", width/2, 8);
text("Spirograph", width/2, 30);
textSize(10);
text("Click the circles to toggle curves on or off. Reload for different pen colors.",
width/2, height - 5);
noFill();
strokeWeight(1);
}
function createShapes() {
var vCount = 3; var sOn = true; var angle = 30;
var shapeRad = 35;
for(var i = 0; i < maxShapes; i++) {
shape[i] = new Object();
shape[i].verts = vCount;
var r = random(255);
var g = random(255);
var b = random(255);
shape[i].colorB = color(r, g, b, 255); shape[i].colorM = color(r, g, b, 80); shape[i].color = shape[i].colorM;
shape[i].angle = angle;
shape[i].px = 0;
shape[i].px = 0;
shape[i].radius = shapeRad;
shape[i].on = sOn;
sOn = false;
vCount++;
angle += 30;
if (angle == 90 || angle == 180 || angle == 270) {
angle += 30;
}
}
}
function drawSmallShapes(s, r) {
var px = r * cos(radians(shape[s].angle));
var py = r * sin(radians(shape[s].angle));
shape[s].px = px;
shape[s].py = py;
var mx = map(mouseX, 0, width, -240, 240);
var my = map(mouseY, 0, height, -240, 240);
var d = dist(shape[s].px, shape[s].py, mx, my);
if(d <= shape[s].radius) {
var hover = 1.25;
shape[s].color = shape[s].colorB;
} else {
var hover = 1;
shape[s].color = shape[s].colorM;
}
if(shape[s].on == true) {
var c = shape[s].colorB; } else {
var c = shape[s].colorM; }
push();
translate(px, py);
stroke(c); strokeWeight(2);
circle(0, 0, shape[s].radius * 2 * hover); drawCurves(shape[s].verts, shape[s].radius * hover, c)
pop();
}
function drawCurves(n, r, c) {
var nPoints = 50;
beginShape();
for(var i = 0; i < nPoints; i++) {
var t = map(i, 0, nPoints, 0, TWO_PI); var value = r/n;
var px = value * ((n - 1) * cos(t) - cos((n - 1) * t));
var py = value * ((n - 1) * sin(t) + sin((n - 1) * t));
vertex(px, py); }
noFill();
stroke(c);
strokeWeight(1);
endShape(CLOSE);}