What is HTML Canvas?
The HTML <canvas>
element is used to draw graphics, on the fly, via JavaScript.
The <canvas>
element is only a container for graphics. You must use JavaScript to actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
Canvas is supported by all major browsers.
Canvas Examples
A canvas is a rectangular area on an HTML page. By default, a canvas has no border and no content.
The markup looks like this:
<canvas id="myCanvas" width="200" height="100"></canvas>
- HTML Canvas: HTML5 element for drawing graphics with JavaScript.
- getContext('2d'): Method to get the 2D drawing context.
- Basic drawing methods: Drawing shapes, lines, text, etc.
- Coordinates and sizes: Canvas has its own coordinate system.
- Simple shapes and paths: Drawing rectangles, circles, lines, and complex paths.
- Color and style: Managing line and fill colors, styles, and thickness.
- Text and fonts: Drawing text with specified font, size, and color.
- Event handling: Handling mouse clicks, key presses, etc.
- Animation: Creating animations using
requestAnimationFrame()
or setInterval()
.
- Cross-browser compatibility: Ensuring compatibility across major browsers and devices.
- clearRect(x, y, width, height): Clears the specified rectangular area of the canvas.