AI CSS Animations

Thinking Sun Corona

A star with a boiling surface, a streaming corona and curling flares licking off its limb. Faster token output boils the surface, pushes the corona out and lifts taller flares.

Also known as: sun shader, solar corona animation, star flare shader.

HTML + JS

A canvas and a module script, nothing to install. Set canvas.dataset.tps from your stream, or call canvas.token() once per token.

<canvas id="shader-thinking-sun-corona" data-tps="320" data-color="#ff7a2e" style="display:block;width:100%;height:240px;border-radius:12px"></canvas>
<script type="module">
  // Set canvas.dataset.tps from your stream, or call canvas.token() per token.
  const canvas = document.getElementById("shader-thinking-sun-corona");
  const gl = canvas.getContext("webgl2", { alpha: false, antialias: false });
  const fragment = `#version 300 es
precision highp float;
uniform vec3 iResolution;
uniform float iTime;
uniform float uTokens;
uniform float uTps;
uniform vec3 uColor;
out vec4 outColor;
#define TAU 6.28318530718
#define PI 3.14159265359
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float hash12(vec2 p) { vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }
// a phase in radians that wraps at whole turns, so it stays precise for hours
float turn(float x, float k) { return mod(x * k, TAU); }
vec3 toLin(vec3 c) { return pow(max(c, vec3(0.0)), vec3(2.2)); }
vec3 hueShift(vec3 c, float a) {
  vec3 k = vec3(0.57735);
  float cs = cos(a);
  return max(c * cs + cross(k, c) * sin(a) + k * dot(k, c) * (1.0 - cs), vec3(0.0));
}
// ACES filmic curve, gamma and a tiny dither against banding
vec3 finish(vec3 c, vec2 fc) {
  c = max(c, vec3(0.0));
  c = clamp((c * (2.51 * c + 0.03)) / (c * (2.43 * c + 0.59) + 0.14), 0.0, 1.0);
  c = pow(c, vec3(1.0 / 2.2));
  return c + (hash12(fc) + hash12(fc + 19.19) - 1.0) / 255.0;
}

float hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }
float vnoise(vec3 p) {
  vec3 i = floor(p), f = fract(p);
  f = f * f * (3.0 - 2.0 * f);
  return mix(mix(mix(hash13(i), hash13(i + vec3(1, 0, 0)), f.x), mix(hash13(i + vec3(0, 1, 0)), hash13(i + vec3(1, 1, 0)), f.x), f.y),
             mix(mix(hash13(i + vec3(0, 0, 1)), hash13(i + vec3(1, 0, 1)), f.x), mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
// noise streaming along dir forever: two layers trade places so the phase can wrap
float flowNoise(vec3 p, vec3 dir, float ph) {
  float a = fract(ph), b = fract(ph + 0.5);
  float w = 1.0 - abs(2.0 * a - 1.0);
  return mix(vnoise(p - dir * b + vec3(17.3, 9.1, 4.7)), vnoise(p - dir * a), w);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  vec3 acc = toLin(uColor);
  vec3 deep = acc * acc * 0.8;
  vec3 hot = mix(acc, vec3(1.0, 0.95, 0.85), 0.6);
  float e = 1.0 - exp(-uTps / 90.0);
  float px = 1.0 / iResolution.y;
  float R = 0.225;
  float r = length(uv);
  float boil = fract(iTime * 0.14) + fract(uTokens * 0.005);
  float stream = fract(iTime * 0.11) + fract(uTokens * 0.0045);
  vec3 col = vec3(0.0);

  // corona: streamers flowing out from the limb
  vec2 dir = uv / max(r, 1e-4);
  float hgt = max(r - R, 0.0);
  float n1 = flowNoise(vec3(dir * 3.0, hgt * 5.0), vec3(0.0, 0.0, 1.5), stream);
  float n2 = flowNoise(vec3(dir * 8.0 + 3.0, hgt * 9.0), vec3(0.0, 0.0, 2.4), stream);
  float streak = pow(0.55 * n1 + 0.45 * n2, 2.2) * 2.2;
  float fall = exp(-hgt * mix(12.0, 6.5, e));
  col += mix(deep, acc, fall) * (0.2 + streak * (0.9 + 0.8 * e)) * fall * (0.45 + 0.9 * e);
  col += mix(acc, hot, 0.35) * exp(-hgt * 40.0) * (0.4 + 0.6 * e);
  col += acc * exp(-hgt * 3.0) * 0.035;

  // flares: curling tongues of plasma licking off the limb, taller as the rate climbs
  float ang = atan(uv.y, uv.x);
  float y = r - R;
  float lick = turn(uTokens, 0.006) + turn(iTime, 0.2);
  for (int i = 0; i < 7; i++) {
    float fi = float(i);
    float h1 = hash12(vec2(fi, 2.7)), h2 = hash12(vec2(fi, 8.1));
    float th = (fi + 0.4 * h1) * TAU / 7.0 + 0.25 * sin(turn(iTime, 0.03) + fi * 2.0);
    float rise = 0.5 + 0.5 * sin(lick + h2 * TAU);
    float len = (0.03 + (0.04 + 0.13 * e) * rise) * (0.6 + 0.6 * h1);
    float x = (mod(ang - th + PI, TAU) - PI) * R;
    // the tongue curls more toward its tip
    x += (0.012 + 0.01 * e) * sin(y * 70.0 - lick * 2.0 + fi) * smoothstep(0.0, len, y);
    float w = (0.009 + 0.014 * h2) * (1.0 + y * 10.0);
    float tongue = exp(-x * x / (w * w)) * smoothstep(len, 0.0, y) * smoothstep(-0.006, 0.006, y);
    col += mix(acc, hot, tongue * 0.5) * tongue * (0.5 + 1.6 * e) * (0.4 + 0.6 * rise);
  }

  // the boiling surface
  float cover = smoothstep(R + px, R - px, r);
  if (cover > 0.0) {
    float z = sqrt(max(R * R - r * r, 0.0));
    vec3 sp = vec3(uv, z) / R;
    sp.xz *= rot(turn(iTime, 0.05));
    float g1 = flowNoise(sp * 4.0, vec3(0.4, 1.0, 0.3), boil);
    float g2 = flowNoise(sp * 8.5 + 4.0, vec3(-0.6, 0.5, 1.0), boil * 2.0);
    float gran = smoothstep(0.2, 0.9, 0.6 * g1 + 0.4 * g2);
    float mu = z / R;
    float limb = 0.25 + 0.75 * pow(mu, 0.7);
    vec3 surf = mix(acc * 0.6, acc * 1.2, gran);
    surf = mix(surf, hot, 0.25 * gran * gran * mu);
    surf *= limb * (1.3 + 1.3 * e);
    col = mix(col, surf, cover);
  }
  fragColor = vec4(finish(col, fragCoord), 1.0);
}

void main() {
  mainImage(outColor, gl_FragCoord.xy);
  outColor.a = 1.0;
}
`;
  const vertex = `#version 300 es
void main() {
  vec2 p = vec2((gl_VertexID << 1) & 2, gl_VertexID & 2);
  gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
}
`;
  const compile = (type, src) => {
    const s = gl.createShader(type);
    gl.shaderSource(s, src);
    gl.compileShader(s);
    if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s));
    return s;
  };
  const program = gl.createProgram();
  gl.attachShader(program, compile(gl.VERTEX_SHADER, vertex));
  gl.attachShader(program, compile(gl.FRAGMENT_SHADER, fragment));
  gl.linkProgram(program);
  gl.useProgram(program);
  const u = (n) => gl.getUniformLocation(program, n);
  const [res, time, tokens, rate, color] = ["iResolution", "iTime", "uTokens", "uTps", "uColor"].map(u);
  const hex = parseInt((canvas.dataset.color || "#7c5cff").slice(1), 16);
  gl.uniform3f(color, ((hex >> 16) & 255) / 255, ((hex >> 8) & 255) / 255, (hex & 255) / 255);

  // canvas.token(): count real tokens and derive the rate from the last second.
  const stamps = [];
  canvas.token = () => {
    stamps.push(performance.now());
    canvas.dataset.tps = "auto";
  };
  const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
  let count = 0, last = performance.now(), visible = true, frame = 0;
  new IntersectionObserver(([e]) => {
    visible = e.isIntersecting;
    if (visible && !frame) frame = requestAnimationFrame(draw);
  }).observe(canvas);

  function draw(now) {
    frame = 0;
    const dt = Math.min((now - last) / 1000, 0.1);
    last = now;
    while (stamps.length && now - stamps[0] > 1000) stamps.shift();
    const wobble = 1 + 0.25 * Math.sin(now / 588) + 0.15 * Math.sin(now / 233 + 1) + 0.08 * Math.sin(now / 91 + 2);
    const tps = canvas.dataset.tps === "auto" ? stamps.length : Number(canvas.dataset.tps) * wobble;
    count += tps * dt;
    const dpr = Math.min(devicePixelRatio || 1, 2);
    const w = Math.round(canvas.clientWidth * dpr), h = Math.round(canvas.clientHeight * dpr);
    if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h; }
    gl.viewport(0, 0, w, h);
    gl.uniform3f(res, w, h, 1);
    gl.uniform1f(time, still ? 2 : now / 1000);
    gl.uniform1f(tokens, still ? Number(canvas.dataset.tps) * 2 : count);
    gl.uniform1f(rate, tps);
    gl.drawArrays(gl.TRIANGLES, 0, 3);
    if (visible && !still) frame = requestAnimationFrame(draw);
  }
  frame = requestAnimationFrame(draw);
</script>

GLSL for Shadertoy

// Inputs the site passes in, as constants for Shadertoy.
// uTps: tokens per second. uTokens: tokens so far. uColor: the accent.
#define uTps 320.0
#define uTokens (iTime * uTps)
#define uColor vec3(1.000, 0.478, 0.180)

#define TAU 6.28318530718
#define PI 3.14159265359
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float hash12(vec2 p) { vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }
// a phase in radians that wraps at whole turns, so it stays precise for hours
float turn(float x, float k) { return mod(x * k, TAU); }
vec3 toLin(vec3 c) { return pow(max(c, vec3(0.0)), vec3(2.2)); }
vec3 hueShift(vec3 c, float a) {
  vec3 k = vec3(0.57735);
  float cs = cos(a);
  return max(c * cs + cross(k, c) * sin(a) + k * dot(k, c) * (1.0 - cs), vec3(0.0));
}
// ACES filmic curve, gamma and a tiny dither against banding
vec3 finish(vec3 c, vec2 fc) {
  c = max(c, vec3(0.0));
  c = clamp((c * (2.51 * c + 0.03)) / (c * (2.43 * c + 0.59) + 0.14), 0.0, 1.0);
  c = pow(c, vec3(1.0 / 2.2));
  return c + (hash12(fc) + hash12(fc + 19.19) - 1.0) / 255.0;
}

float hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }
float vnoise(vec3 p) {
  vec3 i = floor(p), f = fract(p);
  f = f * f * (3.0 - 2.0 * f);
  return mix(mix(mix(hash13(i), hash13(i + vec3(1, 0, 0)), f.x), mix(hash13(i + vec3(0, 1, 0)), hash13(i + vec3(1, 1, 0)), f.x), f.y),
             mix(mix(hash13(i + vec3(0, 0, 1)), hash13(i + vec3(1, 0, 1)), f.x), mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x), f.y), f.z);
}
// noise streaming along dir forever: two layers trade places so the phase can wrap
float flowNoise(vec3 p, vec3 dir, float ph) {
  float a = fract(ph), b = fract(ph + 0.5);
  float w = 1.0 - abs(2.0 * a - 1.0);
  return mix(vnoise(p - dir * b + vec3(17.3, 9.1, 4.7)), vnoise(p - dir * a), w);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  vec3 acc = toLin(uColor);
  vec3 deep = acc * acc * 0.8;
  vec3 hot = mix(acc, vec3(1.0, 0.95, 0.85), 0.6);
  float e = 1.0 - exp(-uTps / 90.0);
  float px = 1.0 / iResolution.y;
  float R = 0.225;
  float r = length(uv);
  float boil = fract(iTime * 0.14) + fract(uTokens * 0.005);
  float stream = fract(iTime * 0.11) + fract(uTokens * 0.0045);
  vec3 col = vec3(0.0);

  // corona: streamers flowing out from the limb
  vec2 dir = uv / max(r, 1e-4);
  float hgt = max(r - R, 0.0);
  float n1 = flowNoise(vec3(dir * 3.0, hgt * 5.0), vec3(0.0, 0.0, 1.5), stream);
  float n2 = flowNoise(vec3(dir * 8.0 + 3.0, hgt * 9.0), vec3(0.0, 0.0, 2.4), stream);
  float streak = pow(0.55 * n1 + 0.45 * n2, 2.2) * 2.2;
  float fall = exp(-hgt * mix(12.0, 6.5, e));
  col += mix(deep, acc, fall) * (0.2 + streak * (0.9 + 0.8 * e)) * fall * (0.45 + 0.9 * e);
  col += mix(acc, hot, 0.35) * exp(-hgt * 40.0) * (0.4 + 0.6 * e);
  col += acc * exp(-hgt * 3.0) * 0.035;

  // flares: curling tongues of plasma licking off the limb, taller as the rate climbs
  float ang = atan(uv.y, uv.x);
  float y = r - R;
  float lick = turn(uTokens, 0.006) + turn(iTime, 0.2);
  for (int i = 0; i < 7; i++) {
    float fi = float(i);
    float h1 = hash12(vec2(fi, 2.7)), h2 = hash12(vec2(fi, 8.1));
    float th = (fi + 0.4 * h1) * TAU / 7.0 + 0.25 * sin(turn(iTime, 0.03) + fi * 2.0);
    float rise = 0.5 + 0.5 * sin(lick + h2 * TAU);
    float len = (0.03 + (0.04 + 0.13 * e) * rise) * (0.6 + 0.6 * h1);
    float x = (mod(ang - th + PI, TAU) - PI) * R;
    // the tongue curls more toward its tip
    x += (0.012 + 0.01 * e) * sin(y * 70.0 - lick * 2.0 + fi) * smoothstep(0.0, len, y);
    float w = (0.009 + 0.014 * h2) * (1.0 + y * 10.0);
    float tongue = exp(-x * x / (w * w)) * smoothstep(len, 0.0, y) * smoothstep(-0.006, 0.006, y);
    col += mix(acc, hot, tongue * 0.5) * tongue * (0.5 + 1.6 * e) * (0.4 + 0.6 * rise);
  }

  // the boiling surface
  float cover = smoothstep(R + px, R - px, r);
  if (cover > 0.0) {
    float z = sqrt(max(R * R - r * r, 0.0));
    vec3 sp = vec3(uv, z) / R;
    sp.xz *= rot(turn(iTime, 0.05));
    float g1 = flowNoise(sp * 4.0, vec3(0.4, 1.0, 0.3), boil);
    float g2 = flowNoise(sp * 8.5 + 4.0, vec3(-0.6, 0.5, 1.0), boil * 2.0);
    float gran = smoothstep(0.2, 0.9, 0.6 * g1 + 0.4 * g2);
    float mu = z / R;
    float limb = 0.25 + 0.75 * pow(mu, 0.7);
    vec3 surf = mix(acc * 0.6, acc * 1.2, gran);
    surf = mix(surf, hot, 0.25 * gran * gran * mu);
    surf *= limb * (1.3 + 1.3 * e);
    col = mix(col, surf, cover);
  }
  fragColor = vec4(finish(col, fragCoord), 1.0);
}

Customizing this shader

  • Speed: uTps is the token rate and uTokens the running count. The standalone runner adds a natural wobble to a fixed rate, the way real streams speed up and slow down.
  • Color: set data-color on the canvas, or change uColor in the Shadertoy version. Currently previewing #ff7a2e.
  • Size: style the canvas with CSS. It renders at up to twice your screen's pixel density and stops drawing while it is off screen.
  • Reduced motion: the runner draws a single still frame for people who asked their OS for less motion.

Similar shaders