@yorab/react-paint 0.11.5 → 0.11.6
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 +382 -382
- package/dist/canvas/utils/string.d.ts +2 -0
- package/dist/react-paint.js +178 -178
- package/dist/react-paint.umd.cjs +1 -1
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,382 +1,382 @@
|
|
|
1
|
-
# React Paint V0.11.5
|
|
2
|
-
|
|
3
|
-
An open-source canvas-based library for React used for image annotation or as a digital whiteboard
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
## Quick start
|
|
9
|
-
|
|
10
|
-
```bash
|
|
11
|
-
npm i @yorab/react-paint
|
|
12
|
-
```
|
|
13
|
-
Note : react-paint needs <a href="https://www.npmjs.com/package/react">react</a> and <a href="https://www.npmjs.com/package/react-dom">react-dom</a> packages to work properly
|
|
14
|
-
|
|
15
|
-
## Usage
|
|
16
|
-
|
|
17
|
-
### Basic usage
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
import { Canvas, Editor, useReactPaint } from '@yorab/react-paint'
|
|
21
|
-
|
|
22
|
-
const BasicUsage = () => {
|
|
23
|
-
const { editorProps, canvasProps } = useReactPaint()
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<Editor editorProps={editorProps}>
|
|
27
|
-
<Canvas canvasProps={canvasProps} />
|
|
28
|
-
</Editor>
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Advanced usage
|
|
34
|
-
|
|
35
|
-
```tsx
|
|
36
|
-
import { useEffect, useState } from 'react'
|
|
37
|
-
import { Canvas, Editor, useReactPaint, type DrawableShape, type StateData } from '@yorab/react-paint'
|
|
38
|
-
|
|
39
|
-
const SHAPES_INIT = [
|
|
40
|
-
{
|
|
41
|
-
type: 'rect',
|
|
42
|
-
x: 300,
|
|
43
|
-
y: 300,
|
|
44
|
-
width: 100,
|
|
45
|
-
height: 200,
|
|
46
|
-
rotation: 0,
|
|
47
|
-
style: {
|
|
48
|
-
fillColor: '#6a4e01',
|
|
49
|
-
strokeColor: 'transparent',
|
|
50
|
-
opacity: 100,
|
|
51
|
-
lineWidth: 1,
|
|
52
|
-
lineDash: 0,
|
|
53
|
-
lineArrow: 0,
|
|
54
|
-
pointsCount: 3,
|
|
55
|
-
fontFamily: 'serif'
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
]
|
|
59
|
-
|
|
60
|
-
const AdvancedUsage = () => {
|
|
61
|
-
const [shapes, setShapes] = useState<DrawableShape[] | undefined>(SHAPES_INIT) // keep shapes state for internal use
|
|
62
|
-
|
|
63
|
-
const { editorProps, canvasProps, registerEvent, unregisterEvent } = useReactPaint({
|
|
64
|
-
width: 1920,
|
|
65
|
-
height: 1080,
|
|
66
|
-
shapes,
|
|
67
|
-
options: {
|
|
68
|
-
canGrow: true,
|
|
69
|
-
canShrink: true,
|
|
70
|
-
brushAlgo: 'quadratic',
|
|
71
|
-
clearCallback: 'defaultShapes'
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
const editorOptions = {
|
|
76
|
-
fontColor: '#2c1e69',
|
|
77
|
-
fontHoverColor: '#0d0931'
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const canvasOptions = {
|
|
81
|
-
canvasBackgroundColor: '#ffffff'
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
useEffect(() => {
|
|
85
|
-
const onDataChanged = (data: StateData, source: 'user' | 'remote') => {
|
|
86
|
-
setShapes(data.shapes)
|
|
87
|
-
}
|
|
88
|
-
registerEvent('dataChanged', onDataChanged)
|
|
89
|
-
|
|
90
|
-
return () => {
|
|
91
|
-
unregisterEvent('dataChanged', onDataChanged)
|
|
92
|
-
}
|
|
93
|
-
}, [registerEvent, unregisterEvent])
|
|
94
|
-
|
|
95
|
-
return (
|
|
96
|
-
<Editor editorProps={editorProps} options={editorOptions}>
|
|
97
|
-
<Canvas canvasProps={canvasProps} options={canvasOptions} />
|
|
98
|
-
</Editor>
|
|
99
|
-
)
|
|
100
|
-
}
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### Other usages
|
|
104
|
-
|
|
105
|
-
you will find other usages in <a href="https://github.com/YoRab/react-paint-editor/tree/main/stories">stories</a>
|
|
106
|
-
|
|
107
|
-
### API
|
|
108
|
-
|
|
109
|
-
#### `useReactPaint`
|
|
110
|
-
|
|
111
|
-
##### Parameters
|
|
112
|
-
|
|
113
|
-
| Parameter | Type | Description | Default value |
|
|
114
|
-
| :--- | :--- | :--- | :--- |
|
|
115
|
-
| `width` | `number` | *Optional*. Canvas width in px | `1000` |
|
|
116
|
-
| `height` | `number` | *Optional*. Canvas height in px | `600` |
|
|
117
|
-
| `shapes` | [`DrawableShape[]`](####drawableshape) | *Optional*. Array of shapes used to init the canvas | `undefined` |
|
|
118
|
-
| `mode` | `editor\|viewer` | *Optional*. `editor` lets you interact with the canvas and editor. `viewer` hides the editor and makes the canvas read-only| `editor` |
|
|
119
|
-
| `disabled` | `boolean` | *Optional*. Global parameter used to prevent interaction with every element in the editor or canvas | `false` |
|
|
120
|
-
| `options` | [`OptionalOptions`](####optionaloptions) | *Optional*. Set of options to customize available features | See [`OptionalOptions`]((####optionaloptions)) |
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
##### Returns
|
|
124
|
-
|
|
125
|
-
| Parameter | Type | Description |
|
|
126
|
-
| :--- | :--- | :--- |
|
|
127
|
-
| `canvasProps` | `object` | Set of properties to forward to Canvas component |
|
|
128
|
-
| `editorProps` | `object` | Set of properties to forward to Editor component |
|
|
129
|
-
| `registerEvent` | `(event: "dataChanged", listener: (data: StateData, source: 'user' \| 'remote') => void) => void` | registerEvent is used to register a listener for special events triggered by react-paint. The only currently available event is `dataChanged`. `source` indicates the origin of the state update |
|
|
130
|
-
| `unregisterEvent` | `(event: "dataChanged", listener?: ((data: StateData, source: 'user' \| 'remote') => void) \| undefined) => void` | unregisterEvent is used to unregister a listener previously registered. Omitting listener will result in unregistering every listeners for the the given event |
|
|
131
|
-
| `resetCanvas` | `(shapes: DrawableShape[], clearHistory?: boolean) => Promise<void>` | reset canvas with the given shapes. use `[]` or `undefined` to clear the canvas. Set `false` to `clearHistory` to prevent history stack to be cleared |
|
|
132
|
-
| `getCurrentImage` | `() => string \| undefined` | Returns a data URL containing the content of the current canvas as a PNG image, or undefined if an error occured |
|
|
133
|
-
| `getCurrentData` | `() => StateData` | Returns the current state of the canvas |
|
|
134
|
-
|
|
135
|
-
#### `OptionalOptions`
|
|
136
|
-
|
|
137
|
-
| Parameter | Type | Description | Default value |
|
|
138
|
-
| :--- | :--- | :--- | :--- |
|
|
139
|
-
| `layersManipulation` | `boolean` | *Optional*. Show panel to manipulate layers | `true` |
|
|
140
|
-
| `grid` | `number` | *Optional*. Size in px for grid cells. Set to 0 to hide grid | `0` |
|
|
141
|
-
| `canGrow` | `boolean` | *Optional*. Allow canvas to upscale and grow to fit container | `false` |
|
|
142
|
-
| `canShrink` | `boolean` | *Optional*. Allow canvas to downscale and shrink to fit container | `true` |
|
|
143
|
-
| `withExport` | `boolean` | *Optional*. Show button to manually export to PNG | `true` |
|
|
144
|
-
| `withLoadAndSave` | `boolean` | *Optional*.Show button to manually save or load data | `true` |
|
|
145
|
-
| `withUploadPicture` | `boolean` | *Optional*. Show button to add picture shape stored in base64 | `true` |
|
|
146
|
-
| `withUrlPicture` | `boolean` | *Optional*.Show button to add picture shape with only the url stored. Need connectivity to be displayed | `false` |
|
|
147
|
-
| `withFrameSelection` | `boolean` | *Optional*.Enable frame selection. **Currently, does not support multi shapes selection** | `false` |
|
|
148
|
-
| `withSkeleton` | `boolean` | *Optional*. Display skeleton when hovering shape | `false` |
|
|
149
|
-
| `clearCallback` | `'empty' \| 'defaultShapes' \| (() => DrawableShape[])` | *Optional*. Set clear button behavior. `empty` clear all shapes, `defaultShapes` uses shapes given in props. It is also possible to set a function returning an array of shapes | `empty` |
|
|
150
|
-
| `brushAlgo` | `'simple' \| 'quadratic'` | *Optional*. Choose which algorithm to display brush shape. `simple` displays path as is, `quadratic` uses quadratic bézier curves | `simple` |
|
|
151
|
-
| `isBrushShapeDoneOnMouseUp` | `boolean` | *Optional*. Choose whether drawing brush shape after releasing mouse should create a new shape or not | `true` |
|
|
152
|
-
| `canvasSelectionPadding` | `boolean` | *Optional*. Padding between shape and selection frame | `0` |
|
|
153
|
-
| `availableTools` | `CustomToolInput[]` | *Optional*. List of available tools. See CustomTool for more details | `` |
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
#### Canvas
|
|
159
|
-
|
|
160
|
-
##### Parameters
|
|
161
|
-
|
|
162
|
-
| Parameter | Type | Description | Default value |
|
|
163
|
-
| :--- | :--- | :--- | :--- |
|
|
164
|
-
| `canvasProps` | `object` | **Required**. Set of properties coming from useReactPaint | |
|
|
165
|
-
| `className` | `string` | *Optional*. Classname for canvas parent node | `undefined` |
|
|
166
|
-
| `style` | `CSSProperties` | *Optional*. css properties to inject to canvas parent node | `undefined` |
|
|
167
|
-
| `options` | `{canvasBackgroundColor?: string; canvasSelectionColor?: string; canvasSelectionWidth?: number}` | *Optional*. Set of properties to use to customize canvas style | `{canvasBackgroundColor: 'white'; canvasSelectionColor: 'blue'; canvasSelectionWidth: 2}` |
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
#### Editor
|
|
171
|
-
|
|
172
|
-
##### Parameters
|
|
173
|
-
|
|
174
|
-
| Parameter | Type | Description | Default value |
|
|
175
|
-
| :--- | :--- | :--- | :--- |
|
|
176
|
-
| `editorProps` | `object` | **Required**. Set of properties coming from useReactPaint | |
|
|
177
|
-
| `children` | `ReactNode` | **Required**. Need Canvas component to work properly | |
|
|
178
|
-
| `className` | `string` | *Optional*. Classname for canvas parent node | `undefined` |
|
|
179
|
-
| `style` | `CSSProperties` | *Optional*. css properties to inject to canvas parent node | `undefined` |
|
|
180
|
-
| `options` | `{ toolbarBackgroundColor?: string; dividerColor?: string; fontRadius?: number; fontDisabledColor?: string; fontDisabledBackgroundColor?: string; fontColor?: string; fontBackgroundColor?: string; fontSelectedColor?: string; fontSelectedBackgroundColor?: string; fontHoverColor?: string; fontHoverBackgroundColor?: string; }` | *Optional*. Set of properties to use to customize editor style | ` { toolbarBackgroundColor: 'white', dividerColor: '#36418129', fontRadius: 8, fontDisabledColor: '#3641812b', fontDisabledBackgroundColor: 'transparent', fontColor: '#364181', fontBackgroundColor: 'transparent', fontSelectedColor: 'white', fontSelectedBackgroundColor: '#364181', fontHoverColor: '#364181', fontHoverBackgroundColor: '#afd8d8'}` |
|
|
181
|
-
|
|
182
|
-
### Types
|
|
183
|
-
|
|
184
|
-
#### `DrawableShape`
|
|
185
|
-
#### `StateData`
|
|
186
|
-
|
|
187
|
-
#### `CustomTool`
|
|
188
|
-
|
|
189
|
-
```
|
|
190
|
-
type CustomTool = {
|
|
191
|
-
id: string
|
|
192
|
-
icon: string
|
|
193
|
-
label: string
|
|
194
|
-
} & (
|
|
195
|
-
| {
|
|
196
|
-
type: 'brush'
|
|
197
|
-
settings: ToolsSettingsType<'brush'>
|
|
198
|
-
}
|
|
199
|
-
| {
|
|
200
|
-
type: 'circle'
|
|
201
|
-
settings: ToolsSettingsType<'circle'>
|
|
202
|
-
}
|
|
203
|
-
| {
|
|
204
|
-
type: 'ellipse'
|
|
205
|
-
settings: ToolsSettingsType<'ellipse'>
|
|
206
|
-
}
|
|
207
|
-
| {
|
|
208
|
-
type: 'rect'
|
|
209
|
-
settings: ToolsSettingsType<'rect'>
|
|
210
|
-
}
|
|
211
|
-
| {
|
|
212
|
-
type: 'square'
|
|
213
|
-
settings: ToolsSettingsType<'square'>
|
|
214
|
-
}
|
|
215
|
-
| {
|
|
216
|
-
type: 'line'
|
|
217
|
-
settings: ToolsSettingsType<'line'>
|
|
218
|
-
}
|
|
219
|
-
| {
|
|
220
|
-
type: 'polygon'
|
|
221
|
-
settings: ToolsSettingsType<'polygon'>
|
|
222
|
-
}
|
|
223
|
-
| {
|
|
224
|
-
type: 'curve'
|
|
225
|
-
settings: ToolsSettingsType<'curve'>
|
|
226
|
-
}
|
|
227
|
-
| {
|
|
228
|
-
type: 'text'
|
|
229
|
-
settings: ToolsSettingsType<'text'>
|
|
230
|
-
}
|
|
231
|
-
| {
|
|
232
|
-
type: 'picture'
|
|
233
|
-
settings: ToolsSettingsType<'picture'>
|
|
234
|
-
}
|
|
235
|
-
)
|
|
236
|
-
|
|
237
|
-
type SettingsOpacity = {
|
|
238
|
-
opacity: {
|
|
239
|
-
min: number
|
|
240
|
-
max: number
|
|
241
|
-
step: number
|
|
242
|
-
default: number
|
|
243
|
-
hidden?: boolean
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
type SettingsStrokeColor = {
|
|
248
|
-
strokeColor: {
|
|
249
|
-
values: string[]
|
|
250
|
-
default: string
|
|
251
|
-
hidden?: boolean
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
type SettingsFillColor = {
|
|
256
|
-
fillColor: {
|
|
257
|
-
values: string[]
|
|
258
|
-
default: string
|
|
259
|
-
hidden?: boolean
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
type SettingsLineWidth = {
|
|
264
|
-
lineWidth: {
|
|
265
|
-
min: number
|
|
266
|
-
max: number
|
|
267
|
-
step: number
|
|
268
|
-
default: number
|
|
269
|
-
hidden?: boolean
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
type SettingsLineDash = {
|
|
274
|
-
lineDash: {
|
|
275
|
-
values: number[]
|
|
276
|
-
default: number
|
|
277
|
-
hidden?: boolean
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
type SettingsLineArrow = {
|
|
282
|
-
lineArrow: {
|
|
283
|
-
values: number[]
|
|
284
|
-
default: number
|
|
285
|
-
hidden?: boolean
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
type SettingsFont = {
|
|
290
|
-
fontFamily: {
|
|
291
|
-
values: string[]
|
|
292
|
-
default: string
|
|
293
|
-
hidden?: boolean
|
|
294
|
-
}
|
|
295
|
-
fontBold: {
|
|
296
|
-
values: boolean[]
|
|
297
|
-
default: boolean
|
|
298
|
-
hidden?: boolean
|
|
299
|
-
}
|
|
300
|
-
fontItalic: {
|
|
301
|
-
values: boolean[]
|
|
302
|
-
default: boolean
|
|
303
|
-
hidden?: boolean
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
type SettingsPointsCount = {
|
|
308
|
-
pointsCount: {
|
|
309
|
-
min: number
|
|
310
|
-
max: number
|
|
311
|
-
step: number
|
|
312
|
-
default: number
|
|
313
|
-
hidden?: boolean
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
type ToolsRectSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
318
|
-
|
|
319
|
-
type ToolsSquareSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
320
|
-
|
|
321
|
-
type ToolsCircleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
322
|
-
|
|
323
|
-
type ToolsEllipseSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
324
|
-
|
|
325
|
-
type ToolsTextSettings = SettingsOpacity & SettingsStrokeColor & SettingsFont
|
|
326
|
-
|
|
327
|
-
type ToolsLineSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash & SettingsLineArrow
|
|
328
|
-
|
|
329
|
-
type ToolsBrushSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash
|
|
330
|
-
|
|
331
|
-
type ToolsPolygonSettings = SettingsOpacity &
|
|
332
|
-
SettingsStrokeColor &
|
|
333
|
-
SettingsFillColor &
|
|
334
|
-
SettingsLineWidth &
|
|
335
|
-
SettingsLineDash &
|
|
336
|
-
SettingsPointsCount
|
|
337
|
-
|
|
338
|
-
type ToolsCurveSettings = SettingsOpacity &
|
|
339
|
-
SettingsStrokeColor &
|
|
340
|
-
SettingsFillColor &
|
|
341
|
-
SettingsLineWidth &
|
|
342
|
-
SettingsLineDash &
|
|
343
|
-
SettingsPointsCount
|
|
344
|
-
|
|
345
|
-
type ToolsTriangleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
346
|
-
|
|
347
|
-
type ToolsPictureSettings = SettingsOpacity
|
|
348
|
-
|
|
349
|
-
type ToolsSettingsType<T extends ShapeType> = T extends 'rect'
|
|
350
|
-
? ToolsRectSettings
|
|
351
|
-
: T extends 'square'
|
|
352
|
-
? ToolsSquareSettings
|
|
353
|
-
: T extends 'circle'
|
|
354
|
-
? ToolsCircleSettings
|
|
355
|
-
: T extends 'ellipse'
|
|
356
|
-
? ToolsEllipseSettings
|
|
357
|
-
: T extends 'text'
|
|
358
|
-
? ToolsTextSettings
|
|
359
|
-
: T extends 'line'
|
|
360
|
-
? ToolsLineSettings
|
|
361
|
-
: T extends 'brush'
|
|
362
|
-
? ToolsBrushSettings
|
|
363
|
-
: T extends 'polygon'
|
|
364
|
-
? ToolsPolygonSettings
|
|
365
|
-
: T extends 'curve'
|
|
366
|
-
? ToolsCurveSettings
|
|
367
|
-
: T extends 'triangle'
|
|
368
|
-
? ToolsTriangleSettings
|
|
369
|
-
: T extends 'picture'
|
|
370
|
-
? ToolsPictureSettings
|
|
371
|
-
: never
|
|
372
|
-
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
## Changelog
|
|
376
|
-
https://github.com/YoRab/react-paint-editor/blob/main/Changelog.md
|
|
377
|
-
|
|
378
|
-
## License
|
|
379
|
-
<a href="https://github.com/YoRab/react-paint-editor/blob/main/LICENSE.txt">MIT</a>
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
## Contributing
|
|
1
|
+
# React Paint V0.11.5
|
|
2
|
+
|
|
3
|
+
An open-source canvas-based library for React used for image annotation or as a digital whiteboard
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Quick start
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i @yorab/react-paint
|
|
12
|
+
```
|
|
13
|
+
Note : react-paint needs <a href="https://www.npmjs.com/package/react">react</a> and <a href="https://www.npmjs.com/package/react-dom">react-dom</a> packages to work properly
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { Canvas, Editor, useReactPaint } from '@yorab/react-paint'
|
|
21
|
+
|
|
22
|
+
const BasicUsage = () => {
|
|
23
|
+
const { editorProps, canvasProps } = useReactPaint()
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Editor editorProps={editorProps}>
|
|
27
|
+
<Canvas canvasProps={canvasProps} />
|
|
28
|
+
</Editor>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Advanced usage
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { useEffect, useState } from 'react'
|
|
37
|
+
import { Canvas, Editor, useReactPaint, type DrawableShape, type StateData } from '@yorab/react-paint'
|
|
38
|
+
|
|
39
|
+
const SHAPES_INIT = [
|
|
40
|
+
{
|
|
41
|
+
type: 'rect',
|
|
42
|
+
x: 300,
|
|
43
|
+
y: 300,
|
|
44
|
+
width: 100,
|
|
45
|
+
height: 200,
|
|
46
|
+
rotation: 0,
|
|
47
|
+
style: {
|
|
48
|
+
fillColor: '#6a4e01',
|
|
49
|
+
strokeColor: 'transparent',
|
|
50
|
+
opacity: 100,
|
|
51
|
+
lineWidth: 1,
|
|
52
|
+
lineDash: 0,
|
|
53
|
+
lineArrow: 0,
|
|
54
|
+
pointsCount: 3,
|
|
55
|
+
fontFamily: 'serif'
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
const AdvancedUsage = () => {
|
|
61
|
+
const [shapes, setShapes] = useState<DrawableShape[] | undefined>(SHAPES_INIT) // keep shapes state for internal use
|
|
62
|
+
|
|
63
|
+
const { editorProps, canvasProps, registerEvent, unregisterEvent } = useReactPaint({
|
|
64
|
+
width: 1920,
|
|
65
|
+
height: 1080,
|
|
66
|
+
shapes,
|
|
67
|
+
options: {
|
|
68
|
+
canGrow: true,
|
|
69
|
+
canShrink: true,
|
|
70
|
+
brushAlgo: 'quadratic',
|
|
71
|
+
clearCallback: 'defaultShapes'
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const editorOptions = {
|
|
76
|
+
fontColor: '#2c1e69',
|
|
77
|
+
fontHoverColor: '#0d0931'
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const canvasOptions = {
|
|
81
|
+
canvasBackgroundColor: '#ffffff'
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const onDataChanged = (data: StateData, source: 'user' | 'remote') => {
|
|
86
|
+
setShapes(data.shapes)
|
|
87
|
+
}
|
|
88
|
+
registerEvent('dataChanged', onDataChanged)
|
|
89
|
+
|
|
90
|
+
return () => {
|
|
91
|
+
unregisterEvent('dataChanged', onDataChanged)
|
|
92
|
+
}
|
|
93
|
+
}, [registerEvent, unregisterEvent])
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<Editor editorProps={editorProps} options={editorOptions}>
|
|
97
|
+
<Canvas canvasProps={canvasProps} options={canvasOptions} />
|
|
98
|
+
</Editor>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Other usages
|
|
104
|
+
|
|
105
|
+
you will find other usages in <a href="https://github.com/YoRab/react-paint-editor/tree/main/stories">stories</a>
|
|
106
|
+
|
|
107
|
+
### API
|
|
108
|
+
|
|
109
|
+
#### `useReactPaint`
|
|
110
|
+
|
|
111
|
+
##### Parameters
|
|
112
|
+
|
|
113
|
+
| Parameter | Type | Description | Default value |
|
|
114
|
+
| :--- | :--- | :--- | :--- |
|
|
115
|
+
| `width` | `number` | *Optional*. Canvas width in px | `1000` |
|
|
116
|
+
| `height` | `number` | *Optional*. Canvas height in px | `600` |
|
|
117
|
+
| `shapes` | [`DrawableShape[]`](####drawableshape) | *Optional*. Array of shapes used to init the canvas | `undefined` |
|
|
118
|
+
| `mode` | `editor\|viewer` | *Optional*. `editor` lets you interact with the canvas and editor. `viewer` hides the editor and makes the canvas read-only| `editor` |
|
|
119
|
+
| `disabled` | `boolean` | *Optional*. Global parameter used to prevent interaction with every element in the editor or canvas | `false` |
|
|
120
|
+
| `options` | [`OptionalOptions`](####optionaloptions) | *Optional*. Set of options to customize available features | See [`OptionalOptions`]((####optionaloptions)) |
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
##### Returns
|
|
124
|
+
|
|
125
|
+
| Parameter | Type | Description |
|
|
126
|
+
| :--- | :--- | :--- |
|
|
127
|
+
| `canvasProps` | `object` | Set of properties to forward to Canvas component |
|
|
128
|
+
| `editorProps` | `object` | Set of properties to forward to Editor component |
|
|
129
|
+
| `registerEvent` | `(event: "dataChanged", listener: (data: StateData, source: 'user' \| 'remote') => void) => void` | registerEvent is used to register a listener for special events triggered by react-paint. The only currently available event is `dataChanged`. `source` indicates the origin of the state update |
|
|
130
|
+
| `unregisterEvent` | `(event: "dataChanged", listener?: ((data: StateData, source: 'user' \| 'remote') => void) \| undefined) => void` | unregisterEvent is used to unregister a listener previously registered. Omitting listener will result in unregistering every listeners for the the given event |
|
|
131
|
+
| `resetCanvas` | `(shapes: DrawableShape[], clearHistory?: boolean) => Promise<void>` | reset canvas with the given shapes. use `[]` or `undefined` to clear the canvas. Set `false` to `clearHistory` to prevent history stack to be cleared |
|
|
132
|
+
| `getCurrentImage` | `() => string \| undefined` | Returns a data URL containing the content of the current canvas as a PNG image, or undefined if an error occured |
|
|
133
|
+
| `getCurrentData` | `() => StateData` | Returns the current state of the canvas |
|
|
134
|
+
|
|
135
|
+
#### `OptionalOptions`
|
|
136
|
+
|
|
137
|
+
| Parameter | Type | Description | Default value |
|
|
138
|
+
| :--- | :--- | :--- | :--- |
|
|
139
|
+
| `layersManipulation` | `boolean` | *Optional*. Show panel to manipulate layers | `true` |
|
|
140
|
+
| `grid` | `number` | *Optional*. Size in px for grid cells. Set to 0 to hide grid | `0` |
|
|
141
|
+
| `canGrow` | `boolean` | *Optional*. Allow canvas to upscale and grow to fit container | `false` |
|
|
142
|
+
| `canShrink` | `boolean` | *Optional*. Allow canvas to downscale and shrink to fit container | `true` |
|
|
143
|
+
| `withExport` | `boolean` | *Optional*. Show button to manually export to PNG | `true` |
|
|
144
|
+
| `withLoadAndSave` | `boolean` | *Optional*.Show button to manually save or load data | `true` |
|
|
145
|
+
| `withUploadPicture` | `boolean` | *Optional*. Show button to add picture shape stored in base64 | `true` |
|
|
146
|
+
| `withUrlPicture` | `boolean` | *Optional*.Show button to add picture shape with only the url stored. Need connectivity to be displayed | `false` |
|
|
147
|
+
| `withFrameSelection` | `boolean` | *Optional*.Enable frame selection. **Currently, does not support multi shapes selection** | `false` |
|
|
148
|
+
| `withSkeleton` | `boolean` | *Optional*. Display skeleton when hovering shape | `false` |
|
|
149
|
+
| `clearCallback` | `'empty' \| 'defaultShapes' \| (() => DrawableShape[])` | *Optional*. Set clear button behavior. `empty` clear all shapes, `defaultShapes` uses shapes given in props. It is also possible to set a function returning an array of shapes | `empty` |
|
|
150
|
+
| `brushAlgo` | `'simple' \| 'quadratic'` | *Optional*. Choose which algorithm to display brush shape. `simple` displays path as is, `quadratic` uses quadratic bézier curves | `simple` |
|
|
151
|
+
| `isBrushShapeDoneOnMouseUp` | `boolean` | *Optional*. Choose whether drawing brush shape after releasing mouse should create a new shape or not | `true` |
|
|
152
|
+
| `canvasSelectionPadding` | `boolean` | *Optional*. Padding between shape and selection frame | `0` |
|
|
153
|
+
| `availableTools` | `CustomToolInput[]` | *Optional*. List of available tools. See CustomTool for more details | `` |
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
#### Canvas
|
|
159
|
+
|
|
160
|
+
##### Parameters
|
|
161
|
+
|
|
162
|
+
| Parameter | Type | Description | Default value |
|
|
163
|
+
| :--- | :--- | :--- | :--- |
|
|
164
|
+
| `canvasProps` | `object` | **Required**. Set of properties coming from useReactPaint | |
|
|
165
|
+
| `className` | `string` | *Optional*. Classname for canvas parent node | `undefined` |
|
|
166
|
+
| `style` | `CSSProperties` | *Optional*. css properties to inject to canvas parent node | `undefined` |
|
|
167
|
+
| `options` | `{canvasBackgroundColor?: string; canvasSelectionColor?: string; canvasSelectionWidth?: number}` | *Optional*. Set of properties to use to customize canvas style | `{canvasBackgroundColor: 'white'; canvasSelectionColor: 'blue'; canvasSelectionWidth: 2}` |
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
#### Editor
|
|
171
|
+
|
|
172
|
+
##### Parameters
|
|
173
|
+
|
|
174
|
+
| Parameter | Type | Description | Default value |
|
|
175
|
+
| :--- | :--- | :--- | :--- |
|
|
176
|
+
| `editorProps` | `object` | **Required**. Set of properties coming from useReactPaint | |
|
|
177
|
+
| `children` | `ReactNode` | **Required**. Need Canvas component to work properly | |
|
|
178
|
+
| `className` | `string` | *Optional*. Classname for canvas parent node | `undefined` |
|
|
179
|
+
| `style` | `CSSProperties` | *Optional*. css properties to inject to canvas parent node | `undefined` |
|
|
180
|
+
| `options` | `{ toolbarBackgroundColor?: string; dividerColor?: string; fontRadius?: number; fontDisabledColor?: string; fontDisabledBackgroundColor?: string; fontColor?: string; fontBackgroundColor?: string; fontSelectedColor?: string; fontSelectedBackgroundColor?: string; fontHoverColor?: string; fontHoverBackgroundColor?: string; }` | *Optional*. Set of properties to use to customize editor style | ` { toolbarBackgroundColor: 'white', dividerColor: '#36418129', fontRadius: 8, fontDisabledColor: '#3641812b', fontDisabledBackgroundColor: 'transparent', fontColor: '#364181', fontBackgroundColor: 'transparent', fontSelectedColor: 'white', fontSelectedBackgroundColor: '#364181', fontHoverColor: '#364181', fontHoverBackgroundColor: '#afd8d8'}` |
|
|
181
|
+
|
|
182
|
+
### Types
|
|
183
|
+
|
|
184
|
+
#### `DrawableShape`
|
|
185
|
+
#### `StateData`
|
|
186
|
+
|
|
187
|
+
#### `CustomTool`
|
|
188
|
+
|
|
189
|
+
```
|
|
190
|
+
type CustomTool = {
|
|
191
|
+
id: string
|
|
192
|
+
icon: string
|
|
193
|
+
label: string
|
|
194
|
+
} & (
|
|
195
|
+
| {
|
|
196
|
+
type: 'brush'
|
|
197
|
+
settings: ToolsSettingsType<'brush'>
|
|
198
|
+
}
|
|
199
|
+
| {
|
|
200
|
+
type: 'circle'
|
|
201
|
+
settings: ToolsSettingsType<'circle'>
|
|
202
|
+
}
|
|
203
|
+
| {
|
|
204
|
+
type: 'ellipse'
|
|
205
|
+
settings: ToolsSettingsType<'ellipse'>
|
|
206
|
+
}
|
|
207
|
+
| {
|
|
208
|
+
type: 'rect'
|
|
209
|
+
settings: ToolsSettingsType<'rect'>
|
|
210
|
+
}
|
|
211
|
+
| {
|
|
212
|
+
type: 'square'
|
|
213
|
+
settings: ToolsSettingsType<'square'>
|
|
214
|
+
}
|
|
215
|
+
| {
|
|
216
|
+
type: 'line'
|
|
217
|
+
settings: ToolsSettingsType<'line'>
|
|
218
|
+
}
|
|
219
|
+
| {
|
|
220
|
+
type: 'polygon'
|
|
221
|
+
settings: ToolsSettingsType<'polygon'>
|
|
222
|
+
}
|
|
223
|
+
| {
|
|
224
|
+
type: 'curve'
|
|
225
|
+
settings: ToolsSettingsType<'curve'>
|
|
226
|
+
}
|
|
227
|
+
| {
|
|
228
|
+
type: 'text'
|
|
229
|
+
settings: ToolsSettingsType<'text'>
|
|
230
|
+
}
|
|
231
|
+
| {
|
|
232
|
+
type: 'picture'
|
|
233
|
+
settings: ToolsSettingsType<'picture'>
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
type SettingsOpacity = {
|
|
238
|
+
opacity: {
|
|
239
|
+
min: number
|
|
240
|
+
max: number
|
|
241
|
+
step: number
|
|
242
|
+
default: number
|
|
243
|
+
hidden?: boolean
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
type SettingsStrokeColor = {
|
|
248
|
+
strokeColor: {
|
|
249
|
+
values: string[]
|
|
250
|
+
default: string
|
|
251
|
+
hidden?: boolean
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
type SettingsFillColor = {
|
|
256
|
+
fillColor: {
|
|
257
|
+
values: string[]
|
|
258
|
+
default: string
|
|
259
|
+
hidden?: boolean
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
type SettingsLineWidth = {
|
|
264
|
+
lineWidth: {
|
|
265
|
+
min: number
|
|
266
|
+
max: number
|
|
267
|
+
step: number
|
|
268
|
+
default: number
|
|
269
|
+
hidden?: boolean
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
type SettingsLineDash = {
|
|
274
|
+
lineDash: {
|
|
275
|
+
values: number[]
|
|
276
|
+
default: number
|
|
277
|
+
hidden?: boolean
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
type SettingsLineArrow = {
|
|
282
|
+
lineArrow: {
|
|
283
|
+
values: number[]
|
|
284
|
+
default: number
|
|
285
|
+
hidden?: boolean
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
type SettingsFont = {
|
|
290
|
+
fontFamily: {
|
|
291
|
+
values: string[]
|
|
292
|
+
default: string
|
|
293
|
+
hidden?: boolean
|
|
294
|
+
}
|
|
295
|
+
fontBold: {
|
|
296
|
+
values: boolean[]
|
|
297
|
+
default: boolean
|
|
298
|
+
hidden?: boolean
|
|
299
|
+
}
|
|
300
|
+
fontItalic: {
|
|
301
|
+
values: boolean[]
|
|
302
|
+
default: boolean
|
|
303
|
+
hidden?: boolean
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
type SettingsPointsCount = {
|
|
308
|
+
pointsCount: {
|
|
309
|
+
min: number
|
|
310
|
+
max: number
|
|
311
|
+
step: number
|
|
312
|
+
default: number
|
|
313
|
+
hidden?: boolean
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
type ToolsRectSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
318
|
+
|
|
319
|
+
type ToolsSquareSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
320
|
+
|
|
321
|
+
type ToolsCircleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
322
|
+
|
|
323
|
+
type ToolsEllipseSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
324
|
+
|
|
325
|
+
type ToolsTextSettings = SettingsOpacity & SettingsStrokeColor & SettingsFont
|
|
326
|
+
|
|
327
|
+
type ToolsLineSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash & SettingsLineArrow
|
|
328
|
+
|
|
329
|
+
type ToolsBrushSettings = SettingsOpacity & SettingsStrokeColor & SettingsLineWidth & SettingsLineDash
|
|
330
|
+
|
|
331
|
+
type ToolsPolygonSettings = SettingsOpacity &
|
|
332
|
+
SettingsStrokeColor &
|
|
333
|
+
SettingsFillColor &
|
|
334
|
+
SettingsLineWidth &
|
|
335
|
+
SettingsLineDash &
|
|
336
|
+
SettingsPointsCount
|
|
337
|
+
|
|
338
|
+
type ToolsCurveSettings = SettingsOpacity &
|
|
339
|
+
SettingsStrokeColor &
|
|
340
|
+
SettingsFillColor &
|
|
341
|
+
SettingsLineWidth &
|
|
342
|
+
SettingsLineDash &
|
|
343
|
+
SettingsPointsCount
|
|
344
|
+
|
|
345
|
+
type ToolsTriangleSettings = SettingsStrokeColor & SettingsFillColor & SettingsOpacity & SettingsLineWidth & SettingsLineDash
|
|
346
|
+
|
|
347
|
+
type ToolsPictureSettings = SettingsOpacity
|
|
348
|
+
|
|
349
|
+
type ToolsSettingsType<T extends ShapeType> = T extends 'rect'
|
|
350
|
+
? ToolsRectSettings
|
|
351
|
+
: T extends 'square'
|
|
352
|
+
? ToolsSquareSettings
|
|
353
|
+
: T extends 'circle'
|
|
354
|
+
? ToolsCircleSettings
|
|
355
|
+
: T extends 'ellipse'
|
|
356
|
+
? ToolsEllipseSettings
|
|
357
|
+
: T extends 'text'
|
|
358
|
+
? ToolsTextSettings
|
|
359
|
+
: T extends 'line'
|
|
360
|
+
? ToolsLineSettings
|
|
361
|
+
: T extends 'brush'
|
|
362
|
+
? ToolsBrushSettings
|
|
363
|
+
: T extends 'polygon'
|
|
364
|
+
? ToolsPolygonSettings
|
|
365
|
+
: T extends 'curve'
|
|
366
|
+
? ToolsCurveSettings
|
|
367
|
+
: T extends 'triangle'
|
|
368
|
+
? ToolsTriangleSettings
|
|
369
|
+
: T extends 'picture'
|
|
370
|
+
? ToolsPictureSettings
|
|
371
|
+
: never
|
|
372
|
+
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## Changelog
|
|
376
|
+
https://github.com/YoRab/react-paint-editor/blob/main/Changelog.md
|
|
377
|
+
|
|
378
|
+
## License
|
|
379
|
+
<a href="https://github.com/YoRab/react-paint-editor/blob/main/LICENSE.txt">MIT</a>
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
## Contributing
|