Registry / serialization / canvaskit-wasm

canvaskit-wasm

JSON →
library0.41.1jsnpmunverified

The `canvaskit-wasm` package provides a WebAssembly (WASM) implementation of Skia's 2D graphics API, enabling high-performance rendering capabilities across diverse JavaScript environments including browsers, Node.js, and WebWorkers. Currently at version 0.41.1, its release cycle generally aligns with updates from the upstream Skia project, which is known for its robust and feature-rich graphics engine. A key differentiator is its ability to offer a comprehensive, portable alternative to native Canvas2D APIs, particularly for complex vectorial graphics, text rendering, and advanced animations like Lottie/Skottie. The package offers several bundles – a default minimal version, a 'full' version including Skottie and other advanced features, and a 'profiling' version – allowing developers to optimize for bundle size and debugging needs. This flexibility, combined with its C++ backend performance via WASM, positions it as a powerful tool for demanding visual applications.

npm install canvaskit-wasm
INSTALL
IMPORT
SIG · CANVASKIT-WASM
C
canvaskit-wasm
serializationjavascriptv0.41.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

InitCanvasKit
import InitCanvasKit from 'canvaskit-wasm';
import { InitCanvasKit } from 'canvaskit-wasm';
This imports the default bundle's initialization function. It is a default export, not a named one. This function returns a Promise that resolves to the main CanvasKit API object.
InitCanvasKit (full bundle)
import InitCanvasKit from 'canvaskit-wasm/full';
import { InitCanvasKit } from 'canvaskit-wasm/full';
Imports the 'full' bundle which includes Skottie and other extended functionalities. For CommonJS, use `require('canvaskit-wasm/bin/full/canvaskit.js')`.
CanvasKit (resolved object)
CanvasKitInit({ /* ... */ }).then((CanvasKit) => { /* use CanvasKit */ });
import { CanvasKit } from 'canvaskit-wasm';
The primary CanvasKit API object is obtained asynchronously after `InitCanvasKit` has loaded the WASM module and its assets. It is not directly importable.
InitCanvasKit (CommonJS)
const InitCanvasKit = require('canvaskit-wasm/bin/canvaskit.js');
const InitCanvasKit = require('canvaskit-wasm');
When using CommonJS (e.g., in Node.js or Webpack), specify the direct path to the JavaScript wrapper for the WASM module. The root package export is often for ESM.

Initializes CanvasKit from a CDN, creates a canvas surface, draws a red rectangle, and flushes the drawing to an HTML canvas element.

import InitCanvasKit from 'canvaskit-wasm'; async function setupCanvasKit() { // Ensure an HTML canvas element with id 'my-canvas' exists in your page. // Example HTML: <canvas id="my-canvas" width="200" height="200"></canvas> const CanvasKit = await InitCanvasKit({ // `locateFile` tells CanvasKit where to find its .wasm file. // Using unpkg.com for a quick CDN example. locateFile: (file: string) => `https://unpkg.com/canvaskit-wasm@0.41.1/bin/${file}` }); if (!CanvasKit) { console.error('Failed to initialize CanvasKit.'); return; } // Create a SkSurface attached to the HTML canvas element. const surface = CanvasKit.MakeSWCanvasSurface('my-canvas'); if (!surface) { console.error('Could not create SkSurface'); // Handle error or fallback return; } const canvas = surface.getCanvas(); const paint = new CanvasKit.SkPaint(); paint.setColor(CanvasKit.RED); paint.setStyle(CanvasKit.PaintStyle.Stroke); paint.setStrokeWidth(5); canvas.drawRect(CanvasKit.SkRect.MakeXYWH(10, 10, 100, 100), paint); // Flush the SkSurface to render content to the HTML canvas. surface.flush(); // Clean up Skia objects when no longer needed to prevent memory leaks. paint.delete(); surface.delete(); // Note: CanvasKit itself should only be initialized once. } // Execute the setup function. setupCanvasKit();
Debug
Known issues
gotchaThe `locateFile` function passed to `CanvasKitInit` is critical. It determines how the browser or Node.js locates the `canvaskit.wasm` binary. Incorrect paths will lead to `Failed to load WebAssembly module` errors.
fix
Ensure the `locateFile` function correctly returns the URL or local path to `canvaskit.wasm` relative to where your application will be served or run. For bundlers, this often requires specific asset handling.
affects: >=0.1.0
breakingWebpack users (and similar bundlers) must configure asset handling to copy `canvaskit.wasm` to their build directory, as Webpack typically does not include raw WASM files by default.
fix
Use `CopyWebpackPlugin` in your Webpack configuration to copy `node_modules/canvaskit-wasm/bin/canvaskit.wasm` (and other bundle variants if used) to your output directory. Adjust `locateFile` accordingly.
affects: >=0.1.0
gotchaTypeScript users leveraging modern ESM imports need to enable `resolvePackageJsonExports` in their `tsconfig.json` to properly resolve `canvaskit-wasm`'s subpath exports (e.g., `canvaskit-wasm/full`).
fix
Add `"resolvePackageJsonExports": true` to your `compilerOptions` in `tsconfig.json`.
affects: >=0.30.0
gotchaThe `CanvasKit` object (the main API) is not directly importable or available globally; it is provided as the resolved value of the Promise returned by `CanvasKitInit`.
fix
Always initialize CanvasKit using the asynchronous `CanvasKitInit()` function and wait for its Promise to resolve: `CanvasKitInit({...}).then((CanvasKit) => { /* use CanvasKit */ });`
affects: >=0.1.0
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'fs' in '.../node_modules/canvaskit-wasm/bin/canvaskit.js'
Webpack attempts to bundle a Node.js-specific `fs` module usage when targeting a browser environment, as `canvaskit.js` might have conditional imports.
fix
In your Webpack configuration, add `node: { fs: 'empty' }` to the configuration object to tell Webpack to ignore the `fs` module in browser builds.
Failed to load WebAssembly module: CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found ...
The browser or Node.js environment failed to load or compile the `canvaskit.wasm` file, often because `locateFile` provided an incorrect path, or the file was not served correctly.
fix
Verify that the path provided by the `locateFile` function in `CanvasKitInit` accurately points to the `canvaskit.wasm` file. Ensure the `.wasm` file is correctly placed in your deployment, has correct permissions, and is being served with the `application/wasm` MIME type.
Upgrade
Version history
0.41.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
6
OpenAI (training)
1
Resources
canvaskit-wasm — npm install canvaskit-wasm · libregistry