Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Welcome to Topic 10.4 in Chapter 10: Media!
Until now, whenever we wanted to display artwork or illustrations on a webpage, we loaded external image files like .png, .jpg, or .webp.
What happens when a visitor zooms in to 400% on a smartphone? Normal raster photos become blurry and pixelated!
What if you want to:
HTML5 introduced two native graphical powerhouses:
<canvas>In this lesson, you will discover how these two technologies work and when to pick each one!
Think about two common items in your School Geometry & Math Kit:
When you use a plastic circular stencil to trace a circle with a fine-tip pen:
Think of a grid of tiny square millimeters in your mathematics lab manual:
<canvas>! It gives you a blank grid of pixels where you can paint dynamically using code!<svg> 🛡️SVG is an XML-based language for describing 2D vector shapes directly inside your HTML document.
Because SVG is made of mathematical equations (lines, curves, and angles), it has infinite resolution — it never blurs, no matter how much you zoom in!
<title> and <desc> tags placed inside SVG.<canvas> 🎮While SVG is made of independent vector shapes, <canvas> is an empty rectangular pixel board.
You create the board in HTML, and then use JavaScript like an artist's paintbrush to draw lines, circles, text, and animations:
Always remember how canvas coordinates work:
| Feature | SVG (Vector Shapes) 📐 | Canvas (Pixel Bitmap) 🖼️ |
|---|---|---|
| Drawing Type | Vector-based (Mathematical formulas) | Raster / Bitmap (Pixel grid) |
| Resolution | Infinite! Never pixelates or blurs. | Fixed resolution. Can blur if stretched. |
| DOM Nodes | Every shape is an HTML DOM element. | A single <canvas> tag (shapes are drawn into memory). |
| CSS Styling | Yes! Can style and animate shapes via CSS. | No CSS styling for individual shapes. |
| Event Handling | Can add onclick directly to individual shapes! | Must manually calculate $(x, y)$ mouse click coordinates. |
| Performance | Slower if rendering thousands of shapes. | Lightning fast! Can render 100,000 particles at 60 FPS. |
| Best Used For | Logos, UI icons, school emblems, charts. | 2D/3D browser games, video processing, canvas drawing tools. |
Here is a complete, working HTML webpage featuring a pure SVG School Shield Emblem and an interactive Canvas target score game:
| ❌ Common Mistake | ✅ Best Practice | Why It Matters |
|---|---|---|
Setting Canvas size with CSS width/height (style="width: 400px;"). | Always set Canvas dimensions using HTML attributes: <canvas width="400" height="300">. | CSS width stretches the pixel bitmap, causing blurry, distorted graphics! |
| Using Canvas for logos and website icons. | Use SVG for logos and icons. | SVG scales infinitely without blurriness and has a much smaller file size. |
Forgetting ctx.beginPath() before drawing paths on Canvas. | Always call ctx.beginPath() before starting a new shape. | Without beginPath(), previous shapes reconnect and create messy unwanted lines. |
Forgetting accessibility on SVG (aria-label). | Add aria-label or <title> inside <svg> icons. | Screen readers need accessible labels to describe the graphic to visually impaired users. |
| Thinking $(0, 0)$ is at the bottom-left of Canvas. | Remember that $(0, 0)$ is at the top-left corner. | Canvas coordinates increase downwards on the $Y$-axis! |
<rect>, <circle>, <line>, <polygon>, and <text>.<canvas> is an empty pixel-based bitmap grid manipulated dynamically with JavaScript via its 2D context (getContext('2d')).Test your understanding with these multiple-choice questions:
<canvas>?<canvas>?Open VS Code and create a file named creative-graphics-lab.html.
Build a School Science & Sports Graphic Studio:
<svg width="200" height="200">.<circle cx="100" cy="100" r="80" fill="#0A2540" />.<circle cx="100" cy="100" r="70" fill="white" />.<text x="100" y="110" text-anchor="middle" fill="#FF6B00" font-size="28" font-weight="bold">MSK</text>.<canvas id="pitch" width="300" height="200">.ctx.fillStyle = '#2d6a4f'; ctx.fillRect(0, 0, 300, 200);.ctx.fillStyle = '#e9c46a'; ctx.fillRect(100, 20, 100, 160);.Open the file in your browser to admire your custom vector badge and painted canvas pitch!
In the next lesson, we will continue your web development journey with Forms & Input Types (Form).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Iframes & Web Embeds | Forms & Input Types |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.