Welcome to our JS Canvas API tutorial! In this lesson, we'll delve into the world of creating interactive graphics, animations, and games using JavaScript and the Canvas API. By the end of this tutorial, you'll be well-equipped to bring your creative ideas to life.
The Canvas API is a powerful JavaScript feature that allows developers to draw graphics on a web page. It consists of a <canvas> HTML element and a JavaScript context for rendering graphics. Let's take a look at the basic structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Canvas API</title>
</head>
<body>
<canvas id="myCanvas"></canvas>
<script>
// JavaScript code for drawing graphics
</script>
</body>
</html>To access the canvas and get the drawing context, we use the following code snippet:
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');Now that we've set up the canvas, let's start drawing some basic shapes.
To draw a line, we'll use the moveTo() and lineTo() methods. Here's an example of drawing a line between two points:
ctx.beginPath();
ctx.moveTo(50, 50);
ctx.lineTo(100, 100);
ctx.stroke();The Canvas API provides rect(), fillRect(), arc(), and fill() methods to draw rectangles and circles. Here's an example of drawing a rectangle and a circle:
ctx.fillStyle = 'red';
ctx.fillRect(50, 50, 50, 50);
ctx.beginPath();
ctx.arc(100, 100, 30, 0, Math.PI * 2);
ctx.fill();Before filling or stroking a path, we need to use the fillStyle or strokeStyle properties to set the desired color. Here's an example of filling a path:
ctx.fillStyle = 'green';
ctx.fill();And here's an example of stroking a path:
ctx.strokeStyle = 'blue';
ctx.stroke();To create animations, we'll use the requestAnimationFrame() function. This function ensures that the animation is smooth and syncs with the device's refresh rate.
function animate() {
requestAnimationFrame(animate);
// Update animation code here
}
animate();As you progress through this tutorial, don't forget to explore and experiment with the Canvas API. Here are two advanced examples to help you get started:
const points = [100, 100, 200, 200, 200, 100, 100, 100];
ctx.beginPath();
ctx.moveTo(points[0], points[1]);
for (let i = 2; i < points.length; i += 2) {
ctx.lineTo(points[i], points[i + 1]);
}
ctx.closePath();
ctx.fill();let x = 50;
let y = 50;
const radius = 10;
let dx = 2;
let dy = 2;
function animate() {
requestAnimationFrame(animate);
// Update ball position
x += dx;
y += dy;
// Bounce ball off canvas edges
if (x + radius > canvas.width || x - radius < 0) {
dx = -dx;
}
if (y + radius > canvas.height || y - radius < 0) {
dy = -dy;
}
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw ball
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = 'red';
ctx.fill();
}
animate();Which JavaScript method sets the drawing color for the Canvas API?
That's it for now! As you dive deeper into the Canvas API, don't forget to have fun and let your creativity shine. Happy coding! 💡🎯🚀