Getting started
From a canvas to a first frame.
Skia Wasm draws with Skia's Graphite renderer, on WebGPU. Compared with CanvasKit (canvaskit-wasm), in main-thread time per frame (M5 Max, Chrome 158) and in download size:
| CanvasKit | Skia Wasm | ||
|---|---|---|---|
| 10,000 rounded rects | 6.39 ms | 5.68 ms | 1.1x faster |
| 1,000 rects with a runtime shader | 2.11 ms | 0.78 ms | 2.7x faster |
| 200 animated paths | 1.08 ms | 0.42 ms | 2.6x faster |
| 2,000 images | 1.50 ms | 1.53 ms | Same |
| Download, Brotli | 2.42 MB | 1.71 MB | 29% smaller |
Performance has the details.
What you need
- WebGPU. In a browser, or headless: in Node with Dawn's bindings, and in Deno.
- The library.
npm install skia-wasm. In the package,dist/libholds three files to serve together:index.js,skia-wasm.jsandskia-wasm.wasm. - A canvas. The examples of this site use one of 512 by 512.
<canvas width="512" height="512"></canvas>
<script type="module" src="main.js"></script>A first frame
import { Skia } from "skia-wasm";
// Skia draws with the GPUDevice of the page.
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter!.requestDevice();
// Loads the wasm module and creates Skia's context on the device.
const sk = await Skia.init({ device });
// A surface is where drawing ends up: this one configures the canvas.
const surface = sk.surface(document.querySelector("canvas")!);
// A paint says how something is drawn.
const paint = sk.paint({ color: "#3b82f6" });
// A frame: ask for the canvas, draw, flush.
const canvas = surface.getCanvas();
canvas.clear("white");
canvas.drawRoundedRect(96, 156, 320, 200, 32, paint);
surface.flush();Five things happen here:
- The device is yours. Skia draws with the
GPUDeviceof the page. That is why it can wrap your textures, and why your own WebGPU code can run next to it. Skia.init()gives yousk. It loads the wasm module and creates Skia's context on the device. Everything else comes fromsk.- A surface is where drawing ends up.
sk.surface()takes the canvas and configures it for the device. - A paint says how to draw. A color here; it also holds strokes, shaders, blend modes and filters.
- A frame is
getCanvas(), draw,flush(). Nothing reaches the GPU before the flush.
Animating
Draw one frame per animation frame. The canvas of a new frame starts from scratch: no transform and no clip carry over from the previous one.
const frame = (time: number) => {
const canvas = surface.getCanvas();
canvas.clear("white");
canvas.rotate(time / 1000, 256, 256);
canvas.drawRoundedRect(96, 156, 320, 200, 32, paint);
surface.flush();
requestAnimationFrame(frame);
};
requestAnimationFrame(frame);One device, many surfaces
sk is not tied to a canvas. Call Skia.init() once per device, then make a surface for everything you draw to:
const sk = await Skia.init({ device });
const main = sk.surface(document.querySelector("#main")!);
const minimap = sk.surface(document.querySelector("#minimap")!);
const target = sk.surface(texture); // a GPUTexture of yours
const offscreen = sk.offscreenSurface(256, 256); // Skia owns the textureA canvas of the page, an OffscreenCanvas and a GPUTexture all work. What sk makes is shared by all of them: one paint, one image or one runtime effect can be drawn to every surface, and a snapshot of an offscreen surface is an image like any other.
Frames are per surface. Several can be open at once, and they can be flushed in any order.
A second Skia.init() is a second copy of the wasm module, with objects of its own. It is what another device needs, not another canvas.
Where the module is loaded from
The library loads skia-wasm.js from the folder of the file it is in, and skia-wasm.js loads skia-wasm.wasm from its own. Served as they are, the three files of dist/lib find each other.
A bundler moves the library into a bundle of yours. Either copy skia-wasm.js and skia-wasm.wasm (from node_modules/skia-wasm/dist/lib) next to that bundle, or say where they are:
const sk = await Skia.init({
device,
module: new URL("/skia/skia-wasm.js", location.href).href,
});
const surface = sk.surface(canvas);Next
- Conventions: the few rules that the whole API follows.
- The examples, in the sidebar: each one runs live above its code. Start with the showcase or with the basics.