Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

canvas-context

dmnsgn843MIT3.3.0TypeScript support: included

Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.

canvas, context, offscreen, worker, offscreen-canvas, 2d, webgl, experimental-webgl, webgl2, bitmaprenderer, webgpu, gpupresent

readme

canvas-context

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.

paypal coinbase twitter

See the example and its source with a Worker implementation.

Installation

npm install canvas-context

Usage

import createCanvasContext from "canvas-context";

const { context, canvas } = createCanvasContext("2d", {
  width: 100,
  height: 100,
  offscreen: true,
});

API

Modules

createCanvasContext

Typedefs

ContextType : "2d" | "webgl" | "experimental-webgl" | "webgl2" | "webgl2-compute" | "bitmaprenderer" | "gpupresent" | "webgpu"

A DOMString containing the context identifier defining the drawing context associated to the canvas.

CanvasContextOptions : object

Options for canvas creation. All optional.

CanvasContextReturnValue : object

createCanvasContext

createCanvasContext([contextType], [options]) ⇒ CanvasContextReturnValue

Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.

Kind: Exported function

Param Type Default
[contextType] ContextType "2d"
[options] CanvasContextOptions {}

ContextType : "2d" | "webgl" | "experimental-webgl" | "webgl2" | "webgl2-compute" | "bitmaprenderer" | "gpupresent" | "webgpu"

A DOMString containing the context identifier defining the drawing context associated to the canvas.

Kind: global typedef

CanvasContextOptions : object

Options for canvas creation. All optional.

Kind: global typedef Properties

Name Type Default Description
[width] number 300 Request an initial canvas width.
[height] number 150 Request an initial canvas height.
[offscreen] boolean false Request an offscreen canvas.
[worker] boolean false Handle use in a worker.
[contextAttributes] CanvasRenderingContext2DSettings | WebGLContextAttributes {} Attributes to be passed to getContext.

CanvasContextReturnValue : object

Kind: global typedef Properties

Name Type
canvas HTMLCanvasElement | OffscreenCanvas
context RenderingContext

License

MIT. See license file.

changelog

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

3.3.0 (2024-07-06)

Features

  • log error on getContext failure (66ee6e1)

3.2.0 (2022-04-02)

Features

  • replace gpupresent with webgpu (42a6dc7)

3.1.0 (2021-10-02)

Features

  • add exports field to package.json (1237509)

3.0.2 (2021-04-14)

Bug Fixes

3.0.1 (2021-03-22)

Bug Fixes

  • add sideEffects key to package.json (5a0110e)

3.0.0 (2021-03-18)

⚠ BREAKING CHANGES

  • use ES modules

Bug Fixes