Chapter 38: Canvas Shapes

1. What are “Canvas Shapes” actually?

Canvas Shapes means using JavaScript on the <canvas> element to draw basic geometric figures — rectangles, circles, ellipses, lines, polygons, curves, arcs, and custom paths — that form the building blocks of almost every canvas drawing.

In Canvas, there are no ready-made shape tags like in SVG (<circle>, <rect>, <path>). Instead, you use the 2D drawing context (ctx) to command the browser to draw each shape using methods like:

  • fillRect(), strokeRect(), roundRect()
  • arc()
  • beginPath(), moveTo(), lineTo(), quadraticCurveTo(), bezierCurveTo(), closePath()
  • stroke(), fill()

So Canvas Shapes = code instructions that tell the canvas “draw a rectangle here”, “draw a circle there”, “connect these points with lines”, etc.

2. Your First Canvas Shapes Example – All Basic Shapes in One File (Copy-Paste & Run)

Create canvas-shapes.html and paste this complete code:

HTML

What you see:

  • Rectangle (filled + stroked)
  • Rounded rectangle
  • Circle (filled + stroked)
  • Straight line
  • Zigzag closed path (triangle-like)
  • Half-circle pie slice

4. All Important Shape-Drawing Commands (With Copy-Paste Snippets)

A. Rectangles

JavaScript

B. Circles & Arcs

JavaScript

C. Lines & Paths (the most flexible)

JavaScript

D. Curves

JavaScript

E. Ellipse (oval)

JavaScript

5. Teacher’s Quick Tips (Hyderabad Student Style 😄)

  • Always beginPath() before new shape — prevents connecting to previous drawing
  • stroke() draws the border — fill() fills the inside
  • Order matters: fill() first, then stroke() → stroke sits on top
  • Common mistake #1: Draw shape but forget fill() or stroke() → invisible!
  • Common mistake #2: Forget beginPath() → lines connect to previous shape
  • Pro tip: Use ctx.save() / ctx.restore() when changing styles
JavaScript

Understood Canvas Shapes fully now? These are the building blocks of almost every canvas project — master them and you’re ready for animations, games, charts, image editors, anything.

Tell me what you want next — we can build on this:

  • Full paint app (click to draw shapes)?
  • Animated growing/shrinking shapes?
  • Shape collision detection (game style)?
  • Custom polygon / star generator?
  • Or 15-question Canvas shapes quiz?

Just say — we keep going step by step together! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *