Skip to content
Tutorials Center
My learning

Topics

Lesson 119 of 132 ยท Projects

Project: Paint App with Canvas

Build a drawing app in React with the canvas element - a ref to the canvas, pointer events for mouse, touch and pen, strokes kept in state, and undo, redo and clear that redraw from data.

A paint app is a great way to learn how React works with a drawing surface it does not control. A <canvas> is a bitmap: once you draw a line, the browser forgets that it was a line, it only has pixels. That makes undo impossible unless you remember what was drawn. The solution is the idea this whole track keeps repeating: keep the data in React state (a list of strokes), and treat the canvas as a view that is redrawn from that data. You will use refs, effects and events.

We use pointer events (pointerdown, pointermove, pointerup). One set of handlers covers mouse, touch and stylus, so you never write separate touch code.

Stage 1: draw straight onto the canvas

The simplest version needs no state at all: grab the 2D context from a ref and draw segments as the pointer moves. Drawing is a side effect on an external object, so it lives in event handlers, not in render. Two details matter: setPointerCapture keeps events flowing even if the pointer leaves the canvas mid-stroke, and touch-action: none stops the page scrolling while a finger draws.

Stage 1: direct drawing

drawing is a ref, not state: flipping it should not re-render anything. This version is fast but has no memory. There is no way to undo.

Stage 2: strokes as data, canvas as a view

Now each stroke is an object { color, size, points }. While drawing, we append points to the current stroke in state; an effect redraws every stroke whenever strokes changes. Undo is then just "remove the last stroke", and Clear is "empty the list". The redraw function is pure drawing code: clear, then loop.

Stage 2: strokes in state with undo

Why redraw everything on each move? For a hobby app it is fast enough, and it is simple and correct. Large drawing apps cache finished strokes on an offscreen canvas and only draw the current one live. Note how every state update is immutable: [...s.slice(0, -1), { ...last, points: [...] }] builds new arrays and objects (objects and arrays in state).

Stage 3: redo and keyboard shortcuts

Redo needs a second list: strokes you undid. Undo moves the last stroke from strokes to redoStack; redo moves it back; starting a new stroke empties the redo stack (a new action invalidates the "future"). A reducer keeps these related lists consistent, which is the job reducers do best (reducers). Ctrl+Z and Ctrl+Y (or Cmd on a Mac) call the same actions.

Stage 3: undo, redo and shortcuts

Things left as exercises: scale the canvas for high-density screens with devicePixelRatio, add an eraser (draw in the background colour), or export the picture with canvas.toDataURL(). Saving files and clipboard access are blocked in the lesson sandbox, so they are easier to try in your own project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does stage 2 keep strokes in state instead of only drawing on the canvas?

    A canvas does not remember shapes. Keeping strokes as data lets you remove the last one and redraw everything.
  2. 2. What is the benefit of pointer events over separate mouse and touch events?

    Pointer events unify input devices. Pair them with touch-action: none so the page does not scroll while drawing.
  3. 3. Why is the drawing flag a ref and not state?

    Refs hold mutable values that do not affect what is rendered. A re-render on pointer down would add nothing.
  4. 4. What should happen to the redo stack when you start a new stroke?

    After undoing and then drawing something new, the old undone strokes no longer make sense, so the reducer clears redo on begin.

Summary

  • A canvas is a bitmap: keep the drawing as data and redraw from it in an effect.
  • Use a ref for the canvas and for flags like "is drawing" that should not trigger renders.
  • Pointer events plus setPointerCapture and touch-action: none work for mouse, touch and stylus.
  • Undo and redo are two lists managed by a reducer; a new stroke clears redo.
  • Update state immutably, even for points inside a stroke.

Next: Project: Rock Paper Scissors and Dice/Coin Games