Spirograph

glint · Examples · Tutorials · Gallery

A pen on a wheel rolling inside a ring (a hypotrochoid), drawing into its own last frame, which fades very slowly. Change the wheel and the pen and it redraws.

Spirograph: a GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform float timeDelta;
uniform sampler2D feedback;
uniform int frame;
uniform float wheel;
uniform float pen;

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec2 penAt(float t) {
    float R = 1.0, r = wheel, d = pen * r;
    return ((R - r) * vec2(cos(t), sin(t)) + d * vec2(cos((R - r) / r * t), -sin((R - r) / r * t))) * 0.42;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 col = frame < 2 ? vec3(0.96, 0.94, 0.9) : texture(feedback, gl_FragCoord.xy / resolution).rgb;
    col = mix(col, vec3(0.96, 0.94, 0.9), 0.002);                  // paper slowly showing through again
    // Everywhere the pen went since the last frame, in small steps so the line is continuous.
    float speed = 3.0, d = 1e9;
    for (int k = 0; k < 24; k++) {
        float t = (time - timeDelta * float(k) / 24.0) * speed;
        d = min(d, length(uv - penAt(t)));
    }
    vec3 ink = pal(time * 0.02 + 0.6) * 0.7;
    col = mix(col, ink, smoothstep(0.003, 0.0015, d));
    fragColor = vec4(col, 1.0);
}

More More GLSL examples

Lorenz attractor · Voxel world · Snowflakes · Maurer rose · Field lines · Tesseract · Black hole · Low poly