react-img-cutout 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # react-img-cutout
2
+ [![npm version](https://img.shields.io/npm/v/react-img-cutout.svg)](https://www.npmjs.com/package/react-img-cutout)
2
3
 
3
4
  `react-img-cutout` provides a simple, composable component for creating interactive image regions.
4
5
  It enables pixel-perfect interaction using transparent PNG cutouts, while also supporting standard bounding boxes and polygons for geometric shapes.
@@ -3,8 +3,11 @@ import { type HoverEffectPreset, type HoverEffect } from "./hover-effects";
3
3
  import { Cutout } from "./cutouts/image/cutout";
4
4
  import { BBoxCutout } from "./cutouts/bbox/bbox-cutout";
5
5
  import { PolygonCutout } from "./cutouts/polygon/polygon-cutout";
6
+ import { CircleCutout } from "./cutouts/circle/circle-cutout";
6
7
  import { CutoutOverlay } from "./cutouts/cutout-overlay";
7
8
  import { DrawPolygon } from "./drawing/draw-polygon";
9
+ import { DrawRectangle } from "./drawing/draw-rectangle";
10
+ import { DrawCircle } from "./drawing/draw-circle";
8
11
  export interface CutoutViewerProps {
9
12
  /** URL of the main background image */
10
13
  mainImage: string;
@@ -16,6 +19,8 @@ export interface CutoutViewerProps {
16
19
  enabled?: boolean;
17
20
  /** When true, all cutouts show their active/hovered state simultaneously */
18
21
  showAll?: boolean;
22
+ /** When true, forces all overlays to be visible regardless of hover or selected state (default: false) */
23
+ showOverlays?: boolean;
19
24
  /** Minimum alpha value 0-255 for pixel hit-testing (default: 30) */
20
25
  alphaThreshold?: number;
21
26
  /** Delay in ms before the hover state clears after leaving a cutout (default: 150) */
@@ -40,8 +45,11 @@ type CutoutViewerComponent = ((props: CutoutViewerProps) => ReactElement) & {
40
45
  Cutout: typeof Cutout;
41
46
  BBoxCutout: typeof BBoxCutout;
42
47
  PolygonCutout: typeof PolygonCutout;
48
+ CircleCutout: typeof CircleCutout;
43
49
  Overlay: typeof CutoutOverlay;
44
50
  DrawPolygon: typeof DrawPolygon;
51
+ DrawRectangle: typeof DrawRectangle;
52
+ DrawCircle: typeof DrawCircle;
45
53
  };
46
54
  export declare const CutoutViewer: CutoutViewerComponent;
47
55
  export {};
@@ -0,0 +1,23 @@
1
+ import { type ReactNode } from "react";
2
+ import { type HoverEffect, type HoverEffectPreset } from "../../hover-effects";
3
+ import type { RenderLayerProps } from "../image/cutout";
4
+ export interface CircleCutoutProps {
5
+ /** Unique identifier for this cutout */
6
+ id: string;
7
+ /** Normalized 0-1 center coordinate */
8
+ center: {
9
+ x: number;
10
+ y: number;
11
+ };
12
+ /** Normalized 0-1 radius as a fraction of min(viewerWidth, viewerHeight) */
13
+ radius: number;
14
+ /** Human-readable label */
15
+ label?: string;
16
+ /** Override the viewer-level hover effect for this specific cutout */
17
+ effect?: HoverEffectPreset | HoverEffect;
18
+ /** Children rendered inside this cutout's context (e.g. `<Overlay>`) */
19
+ children?: ReactNode;
20
+ /** Custom renderer for the cutout layer. When provided, replaces the default rendering. */
21
+ renderLayer?: (props: RenderLayerProps) => ReactNode;
22
+ }
23
+ export declare function CircleCutout({ id, center: defCenter, radius: defRadius, label, effect: effectOverride, children, renderLayer, }: CircleCutoutProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,11 @@
1
+ import type { CutoutBounds, HitTestStrategy, CircleCutoutDefinition } from "../../hit-test-strategy";
2
+ export declare class CircleHitTestStrategy implements HitTestStrategy {
3
+ id: string;
4
+ bounds: CutoutBounds;
5
+ private cx;
6
+ private cy;
7
+ private rx;
8
+ private ry;
9
+ constructor(def: CircleCutoutDefinition, viewportWidth?: number, viewportHeight?: number);
10
+ hitTest(nx: number, ny: number): boolean;
11
+ }
@@ -12,6 +12,8 @@ export interface CutoutOverlayProps {
12
12
  className?: string;
13
13
  /** Additional inline styles (merged after placement styles) */
14
14
  style?: CSSProperties;
15
+ /** When true, forces the overlay to be visible regardless of hover or selected state. This overrides the CutoutViewer's `showOverlays` setting if set. */
16
+ showOverlay?: boolean;
15
17
  }
16
18
  /**
17
19
  * Renders custom UI positioned relative to the parent `<CutoutViewer.Cutout>`'s
@@ -25,4 +27,4 @@ export interface CutoutOverlayProps {
25
27
  * </CutoutViewer.Overlay>
26
28
  * </CutoutViewer.Cutout>
27
29
  */
28
- export declare function CutoutOverlay({ placement, children, className, style, }: CutoutOverlayProps): import("react/jsx-runtime").JSX.Element;
30
+ export declare function CutoutOverlay({ placement, children, className, style, showOverlay, }: CutoutOverlayProps): import("react/jsx-runtime").JSX.Element;
@@ -1,27 +1,29 @@
1
1
  /**
2
- * Contour-tracing utilities for extracting polygon outlines from alpha data.
2
+ * Contour-tracing utilities for extracting smooth polygon outlines
3
+ * from alpha data.
3
4
  *
4
- * Used by the trace effect to render image cutouts with the same SVG
5
- * stroke-dasharray animation that geometric shapes use.
5
+ * Used by the trace effect to render image cutouts with SVG stroke
6
+ * animations that closely follow the subject silhouette.
6
7
  *
7
8
  * Pipeline:
8
9
  * 1. Downscale alpha buffer to a working resolution for performance
9
- * 2. Build binary grid from alpha threshold
10
- * 3. Trace the outer boundary using Moore-Neighbor contour tracing
11
- * 4. Simplify with RamerDouglasPeucker
10
+ * 2. Marching Squares with linear interpolation for sub-pixel contours
11
+ * 3. Stitch edge segments into closed loops
12
+ * 4. Simplify with Ramer-Douglas-Peucker
12
13
  * 5. Return normalized [0-1] coordinate pairs
13
14
  */
15
+ type Point = [number, number];
14
16
  /**
15
- * Extract a simplified polygon contour from a pre-computed alpha buffer.
16
- *
17
- * Accepts the alpha data already extracted by the hit-test strategy,
18
- * avoiding a redundant image load.
17
+ * Extract a smooth polygon contour from a pre-computed alpha buffer
18
+ * using Marching Squares with linear interpolation for sub-pixel accuracy.
19
+ */
20
+ export declare function traceContour(alpha: Uint8Array, srcW: number, srcH: number, alphaThreshold?: number, epsilon?: number): Point[];
21
+ /**
22
+ * Convert a closed contour to a smooth SVG path using
23
+ * Catmull-Rom to Cubic Bezier conversion.
19
24
  *
20
- * @param alpha Compact alpha buffer (one byte per pixel)
21
- * @param srcW Source image width in pixels
22
- * @param srcH Source image height in pixels
23
- * @param alphaThreshold Minimum alpha value (0-255) to consider opaque
24
- * @param epsilon RDP simplification tolerance in normalized coords
25
- * (default: 0.003 — roughly 0.3 % of image size)
25
+ * Produces curves that pass through every control point with smooth
26
+ * tangent continuity, eliminating the angular straight-line look.
26
27
  */
27
- export declare function traceContour(alpha: Uint8Array, srcW: number, srcH: number, alphaThreshold?: number, epsilon?: number): [number, number][];
28
+ export declare function contourToSmoothPath(points: Point[]): string;
29
+ export {};
@@ -0,0 +1,35 @@
1
+ import { type CSSProperties } from "react";
2
+ import { type UseDrawCircleOptions } from "./use-draw-circle";
3
+ export interface DrawCircleProps extends UseDrawCircleOptions {
4
+ /**
5
+ * Stroke / accent color used for the in-progress circle visualization.
6
+ * Accepts any CSS color string.
7
+ * @default "#3b82f6"
8
+ */
9
+ strokeColor?: string;
10
+ /**
11
+ * When `false`, the drawing overlay becomes fully transparent to pointer
12
+ * events and any in-progress drawing is cleared. Useful for toggling draw
13
+ * mode on/off without unmounting the component.
14
+ * @default true
15
+ */
16
+ enabled?: boolean;
17
+ /** Additional inline styles applied to the drawing overlay container */
18
+ style?: CSSProperties;
19
+ /** Additional class name applied to the drawing overlay container */
20
+ className?: string;
21
+ }
22
+ /**
23
+ * Composable sub-component of `<CutoutViewer>` that lets users draw circle
24
+ * regions directly on the main image via click-and-drag.
25
+ *
26
+ * ### Interactions
27
+ * - **Pointer down** — set the center point
28
+ * - **Drag** — preview the circle in real time (radius = distance from center)
29
+ * - **Pointer up** — complete the circle and call `onComplete`
30
+ * - **Escape** — cancel and reset the in-progress drawing
31
+ *
32
+ * All coordinates passed to `onComplete` are normalized (0–1), matching the
33
+ * convention used by `<CutoutViewer.CircleCutout>`.
34
+ */
35
+ export declare function DrawCircle({ onComplete, minRadius, strokeColor, enabled, style, className, }: DrawCircleProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,35 @@
1
+ import { type CSSProperties } from "react";
2
+ import { type UseDrawRectangleOptions } from "./use-draw-rectangle";
3
+ export interface DrawRectangleProps extends UseDrawRectangleOptions {
4
+ /**
5
+ * Stroke / accent color used for the in-progress rectangle visualization.
6
+ * Accepts any CSS color string.
7
+ * @default "#3b82f6"
8
+ */
9
+ strokeColor?: string;
10
+ /**
11
+ * When `false`, the drawing overlay becomes fully transparent to pointer
12
+ * events and any in-progress drawing is cleared. Useful for toggling draw
13
+ * mode on/off without unmounting the component.
14
+ * @default true
15
+ */
16
+ enabled?: boolean;
17
+ /** Additional inline styles applied to the drawing overlay container */
18
+ style?: CSSProperties;
19
+ /** Additional class name applied to the drawing overlay container */
20
+ className?: string;
21
+ }
22
+ /**
23
+ * Composable sub-component of `<CutoutViewer>` that lets users draw rectangle
24
+ * regions directly on the main image via click-and-drag.
25
+ *
26
+ * ### Interactions
27
+ * - **Pointer down** — anchor the first corner
28
+ * - **Drag** — preview the rectangle in real time
29
+ * - **Pointer up** — complete the rectangle and call `onComplete`
30
+ * - **Escape** — cancel and reset the in-progress drawing
31
+ *
32
+ * All coordinates passed to `onComplete` are normalized (0–1), matching the
33
+ * convention used by `<CutoutViewer.BBoxCutout>`.
34
+ */
35
+ export declare function DrawRectangle({ onComplete, minSize, strokeColor, enabled, style, className, }: DrawRectangleProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,41 @@
1
+ export interface UseDrawCircleOptions {
2
+ /** Called when the user finishes drawing a circle */
3
+ onComplete: (circle: {
4
+ center: {
5
+ x: number;
6
+ y: number;
7
+ };
8
+ radius: number;
9
+ }) => void;
10
+ /** Minimum radius required to complete a circle (default: 0.01) */
11
+ minRadius?: number;
12
+ }
13
+ export interface UseDrawCircleReturn {
14
+ /** The in-progress circle, or null when not dragging */
15
+ circle: {
16
+ center: {
17
+ x: number;
18
+ y: number;
19
+ };
20
+ radius: number;
21
+ } | null;
22
+ /** Current drawing viewport size in pixels */
23
+ viewportSize: {
24
+ width: number;
25
+ height: number;
26
+ };
27
+ /** True while the user is dragging */
28
+ isDragging: boolean;
29
+ /** Reset (cancel) the current in-progress drawing */
30
+ reset: () => void;
31
+ /** Ref to attach to the drawing container element */
32
+ containerRef: React.RefObject<HTMLDivElement | null>;
33
+ /** Event handlers to spread onto the drawing container element */
34
+ containerProps: {
35
+ onPointerDown: (e: React.PointerEvent<HTMLDivElement>) => void;
36
+ onPointerMove: (e: React.PointerEvent<HTMLDivElement>) => void;
37
+ onPointerUp: (e: React.PointerEvent<HTMLDivElement>) => void;
38
+ onPointerLeave: () => void;
39
+ };
40
+ }
41
+ export declare function useDrawCircle({ onComplete, minRadius, }: UseDrawCircleOptions): UseDrawCircleReturn;
@@ -0,0 +1,34 @@
1
+ export interface UseDrawRectangleOptions {
2
+ /** Called when the user finishes drawing a rectangle */
3
+ onComplete: (bounds: {
4
+ x: number;
5
+ y: number;
6
+ w: number;
7
+ h: number;
8
+ }) => void;
9
+ /** Minimum size (width or height) required to complete a rectangle (default: 0.01) */
10
+ minSize?: number;
11
+ }
12
+ export interface UseDrawRectangleReturn {
13
+ /** The in-progress rectangle, or null when not dragging */
14
+ rect: {
15
+ x: number;
16
+ y: number;
17
+ w: number;
18
+ h: number;
19
+ } | null;
20
+ /** True while the user is dragging */
21
+ isDragging: boolean;
22
+ /** Reset (cancel) the current in-progress drawing */
23
+ reset: () => void;
24
+ /** Ref to attach to the drawing container element */
25
+ containerRef: React.RefObject<HTMLDivElement | null>;
26
+ /** Event handlers to spread onto the drawing container element */
27
+ containerProps: {
28
+ onPointerDown: (e: React.PointerEvent<HTMLDivElement>) => void;
29
+ onPointerMove: (e: React.PointerEvent<HTMLDivElement>) => void;
30
+ onPointerUp: (e: React.PointerEvent<HTMLDivElement>) => void;
31
+ onPointerLeave: () => void;
32
+ };
33
+ }
34
+ export declare function useDrawRectangle({ onComplete, minSize, }: UseDrawRectangleOptions): UseDrawRectangleReturn;
@@ -36,7 +36,17 @@ export interface PolygonCutoutDefinition extends BaseCutoutDefinition {
36
36
  /** Array of [x, y] normalized points forming a closed path */
37
37
  points: [number, number][];
38
38
  }
39
- export type CutoutDefinition = ImageCutoutDefinition | BoundingBoxCutoutDefinition | PolygonCutoutDefinition;
39
+ export interface CircleCutoutDefinition extends BaseCutoutDefinition {
40
+ type: "circle";
41
+ /** Normalized 0-1 center coordinate */
42
+ center: {
43
+ x: number;
44
+ y: number;
45
+ };
46
+ /** Normalized 0-1 radius as a fraction of min(viewerWidth, viewerHeight) */
47
+ radius: number;
48
+ }
49
+ export type CutoutDefinition = ImageCutoutDefinition | BoundingBoxCutoutDefinition | PolygonCutoutDefinition | CircleCutoutDefinition;
40
50
  export interface HitTestStrategy {
41
51
  /** Cutout identifier */
42
52
  id: string;
@@ -54,4 +64,9 @@ export interface HitTestStrategy {
54
64
  export { ImageHitTestStrategy } from "./cutouts/image/image-hit-test-strategy";
55
65
  export { RectHitTestStrategy } from "./cutouts/bbox/bbox-hit-test-strategy";
56
66
  export { PolygonHitTestStrategy } from "./cutouts/polygon/polygon-hit-test-strategy";
57
- export declare function createHitTestStrategy(def: CutoutDefinition, alphaThreshold: number): HitTestStrategy;
67
+ export { CircleHitTestStrategy } from "./cutouts/circle/circle-hit-test-strategy";
68
+ export interface HitTestViewportOptions {
69
+ viewportWidth?: number;
70
+ viewportHeight?: number;
71
+ }
72
+ export declare function createHitTestStrategy(def: CutoutDefinition, alphaThreshold: number, options?: HitTestViewportOptions): HitTestStrategy;
@@ -5,14 +5,23 @@ export type { CutoutOverlayProps, Placement } from "./cutouts/cutout-overlay";
5
5
  export type { CutoutProps, RenderLayerProps } from "./cutouts/image/cutout";
6
6
  export type { BBoxCutoutProps } from "./cutouts/bbox/bbox-cutout";
7
7
  export type { PolygonCutoutProps } from "./cutouts/polygon/polygon-cutout";
8
+ export type { CircleCutoutProps } from "./cutouts/circle/circle-cutout";
8
9
  export { DrawPolygon } from "./drawing/draw-polygon";
9
10
  export type { DrawPolygonProps } from "./drawing/draw-polygon";
11
+ export { DrawRectangle } from "./drawing/draw-rectangle";
12
+ export type { DrawRectangleProps } from "./drawing/draw-rectangle";
13
+ export { DrawCircle } from "./drawing/draw-circle";
14
+ export type { DrawCircleProps } from "./drawing/draw-circle";
10
15
  export { useDrawPolygon } from "./drawing/use-draw-polygon";
11
16
  export type { UseDrawPolygonOptions, UseDrawPolygonReturn, } from "./drawing/use-draw-polygon";
17
+ export { useDrawRectangle } from "./drawing/use-draw-rectangle";
18
+ export type { UseDrawRectangleOptions, UseDrawRectangleReturn, } from "./drawing/use-draw-rectangle";
19
+ export { useDrawCircle } from "./drawing/use-draw-circle";
20
+ export type { UseDrawCircleOptions, UseDrawCircleReturn, } from "./drawing/use-draw-circle";
12
21
  export { useCutout } from "./cutouts/cutout-context";
13
22
  export { useCutoutHitTest } from "./use-cutout-hit-test";
14
23
  export type { CutoutImage, CutoutBounds } from "./use-cutout-hit-test";
15
- export type { CutoutDefinition, ImageCutoutDefinition, BoundingBoxCutoutDefinition, PolygonCutoutDefinition, HitTestStrategy, } from "./hit-test-strategy";
16
- export { ImageHitTestStrategy, RectHitTestStrategy, PolygonHitTestStrategy, createHitTestStrategy, } from "./hit-test-strategy";
24
+ export type { CutoutDefinition, ImageCutoutDefinition, BoundingBoxCutoutDefinition, PolygonCutoutDefinition, CircleCutoutDefinition, HitTestStrategy, } from "./hit-test-strategy";
25
+ export { ImageHitTestStrategy, RectHitTestStrategy, PolygonHitTestStrategy, CircleHitTestStrategy, createHitTestStrategy, } from "./hit-test-strategy";
17
26
  export { hoverEffects, elevateEffect, glowEffect, liftEffect, subtleEffect, traceEffect, shimmerEffect, defineKeyframes, } from "./hover-effects";
18
27
  export type { HoverEffect, HoverEffectPreset, GeometryStyle, KeyframeAnimation, } from "./hover-effects";
@@ -1,6 +1,6 @@
1
1
  import { type CutoutDefinition, type CutoutBounds } from "./hit-test-strategy";
2
2
  export type { CutoutBounds } from "./hit-test-strategy";
3
- export type { CutoutDefinition, ImageCutoutDefinition, BoundingBoxCutoutDefinition, PolygonCutoutDefinition, HitTestStrategy, } from "./hit-test-strategy";
3
+ export type { CutoutDefinition, ImageCutoutDefinition, BoundingBoxCutoutDefinition, PolygonCutoutDefinition, CircleCutoutDefinition, HitTestStrategy, } from "./hit-test-strategy";
4
4
  /** @deprecated Use `ImageCutoutDefinition` instead */
5
5
  export interface CutoutImage {
6
6
  id: string;
@@ -19,6 +19,10 @@ export declare function useCutoutHitTest(definitions: CutoutDefinition[], enable
19
19
  hoveredId: string | null;
20
20
  selectedId: string | null;
21
21
  activeId: string | null;
22
+ viewportSize: {
23
+ width: number;
24
+ height: number;
25
+ };
22
26
  boundsMap: Record<string, CutoutBounds>;
23
27
  contourMap: Record<string, [number, number][]>;
24
28
  containerRef: import("react").RefObject<HTMLDivElement | null>;
@@ -9,9 +9,14 @@ export interface CutoutViewerContextValue {
9
9
  activeId: string | null;
10
10
  selectedId: string | null;
11
11
  hoveredId: string | null;
12
+ viewportSize: {
13
+ width: number;
14
+ height: number;
15
+ };
12
16
  effect: HoverEffect;
13
17
  enabled: boolean;
14
18
  showAll: boolean;
19
+ showOverlays: boolean;
15
20
  boundsMap: Record<string, CutoutBounds>;
16
21
  contourMap: Record<string, [number, number][]>;
17
22
  isAnyActive: boolean;