HTML5: How to draw graphics, on the fly, via scripting?
Solution:
HTML Code:
Explanation:
- This HTML document starts with a doctype declaration indicating it's an HTML5 document.
- Inside the <html> tags, the document structure begins.
- The <head> section contains metadata about the document, such as character encoding and the title.
- Within the head section, there's an inline <style> element for defining CSS styles.
- The CSS styles define the appearance of the canvas element with the id "FirstCanvas". It sets the width, height, border, and background color of the canvas.
- The <body> section is where the visible content of the webpage resides.
- Within the body section, there's a canvas element with the id "FirstCanvas". It will be used to draw graphics on the fly via scripting.
- The HTML document ends with the closing </html> tag. Note: There was an error in the original code where the <body> tag was nested twice. I corrected it to have only one <body> tag.
Live Demo:
See the solution in the browser
Supported browser
![]() |
![]() |
![]() |
![]() |
![]() |
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.