CanvasKit
The singleton you get back from CanvasKitInit(...). Throughout this docs site it's called CK. Everything else — types, factories, enums, color helpers, surface constructors, GPU contexts — hangs off this object.
import CanvasKitInit, { CanvasKit } from 'canvaskit-wasm';
const CK: CanvasKit = await CanvasKitInit({ locateFile: () => '/canvaskit.wasm' });
You don't delete() the singleton. It owns the WASM module; the page's lifetime is its lifetime.
Color & rect helpers
Constructors that build the lightweight typed-array shapes used everywhere. None of these allocate WASM memory — they're plain Float32Array (or number).
| Member | Args | Returns | Notes |
|---|---|---|---|
Color | r, g, b, a?: number | Color | Build a 4-float RGBA. r/g/b are 0–255, a is 0–1. Default alpha = 1. |
Color4f | r, g, b, a?: number | Color | Same shape; all four components 0–1. Use when you already have float colors. |
ColorAsInt | r, g, b, a?: number | number | 32-bit packed ARGB int. Skia accepts InputColor as either array or int. |
getColorComponents | c: Color | number[] | Decompose a Color into [r, g, b, a] 0–255 ints + 0–1 alpha. |
parseColorString | color: string, colorMap?: Record<string, Color> | Color | CSS-style color strings: '#abc', '#aabbccdd', 'rgba(...)', named colors via the optional map. |
multiplyByAlpha | c: Color, alpha: number | Color | Scale a color's alpha — handy for fading without rebuilding RGB. |
computeTonalColors | colors: TonalColorsInput | TonalColorsOutput | Adjust ambient/spot colors for drawShadow. |
LTRBRect | left, top, right, bottom: number | Rect | Build a Float32Array rect from corners. |
XYWHRect | x, y, width, height: number | Rect | Same, from origin + size. |
LTRBiRect | left, top, right, bottom: number | IRect | Integer variant. |
XYWHiRect | x, y, w, h: number | IRect | Integer variant. |
RRectXY | rect: InputRect, rx, ry: number | RRect | Rounded rect with uniform corner radius. |
getShadowLocalBounds | ctm, path, zPlaneParams, lightPos, lightRadius, flags | Rect | Bounds the next drawShadow would paint into. Useful for invalidation. |
Constants
CK.TRANSPARENT // 0x00000000
CK.BLACK // 0xFF000000
CK.WHITE // 0xFFFFFFFF
CK.RED // 0xFFFF0000
CK.GREEN // 0xFF00FF00
CK.BLUE // 0xFF0000FF
CK.YELLOW // 0xFFFFFF00
CK.CYAN // 0xFF00FFFF
CK.MAGENTA // 0xFFFF00FF
These are 32-bit ARGB ints, accepted anywhere InputColor is. Useful one-liners — canvas.clear(CK.WHITE) is the idiom you'll see most.
Path-verb codes for path.toCmds()/path.fromCmds():
CK.MOVE_VERB // 0
CK.LINE_VERB // 1
CK.QUAD_VERB // 2
CK.CONIC_VERB // 3
CK.CUBIC_VERB // 4
CK.CLOSE_VERB // 5
See Paths and cubics for the whole verb scheme.
Flags for save-layer and shadow:
CK.SaveLayerInitWithPrevious // initialize the layer with the parent contents
CK.SaveLayerF16ColorType // request 16-bit float backing for HDR layers
CK.ShadowTransparentOccluder // drawShadow flag
CK.ShadowGeometricOnly // drawShadow flag
CK.ShadowDirectionalLight // drawShadow flag
CK.gpu // boolean — true if this build has GPU support compiled in
Surface factories
// MakeSurface allocates an off-screen software surface — useful for blitting
// pre-rendered content into the visible canvas.
const off = CK.MakeSurface(200, 200);
const offCanvas = off.getCanvas();
offCanvas.clear(CK.Color(40, 90, 180, 1));
const paint = new CK.Paint();
paint.setColor(CK.WHITE);
offCanvas.drawCircle(100, 100, 60, paint);
const snap = off.makeImageSnapshot();
canvas.clear(CK.WHITE);
canvas.drawImage(snap, 110, 28, null);
surface.flush();
snap.delete();
off.delete();
| Member | Args | Returns | Notes |
|---|---|---|---|
MakeSurface | width, height: number | Surface | null | Software off-screen surface in WASM memory. No browser canvas required. |
MakeSWCanvasSurface | canvas: HTMLCanvasElement | string | Surface | null | Software surface that blits to a 2D canvas every flush(). |
MakeWebGLCanvasSurface | canvas, colorSpace?, opts?: WebGLOptions | Surface | null | One-call WebGL setup — does context creation + GrContext + surface in one go. |
MakeRasterDirectSurface | ii: ImageInfo, pixels: MallocObj, bytesPerRow: number | Surface | null | Software surface that draws straight into a Malloc'd pixel buffer you own. |
MakeCanvasSurface | canvas: HTMLCanvasElement | string | Surface | null | Deprecated. Picks WebGL if available, falls back to SW. Use MakeWebGLCanvasSurface / MakeSWCanvasSurface directly. |
GPU & context factories
Lower-level path: get a context handle, build a GrDirectContext, then build surfaces against it.
| Member | Args | Returns | Notes |
|---|---|---|---|
GetWebGLContext | canvas, opts?: WebGLOptions | WebGLContextHandle | Get a WebGL handle Skia can drive. |
MakeWebGLContext | ctx: WebGLContextHandle | GrDirectContext | null | Wrap that handle in Skia's GPU context. Preferred over MakeGrContext. |
MakeGrContext | ctx: WebGLContextHandle | GrDirectContext | null | Deprecated alias of MakeWebGLContext. |
MakeOnScreenGLSurface | ctx, w, h, colorSpace, sampleCount?, stencil? | Surface | null | Build a surface that targets the canvas's default framebuffer. |
MakeRenderTarget | ctx, w, h: number or ctx, info: ImageInfo | Surface | null | Off-screen GPU surface (FBO). Two overloads. |
MakeGPUDeviceContext | device: GPUDevice | GrDirectContext | null | WebGPU equivalent of MakeWebGLContext. |
MakeGPUTextureSurface | ctx, texture, w, h, colorSpace | Surface | null | Surface targeting a caller-supplied GPUTexture. |
MakeGPUCanvasContext | ctx, canvas, opts?: WebGPUCanvasOptions | WebGPUCanvasContext | null | Owns the swapchain. |
MakeGPUCanvasSurface | canvasContext, colorSpace, w?, h? | Surface | null | Pull one frame's surface from the swapchain. |
MakeLazyImageFromTextureSource | src, info?, srgb? | Image | Treat a DOM image source (HTMLImageElement, ImageBitmap, etc.) as an Image without an upfront upload. |
deleteContext | ctx: WebGLContextHandle | void | Release the WebGL handle — call after deleting the GrContext + surface. |
Image & picture factories
| Member | Args | Returns | Notes |
|---|---|---|---|
MakeImage | info: ImageInfo, bytes, bytesPerRow: number | Image | null | Image from raw pixel bytes. |
MakeImageFromEncoded | bytes: Uint8Array | ArrayBuffer | Image | null | PNG / JPEG / WEBP / GIF / etc. Decoded synchronously. |
MakeImageFromCanvasImageSource | src: CanvasImageSource | Image | Wrap an HTMLImageElement, HTMLVideoElement, ImageBitmap etc. |
MakeAnimatedImageFromEncoded | bytes | AnimatedImage | null | For animated GIF / APNG / WEBP. |
MakePicture | bytes | Picture | null | Deserialize an SKP byte stream. |
MakeVertices | mode: VertexMode, positions, textureCoords?, colors?, indices?, isVolatile? | Vertices | Triangle / strip / fan mesh; flat coords. colors must be Float32 4-per-vertex. |
MakeCanvas | width, height: number | EmulatedCanvas2D | null | Build the Canvas2D-API emulator. Returns null in this build — depends on the font subsystem stripped from FCK. |
Memory helpers
| Member | Args | Returns | Notes |
|---|---|---|---|
Malloc | typedArray: TypedArrayConstructor, len: number | MallocObj | Allocate a typed array backed by WASM memory; pass to APIs that accept big buffers without copying. |
MallocGlyphIDs | len: number | MallocObj | Convenience for glyph-ID arrays (matches Skia's expected element type). |
Free | m: MallocObj | void | Release a Malloc'd buffer. Required. |
Image-decode cache
CanvasKit keeps a small LRU cache of decoded image pixels (separate from any Image objects you hold). Tune the size if you're streaming many images.
| Member | Args | Returns | Notes |
|---|---|---|---|
getDecodeCacheLimitBytes | — | number | Current cap. |
getDecodeCacheUsedBytes | — | number | Currently held. |
setDecodeCacheLimitBytes | bytes: number | void | Tighten or loosen at runtime. |
Class constructors on CK
Several classes expose their constructor through the singleton:
new CK.Paint()→Paintnew CK.Path()→Pathnew CK.PictureRecorder()→PictureRecordernew CK.ContourMeasureIter(path, forceClosed, resScale)→ContourMeasureIter
The text/typeface families (Font, Typeface, TypefaceFontProvider, TextBlob, ParagraphBuilder, ParagraphStyle, TextStyle) are present in the typings but stripped from this build — see the landing page scope notes.
Factory namespaces
CK.ColorFilterCK.ImageFilterCK.MaskFilterCK.PathEffectCK.ShaderCK.RuntimeEffect— seeRuntimeEffect.CK.ColorMatrix— seeColorMatrixHelpers.CK.Matrix/CK.M44— seeMatrix3x3Helpers,Matrix4x4Helpers.CK.Vector— seeVectorHelpers.
Enum namespaces
Every enum the build exposes is reachable as CK.<Name>.<Value>:
CK.AlphaType,BlendMode,BlurStyle,ClipOp,ColorChannel,ColorSpace,ColorType.FillType,FilterMode,ImageFormat,MipmapMode.PaintStyle,Path1DEffect,PathOp,PointMode.StrokeCap,StrokeJoin,TileMode,VertexMode.
CK.FontEdging, FontHinting, GlyphRunFlags, DecorationStyle, FontSlant, FontWeight, FontWidth, PlaceholderAlignment, RectHeightStyle, RectWidthStyle, TextAlign, TextBaseline, TextDirection, TextHeightBehavior, and the *Decoration constants exist on the namespace but only matter in the upstream "full" build with text/paragraph layout.
See also
CanvasKitInitOptions— whatCanvasKitInittakes.- Bundler integration — how to actually load this on a page.
- Memory management — rules for the WASM objects this singleton constructs.
Surface— every drawing path starts here.