Canvas class is a static abstraction over three rendering backends. Rather than calling the browser Canvas API or CanvasKit directly, all rendering in GeoPackage JS goes through Canvas, which delegates to whichever adapter is registered for the current runtime environment.
GeoPackageManager.open() and GeoPackageManager.create() call Canvas.initializeAdapter() automatically, so you normally do not need to call it yourself. You only need to interact with Canvas directly when:
- Running in Node.js and freeing memory (
disposeCanvas,disposeImage). - Overriding the default WASM file locations.
- Registering a custom adapter.
Adapters
The correct adapter is selected automatically based on the detected runtime environment.| Adapter | Environment | Backend |
|---|---|---|
HtmlCanvasAdapter | Browser (main thread) | Standard HTMLCanvasElement / Canvas 2D API |
CanvasKitCanvasAdapter | Node.js | CanvasKit WebAssembly (Skia) |
OffscreenCanvasAdapter | Web Worker | OffscreenCanvas + createImageBitmap |
Context.setupDefaultContext(), which is called as part of GeoPackageManager.open() and GeoPackageManager.create().
WASM file location
Node.js — CanvasKit WASM
By default,CanvasKitCanvasAdapter looks for canvaskit.wasm relative to the installed package. If you bundle the library or move the WASM file, call setCanvasKitWasmLocateFile before opening any GeoPackage:
A function that receives the WASM filename (e.g.
"canvaskit.wasm") and returns the full path or URL where it can be found.Browser — sql.js WASM
TheSqljsAdapter (used for the underlying SQLite layer in the browser) also requires a WASM file. Configure its location with setSqljsWasmLocateFile:
A function that receives the WASM filename (e.g.
"sql-wasm.wasm") and returns the URL where it can be fetched.You must call
setSqljsWasmLocateFile before calling GeoPackageManager.open() or GeoPackageManager.create(), otherwise the browser may fail to load the WASM bundle from the wrong path.Static methods
initializeAdapter()
Initializes the registered canvas adapter. This is called automatically byGeoPackageManager.open() and GeoPackageManager.create(); you only need to call it manually if you are constructing a canvas outside of those entry points.
create()
Creates a new canvas of the given dimensions using the active adapter.Width of the canvas in pixels.
Height of the canvas in pixels.
HTMLCanvasElement in the browser, an EmulatedCanvas2D in Node.js (CanvasKit), or an OffscreenCanvas in a Web Worker.
disposeCanvas()
Releases resources held by a canvas created withCanvas.create(). In Node.js (CanvasKit), failing to call this leaks native WASM memory. In the browser the call is a no-op, but it is good practice to call it unconditionally.
The canvas object previously returned by
Canvas.create().disposeImage()
Releases resources held by aGeoPackageImage. In Node.js (CanvasKit), failing to call this leaks native WASM memory.
The image to dispose. The image must not be used after this call.
createImage()
Decodes raw bytes, a data URL, or aBlob into a GeoPackageImage.
Raw image bytes, a base64 data URL, or a
Blob.MIME type of the image data. Defaults to
'image/png'.toDataURL()
Encodes the current content of a canvas as a base64 data URL.The canvas to encode, previously created with
Canvas.create().MIME type for the output image. Defaults to
'image/png'.Compression quality from
0 to 1, used only by lossy formats such as 'image/jpeg'.toBytes()
Encodes the current content of a canvas as aUint8Array.
The canvas to encode.
Output format. Defaults to
ImageType.PNG.Quality from
0 to 1 for lossy formats.scaleImage()
Returns a newGeoPackageImage scaled by a uniform factor.
The source image.
Scale factor. Use
0.5 to halve the dimensions, 2 to double them.scaleImageToDimensions()
Returns a newGeoPackageImage scaled to exact pixel dimensions.
The source image.
Target width in pixels.
Target height in pixels.
getImageData()
Returns the raw pixel data of an image as anImageData object.
The image to read pixel data from.
measureText()
Returns the rendered width of a text string in pixels for the given font settings.drawText()
Draws text centred on a point inside a canvas rendering context.Two-element array
[x, y] specifying the centre point for the text.registerCanvasAdapter()
Registers a canvas adapter class. Called automatically by the context setup helpers; use this only when implementing a custom adapter.A class (constructor) that implements the
CanvasAdapter interface.GeoPackageImage
GeoPackageImage wraps the adapter-specific image handle (an HTMLImageElement, a CanvasKit Image, or an ImageBitmap) together with its dimensions and MIME type.
Constructor
GeoPackageImage instances from Canvas.createImage() or ImageUtils.getImage() rather than constructing them directly.
Methods
getImage()
Returns the underlying adapter-specific image object.HTMLImageElement (browser), a CanvasKit Image (Node.js), or ImageBitmap (Web Worker).
getWidth()
getHeight()
getFormat()
'image/png').
getImageType()
ImageType enum value corresponding to the image’s MIME type.
getImageData()
ImageData object. The result is cached after the first call. Internally calls Canvas.getImageData().
getPixel()
Returns the packed ARGB value of a single pixel.isPixelTransparent()
Returnstrue if the alpha channel of the given pixel is zero.
dispose()
Convenience wrapper aroundCanvas.disposeImage(this). Call this in Node.js to free CanvasKit WASM memory.