canvaskit-wasm 0.39 build 2026-04-29

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).

MemberArgsReturnsNotes
Colorr, g, b, a?: numberColorBuild a 4-float RGBA. r/g/b are 0–255, a is 0–1. Default alpha = 1.
Color4fr, g, b, a?: numberColorSame shape; all four components 0–1. Use when you already have float colors.
ColorAsIntr, g, b, a?: numbernumber32-bit packed ARGB int. Skia accepts InputColor as either array or int.
getColorComponentsc: Colornumber[]Decompose a Color into [r, g, b, a] 0–255 ints + 0–1 alpha.
parseColorStringcolor: string, colorMap?: Record<string, Color>ColorCSS-style color strings: '#abc', '#aabbccdd', 'rgba(...)', named colors via the optional map.
multiplyByAlphac: Color, alpha: numberColorScale a color's alpha — handy for fading without rebuilding RGB.
computeTonalColorscolors: TonalColorsInputTonalColorsOutputAdjust ambient/spot colors for drawShadow.
LTRBRectleft, top, right, bottom: numberRectBuild a Float32Array rect from corners.
XYWHRectx, y, width, height: numberRectSame, from origin + size.
LTRBiRectleft, top, right, bottom: numberIRectInteger variant.
XYWHiRectx, y, w, h: numberIRectInteger variant.
RRectXYrect: InputRect, rx, ry: numberRRectRounded rect with uniform corner radius.
getShadowLocalBoundsctm, path, zPlaneParams, lightPos, lightRadius, flagsRectBounds 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();
MemberArgsReturnsNotes
MakeSurfacewidth, height: numberSurface | nullSoftware off-screen surface in WASM memory. No browser canvas required.
MakeSWCanvasSurfacecanvas: HTMLCanvasElement | stringSurface | nullSoftware surface that blits to a 2D canvas every flush().
MakeWebGLCanvasSurfacecanvas, colorSpace?, opts?: WebGLOptionsSurface | nullOne-call WebGL setup — does context creation + GrContext + surface in one go.
MakeRasterDirectSurfaceii: ImageInfo, pixels: MallocObj, bytesPerRow: numberSurface | nullSoftware surface that draws straight into a Malloc'd pixel buffer you own.
MakeCanvasSurfacecanvas: HTMLCanvasElement | stringSurface | nullDeprecated. 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.

MemberArgsReturnsNotes
GetWebGLContextcanvas, opts?: WebGLOptionsWebGLContextHandleGet a WebGL handle Skia can drive.
MakeWebGLContextctx: WebGLContextHandleGrDirectContext | nullWrap that handle in Skia's GPU context. Preferred over MakeGrContext.
MakeGrContextctx: WebGLContextHandleGrDirectContext | nullDeprecated alias of MakeWebGLContext.
MakeOnScreenGLSurfacectx, w, h, colorSpace, sampleCount?, stencil?Surface | nullBuild a surface that targets the canvas's default framebuffer.
MakeRenderTargetctx, w, h: number or ctx, info: ImageInfoSurface | nullOff-screen GPU surface (FBO). Two overloads.
MakeGPUDeviceContextdevice: GPUDeviceGrDirectContext | nullWebGPU equivalent of MakeWebGLContext.
MakeGPUTextureSurfacectx, texture, w, h, colorSpaceSurface | nullSurface targeting a caller-supplied GPUTexture.
MakeGPUCanvasContextctx, canvas, opts?: WebGPUCanvasOptionsWebGPUCanvasContext | nullOwns the swapchain.
MakeGPUCanvasSurfacecanvasContext, colorSpace, w?, h?Surface | nullPull one frame's surface from the swapchain.
MakeLazyImageFromTextureSourcesrc, info?, srgb?ImageTreat a DOM image source (HTMLImageElement, ImageBitmap, etc.) as an Image without an upfront upload.
deleteContextctx: WebGLContextHandlevoidRelease the WebGL handle — call after deleting the GrContext + surface.

Image & picture factories

MemberArgsReturnsNotes
MakeImageinfo: ImageInfo, bytes, bytesPerRow: numberImage | nullImage from raw pixel bytes.
MakeImageFromEncodedbytes: Uint8Array | ArrayBufferImage | nullPNG / JPEG / WEBP / GIF / etc. Decoded synchronously.
MakeImageFromCanvasImageSourcesrc: CanvasImageSourceImageWrap an HTMLImageElement, HTMLVideoElement, ImageBitmap etc.
MakeAnimatedImageFromEncodedbytesAnimatedImage | nullFor animated GIF / APNG / WEBP.
MakePicturebytesPicture | nullDeserialize an SKP byte stream.
MakeVerticesmode: VertexMode, positions, textureCoords?, colors?, indices?, isVolatile?VerticesTriangle / strip / fan mesh; flat coords. colors must be Float32 4-per-vertex.
MakeCanvaswidth, height: numberEmulatedCanvas2D | nullBuild the Canvas2D-API emulator. Returns null in this build — depends on the font subsystem stripped from FCK.

Memory helpers

MemberArgsReturnsNotes
MalloctypedArray: TypedArrayConstructor, len: numberMallocObjAllocate a typed array backed by WASM memory; pass to APIs that accept big buffers without copying.
MallocGlyphIDslen: numberMallocObjConvenience for glyph-ID arrays (matches Skia's expected element type).
Freem: MallocObjvoidRelease 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.

MemberArgsReturnsNotes
getDecodeCacheLimitBytesnumberCurrent cap.
getDecodeCacheUsedBytesnumberCurrently held.
setDecodeCacheLimitBytesbytes: numbervoidTighten or loosen at runtime.

Class constructors on CK

Several classes expose their constructor through the singleton:

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

Enum namespaces

Every enum the build exposes is reachable as CK.<Name>.<Value>:

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