BASIC CANVAS USAGE
The <canvas>
element requires at least its width
and height
attributes to be set in order to indicate the size of the drawing to be created. Any content appearing between the opening and closing tags is fallback data that is displayed only if the <canvas>
element isn't supported. For example:
<canvas id="drawing" width="200" height="200">A drawing of something.</canvas>
As with other elements, the width
and height
attributes are also available as properties on the DOM element object and may be changed at any time. The entire element may be styled using CSS as well, and the element is invisible until it is styled or drawn upon.
To begin drawing on a canvas, you need to retrieve a drawing context. A reference to a drawing context is retrieved using the getContext()
method and passing in the name of the context. For example, passing "2d"
retrieves a 2D context object:
let drawing = document.getElementById...