@archireport/react-native-svg-draw 1.3.0 → 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/README.md +73 -27
- package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js +55 -54
- package/lib/commonjs/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
- package/lib/commonjs/components/DrawCore/CurrentAnimatedText.js +3 -14
- package/lib/commonjs/components/DrawCore/CurrentAnimatedText.js.map +1 -1
- package/lib/commonjs/components/DrawCore/DrawContext.js +18 -0
- package/lib/commonjs/components/DrawCore/DrawContext.js.map +1 -0
- package/lib/commonjs/components/DrawCore/DrawPad.js +1 -13
- package/lib/commonjs/components/DrawCore/DrawPad.js.map +1 -1
- package/lib/commonjs/components/DrawCore/DrawProvider.js +103 -0
- package/lib/commonjs/components/DrawCore/DrawProvider.js.map +1 -0
- package/lib/commonjs/components/DrawCore/Item.js +0 -15
- package/lib/commonjs/components/DrawCore/Item.js.map +1 -1
- package/lib/commonjs/components/DrawCore/index.js +47 -287
- package/lib/commonjs/components/DrawCore/index.js.map +1 -1
- package/lib/commonjs/components/DrawCore/useDrawHook.js +76 -0
- package/lib/commonjs/components/DrawCore/useDrawHook.js.map +1 -0
- package/lib/commonjs/components/DrawWithOptions/ArrowSvg.js +0 -6
- package/lib/commonjs/components/DrawWithOptions/ArrowSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/CancelSvg.js +0 -6
- package/lib/commonjs/components/DrawWithOptions/CancelSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/CircleSvg.js +0 -6
- package/lib/commonjs/components/DrawWithOptions/CircleSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/CloseSvg.js +0 -6
- package/lib/commonjs/components/DrawWithOptions/CloseSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/DoubleHeadSvg.js +0 -7
- package/lib/commonjs/components/DrawWithOptions/DoubleHeadSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/PenSvg.js +0 -6
- package/lib/commonjs/components/DrawWithOptions/PenSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/SendSvg.js +0 -7
- package/lib/commonjs/components/DrawWithOptions/SendSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/SliderSvg.js +31 -0
- package/lib/commonjs/components/DrawWithOptions/SliderSvg.js.map +1 -0
- package/lib/commonjs/components/DrawWithOptions/SquareSvg.js +0 -7
- package/lib/commonjs/components/DrawWithOptions/SquareSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/TextSvg.js +0 -7
- package/lib/commonjs/components/DrawWithOptions/TextSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/ThrashSvg.js +27 -10
- package/lib/commonjs/components/DrawWithOptions/ThrashSvg.js.map +1 -1
- package/lib/commonjs/components/DrawWithOptions/index.js +126 -93
- package/lib/commonjs/components/DrawWithOptions/index.js.map +1 -1
- package/lib/commonjs/components/{DrawCore → Slider}/ColorSlider.js +47 -78
- package/lib/commonjs/components/Slider/ColorSlider.js.map +1 -0
- package/lib/commonjs/components/Slider/Sliders.js +88 -0
- package/lib/commonjs/components/Slider/Sliders.js.map +1 -0
- package/lib/commonjs/components/{DrawCore → Slider}/StrokeSlider.js +38 -57
- package/lib/commonjs/components/Slider/StrokeSlider.js.map +1 -0
- package/lib/commonjs/components/Slider/sliderStyle.js +37 -0
- package/lib/commonjs/components/Slider/sliderStyle.js.map +1 -0
- package/lib/commonjs/index.js +7 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types.d.js.map +1 -1
- package/lib/module/components/DrawCore/CurrentAnimatedItem.js +56 -43
- package/lib/module/components/DrawCore/CurrentAnimatedItem.js.map +1 -1
- package/lib/module/components/DrawCore/CurrentAnimatedText.js +3 -7
- package/lib/module/components/DrawCore/CurrentAnimatedText.js.map +1 -1
- package/lib/module/components/DrawCore/DrawContext.js +11 -0
- package/lib/module/components/DrawCore/DrawContext.js.map +1 -0
- package/lib/module/components/DrawCore/DrawPad.js +1 -5
- package/lib/module/components/DrawCore/DrawPad.js.map +1 -1
- package/lib/module/components/DrawCore/DrawProvider.js +94 -0
- package/lib/module/components/DrawCore/DrawProvider.js.map +1 -0
- package/lib/module/components/DrawCore/Item.js +0 -8
- package/lib/module/components/DrawCore/Item.js.map +1 -1
- package/lib/module/components/DrawCore/index.js +49 -271
- package/lib/module/components/DrawCore/index.js.map +1 -1
- package/lib/module/components/DrawCore/useDrawHook.js +69 -0
- package/lib/module/components/DrawCore/useDrawHook.js.map +1 -0
- package/lib/module/components/DrawWithOptions/ArrowSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/ArrowSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/CancelSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/CancelSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/CircleSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/CircleSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/CloseSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/CloseSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/DoubleHeadSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/DoubleHeadSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/PenSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/PenSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/SendSvg.js +0 -3
- package/lib/module/components/DrawWithOptions/SendSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/SliderSvg.js +22 -0
- package/lib/module/components/DrawWithOptions/SliderSvg.js.map +1 -0
- package/lib/module/components/DrawWithOptions/SquareSvg.js +0 -1
- package/lib/module/components/DrawWithOptions/SquareSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/TextSvg.js +0 -3
- package/lib/module/components/DrawWithOptions/TextSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/ThrashSvg.js +27 -5
- package/lib/module/components/DrawWithOptions/ThrashSvg.js.map +1 -1
- package/lib/module/components/DrawWithOptions/index.js +126 -73
- package/lib/module/components/DrawWithOptions/index.js.map +1 -1
- package/lib/module/components/Slider/ColorSlider.js +95 -0
- package/lib/module/components/Slider/ColorSlider.js.map +1 -0
- package/lib/module/components/Slider/Sliders.js +79 -0
- package/lib/module/components/Slider/Sliders.js.map +1 -0
- package/lib/module/components/Slider/StrokeSlider.js +82 -0
- package/lib/module/components/Slider/StrokeSlider.js.map +1 -0
- package/lib/module/components/Slider/sliderStyle.js +29 -0
- package/lib/module/components/Slider/sliderStyle.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/types.d.js.map +1 -1
- package/lib/typescript/components/DrawCore/CurrentAnimatedItem.d.ts +1 -1
- package/lib/typescript/components/DrawCore/CurrentAnimatedItem.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/CurrentAnimatedText.d.ts +1 -1
- package/lib/typescript/components/DrawCore/CurrentAnimatedText.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/DrawContext.d.ts +14 -0
- package/lib/typescript/components/DrawCore/DrawContext.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/DrawPad.d.ts +1 -1
- package/lib/typescript/components/DrawCore/DrawPad.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/DrawProvider.d.ts +6 -0
- package/lib/typescript/components/DrawCore/DrawProvider.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/Item.d.ts +1 -1
- package/lib/typescript/components/DrawCore/Item.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/index.d.ts +4 -11
- package/lib/typescript/components/DrawCore/index.d.ts.map +1 -0
- package/lib/typescript/components/DrawCore/useDrawHook.d.ts +18 -0
- package/lib/typescript/components/DrawCore/useDrawHook.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/ArrowSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/ArrowSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/CancelSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/CancelSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/CircleSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/CircleSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/CloseSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/CloseSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/DoubleHeadSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/DoubleHeadSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/PenSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/PenSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/SendSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/SendSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/SliderSvg.d.ts +4 -0
- package/lib/typescript/components/DrawWithOptions/SliderSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/SquareSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/SquareSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/TextSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/TextSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/ThrashSvg.d.ts +1 -1
- package/lib/typescript/components/DrawWithOptions/ThrashSvg.d.ts.map +1 -0
- package/lib/typescript/components/DrawWithOptions/index.d.ts +4 -5
- package/lib/typescript/components/DrawWithOptions/index.d.ts.map +1 -0
- package/lib/typescript/components/Slider/ColorSlider.d.ts +8 -0
- package/lib/typescript/components/Slider/ColorSlider.d.ts.map +1 -0
- package/lib/typescript/components/Slider/Sliders.d.ts +8 -0
- package/lib/typescript/components/Slider/Sliders.d.ts.map +1 -0
- package/lib/typescript/components/Slider/StrokeSlider.d.ts +6 -0
- package/lib/typescript/components/Slider/StrokeSlider.d.ts.map +1 -0
- package/lib/typescript/components/Slider/sliderStyle.d.ts +28 -0
- package/lib/typescript/components/Slider/sliderStyle.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +2 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/package.json +32 -33
- package/src/components/DrawCore/CurrentAnimatedItem.tsx +166 -106
- package/src/components/DrawCore/DrawContext.tsx +22 -0
- package/src/components/DrawCore/DrawPad.tsx +3 -10
- package/src/components/DrawCore/DrawProvider.tsx +122 -0
- package/src/components/DrawCore/index.tsx +938 -1132
- package/src/components/DrawCore/useDrawHook.tsx +70 -0
- package/src/components/DrawWithOptions/SliderSvg.tsx +24 -0
- package/src/components/DrawWithOptions/ThrashSvg.tsx +29 -2
- package/src/components/DrawWithOptions/index.tsx +199 -146
- package/src/components/Slider/ColorSlider.tsx +144 -0
- package/src/components/Slider/Sliders.tsx +75 -0
- package/src/components/Slider/StrokeSlider.tsx +104 -0
- package/src/components/Slider/sliderStyle.tsx +28 -0
- package/src/index.tsx +1 -0
- package/src/types.d.ts +35 -1
- package/lib/commonjs/components/DrawCore/ColorSlider.js.map +0 -1
- package/lib/commonjs/components/DrawCore/StrokeSlider.js.map +0 -1
- package/lib/module/components/DrawCore/ColorSlider.js +0 -119
- package/lib/module/components/DrawCore/ColorSlider.js.map +0 -1
- package/lib/module/components/DrawCore/StrokeSlider.js +0 -93
- package/lib/module/components/DrawCore/StrokeSlider.js.map +0 -1
- package/lib/typescript/components/DrawCore/ColorSlider.d.ts +0 -12
- package/lib/typescript/components/DrawCore/StrokeSlider.d.ts +0 -9
- package/src/components/DrawCore/ColorSlider.tsx +0 -169
- package/src/components/DrawCore/StrokeSlider.tsx +0 -117
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import Animated, {
|
|
2
|
+
import Animated, {
|
|
3
|
+
createAnimatedPropAdapter,
|
|
4
|
+
processColor,
|
|
5
|
+
useAnimatedProps,
|
|
6
|
+
} from 'react-native-reanimated';
|
|
3
7
|
import { Path, Ellipse, Rect, Line, G } from 'react-native-svg';
|
|
4
8
|
import type { DrawItem, hslColor, Point } from '../../types';
|
|
5
9
|
|
|
@@ -49,13 +53,26 @@ const controlPoint = (
|
|
|
49
53
|
// create the bezier curve command
|
|
50
54
|
const bezierCommand = (point: Point, i: number, a: Point[]) => {
|
|
51
55
|
'worklet';
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const previousPoint = a[i - 1];
|
|
57
|
+
const nextPoint = a[i + 1];
|
|
58
|
+
if (previousPoint && nextPoint) {
|
|
59
|
+
const endPoint: Point = controlPoint(point, previousPoint, nextPoint, true);
|
|
60
|
+
if (i === 1) {
|
|
61
|
+
const pointBefore = a[i - 2];
|
|
62
|
+
if (pointBefore) {
|
|
63
|
+
const startPoint: Point = controlPoint(
|
|
64
|
+
previousPoint,
|
|
65
|
+
pointBefore,
|
|
66
|
+
point,
|
|
67
|
+
true
|
|
68
|
+
);
|
|
69
|
+
return `C ${startPoint.x},${startPoint.y} ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
return `S ${endPoint.x},${endPoint.y} ${point.x},${point.y}`;
|
|
73
|
+
}
|
|
58
74
|
}
|
|
75
|
+
return '';
|
|
59
76
|
};
|
|
60
77
|
|
|
61
78
|
export const pointsToPath = (points: Point[]) => {
|
|
@@ -97,9 +114,9 @@ function hslToRgb(col: hslColor) {
|
|
|
97
114
|
const hslRegExp = new RegExp(/hsl\(([\d.]+),\s*(\d+)%,\s*([\d.]+)%\)/);
|
|
98
115
|
const res = hslRegExp.exec(col);
|
|
99
116
|
|
|
100
|
-
const h = res ? parseFloat(res[1]) / 360 : 0;
|
|
101
|
-
const s = res ? parseFloat(res[2]) / 100 : 0;
|
|
102
|
-
const l = res ? parseFloat(res[3]) / 100 : 0;
|
|
117
|
+
const h = res ? parseFloat(res[1] ?? '0') / 360 : 0;
|
|
118
|
+
const s = res ? parseFloat(res[2] ?? '0') / 100 : 0;
|
|
119
|
+
const l = res ? parseFloat(res[3] ?? '0') / 100 : 0;
|
|
103
120
|
|
|
104
121
|
var r, g, b;
|
|
105
122
|
|
|
@@ -118,111 +135,154 @@ function hslToRgb(col: hslColor) {
|
|
|
118
135
|
)})`;
|
|
119
136
|
}
|
|
120
137
|
|
|
138
|
+
const propAdapter = createAnimatedPropAdapter(
|
|
139
|
+
(props: Record<string, unknown>) => {
|
|
140
|
+
if (
|
|
141
|
+
Object.keys(props).includes('fill') &&
|
|
142
|
+
(typeof props.fill === 'string' || typeof props.fill === 'number')
|
|
143
|
+
) {
|
|
144
|
+
props.fill = { type: 0, payload: processColor(props.fill) };
|
|
145
|
+
}
|
|
146
|
+
if (
|
|
147
|
+
Object.keys(props).includes('stroke') &&
|
|
148
|
+
(typeof props.stroke === 'string' || typeof props.stroke === 'number')
|
|
149
|
+
) {
|
|
150
|
+
props.stroke = { type: 0, payload: processColor(props.stroke) };
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
['fill', 'stroke']
|
|
154
|
+
);
|
|
155
|
+
|
|
121
156
|
export default function CurrentAnimatedItem({
|
|
122
157
|
currentItem,
|
|
123
158
|
}: {
|
|
124
159
|
currentItem: Animated.SharedValue<DrawItem | null>;
|
|
125
160
|
}) {
|
|
126
|
-
const ellipseAnimatedProps = useAnimatedProps(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
? currentItem.value.data
|
|
130
|
-
: { cx: -10, cy: -10, rx: 0, ry: 0 };
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
cx: coordinates.cx,
|
|
134
|
-
cy: coordinates.cy,
|
|
135
|
-
rx: coordinates.rx,
|
|
136
|
-
ry: coordinates.ry,
|
|
137
|
-
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
138
|
-
opacity: currentItem.value?.type === 'ellipse' ? 1 : 0,
|
|
139
|
-
strokeWidth:
|
|
161
|
+
const ellipseAnimatedProps = useAnimatedProps(
|
|
162
|
+
() => {
|
|
163
|
+
const coordinates =
|
|
140
164
|
currentItem.value?.type === 'ellipse'
|
|
141
|
-
? currentItem.value.
|
|
142
|
-
: 0,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
:
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
165
|
+
? currentItem.value.data
|
|
166
|
+
: { cx: -10, cy: -10, rx: 0, ry: 0 };
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
cx: coordinates.cx,
|
|
170
|
+
cy: coordinates.cy,
|
|
171
|
+
rx: coordinates.rx,
|
|
172
|
+
ry: coordinates.ry,
|
|
173
|
+
fill: 'transparent',
|
|
174
|
+
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
175
|
+
opacity: currentItem.value?.type === 'ellipse' ? 1 : 0,
|
|
176
|
+
strokeWidth:
|
|
177
|
+
currentItem.value?.type === 'ellipse'
|
|
178
|
+
? currentItem.value.strokeWidth
|
|
179
|
+
: 0,
|
|
180
|
+
marker: 'url(#selection)',
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
null,
|
|
184
|
+
propAdapter
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const singleHeadAnimatedProps = useAnimatedProps(
|
|
188
|
+
() => {
|
|
189
|
+
const coordinates =
|
|
160
190
|
currentItem.value?.type === 'singleHead'
|
|
161
|
-
? currentItem.value.
|
|
162
|
-
:
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
:
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
? currentItem.value.data
|
|
192
|
+
: { x1: -10, y1: -10, x2: -10, y2: -10 };
|
|
193
|
+
return {
|
|
194
|
+
x1: coordinates.x1,
|
|
195
|
+
y1: coordinates.y1,
|
|
196
|
+
x2: coordinates.x2,
|
|
197
|
+
y2: coordinates.y2,
|
|
198
|
+
fill: 'transparent',
|
|
199
|
+
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
200
|
+
opacity: currentItem.value?.type === 'singleHead' ? 1 : 0,
|
|
201
|
+
strokeWidth:
|
|
202
|
+
currentItem.value?.type === 'singleHead'
|
|
203
|
+
? currentItem.value.strokeWidth
|
|
204
|
+
: 0,
|
|
205
|
+
markerEnd: 'arrowhead',
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
null,
|
|
209
|
+
propAdapter
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const doubleHeadAnimatedProps = useAnimatedProps(
|
|
213
|
+
() => {
|
|
214
|
+
const coordinates =
|
|
181
215
|
currentItem.value?.type === 'doubleHead'
|
|
182
|
-
? currentItem.value.
|
|
183
|
-
:
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
:
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
216
|
+
? currentItem.value.data
|
|
217
|
+
: { x1: -10, y1: -10, x2: -10, y2: -10 };
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
x1: coordinates.x1,
|
|
221
|
+
y1: coordinates.y1,
|
|
222
|
+
x2: coordinates.x2,
|
|
223
|
+
y2: coordinates.y2,
|
|
224
|
+
fill: 'transparent',
|
|
225
|
+
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
226
|
+
opacity: currentItem.value?.type === 'doubleHead' ? 1 : 0,
|
|
227
|
+
strokeWidth:
|
|
228
|
+
currentItem.value?.type === 'doubleHead'
|
|
229
|
+
? currentItem.value.strokeWidth
|
|
230
|
+
: 0,
|
|
231
|
+
markerStart: 'side',
|
|
232
|
+
markerEnd: 'side',
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
null,
|
|
236
|
+
propAdapter
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const rectangleAnimatedProps = useAnimatedProps(
|
|
240
|
+
() => {
|
|
241
|
+
const coordinates =
|
|
202
242
|
currentItem.value?.type === 'rectangle'
|
|
203
|
-
? currentItem.value.
|
|
204
|
-
: 0,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
243
|
+
? currentItem.value.data
|
|
244
|
+
: { x: -10, y: -10, width: 0, height: 0 };
|
|
245
|
+
return {
|
|
246
|
+
x: coordinates.x,
|
|
247
|
+
y: coordinates.y,
|
|
248
|
+
width: coordinates.width,
|
|
249
|
+
height: coordinates.height,
|
|
250
|
+
fill: 'transparent',
|
|
251
|
+
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
252
|
+
opacity: currentItem.value?.type === 'rectangle' ? 1 : 0,
|
|
253
|
+
strokeWidth:
|
|
254
|
+
currentItem.value?.type === 'rectangle'
|
|
255
|
+
? currentItem.value.strokeWidth
|
|
256
|
+
: 0,
|
|
257
|
+
|
|
258
|
+
marker: 'url(#selection)',
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
null,
|
|
262
|
+
propAdapter
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
const penAnimatedProps = useAnimatedProps(
|
|
266
|
+
() => {
|
|
267
|
+
const d = pointsToPath(
|
|
268
|
+
currentItem.value?.type === 'pen'
|
|
269
|
+
? currentItem.value.data
|
|
270
|
+
: [{ x: -10, y: -10 }]
|
|
271
|
+
);
|
|
272
|
+
return {
|
|
273
|
+
d: d,
|
|
274
|
+
strokeWidth:
|
|
275
|
+
currentItem.value?.type === 'pen' ? currentItem.value.strokeWidth : 0,
|
|
276
|
+
stroke: hslToRgb(currentItem.value?.color || 'hsl(0, 0%, 0%)'),
|
|
277
|
+
opacity: currentItem.value?.type === 'pen' ? 1 : 0,
|
|
278
|
+
fill: 'transparent',
|
|
279
|
+
markerStart: 'selection',
|
|
280
|
+
markerEnd: 'selection',
|
|
281
|
+
};
|
|
282
|
+
},
|
|
283
|
+
null,
|
|
284
|
+
propAdapter
|
|
285
|
+
);
|
|
226
286
|
|
|
227
287
|
return (
|
|
228
288
|
<>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DrawItem, hslColor, DrawState, Action } from '../../types';
|
|
2
|
+
import { createContext, RefObject } from 'react';
|
|
3
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
4
|
+
import type ViewShot from 'react-native-view-shot';
|
|
5
|
+
|
|
6
|
+
export const DrawContext = createContext<{
|
|
7
|
+
drawState: DrawState;
|
|
8
|
+
dispatchDrawStates: React.Dispatch<Action>;
|
|
9
|
+
strokeWidth?: SharedValue<number>;
|
|
10
|
+
color?: SharedValue<hslColor>;
|
|
11
|
+
currentItem?: SharedValue<DrawItem | null>;
|
|
12
|
+
itemIsSelected?: SharedValue<boolean>;
|
|
13
|
+
viewShot?: RefObject<ViewShot>;
|
|
14
|
+
}>({
|
|
15
|
+
drawState: {
|
|
16
|
+
doneItems: [],
|
|
17
|
+
screenStates: [[]],
|
|
18
|
+
drawingMode: 'ellipse',
|
|
19
|
+
cancelEnabled: false,
|
|
20
|
+
},
|
|
21
|
+
dispatchDrawStates: () => true,
|
|
22
|
+
});
|
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type Animated from 'react-native-reanimated';
|
|
3
|
-
import Svg, {
|
|
4
|
-
Circle,
|
|
5
|
-
Defs,
|
|
6
|
-
Marker,
|
|
7
|
-
MarkerUnits,
|
|
8
|
-
Polyline,
|
|
9
|
-
Use,
|
|
10
|
-
} from 'react-native-svg';
|
|
3
|
+
import Svg, { Circle, Defs, Marker, Polyline, Use } from 'react-native-svg';
|
|
11
4
|
import CurrentAnimatedItem from './CurrentAnimatedItem';
|
|
12
5
|
import CurrentAnimatedText from './CurrentAnimatedText';
|
|
13
6
|
import Item from './Item';
|
|
14
7
|
import type { DrawItem } from '../../types';
|
|
8
|
+
import type { MarkerUnits } from 'react-native-svg/lib/typescript/elements/Marker';
|
|
15
9
|
|
|
16
10
|
const DrawPad = ({
|
|
17
11
|
currentItem,
|
|
@@ -26,7 +20,7 @@ const DrawPad = ({
|
|
|
26
20
|
}) => {
|
|
27
21
|
return (
|
|
28
22
|
<>
|
|
29
|
-
<Svg
|
|
23
|
+
<Svg>
|
|
30
24
|
<Defs>
|
|
31
25
|
<Circle
|
|
32
26
|
id="selectionIndicator"
|
|
@@ -84,7 +78,6 @@ const DrawPad = ({
|
|
|
84
78
|
|
|
85
79
|
<CurrentAnimatedItem currentItem={currentItem} />
|
|
86
80
|
</Svg>
|
|
87
|
-
|
|
88
81
|
<CurrentAnimatedText
|
|
89
82
|
currentItem={currentItem}
|
|
90
83
|
onHeightChange={onTextHeightChange}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { DrawContext } from './DrawContext';
|
|
2
|
+
import React, { ReactElement, useMemo, useReducer, useRef } from 'react';
|
|
3
|
+
import type { Action, DrawItem, DrawState, hslColor } from '../../types';
|
|
4
|
+
import { useSharedValue } from 'react-native-reanimated';
|
|
5
|
+
import type ViewShot from 'react-native-view-shot';
|
|
6
|
+
|
|
7
|
+
const initialState: DrawState = {
|
|
8
|
+
doneItems: [],
|
|
9
|
+
screenStates: [[]],
|
|
10
|
+
cancelEnabled: false,
|
|
11
|
+
drawingMode: 'pen',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const reducerDrawStates = (drawState: DrawState, action: Action): DrawState => {
|
|
15
|
+
'worklet';
|
|
16
|
+
switch (action.type) {
|
|
17
|
+
case 'SET_CANCEL_ENABLED':
|
|
18
|
+
return {
|
|
19
|
+
...drawState,
|
|
20
|
+
cancelEnabled: action.cancelEnabled,
|
|
21
|
+
};
|
|
22
|
+
case 'SET_DRAWING_MODE':
|
|
23
|
+
return {
|
|
24
|
+
...drawState,
|
|
25
|
+
drawingMode: action.drawingMode,
|
|
26
|
+
};
|
|
27
|
+
case 'ADD_DONE_ITEM':
|
|
28
|
+
return {
|
|
29
|
+
...drawState,
|
|
30
|
+
doneItems: drawState.doneItems.concat(action.item),
|
|
31
|
+
};
|
|
32
|
+
case 'DELETE_DONE_ITEM':
|
|
33
|
+
const newDoneItems = drawState.doneItems;
|
|
34
|
+
newDoneItems.splice(action.indice, 1);
|
|
35
|
+
return {
|
|
36
|
+
...drawState,
|
|
37
|
+
doneItems: newDoneItems,
|
|
38
|
+
};
|
|
39
|
+
case 'ADD_SCREEN_STATE':
|
|
40
|
+
if (action.currentItem) {
|
|
41
|
+
// hack sometime rectangle is not draw
|
|
42
|
+
if (
|
|
43
|
+
action.currentItem.type === 'rectangle' &&
|
|
44
|
+
(!action.currentItem.data.width || !action.currentItem.data.height)
|
|
45
|
+
) {
|
|
46
|
+
return drawState;
|
|
47
|
+
} else {
|
|
48
|
+
return {
|
|
49
|
+
...drawState,
|
|
50
|
+
screenStates: drawState.screenStates.concat([
|
|
51
|
+
[...drawState.doneItems, action.currentItem],
|
|
52
|
+
]),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
return {
|
|
57
|
+
...drawState,
|
|
58
|
+
cancelEnabled: action.cancelEnabled ?? drawState.cancelEnabled,
|
|
59
|
+
screenStates: drawState.screenStates.concat([
|
|
60
|
+
[...drawState.doneItems],
|
|
61
|
+
]),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
case 'CANCEL':
|
|
66
|
+
const len = drawState.screenStates.length;
|
|
67
|
+
if (len > 1) {
|
|
68
|
+
const newScreenStates = drawState.screenStates;
|
|
69
|
+
newScreenStates.pop();
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
...drawState,
|
|
73
|
+
cancelEnabled:
|
|
74
|
+
newScreenStates.length === 1 ? false : drawState.cancelEnabled,
|
|
75
|
+
doneItems: drawState.screenStates[len - 2] ?? [],
|
|
76
|
+
screenStates: newScreenStates,
|
|
77
|
+
};
|
|
78
|
+
} else {
|
|
79
|
+
return drawState;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const DrawProvider = ({ children }: { children: ReactElement }) => {
|
|
85
|
+
const itemIsSelected = useSharedValue<boolean>(false);
|
|
86
|
+
const strokeWidth = useSharedValue<number>(2);
|
|
87
|
+
const color = useSharedValue<hslColor>('hsl(0, 100%, 0%)');
|
|
88
|
+
const currentItem = useSharedValue<DrawItem | null>(null);
|
|
89
|
+
const viewShot = useRef<ViewShot>(null);
|
|
90
|
+
|
|
91
|
+
const [drawState, dispatchDrawStates] = useReducer(
|
|
92
|
+
reducerDrawStates,
|
|
93
|
+
initialState
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const contextValue = useMemo(
|
|
97
|
+
() => ({
|
|
98
|
+
drawState,
|
|
99
|
+
dispatchDrawStates,
|
|
100
|
+
strokeWidth,
|
|
101
|
+
color,
|
|
102
|
+
currentItem,
|
|
103
|
+
itemIsSelected,
|
|
104
|
+
viewShot,
|
|
105
|
+
}),
|
|
106
|
+
[
|
|
107
|
+
drawState,
|
|
108
|
+
dispatchDrawStates,
|
|
109
|
+
strokeWidth,
|
|
110
|
+
color,
|
|
111
|
+
currentItem,
|
|
112
|
+
itemIsSelected,
|
|
113
|
+
viewShot,
|
|
114
|
+
]
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<DrawContext.Provider value={contextValue}>{children}</DrawContext.Provider>
|
|
119
|
+
);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export default DrawProvider;
|