HTML and CSS Reference
In-Depth Information
Using the document Object to Reference the Canvas Element
in JavaScript
We will now make use of the DOM to reference the
<canvas>
we defined in HTML. Recall
that the
document
object represents every element of an HTML page after it has loaded.
We need a reference to the
Canvas
object so that we will know where to display the Canvas
API calls we will make from JavaScript.
First, wewill define anewvariable named
theCanvas
that will holdthe reference tothe
Can-
vas
object.
Next, we retrieve a reference to
canvasOne
by calling the
getElementById()
function of
document
, and passing the name
canvasOne
, which we defined as the
id
of the
<canvas>
tag
we created in the HTML page:
var
var
theCanvas
=
document
.
getElementById
(
"canvasOne"
);