The <canvas> HTML element allows you to render graphics using JavaScript directly in the browser. It’s suitable for games, animations, visualizations, and other dynamic content.
Creating a <canvas> is simple. You define a width and height, and optionally a style such as a border.
To draw on a canvas, you must first get its drawing context. Most often, this is the '2d' context, obtained using the getContext('2d') method.
Use the fillRect(x, y, width, height) method to draw a rectangle. The fillStyle property sets its color.
To draw lines, use the beginPath(), moveTo(), lineTo(), and stroke() methods. These create straight lines between points.
To draw circles, use the arc() method. Its parameters include center coordinates, radius, and start/end angles in radians.
Use fillText() to render text on the canvas. The font and fillStyle properties define its appearance.
To create animation, use requestAnimationFrame(). Clear the previous frame with clearRect(), then redraw. This lets you create moving objects.
Select Language
Set theme
© 2025 ReadyTools. All rights reserved.