Index of every page on this site: llms.txt
All skills
johnpolacek avatar

/animaxxing-webgl

@8ddf542
Run it yourself

Add GSAP-driven WebGL to a site without restyling it, on one shared OGL renderer and canvas per document. Image planes track each img for a hover lens, scroll-velocity wave, wipe, glitch, dissolve, pixelation, and ripple. Framed views draw in an element's box: a camera flight through a 3D spiral of images, GPU particles morphing between shapes, a liquid image, and liquid-metal drops that melt into a word or pour down a page. GSAP tweens shader uniforms. The real img, or a poster img for a view, stays the accessible content and the fallback when WebGL is missing, the context is lost, an image lacks CORS, or motion is reduced. Use for WebGL image effects, shader hover, 3D scroll flights, particle morphs, liquid hover, uniform tweens, or keeping a canvas alive across routes. Pair with the matching GSAP framework skill for lifecycle timing. Not for glTF models, physics, DOM-only effects (use animaxxing), branding, or GSAP API questions.

Use this Skill: https://skilld.dev/gh/johnpolacek/animaxxing-skills/animaxxing-webgl

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesrecipeswebgl-stage.md

≈2.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Recipe: WebGL stage

One renderer and one fixed canvas per document. Recipes attach layers to it and detach them; the canvas exists only while something holds it. The stage caps pixel density, stops drawing when no layer is on screen or the tab is hidden, and survives a lost GPU context. Without WebGL, or with reduced motion, holdStage returns null and nothing is created.

Lifecycle: recipes such as image planes hold the stage themselves; the framework controller only holds it directly to keep the canvas alive across routes (see Wiring). Every hold ends with release; the last release destroys the canvas and frees the GPU context.

Dependencies: gsap, ogl.

import gsap from "gsap";
import { Renderer, Transform, type OGLRenderingContext } from "ogl";

/* Swap for the project's helper if it has one. */
function prefersReducedMotion(): boolean {
  if (typeof window === "undefined") return true;
  const choice = document.documentElement.dataset.motion;
  if (choice === "reduced") return true;
  if (choice === "full") return false;
  return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}

export type StageOptions = {
  /** Highest device pixel ratio drawn. Defaults to 1.5 on coarse pointers, else 2. */
  maxDpr?: number;
  /** Most drawing-buffer pixels; lowers the ratio on large screens. */
  maxPixels?: number;
  /** Stacking of the fixed canvas. It never takes pointer input. */
  zIndex?: number;
};

/** A recipe's drawing on the shared stage. */
export type StageLayer = {
  /** Creates GL resources: on attach, and again after a context restore. Clean up and throw on failure. */
  build(gl: OGLRenderingContext, stage: Stage): void;
  /** Syncs uniforms and visibility to the DOM before each frame. */
  update(stage: Stage): void;
  /** Runs after each drawn frame. */
  rendered?(): void;
  /** Frees GL resources. `lost` means they died with the context: drop references and show the DOM. */
  dispose(lost: boolean): void;
};

export type Stage = {
  readonly canvas: HTMLCanvasElement;
  readonly scene: Transform;
  /** Canvas size in CSS pixels, and the ratio drawn at. */
  width: number;
  height: number;
  dpr: number;
  /** Builds a layer and returns its idempotent removal. */
  add(layer: StageLayer): () => void;
  /** Only active layers keep the stage ticking, such as while on screen. */
  setActive(layer: StageLayer, active: boolean): void;
  lost(): boolean;
};

export type StageHold = { stage: Stage; release: () => void };

let shared: { stage: Stage; holds: number; destroy: () => void } | undefined;
let unsupported = false;

/** Holds the document's stage, creating it on first use. Options apply only on creation. */
export function holdStage(options: StageOptions = {}): StageHold | null {
  if (typeof window === "undefined" || unsupported || prefersReducedMotion()) return null;
  if (!shared) {
    const created = createStage(options);
    if (!created) {
      unsupported = true;
      return null;
    }
    shared = { ...created, holds: 0 };
  }
  const entry = shared;
  entry.holds++;
  let released = false;
  return {
    stage: entry.stage,
    release() {
      if (released) return;
      released = true;
      if (--entry.holds > 0 || shared !== entry) return;
      shared = undefined;
      entry.destroy();
    },
  };
}

function createStage({
  maxDpr = window.matchMedia("(pointer: coarse)").matches ? 1.5 : 2,
  maxPixels = 4_000_000,
  zIndex = 1,
}: StageOptions): { stage: Stage; destroy: () => void } | null {
  const canvas = document.createElement("canvas");
  const attributes = { alpha: true, premultipliedAlpha: true, antialias: false, depth: false };
  // Probe first: OGL's Renderer throws when no context can be created.
  const probe = canvas.getContext("webgl2", attributes) ?? canvas.getContext("webgl", attributes);
  if (!probe) return null;
  const webgl = typeof WebGL2RenderingContext !== "undefined" && probe instanceof WebGL2RenderingContext ? 2 : 1;
  // A new Renderer on the same canvas reuses its context with a fresh state cache.
  const createRenderer = () => new Renderer({ canvas, webgl, ...attributes });
  let renderer = createRenderer();
  const scene = new Transform();
  const layers = new Map<StageLayer, boolean>();
  let ticking = false;
  let lost = false;
  /** A frame threw: the DOM stays in charge until the stage is recreated. */
  let broken = false;

  canvas.setAttribute("aria-hidden", "true");
  canvas.setAttribute("data-webgl-stage", "");
  Object.assign(canvas.style, { position: "fixed", inset: "0", width: "100%", height: "100%", pointerEvents: "none", zIndex: String(zIndex) });

  const stage: Stage = {
    canvas,
    scene,
    width: 0,
    height: 0,
    dpr: 1,
    add(layer) {
      if (broken) throw new Error("the WebGL stage stopped after a failed frame");
      layers.set(layer, false);
      if (!lost) {
        try {
          layer.build(renderer.gl, stage);
        } catch (error) {
          layers.delete(layer);
          throw error;
        }
      }
      let removed = false;
      return () => {
        if (removed) return;
        removed = true;
        if (!layers.delete(layer)) return;
        layer.dispose(lost);
        // Clear its pixels now; a sleeping stage would otherwise keep them.
        if (!lost) frame();
        if (!anyActive()) sleep();
      };
    },
    setActive(layer, active) {
      if (!layers.has(layer)) return;
      layers.set(layer, active);
      if (active) wake();
      // A sleeping stage would otherwise keep this layer's last frame on screen.
      else if (!ticking && !anyActive()) frame();
    },
    lost: () => lost,
  };

  const anyActive = () => [...layers.values()].some(Boolean);

  const resize = (force = false) => {
    const width = canvas.clientWidth || window.innerWidth;
    const height = canvas.clientHeight || window.innerHeight;
    const dpr = Math.min(window.devicePixelRatio || 1, maxDpr, Math.sqrt(maxPixels / Math.max(1, width * height)));
    if (!force && width === stage.width && height === stage.height && dpr === stage.dpr) return;
    Object.assign(stage, { width, height, dpr });
    renderer.dpr = dpr;
    renderer.setSize(width, height);
    // setSize writes pixel sizes; keep the canvas following the viewport.
    Object.assign(canvas.style, { width: "100%", height: "100%" });
  };

  const frame = () => {
    if (lost || broken) return;
    try {
      layers.forEach((_, layer) => layer.update(stage));
      renderer.render({ scene });
      layers.forEach((_, layer) => layer.rendered?.());
    } catch (error) {
      // Hand every image back to the DOM rather than throw inside GSAP's ticker each frame.
      broken = true;
      sleep();
      layers.forEach((_, layer) => layer.dispose(false));
      // Hide whatever the failed frame presented.
      canvas.style.display = "none";
      console.error(error);
    }
  };
  /** Draws, then sleeps once nothing is active: the final frame clears the last visible layer. */
  const tick = () => {
    frame();
    if (!anyActive()) sleep();
  };
  const wake = () => {
    if (ticking || lost || broken || document.hidden || !anyActive()) return;
    ticking = true;
    gsap.ticker.add(tick);
  };
  const sleep = () => {
    if (!ticking) return;
    ticking = false;
    gsap.ticker.remove(tick);
  };

  const onResize = () => resize();
  // Moving to a screen with another pixel ratio fires no resize event.
  let ratioQuery: MediaQueryList | undefined;
  const onRatio = () => {
    ratioQuery?.removeEventListener("change", onRatio);
    ratioQuery = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
    ratioQuery.addEventListener("change", onRatio);
    resize();
  };
  const onVisibility = () => (document.hidden ? sleep() : wake());
  const onLost = (event: Event) => {
    // Without preventDefault the browser never restores the context.
    event.preventDefault();
    lost = true;
    sleep();
    layers.forEach((_, layer) => layer.dispose(true));
  };
  const onRestored = () => {
    if (broken) return;
    lost = false;
    renderer = createRenderer();
    resize(true);
    layers.forEach((_, layer) => {
      try {
        layer.build(renderer.gl, stage);
      } catch {
        layers.delete(layer);
      }
    });
    wake();
  };

  document.body.append(canvas);
  resize(true);
  window.addEventListener("resize", onResize);
  onRatio();
  document.addEventListener("visibilitychange", onVisibility);
  canvas.addEventListener("webglcontextlost", onLost);
  canvas.addEventListener("webglcontextrestored", onRestored);

  const destroy = () => {
    sleep();
    window.removeEventListener("resize", onResize);
    ratioQuery?.removeEventListener("change", onRatio);
    document.removeEventListener("visibilitychange", onVisibility);
    canvas.removeEventListener("webglcontextlost", onLost);
    canvas.removeEventListener("webglcontextrestored", onRestored);
    layers.forEach((_, layer) => layer.dispose(lost));
    layers.clear();
    // Free the GPU context now rather than at garbage collection.
    if (!lost) renderer.gl.getExtension("WEBGL_lose_context")?.loseContext();
    canvas.remove();
  };

  return { stage, destroy };
}

The canvas covers the viewport above the page at zIndex 1, so anything the page stacks above it (a fixed header at z-index: 10) still covers the planes; content overlapping an image without its own stacking context sits under them. Raise the page's overlays or lower zIndex behind a transparent page to suit the layout. The first holder's options win; pass the same options everywhere or hold the stage once at the root.

Budgets

  • maxDpr defaults to 1.5 on coarse pointers and 2 elsewhere; maxPixels (4 million) lowers it further on large screens. Match the framework skill's references/devices.md tiers.
  • Drawing stops whenever no layer is active or the tab is hidden, and a context loss stops it until the restore.
  • A frame that throws hides the canvas, hands every image back to the DOM, and refuses new layers until the last hold releases and a new stage is created.
  • Each layer is a draw call and a program; keep a page to about a dozen live planes, fewer on phones.

Wiring

// Example: the persistent shell holds the stage so the canvas and context outlive each route.
const shellHold = holdStage({ maxDpr: 2 });
// Pages build and revert image planes as they mount and unmount.
// When the shell itself unmounts:
shellHold?.release();

For where that hold lives in each router, see the framework skill's references/transition-archetypes.md, Persistent WebGL canvas.

Controller contract

Phase Call
initial state None: without a hold there is no canvas.
intro Recipes hold the stage as they build. The shell may hold it once to keep it across routes.
settled Nothing; the stage wakes and sleeps with its active layers and the tab.
outro Nothing; layers keep drawing until their recipe reverts.
unmount Each recipe's revert removes its layer and releases its hold. The last release removes the canvas and frees the context.

A lost context hands every image back to the DOM; the restore rebuilds each layer from its retained source, without refetching.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts1mo3 checks · Risk SAFE
  • Gen Agent Trust Hub1mo

    The skill provides a comprehensive set of TypeScript recipes for creating GSAP-driven WebGL effects such as image transitions, particle morphing, and liquid distortions using the OGL library. It maintains high standards for accessibility and performance, ensuring functional DOM fallbacks when WebGL is unavailable or reduced motion is preferred.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

Signed by skilld at 8ddf542. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 weeks ago.

Activeupdated 2 months ago
Other metadata
metadata
{
  "short-description": "GSAP-tweened WebGL image effects, 3D flights, particle morphs, and liquid images with a DOM fallback"
}

README badge

README badge for johnpolacek/animaxxing-skills/animaxxing-webgl