JS Canvas
In this page:
const canvas = document.getElementById("id");
const ctx = canvas.getContext("2d");
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
Drawing Basic Rectangles
fillRect(x, y, width, height) draws a filled rectangle, strokeRect() draws just its outline, and clearRect() erases a rectangular area back to transparent -- the three simplest drawing operations, and often the first ones learned.
उदाहरण: Drawing Basic Rectangles
<canvas id="c" width="150" height="100"></canvas>
<script>
// Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
const ctx = document.getElementById("c").getContext("2d");
// Call `ctx.fillRect(10, 10, 50, 30)`
ctx.fillRect(10, 10, 50, 30);
// Call `ctx.strokeRect(70, 10, 50, 30)`
ctx.strokeRect(70, 10, 50, 30);
// Call `ctx.clearRect(20, 15, 10, 10)`
ctx.clearRect(20, 15, 10, 10);
</script>
Drawing Custom Paths
beginPath() starts a new shape, moveTo(x, y) moves the drawing "pen" without drawing, lineTo(x, y) draws a straight line from the current position, and closePath() connects back to the starting point -- combined, these build any custom polygon shape point by point.
उदाहरण: Drawing Custom Paths
<canvas id="c" width="150" height="100"></canvas>
<script>
// Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
const ctx = document.getElementById("c").getContext("2d");
// Call `ctx.beginPath()`
ctx.beginPath();
// Call `ctx.moveTo(10, 10)`
ctx.moveTo(10, 10);
// Call `ctx.lineTo(100, 10)`
ctx.lineTo(100, 10);
// Call `ctx.lineTo(50, 80)`
ctx.lineTo(50, 80);
// Call `ctx.closePath()`
ctx.closePath();
// Call `ctx.stroke()`
ctx.stroke();
</script>
Drawing Circles and Arcs
arc(x, y, radius, startAngle, endAngle) draws a circular arc -- passing 0 to Math.PI * 2 as the angles draws a complete circle, while other angle ranges draw partial arcs, the basis for pie-slice shapes and rounded visual elements.
उदाहरण: Drawing Circles and Arcs
<canvas id="c" width="150" height="100"></canvas>
<script>
const ctx = document.getElementById("c").getContext("2d");
ctx.beginPath();
ctx.arc(50, 50, 40, 0, Math.PI * 2); // full circle
ctx.stroke();
</script>
Drawing Text and Images
fillText(text, x, y) draws text directly onto the canvas using the current font setting, and drawImage(imageElement, x, y) draws an already-loaded <img> element (or another canvas) onto the canvas -- letting you combine graphics, photos, and labels together.
उदाहरण: Drawing Text and Images
<canvas id="c" width="150" height="100"></canvas>
<script>
// Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
const ctx = document.getElementById("c").getContext("2d");
// Assign "20px sans-serif" to `ctx.font`
ctx.font = "20px sans-serif";
// Call `ctx.fillText("Hello Canvas", 10, 50)`
ctx.fillText("Hello Canvas", 10, 50);
</script>
Building a Simple Animation Loop
requestAnimationFrame(callback) schedules a function to run right before the browser's next repaint, smoothly synced to the display's refresh rate -- calling it again inside the callback itself creates a continuous, efficient animation loop.
उदाहरण: Building a Simple Animation Loop
<canvas id="c" width="150" height="100"></canvas>
<script>
// Declare the constant `ctx`, set to `document.getElementById("c").getContext("2d")`
const ctx = document.getElementById("c").getContext("2d");
// Declare the variable `x`, set to `0`
let x = 0;
// Define the function `animate` with no parameters
function animate() {
// Call `ctx.clearRect(0, 0, 150, 100)`
ctx.clearRect(0, 0, 150, 100);
// Call `ctx.fillRect(x, 40, 20, 20)`
ctx.fillRect(x, 40, 20, 20);
x = (x + 1) % 150;
// Call `requestAnimationFrame(animate)`
requestAnimationFrame(animate);
}
// Call `animate()`
animate();
</script>
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: