react-lasso-select 1.2.2 → 2.0.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/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ISC License
2
2
 
3
- Copyright (c) 2021, akcyp
3
+ Copyright (c) 2021-2025, akcyp
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
6
 
package/README.md CHANGED
@@ -9,11 +9,7 @@ A responsive react tool for marking irregular areas in images (lasso / free sele
9
9
 
10
10
  ## Demos
11
11
 
12
- - [Simple demo](https://codesandbox.io/s/react-lasso-select-issue2-h92hn?file=/src/App.tsx)
13
-
14
- - [Demo with typescript](https://codesandbox.io/s/react-lasso-select-demo-using-typescript-and-react-hooks-kddyt)
15
-
16
- - [Advanced demo](https://codesandbox.io/s/react-lasso-select-advanced-demo-g0yn4?file=/src/App.tsx)
12
+ See `src/App.tsx`
17
13
 
18
14
  ## Features
19
15
 
@@ -50,7 +46,7 @@ import ReactLassoSelect from 'react-lasso-select';
50
46
  import { useState } from 'react';
51
47
  import ReactLassoSelect, { getCanvas } from 'react-lasso-select';
52
48
 
53
- export default function App () {
49
+ export default function App() {
54
50
  const src = './demo.jpg';
55
51
  const [points, setPoints] = useState([]);
56
52
  const [clippedImg, setClippedImg] = useState();
@@ -59,10 +55,10 @@ export default function App () {
59
55
  <ReactLassoSelect
60
56
  value={points}
61
57
  src={src}
62
- onChange={value => {
58
+ onChange={(value) => {
63
59
  setPoints(value);
64
60
  }}
65
- onComplete={value => {
61
+ onComplete={(value) => {
66
62
  if (!value.length) return;
67
63
  getCanvas(src, value, (err, canvas) => {
68
64
  if (!err) {
@@ -71,9 +67,7 @@ export default function App () {
71
67
  });
72
68
  }}
73
69
  />
74
- <div>
75
- Points: {points.map(({x, y}) => `${x},${y}`).join(' ')}
76
- </div>
70
+ <div>Points: {points.map(({ x, y }) => `${x},${y}`).join(' ')}</div>
77
71
  <div>
78
72
  <img src={clippedImg} alt="" />
79
73
  </div>
@@ -87,13 +81,14 @@ export default function App () {
87
81
  Most important props:
88
82
 
89
83
  - `src` (string) (required) Specifies the path to the image (or base64 string)
90
- - `value` (array of {x: number, y: number}) Specifies input value
84
+ - `value` (array of {x: number, y: number}) Specifies input value
91
85
  - `onComplete(path)` Callback fired every time path has been closed / updated / reset (use it for better performance insead of `onChange`)
92
86
  - `onChange(path)` Callback fired every time path has been changed (ex. point added/removed/replaced)
93
87
 
94
88
  Props related to component:
95
89
 
96
90
  - `disabled` (boolean, default false) Set to true to block selecting
91
+ - `disabledShapeChange` (boolean, default false) Set to true to block shape change, but preserve possibility to move whole selection
97
92
  - `style` (object) CSS style attributes for component container
98
93
  - `viewBox` ({width: number, height: number}) Viewbox attribute for svg element, avoid changing the default value.
99
94
 
@@ -0,0 +1,179 @@
1
+ import { Component } from 'react';
2
+ import { CSSProperties } from 'react';
3
+ import { default as default_2 } from 'prop-types';
4
+ import { JSX as JSX_2 } from 'react/jsx-runtime';
5
+ import { RefObject } from 'react';
6
+ import { SyntheticEvent } from 'react';
7
+
8
+ export declare function getCanvas(src: string, path: Point[], callback: (err: Error | null, canvas: HTMLCanvasElement) => void, crop?: boolean): void;
9
+
10
+ declare enum pathActions {
11
+ ADD = "ADD",
12
+ DELETE = "DELETE",
13
+ MODIFY = "MODIFY",
14
+ MOVE = "MOVE",
15
+ RESET = "RESET",
16
+ CHANGE = "CHANGE"
17
+ }
18
+
19
+ declare type pathReducerAction = {
20
+ type: pathActions.ADD;
21
+ payload: Point;
22
+ } | {
23
+ type: pathActions.DELETE;
24
+ payload: number;
25
+ } | {
26
+ type: pathActions.MODIFY;
27
+ payload: {
28
+ index: number;
29
+ } & Point;
30
+ } | {
31
+ type: pathActions.MOVE;
32
+ payload: Point;
33
+ } | {
34
+ type: pathActions.RESET;
35
+ } | {
36
+ type: pathActions.CHANGE;
37
+ payload: Point[];
38
+ };
39
+
40
+ declare interface PathState {
41
+ points: Point[];
42
+ closed: boolean;
43
+ }
44
+
45
+ declare interface Point {
46
+ x: number;
47
+ y: number;
48
+ }
49
+
50
+ export declare interface ReactLassoProps {
51
+ src: string;
52
+ value: Point[];
53
+ style: CSSProperties;
54
+ viewBox: Size;
55
+ disabled: boolean;
56
+ disabledShapeChange: boolean;
57
+ imageStyle: CSSProperties;
58
+ onImageLoad: (e: SyntheticEvent<HTMLImageElement, Event>) => void;
59
+ onImageError: (e: SyntheticEvent<HTMLImageElement, Event>) => void;
60
+ onChange?: (path: Point[]) => void;
61
+ onComplete?: (path: Point[]) => void;
62
+ imageAlt?: string;
63
+ crossOrigin?: 'anonymous' | 'use-credentials' | '';
64
+ }
65
+
66
+ export declare class ReactLassoSelect extends Component<ReactLassoProps, ReactLassoState> {
67
+ state: ReactLassoState;
68
+ imageRef: RefObject<HTMLImageElement>;
69
+ svgRef: RefObject<SVGSVGElement>;
70
+ svg: SVGHelper;
71
+ angles: number[];
72
+ path: PathState;
73
+ lastEmittedPoints: Point[];
74
+ lastUpdatedPoints: Point[];
75
+ imgError: boolean;
76
+ setPathFromPropsOnMediaLoad: boolean;
77
+ constructor(props: ReactLassoProps);
78
+ render(): JSX_2.Element;
79
+ componentDidUpdate(prevProps: ReactLassoProps): void;
80
+ convertPoints(points: Point[]): Point[];
81
+ checkIfPathUpdated(wasClosedBefore: boolean): void;
82
+ emitOnChange({ points }: PathState): void;
83
+ emitOnComplete(convertedPoints: Point[]): void;
84
+ setPointer({ x, y }: Point, force?: boolean): void;
85
+ hidePointer: () => void;
86
+ dispatchPathAction(action: pathReducerAction & {
87
+ pointer?: Point;
88
+ }): void;
89
+ isLoaded(): boolean;
90
+ getAspectRatio(): Point;
91
+ setPathStateFromProps(): void;
92
+ getRoundedPoints(): Point[];
93
+ getBorder(): Point[];
94
+ getPolygonPoints(): Point[];
95
+ getPolylinePoints(): Point[];
96
+ getMousePosition(e: touchOrMouseEvent<SVGSVGElement>, lookupForNearlyPoints?: boolean, lookupForApproximation?: boolean): [Point, {
97
+ point: Point;
98
+ index: number;
99
+ }];
100
+ onShapeDrag: ({ dx, dy }: Vector) => void;
101
+ onPointDrag: (idx: number, { dx, dy }: Vector) => void;
102
+ onPointClick: (idx: number) => void;
103
+ onDragEnd: () => void;
104
+ onMediaLoaded: (e: React.SyntheticEvent<HTMLImageElement, Event>) => void;
105
+ onMediaError: (e: React.SyntheticEvent<HTMLImageElement, Event>) => void;
106
+ onClickTouchEvent: (e: touchOrMouseEvent<SVGSVGElement>) => void;
107
+ onClick: (e: React.MouseEvent<SVGSVGElement, MouseEvent>) => void;
108
+ onTouchEnd: (e: React.TouchEvent<SVGSVGElement>) => void;
109
+ onMouseTouchMove: (e: touchOrMouseEvent<SVGSVGElement>) => void;
110
+ onContextMenu: (e: React.MouseEvent<SVGSVGElement, MouseEvent>) => void;
111
+ static propTypes: {
112
+ value: default_2.Requireable<(Required<default_2.InferProps<{
113
+ x: default_2.Validator<number>;
114
+ y: default_2.Validator<number>;
115
+ }>> | null | undefined)[]>;
116
+ style: default_2.Requireable<default_2.InferProps<{}>>;
117
+ viewBox: default_2.Requireable<Required<default_2.InferProps<{
118
+ width: default_2.Validator<number>;
119
+ height: default_2.Validator<number>;
120
+ }>>>;
121
+ disabled: default_2.Requireable<boolean>;
122
+ disabledShapeChange: default_2.Requireable<boolean>;
123
+ onChange: default_2.Requireable<(...args: any[]) => any>;
124
+ onComplete: default_2.Requireable<(...args: any[]) => any>;
125
+ src: default_2.Validator<string>;
126
+ imageAlt: default_2.Requireable<string>;
127
+ crossOrigin: default_2.Requireable<string>;
128
+ imageStyle: default_2.Requireable<default_2.InferProps<{}>>;
129
+ onImageLoad: default_2.Requireable<(...args: any[]) => any>;
130
+ onImageError: default_2.Requireable<(...args: any[]) => any>;
131
+ };
132
+ static defaultProps: {
133
+ value: never[];
134
+ style: {};
135
+ imageStyle: {};
136
+ viewBox: {
137
+ width: number;
138
+ height: number;
139
+ };
140
+ disabled: boolean;
141
+ disabledShapeChange: boolean;
142
+ onImageError: Function;
143
+ onImageLoad: Function;
144
+ };
145
+ }
146
+
147
+ export declare interface ReactLassoState {
148
+ path: PathState;
149
+ pointer: Point;
150
+ }
151
+
152
+ declare interface Size {
153
+ width: number;
154
+ height: number;
155
+ }
156
+
157
+ declare class SVGHelper {
158
+ getSvgElement: () => SVGSVGElement | null | undefined;
159
+ constructor(getSvgElement: () => SVGSVGElement | null | undefined);
160
+ getSvg(): SVGSVGElement;
161
+ getCTM(): DOMMatrix;
162
+ getViewboxSize(): DOMRect;
163
+ getRealSize(): Size;
164
+ getViewboxOffset(): Point;
165
+ convertViewboxPointsToReal(points: Point[]): Point[];
166
+ convertRealPointsToViewbox(points: Point[]): Point[];
167
+ getBorderPoints(repeatFirst?: boolean): Point[];
168
+ isAboveTheBorder({ x, y }: Point): boolean;
169
+ getMouseCoordinates(event: touchOrMouseEvent<SVGSVGElement>): Point;
170
+ }
171
+
172
+ declare type touchOrMouseEvent<T> = React.MouseEvent<T, MouseEvent> | React.TouchEvent<T>;
173
+
174
+ declare interface Vector {
175
+ dx: number;
176
+ dy: number;
177
+ }
178
+
179
+ export { }