canvas-context
Version:
Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.
124 lines (85 loc) • 6.37 kB
Markdown
# canvas-context
[](https://www.npmjs.com/package/canvas-context)
[](https://www.npmjs.com/package/canvas-context)
[](https://bundlephobia.com/package/canvas-context)
[](https://github.com/dmnsgn/canvas-context/blob/master/package.json)
[](https://github.com/microsoft/TypeScript)
[](https://conventionalcommits.org)
[](https://github.com/prettier/prettier)
[](https://github.com/eslint/eslint)
[](https://github.com/dmnsgn/canvas-context/blob/master/LICENSE)
Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.
[](https://paypal.me/dmnsgn)
[](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3)
[](https://twitter.com/dmnsgn)

See the [example](https://dmnsgn.github.io/canvas-context/) and its [source](index.html) with a Worker implementation.
## Installation
```bash
npm install canvas-context
```
## Usage
```js
import createCanvasContext from "canvas-context";
const { context, canvas } = createCanvasContext("2d", {
width: 100,
height: 100,
offscreen: true,
});
```
## API
<!-- api-start -->
## Modules
<dl>
<dt><a href="#module_createCanvasContext">createCanvasContext</a></dt>
<dd></dd>
</dl>
## Typedefs
<dl>
<dt><a href="#ContextType">ContextType</a> : <code>"2d"</code> | <code>"webgl"</code> | <code>"experimental-webgl"</code> | <code>"webgl2"</code> | <code>"webgl2-compute"</code> | <code>"bitmaprenderer"</code> | <code>"gpupresent"</code> | <code>"webgpu"</code></dt>
<dd><p>A DOMString containing the context identifier defining the drawing context associated to the canvas.</p>
</dd>
<dt><a href="#CanvasContextOptions">CanvasContextOptions</a> : <code>object</code></dt>
<dd><p>Options for canvas creation. All optional.</p>
</dd>
<dt><a href="#CanvasContextReturnValue">CanvasContextReturnValue</a> : <code>object</code></dt>
<dd></dd>
</dl>
<a name="module_createCanvasContext"></a>
## createCanvasContext
<a name="exp_module_createCanvasContext--createCanvasContext"></a>
### createCanvasContext([contextType], [options]) ⇒ [<code>CanvasContextReturnValue</code>](#CanvasContextReturnValue) ⏏
Create a RenderingContext (2d, webgl, webgl2, bitmaprenderer, webgpu), optionally offscreen for possible use in a Worker.
**Kind**: Exported function
| Param | Type | Default |
| ------------- | ---------------------------------------------------------- | --------------------------- |
| [contextType] | [<code>ContextType</code>](#ContextType) | <code>"2d"</code> |
| [options] | [<code>CanvasContextOptions</code>](#CanvasContextOptions) | <code>{}</code> |
<a name="ContextType"></a>
## ContextType : <code>"2d"</code> \| <code>"webgl"</code> \| <code>"experimental-webgl"</code> \| <code>"webgl2"</code> \| <code>"webgl2-compute"</code> \| <code>"bitmaprenderer"</code> \| <code>"gpupresent"</code> \| <code>"webgpu"</code>
A DOMString containing the context identifier defining the drawing context associated to the canvas.
**Kind**: global typedef
<a name="CanvasContextOptions"></a>
## CanvasContextOptions : <code>object</code>
Options for canvas creation. All optional.
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| ------------------- | ------------------------------------------------------------------------------------ | ------------------ | -------------------------------------- |
| [width] | <code>number</code> | <code>300</code> | Request an initial canvas width. |
| [height] | <code>number</code> | <code>150</code> | Request an initial canvas height. |
| [offscreen] | <code>boolean</code> | <code>false</code> | Request an offscreen canvas. |
| [worker] | <code>boolean</code> | <code>false</code> | Handle use in a worker. |
| [contextAttributes] | <code>CanvasRenderingContext2DSettings</code> \| <code>WebGLContextAttributes</code> | <code>{}</code> | Attributes to be passed to getContext. |
| [canvas] | <code>HTMLCanvasElement</code> | | Canvas element to use. |
<a name="CanvasContextReturnValue"></a>
## CanvasContextReturnValue : <code>object</code>
**Kind**: global typedef
**Properties**
| Name | Type |
| ------- | -------------------------------------------------------------- |
| canvas | <code>HTMLCanvasElement</code> \| <code>OffscreenCanvas</code> |
| context | <code>RenderingContext</code> |
<!-- api-end -->
## License
MIT. See [license file](LICENSE.md).