Name
Canvas.getContext() — returns a context for drawing on the canvas
Synopsis
Object getContext(String contextID)Arguments
contextIDThis 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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access