BCA Student Stories & UpdatesLive
HTML5 2D Canvas API Fundamentals (Coordinates, Shapes & Paths)
The HTML5 <canvas> element provides a resolution-dependent bitmap canvas that can be drawn on dynamically using JavaScript. Unlike regular DOM elements, canvas operates as an immediate-mode raster drawing surface, making it the industry foundation for web games, charts, signature pads, and image manipulation.
Day 42 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
HTML5 2D Canvas API Fundamentals (Coordinates, Shapes & Paths) — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
The HTML5 <canvas> element provides a resolution-dependent bitmap canvas that can be drawn on dynamically using JavaScript. Unlike regular DOM elements, canvas operates as an immediate-mode raster drawing surface, making it the industry foundation for web games, charts, signature pads, and image manipulation.
"Think of the <canvas> element like an artist blank linen canvas mounted on an easel, and the 2D rendering context (ctx) like the artist paintbrush and palette. You command the brush where to move and when to apply paint."
👨🏫3. Teacher Demonstration Code
See how rectangles, diagonal lines, and smooth circular badges are painted programmatically on a single canvas element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas 2D Fundamentals</title>
<style>
body { font-family: system-ui, sans-serif; background: #060c18; color: #fff; padding: 24px; text-align: center; }
canvas { background: #0c1328; border: 2px solid #1e293b; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,0.5); }
</style>
</head>
<body>
<h2>🎨 HTML5 2D Canvas Primitives</h2>
<canvas id="myCanvas" width="480" height="280"></canvas>
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#2563eb";
ctx.fillRect(30, 30, 100, 70);
ctx.strokeStyle = "#38bdf8";
ctx.lineWidth = 3;
ctx.strokeRect(160, 30, 100, 70);
ctx.beginPath();
ctx.moveTo(300, 100);
ctx.lineTo(360, 30);
ctx.lineTo(420, 100);
ctx.strokeStyle = "#c1a648";
ctx.lineWidth = 4;
ctx.stroke();
ctx.beginPath();
ctx.arc(240, 190, 50, 0, Math.PI * 2);
ctx.fillStyle = "#04AA6D";
ctx.fill();
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 42 Practical Task Test Studio
Select canvas element by id "artboard" and retrieve its 2d context into a variable named ctx.
Set the fillStyle to "#38bdf8" and draw a filled rectangle at (50, 50) with width 120 and height 80.
Use beginPath(), arc() with 0 to Math.PI * 2, and fill() to render a circle.
Spot the Bug: Canvas Dimension Scaling Distortion
Tap or click on the exact line below that contains the error:
Click any code line<!-- HTML --><canvas id="stage" style="width: 500px; height: 400px;"></canvas> <script> const ctx = document.getElementById("stage").getContext("2d"); ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2); ctx.stroke();</script>Senior Developer Forensic Analysis:
Canvas has TWO sizes: the drawing buffer coordinate size (defined via HTML width and height attributes) and the CSS display size. If width/height attributes are omitted, the drawing buffer defaults to 300x150 pixels. Applying CSS width/height stretches those 300x150 pixels like an image, distorting all aspect ratios.
📝Independent Practice: Assignment
📄 index.htmlInteractive Geometric Flag & University Badge Painter
- •Initialize an HTML5 canvas with width=600 and height=400.
- •Paint a stylized dark background with a 3-stripe horizontal flag or banner.
- •In the center, construct a circular gold emblem using ctx.arc().
- •Add text onto the canvas using ctx.font, ctx.textAlign = "center", and ctx.fillText().
- •Provide an interactive "Clear Canvas" button that invokes ctx.clearRect(0, 0, canvas.width, canvas.height).
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 42 - Canvas Flag & Badge</title>
<style>
body { font-family: "Segoe UI", sans-serif; background: #060c18; color: #fff; text-align: center; padding: 24px; }
canvas { background: #0c1328; border: 2px solid #1e293b; border-radius: 12px; margin-top: 16px; }
.btn { padding: 10px 20px; background: #2563eb; color: #fff; border: none; border-radius: 6px; font-weight: bold; cursor: pointer; margin: 6px; }
</style>
</head>
<body>
<h2>🎓 BCA Academic Badge Painter</h2>
<div>
<button class="btn" id="drawBtn">🎨 Draw Badge</button>
<button class="btn" style="background:#dc2626;" id="clearBtn">🗑️ Clear Canvas</button>
</div>
<canvas id="badgeCanvas" width="600" height="360"></canvas>
<script>
const canvas = document.getElementById("badgeCanvas");
const ctx = canvas.getContext("2d");
function drawBadge() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#0f172a";
ctx.fillRect(40, 20, 520, 320);
ctx.strokeStyle = "#38bdf8";
ctx.lineWidth = 2;
ctx.strokeRect(40, 20, 520, 320);
ctx.beginPath();
ctx.arc(300, 160, 70, 0, Math.PI * 2);
ctx.fillStyle = "#1e293b";
ctx.fill();
ctx.strokeStyle = "#c1a648";
ctx.lineWidth = 4;
ctx.stroke();
ctx.beginPath();
ctx.arc(300, 160, 55, 0, Math.PI * 2);
ctx.fillStyle = "#04AA6D";
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "bold 20px Segoe UI";
ctx.textAlign = "center";
ctx.fillText("IGNOU BCA ACADEMY", 300, 166);
}
document.getElementById("drawBtn").addEventListener("click", drawBadge);
document.getElementById("clearBtn").addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
drawBadge();
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
What happens to canvas content if you dynamically change canvas.width or canvas.height via JavaScript (e.g. canvas.width = 800)?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 42 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-42/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 42 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 41: Modular JavaScript Architecture (ES6 Modules, Import & Export) first.
Complete Day 41 First