Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer), optionally offscreen for possible use in a Worker.
See the demo and its source for an example with Worker implementation.
npm install canvas-context
const createCanvasContext = require("canvas-context");
const { context, canvas } = createCanvasContext("2d", {
width: 100,
height: 100,
offscreen: true
});
type ContextType =
| "2d"
| "webgl"
| "experimental-webgl"
| "webgl2"
| "bitmaprenderer";
Option | Type | Default | Description |
---|---|---|---|
contextType | ContextType | "2d" | Context identifier defining the drawing context associated to the canvas |
options.canvas | HTMLCanvasElement? | An optional canvas. Necessary when window === "undefined" (eg. Node or Worker context) | |
options.width | number? | 300 | Set canvas.width (should be an integer >= 0) |
options.height | number? | 150 | Set canvas.height (should be an integer >= 0) |
options.offscreen | boolean? | false | Create an OffscreenCanvas or transferControlToOffscreen() if options.canvas |
options.worker | boolean? | false | Try to get an OffscreenCanvas and return only the canvas so it can be transferred in a Worker |
MIT. See license file.