HTML Canvas Reference
Learn HTML through interactive, bite-sized lessons. Build real websites and see your code come to life instantly.
Start HTML Journey →The HTML Canvas Reference provides a comprehensive list of methods and properties for manipulating the <canvas> element. This powerful tool allows developers to create dynamic, interactive graphics directly within web pages.
Canvas Context
To begin working with canvas, you must first obtain its rendering context. The most common context is '2d', used for two-dimensional graphics.
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
Drawing Methods
The canvas API offers various methods for drawing shapes, lines, and paths. Here are some fundamental drawing methods:
fillRect(x, y, width, height): Draws a filled rectanglestrokeRect(x, y, width, height): Draws a rectangular outlinebeginPath(): Starts a new pathmoveTo(x, y): Moves the pen to specified coordinateslineTo(x, y): Draws a line to specified coordinatesarc(x, y, radius, startAngle, endAngle, anticlockwise): Draws an arc or circle
Styling Properties
Customize your canvas drawings with these styling properties:
fillStyle: Sets the color or style for filled shapesstrokeStyle: Sets the color or style for shape outlineslineWidth: Sets the width of linesfont: Specifies the font for text drawing
Text Rendering
Canvas allows you to render text with these methods:
fillText(text, x, y [, maxWidth]): Draws filled textstrokeText(text, x, y [, maxWidth]): Draws text outlines
Practical Example
Let's create a simple drawing that demonstrates some of these methods and properties:
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw a filled rectangle
ctx.fillStyle = 'blue';
ctx.fillRect(10, 10, 100, 80);
// Draw a line
ctx.beginPath();
ctx.moveTo(10, 100);
ctx.lineTo(110, 100);
ctx.strokeStyle = 'red';
ctx.lineWidth = 5;
ctx.stroke();
// Draw some text
ctx.font = '20px Arial';
ctx.fillStyle = 'green';
ctx.fillText('Hello, Canvas!', 10, 150);
Best Practices
- Always set the canvas dimensions using HTML attributes to avoid distortion
- Use
save()andrestore()methods to manage drawing states - Optimize performance by minimizing state changes and batching similar operations
- Consider using HTML5 SVG for scalable graphics that don't require pixel-level manipulation
Browser Support
The canvas element is widely supported in modern browsers. However, always provide fallback content for older browsers or those with JavaScript disabled.
Related Concepts
To further enhance your canvas skills, explore these related topics:
- HTML5 Canvas for an in-depth look at canvas capabilities
- HTML5 Graphics to compare canvas with other graphical options
- HTML and JavaScript for integrating canvas with dynamic web pages
By mastering the HTML Canvas Reference, you'll be equipped to create rich, interactive graphics for web applications, data visualizations, and more. Remember to consult the official documentation for the most up-to-date and comprehensive information on canvas methods and properties.