Skip to content

Instantly share code, notes, and snippets.

@marcedwards
Last active November 16, 2022 07:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcedwards/a164232f245f8cc0f747fc5c34c56bea to your computer and use it in GitHub Desktop.
Save marcedwards/a164232f245f8cc0f747fc5c34c56bea to your computer and use it in GitHub Desktop.
Zooming spiral in Processing
//
// Zooming spiral.
// Created using Processing 3.5.3.
//
// Code by @marcedwards from @bjango.
//
// A GIF of this code can be seen here:
// https://twitter.com/marcedwards/status/1152906086615117824
//
void setup() {
size(400, 400, P3D);
frameRate(30);
smooth(8);
noFill();
}
void draw() {
background(#101530);
pushMatrix();
translate(width / 2, height / 2);
//scale(1); // Make it bigger, if you need to.
rotate(TAU * 0.125);
for (int i = 0; i < 8; i++) {
drawLines(#dddddd, i / 2.0 * 5 + 90);
}
for (int i = 0; i < 8; i++) {
drawLines(#ff3351, i / 2.0 * 5 + 60);
}
for (int i = 0; i < 8; i++) {
drawLines(#4bbcf6, i / 2.0 * 5 + 30);
}
for (int i = 0; i < 8; i++) {
drawLines(#1d62f0, i / 2.0 * 5);
}
popMatrix();
}
void drawLines(color col, float offset) {
float size = 70;
float turns = 22;
float sides = 48; // Try this as 3 or 4 or whatever.
float spread = 40;
float gapseed = 0.1;
boolean gaps = false;
stroke(col);
beginShape();
for (float i = 0; i < TAU * turns; i += TAU / sides) {
float nexti = i + (TAU / sides);
float x = cos(i) * size;
float xn = cos(nexti) * size;
float y = sin(i) * size;
float yn = sin(nexti) * size;
float z = i * spread - (100 * spread) + offset + (timeLoop(60) * spread * TAU);
float zn = nexti * spread - (100 * spread) + offset + (timeLoop(60) * spread * TAU);
float w = clamp(map((i / TAU + timeLoop(60)) / turns, 0.5, 0.9, 0, 1));
float wn = clamp(map((nexti / TAU + timeLoop(60)) / turns, 0.5, 0.9, 0, 1));
for (float seg = 0; seg < 1; seg += 0.25) {
// Comment out the line below to stop the corners being darkened.
stroke(lerpColor(col, 0, Ease.hermite5(1 - Ease.tri(seg)) * 0.2));
strokeWeight(lerp(w, wn, seg) * 2 + 0.2);
if (gaps) {
if (noise(lerp(i, nexti, seg) % TAU, offset, gapseed) > 0.6) {
endShape();
beginShape();
}
}
vertex(lerp(x, xn, seg), lerp(y, yn, seg), lerp(z, zn, seg));
}
}
endShape();
}
//
float timeLoop(float totalframes, float offset) {
return (frameCount + offset) % totalframes / totalframes;
}
float timeLoop(float totalframes) {
return timeLoop(totalframes, 0);
}
float clamp(float value) {
return constrain(value, 0, 1);
}
static class Ease {
static public float hermite5(float t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static public float hermite5(float t, int repeat) {
for (int i = 0; i < repeat; i++) {
t = hermite5(t);
}
return t;
}
static public float tri(float t) {
return t < 0.5 ? t * 2 : 2 - (t * 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment