Name

Canvas.getContext() — returns a context for drawing on the canvas

Synopsis

Object getContext(String contextID)

Arguments

contextID

This argument specifies the type of drawing you want to do with the canvas. Pass the string “2d” to obtain a CanvasRenderingContext2D object with which you can do two-dimensional drawing.

Returns

An object with which you can draw into the Canvas element. When you pass the string “2d”, this will be a CanvasRenderingContext2D object for 2D drawing.

Description

There is only one CanvasRenderingContext2D object per canvas element, so repeated calls to getContext("2d") return the same object.

HTML5 standardizes the “2d” argument to this method and defines no other valid arguments. A separate standard, WebGL, is under development for 3D graphics. In browsers that support it, you can pass the string “webgl” to this method to obtain an object that allows 3D rendering. Note, however, that the CanvasRenderingContext2D object is the only drawing context documented in this book.

See Also

CRC

Get Canvas Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.