Skip to Content
Skia Wasm

Web worker

Draw from a worker, through an OffscreenCanvas.

Open in a new tab

The page transfers its canvas to a worker and forwards pointer events. The worker creates the WebGPU device and draws every frame.

main.ts
// The page hands its canvas over to a worker, which does all the drawing.
const canvas = document.querySelector("canvas")!;
// A worker cannot see the screen: the page pins the size of the canvas and
// tells the worker how many pixels a unit should take.
canvas.style.width = `${canvas.width}px`;
canvas.style.height = `${canvas.height}px`;
const offscreen = canvas.transferControlToOffscreen();
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
  type: "module",
});
worker.postMessage({ canvas: offscreen, pixelRatio: devicePixelRatio }, [
  offscreen,
]);

// Input stays on the page and reaches the worker as plain numbers.
canvas.addEventListener("pointermove", (event) => {
  worker.postMessage({ x: event.offsetX, y: event.offsetY });
});
worker.ts
import { Skia } from "skia-wasm";

type Message =
  { canvas: OffscreenCanvas; pixelRatio: number } | { x: number; y: number };

const pointer = { x: 256, y: 256 };

const start = async (canvas: OffscreenCanvas, pixelRatio: number) => {
  // WebGPU is available in workers: the device is created here.
  const adapter = await navigator.gpu.requestAdapter();
  const device = await adapter!.requestDevice();
  const sk = await Skia.init({ device });
  const surface = sk.surface(canvas, { pixelRatio });

  const effect = sk.effect<{ iTime: "f32"; iPointer: "vec2" }>(`
    uniform float iTime;
    uniform float2 iPointer;

    half4 main(float2 xy) {
      float d = distance(xy, iPointer);
      float ripple = 0.5 + 0.5 * cos(d / 12 - iTime * 4);
      float3 color = mix(
        float3(0.06, 0.09, 0.16),
        float3(0.23, 0.51, 0.96),
        ripple * exp(-d / 160)
      );
      return half4(color, 1);
    }
  `);
  const paint = sk.paint();

  // Workers that own a canvas have requestAnimationFrame too.
  const frame = (time: number) => {
    const shader = effect.shader({
      iTime: time / 1000,
      iPointer: [pointer.x, pointer.y],
    });
    paint.shader = shader;
    shader.dispose();
    surface.getCanvas().drawPaint(paint);
    surface.flush();
    requestAnimationFrame(frame);
  };
  requestAnimationFrame(frame);
};

addEventListener("message", ({ data }: MessageEvent<Message>) => {
  if ("canvas" in data) {
    start(data.canvas, data.pixelRatio);
  } else {
    pointer.x = data.x;
    pointer.y = data.y;
  }
});