Chapter 40: Canvas clearRect

Canvas clearRect in the clearest, most patient, step-by-step way possible.

Imagine I’m sitting right next to you with my screen open. We’re going to build understanding together — slowly, with tiny complete examples you can copy-paste and run immediately, visual thinking, common beginner traps, and repeated explanations until it feels completely natural and obvious.

Canvas clearRect – Complete Beginner-to-Confident Guide

1. The single most important sentence you must remember forever

clearRect() is the only normal way to erase (or “clear”) pixels on the canvas.

Canvas is a bitmap painter, not a scene that remembers shapes. Once you draw something (line, circle, text, image…), those pixels are permanently there until you overwrite them or clear them.

There is no “delete shape” command in Canvas like in SVG. If you want something to disappear, move, or change — you usually have to clear the old pixels first and then redraw the new version.

That is why almost every animation starts with:

JavaScript

2. What clearRect actually does (in plain English)

clearRect(x, y, width, height)

  • It erases (sets to transparent black — rgba(0,0,0,0)) all pixels inside the rectangle you specify.
  • It does not reset styles (colors, lineWidth, etc.) — only the pixels.
  • It is very fast — much faster than drawing a big white rectangle over everything.

Important gotcha If your canvas has a CSS background-color or you set canvas.style.background, clearRect will show that background in the cleared area.

But if you never set any background, cleared areas become fully transparent (you see whatever is behind the <canvas> element).

3. Minimal complete example – see clearRect in action

Copy-paste and run this — click the button to see what happens.

HTML

What you should try right now:

  1. Click “Draw colorful stuff” → see three shapes appear
  2. Click “Clear everything” → whole canvas goes back to light blue (CSS background)
  3. Draw again → then click “Clear only center” → only the middle 300×200 area disappears

Key lessons from this example:

  • clearRect(0, 0, canvas.width, canvas.height) → full erase
  • clearRect(x, y, w, h) → erase only a rectangular region
  • Clearing shows whatever is behind (CSS background or transparent)
  • Clearing does not reset colors, fonts, lineWidth — state remains

4. Common clearRect patterns you will use every day

Pattern 1 – Full clear every frame (animation)

JavaScript

Pattern 2 – Fade trail effect (partial clear)

Instead of full clear → draw semi-transparent black rectangle:

JavaScript

→ Old drawings slowly fade away — new ones stay bright.

Pattern 3 – Erase only one object (advanced)

You calculate the bounding box of the old object and clear only there:

JavaScript

5. Common beginner traps & how to avoid them

Trap 1 “I cleared but the old shapes are still there!”

→ You probably forgot to clear the full area or used wrong coordinates.

Trap 2 “After clearRect my colors / lineWidth changed!”

→ No — clearRect never changes state. It only erases pixels — fillStyle, lineWidth, etc. stay the same.

Trap 3 “Canvas becomes transparent after clearRect”

→ Normal behavior — if no CSS background is set, cleared pixels are transparent.

Fix — add CSS background:

CSS

or paint a background rectangle once:

JavaScript

6. Your three tiny practice tasks (10–15 minutes each)

Task 1 – Clear button Draw a red circle in the center Add a button that says “Erase circle” → when clicked, clear only the circle area (not whole canvas)

Task 2 – Fade trail Make a small blue circle move across the canvas (use animation loop) Instead of full clear → use semi-transparent black fillRect to create a fading trail

Task 3 – Selective erase Draw three colored rectangles Add three buttons — each erases only one rectangle (calculate its bounding box)

Paste any of them here when you finish — I’ll review it like we’re pair-programming together.

Which part still feels a little fuzzy?

  • Why we need to clear every frame in animation?
  • Difference between full clear vs partial clear?
  • Why cleared areas become transparent?
  • How to make a fading trail without full clear?
  • Something else?

Tell me — we’ll stay on clearRect until you feel super confident using it in any situation.

You’re doing really well — mastering clearRect is one of the biggest “aha” moments in Canvas! 🚀

You may also like...

Leave a Reply

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