Skip to content

feat(shape): support custom crop shapes - #107

Merged
imerljak merged 1 commit into
mainfrom
feat/104-custom-shapes
Nov 4, 2026
Merged

imerljak merged 1 commit into
mainfrom
feat/104-custom-shapes

Conversation

@imerljak

@imerljak imerljak commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Adds custom crop shapes: shape now also accepts a CropperShapeDefinition (SVG path + viewBox). One geometry drives the on-screen outline, the export mask and the preview clip, so they cannot drift apart.

Changes

  • CropperShapeDefinition { path, viewBox, stretch? }. viewBox takes [minX, minY, w, h] or the SVG string form.
  • Export: maskToPath clips to Path2D(path), mapped by per-axis scale and the viewBox origin, on a fresh canvas. JPEG fill follows the existing rule.
  • On screen: ShapeOverlay SVG inside <cropper-selection>: preserveAspectRatio="none", non-scaling-stroke, overflow: visible, pointer-events: none. Stroke themable via --react-cropper-2-shape-stroke.
  • Aspect ratio: locked to the viewBox ratio unless stretch: true. A conflicting aspectRatio warns in dev.
  • Defaults: outlined and grid default to false for custom shapes. Explicit props win. rect/circle unchanged.
  • Preview: CropperPreview clips with an objectBoundingBox <clipPath>, so it scales with the thumbnail.
  • Validation: a path not starting with M, or an unusable viewBox, falls back to rect with a dev warning.
  • Shapes are resolved by value, so inline objects don't re-warn or invalidate callbacks.
  • polygonShape(points) helper. CustomShape story. README section.

Testing

  • Unit: shape resolution/validation, viewBox mapping (origin, export size ≠ viewBox), clip/transform order, JPEG fill, overlay attributes, outline/grid defaults, aspect lock, preview clip path, inline-shape stability. Existing rect/circle tests unchanged.
  • Storybook (Chromium): overlay mounts in the selection and updates on shape change.
  • Manually against real CropperJS: overlay box matches the selection before and after a handle resize, dragging a box corner outside the shape moves the selection, and the exported PNG is opaque at the centre and transparent at the corners.

Closes #104

Accept a CropperShapeDefinition (SVG path + viewBox) as the shape. One
geometry drives the on-screen SVG outline, the export mask and the
preview clip, so they always match.

Custom shapes lock the selection to the viewBox ratio unless stretch is
set, default the rectangular outline and grid off, and fall back to rect
with a dev warning when invalid. Adds a polygonShape helper.

Closes #104
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.80%. Comparing base (9d070ea) to head (041e544).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #107      +/-   ##
==========================================
+ Coverage   99.75%   99.80%   +0.04%     
==========================================
  Files           7        9       +2     
  Lines         831     1041     +210     
  Branches      144      194      +50     
==========================================
+ Hits          829     1039     +210     
  Misses          2        2              
Flag Coverage Δ
unittests 99.80% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@imerljak
imerljak merged commit 00c7e4e into main Nov 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support custom crop shapes

1 participant