HTML Full Course Day 16 π» || Canvas Basics in Hindi π | Mohit Decodes
π¨ HTML Tutorial β Part 16: Canvas Basics
Welcome to Day 16 of the HTML Full Course [Hindi] by Mohit Decodes! Today, we explore the HTML5 Canvas β a powerful element for drawing graphics, animations, and visual effects directly in the browser using JavaScript.
πΉ What is Canvas?
The <canvas>
element provides a drawable region in HTML where you can use JavaScript to draw shapes, images, text, and animations dynamically.
βοΈ Basic Canvas Setup
Example:
html
CopyEdit
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #000000;">
Your browser does not support the canvas element.
</canvas>
<script>
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw a blue rectangle
ctx.fillStyle = 'blue';
ctx.fillRect(50, 50, 150, 100);
</script>
πΉ Key Canvas Concepts:
- Context: The drawing surface (usually 2D or WebGL). Use
getContext('2d')
for 2D graphics. - Shapes: Draw rectangles, circles, lines, paths, and more.
- Colors & Styles: Set fill colors, strokes, gradients.
- Text: Draw text on canvas.
- Animations: Use JavaScript timers to create dynamic animations.
π‘ Pro Tips:
- Canvas is pixel-based, so it doesnβt affect the DOM elements inside it.
- Use canvas for games, visualizations, charts, and interactive graphics.
- Combine with CSS and HTML elements for richer UIs.