The HTML5 <canvas> element is used to draw graphics on a web page via JavaScript. It provides a space where developers can render 2D shapes, images, and other visual elements dynamically. This is especially useful for creating games, graphs, charts, animations, and more.
<canvas> ElementThe <canvas> element creates a drawing area within the web page, but it doesn't render anything by default. To draw on the canvas, you need to use JavaScript to access its context, which provides the drawing API.
<canvas id="myCanvas" width="500" height="500">
Your browser does not support the canvas element.
</canvas>
In the above example, we define a 500x500 pixel canvas. The text inside the <canvas> tag will be displayed if the browser doesn't support the canvas element.
To draw on the canvas, we need to access its context using JavaScript. The getContext() method returns an object that provides the drawing methods.
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
The ctx variable now holds the 2D context for drawing on the canvas.
To draw a rectangle on the canvas, use the fillRect() or strokeRect() method, depending on whether you want a filled or just outlined rectangle.
// Draw a filled rectangle
ctx.fillStyle = "#FF0000"; // Set color
ctx.fillRect(50, 50, 200, 100); // x, y, width, height
To draw a line, use the moveTo() and lineTo() methods to define the start and end points of the line.
ctx.beginPath(); // Start a new path
ctx.moveTo(50, 50); // Starting point (x, y)
ctx.lineTo(200, 200); // Ending point (x, y)
ctx.stroke(); // Apply the stroke
To draw a circle, use the arc() method. It requires parameters such as the x and y coordinates, radius, start angle, and end angle in radians.
ctx.beginPath();
ctx.arc(250, 250, 50, 0, 2 * Math.PI); // x, y, radius, start angle, end angle
ctx.fillStyle = "blue";
ctx.fill(); // Fill the circle with color
ctx.stroke(); // Apply the stroke
Arcs are segments of circles. You can define the starting and ending angles to create arc-like shapes.
ctx.beginPath();
ctx.arc(250, 250, 70, 0, Math.PI); // Half-circle
ctx.stroke(); // Apply the stroke
Use the fillText() or strokeText() methods to draw text on the canvas. You can customize the font, size, and color of the text.
ctx.font = "30px Arial"; // Set font size and family
ctx.fillStyle = "green"; // Set text color
ctx.fillText("Hello, Canvas!", 100, 100); // Text, x, y
You can draw images onto the canvas using the drawImage() method. This method allows you to position and scale images as needed.
var img = new Image();
img.src = 'path_to_image.jpg';
img.onload = function() {
ctx.drawImage(img, 100, 100, 200, 200); // x, y, width, height
};
If you only want to draw a part of an image, you can specify a portion of the image with additional parameters in the drawImage() method.
ctx.drawImage(img, 0, 0, 100, 100, 50, 50, 100, 100); // source x, source y, source width, source height, destination x, destination y, destination width, destination height
To create animations, you can repeatedly draw and update the canvas. The requestAnimationFrame() method is commonly used for smooth animations, as it calls the specified function before the next repaint.
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
ctx.fillRect(x, 50, 100, 100); // Draw rectangle
x += 1; // Update position
requestAnimationFrame(animate); // Call animate recursively
}
var x = 0;
animate(); // Start animation
The Canvas API offers powerful functionality for drawing 2D graphics and animations directly in the browser. It allows developers to create interactive, dynamic content such as games, data visualizations, and multimedia effects. By using the Canvas element with JavaScript, you can unleash a wide range of creative possibilities on the web.
Use the <link> tag inside the <head> to attach an external CSS file.
Comments in HTML are written between <!-- and -->.
HTML entities are used to display reserved or special characters.
The <iframe> tag embeds another webpage within the current page.
The id attribute uniquely identifies a single HTML element.
Hyperlinks are created using the <a> tag with an href attribute.
Use the <img> tag and specify the image source with the src attribute.
Use the target="_blank" attribute inside the <a> tag.
Copyrights © 2024 letsupdateskills All rights reserved