Skip to main content
Technology & EngineeringReact Patterns29 lines

Canvas Image Editing and Masking in React

Implement a responsive React canvas image editor with nondestructive masks, reliable image-space coordinates, undo and consistent export.

Quick Summary9 lines
Define a small editable document with a source image, dimensions, transforms and one mask. Keep the original image unchanged. Store editing commands or mask data in image coordinates. The canvas bitmap is a rendered view of that document, so resizing the panel or changing device pixel ratio does not mutate the image.

## Key Points

- [WHATWG canvas rendering and origin-clean export](https://html.spec.whatwg.org/multipage/canvas.html)
- [CSS transforms and matrix semantics](https://www.w3.org/TR/css-transforms-1/)
- [W3C Pointer Events](https://www.w3.org/TR/pointerevents3/)
skilldb get react-patterns-skills/canvas-image-editorFull skill: 29 lines
Paste into your CLAUDE.md or agent config

Canvas Image Editing and Masking in React

Define a small editable document with a source image, dimensions, transforms and one mask. Keep the original image unchanged. Store editing commands or mask data in image coordinates. The canvas bitmap is a rendered view of that document, so resizing the panel or changing device pixel ratio does not mutate the image.

Keep coordinates and rendering separate

Map pointer client coordinates through the displayed canvas bounds and the inverse of the image-to-view transform. Account for pan, zoom, and any letterboxing. Use device pixel ratio for sharp display backing pixels, separate from export dimensions. If applying CSS transforms beyond ordinary layout, ensure the inverse mapping includes them. Test the same image point at two device scales and two zoom levels. Clamp edits to the actual image domain.

In React, keep stable document state and a canvas reference. Render through a bounded drawing path and clean up asynchronous image loading and listeners when inputs change or the component unmounts. Prevent an older decode from replacing a newly selected image. Use a separate mask layer or an equivalent rendering model and replay strokes or commands consistently. Choose one undoable action per completed gesture. A canceled stroke must leave the last committed mask unchanged.

Export from the same document

Render to the selected image dimensions and format using the same document and mask interpretation as the editor. Avoid using a screenshot of the scaled viewport as export. Check alpha behavior when converting between formats. Cross-origin imagery that is not origin-clean cannot be exported through the canvas; only load authorized same-origin or appropriately CORS-enabled sources, and report a useful failure rather than trying to circumvent browser restrictions. Revoke temporary object URLs at the correct point in their lifecycle.

Provide accessible controls for opening, undoing, adjusting, resetting and exporting. Expose equivalent keyboard actions for operations that do not require spatial painting. Use a small synthetic image with color and alpha markers to compare edited preview and export. Test a stroke after resize, undo/redo, cancellation, a stale asynchronous image and a blocked cross-origin export. Record image-space geometry and exported dimensions and sample pixels.

References and rights

Original SkillDB instructions under LicenseRef-SkillDB-Content. References were checked 2026-11-13 and used for technical verification.

Install this skill directly: skilldb add react-patterns-skills

Get CLI access →