Adding Canvas to the HTML Page
In the <body> section of
the HTML page, add a <canvas> tag using code such as the
following:
<canvasid="canvasOne"width="500"height="300">Your browser does not support HTML5 Canvas.</canvas>
Now, let’s break this down to understand what we are doing. The
<canvas> tag has three main
attributes. In HTML, attributes are set within
pointy brackets of an HTML tag. The three attributes we need to set
are:
idThe
idis the name we will use to reference this<canvas>tag in our JavaScript code.canvasOneis the name we will use.widthThe width, in pixels, of the canvas. The
widthwill be500pixels.heightThe height, in pixels, of the canvas. The
heightwill be300pixels.
Note
HTML5 elements, including canvas, have many more attributes: tabindex, title, class, accesskey, dir, draggable, hidden, and so on.
Between the opening <canvas> and closing </canvas> tags, you can put text that
will be displayed if the browser executing the HTML page does not
support Canvas. For our Canvas applications, we will use the text “Your
browser does not support HTML5 Canvas.” However, you can adjust this
text to say anything.
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