get-canvas-context
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
npm install get-canvas-context --save
Example
var getContext = // create a new 50x50 2D canvasvar ctx = // add to DOMdocumentbody // draw to itctx
Or, WebGL using an existing canvas:
var canvas = document var gl = if !gl throw 'webgl not supported'
Usage
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 onewidth
- if specified, will set the canvas widthheight
- if specified, will set the canvas height{...contextAttributes}
any other options for the rendering context, likealpha
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 for details.