Back to projects

Rectangle Drawer

Draw rectangles directly on a page, resize their edges and move them with Ctrl/Cmd drag.

Rectangle Drawer project screenshot

What it does

A DOM-based interaction exercise that uses native mouse events and an object-oriented JavaScript class, without a canvas library.

Decisions

Use DOM elements, not canvas
Rectangles are absolutely positioned HTML elements. Dragging changes their styles instead of redrawing a canvas.
Detect edges by distance
The cursor's proximity to each side or corner determines the resize direction.
Keep interactions separate
Drawing, resizing and movement each have their own flags and mouse event paths.

Full notes

More on the implementation and challenges.

Next project

Number Quest

A number-guessing game with score tracking.

Working on something similar? Get in touch.