# get-canvas-context

> gets canvas context by name

Latest version **1.0.2** (published 2016-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-canvas-context
pnpm add get-canvas-context
yarn add get-canvas-context
bun add get-canvas-context
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-05-10 |
| First published | 2015-06-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Matt DesLauriers |
| Maintainers | mattdesl |
| Keywords | canvas, context, 2d, webgl, experimental, webgl2, 3d, gl, html5 |

## Links

- npm: https://www.npmjs.com/package/get-canvas-context
- Repository: https://github.com/Jam3/get-canvas-context
- Issues: https://github.com/Jam3/get-canvas-context/issues
- npm.io page: https://npm.io/package/get-canvas-context

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-05-10
- 1.0.1 — 2015-06-11
- 1.0.0 — 2015-06-11

## README

# get-canvas-context

[![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)

Creates a new HTML5 Canvas Context with the given dimensions and options. Returns `null` if not supported.

Supports `'webgl'`, `'webgl2'` and `'2d'`, handles vendor prefixing, and runs in Node and the Browser.

## Install

```sh
npm install get-canvas-context --save
```

## Example

```js
var getContext = require('get-canvas-context')

// create a new 50x50 2D canvas
var ctx = getContext('2d', {
  width: 50,
  height: 50
})

// add to DOM
document.body.appendChild(ctx.canvas)

// draw to it
ctx.fillRect(0, 0, 50, 50)
```

Or, WebGL using an existing canvas:

```js
var canvas = document.createElement('canvas')

var gl = getContext('webgl', {
  canvas: canvas,
  antialias: true
})

if (!gl) {
  throw new Error('webgl not supported')
}
```

## Usage

[![NPM](https://nodei.co/npm/get-canvas-context.png)](https://www.npmjs.com/package/get-canvas-context)

#### `ctx = createContext(type, [opt])`

Returns a new canvas context for the given `type`, a string which is either `'2d'`, `'webgl'` or `'webgl2'`. The options:

- `canvas` - an existing canvas element to re-use rather than creating a new one
- `width` - if specified, will set the canvas width
- `height` - if specified, will set the canvas height
- `{...contextAttributes}` any other options for the rendering context, like `alpha`

Handles vendor prefixing for WebGL contexts. Returns `null` if we are not in a browser, or if the context is not available, or if there was an error creating the context.

**Note:** As of the time of writing (Jun 2015), `"webgl2"` is only supported through special flags in Chrome Canary and FireFox Nightly.

## License

MIT, see [LICENSE.md](http://github.com/Jam3/get-canvas-context/blob/master/LICENSE.md) for details.

---
_Source: https://npm.io/package/get-canvas-context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
