panelui-native 0.109.0 → 0.110.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/lib/module/components/avatar-cropper/avatar-cropper-math.js +320 -0
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -0
- package/lib/module/components/avatar-cropper/index.js +656 -0
- package/lib/module/components/avatar-cropper/index.js.map +1 -0
- package/lib/module/icons/index.js +6 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/panel-ui-provider.js +22 -10
- package/lib/module/providers/panel-ui-provider.js.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts +98 -0
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts.map +1 -0
- package/lib/typescript/src/components/avatar-cropper/index.d.ts +168 -0
- package/lib/typescript/src/components/avatar-cropper/index.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +2 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/providers/panel-ui-provider.d.ts.map +1 -1
- package/package.json +9 -1
- package/src/components/avatar-cropper/avatar-cropper-math.ts +311 -0
- package/src/components/avatar-cropper/index.tsx +808 -0
- package/src/icons/index.tsx +3 -0
- package/src/index.ts +16 -0
- package/src/providers/panel-ui-provider.tsx +21 -11
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AvatarCropper — frame a photo under a circle, and get that circle back as an
|
|
5
|
+
* image.
|
|
6
|
+
*
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const cropper = useRef<AvatarCropperHandle>(null);
|
|
9
|
+
*
|
|
10
|
+
* <AvatarCropper ref={cropper} source={photo} className="flex-1">
|
|
11
|
+
* <AvatarCropper.Canvas />
|
|
12
|
+
* <View className="flex-row justify-between p-4">
|
|
13
|
+
* <AvatarCropper.Reset />
|
|
14
|
+
* <AvatarCropper.Rotate />
|
|
15
|
+
* </View>
|
|
16
|
+
* </AvatarCropper>
|
|
17
|
+
*
|
|
18
|
+
* const { uri } = await cropper.current!.crop({ size: 512 });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* ## The mask stays put; the photo moves
|
|
22
|
+
*
|
|
23
|
+
* The mask sits in the middle of the canvas and never moves. Dragging, pinching
|
|
24
|
+
* and turning all move the photo underneath it, which is what makes the result
|
|
25
|
+
* predictable: the image you get back is exactly what is inside the outline
|
|
26
|
+
* when you press Done. The photo is never allowed to leave part of the mask
|
|
27
|
+
* empty — let go past an edge, or zoom out too far, and it springs back to the
|
|
28
|
+
* nearest position that covers it. At an angle that takes extra zoom for a
|
|
29
|
+
* square mask, whose corners reach further than a circle does, so the minimum
|
|
30
|
+
* zoom follows the angle.
|
|
31
|
+
*
|
|
32
|
+
* ## One surface, three gestures at once
|
|
33
|
+
*
|
|
34
|
+
* Pan, pinch and rotation are recognised together, so a two-finger gesture can
|
|
35
|
+
* move, zoom and turn in one motion the way a photo on a table would. Zoom and
|
|
36
|
+
* turn happen about the point between the fingers, which keeps whatever is
|
|
37
|
+
* under them under them.
|
|
38
|
+
*
|
|
39
|
+
* ## The export is the same arithmetic, in pixels
|
|
40
|
+
*
|
|
41
|
+
* `crop()` turns the source by the photo's angle, cuts the square the mask
|
|
42
|
+
* covers and scales it to the size asked for, through the optional
|
|
43
|
+
* `expo-image-manipulator`. The source is redrawn upright first: a camera photo
|
|
44
|
+
* often stores its orientation as a flag rather than in its pixels, and the
|
|
45
|
+
* turn and the cut both work on the raw pixels. `getCrop()` returns the same
|
|
46
|
+
* rectangle without any dependency, for an app that crops on its server.
|
|
47
|
+
*/
|
|
48
|
+
import { createContext, forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useState } from 'react';
|
|
49
|
+
import { Image, View } from 'react-native';
|
|
50
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
|
|
51
|
+
import Animated, { cancelAnimation, runOnJS, useAnimatedStyle, useDerivedValue, useSharedValue, withSpring, withTiming } from 'react-native-reanimated';
|
|
52
|
+
import Svg, { Path } from 'react-native-svg';
|
|
53
|
+
import { useCSSVariable } from 'uniwind';
|
|
54
|
+
import { RotateSquareIcon } from "../../icons/index.js";
|
|
55
|
+
import { cn } from "../../utils/cn.js";
|
|
56
|
+
import { Button } from "../button/index.js";
|
|
57
|
+
import { IDENTITY, baseSize, maskPath, nextQuarterTurn, normalizeDegrees, present, rotateAbout, settle, sourceCrop, zoomAbout } from "./avatar-cropper-math.js";
|
|
58
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
59
|
+
/** Where the photo settles after a gesture. Firm, so it lands without drifting. */
|
|
60
|
+
const SETTLE_SPRING = {
|
|
61
|
+
damping: 34,
|
|
62
|
+
stiffness: 320,
|
|
63
|
+
mass: 1
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** A quarter turn from the button. Long enough to read as a turn, not a jump. */
|
|
67
|
+
const TURN_DURATION = 260;
|
|
68
|
+
|
|
69
|
+
/** How far an accessibility zoom step goes. */
|
|
70
|
+
const ZOOM_STEP = 1.25;
|
|
71
|
+
|
|
72
|
+
/** How far a raw gesture may run past a bound before the rubber band stops it. */
|
|
73
|
+
const OVERSHOOT = 2;
|
|
74
|
+
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// Optional dependencies
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* `expo-image-manipulator`, or null when it is not installed. Each `require`
|
|
81
|
+
* sits directly in its `try` block, which is what lets Metro bundle an app
|
|
82
|
+
* that does not have the package.
|
|
83
|
+
*/
|
|
84
|
+
function loadManipulator() {
|
|
85
|
+
try {
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
87
|
+
return require('expo-image-manipulator');
|
|
88
|
+
} catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** `expo-image-picker`, or null when it is not installed. */
|
|
94
|
+
function loadPicker() {
|
|
95
|
+
try {
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
97
|
+
return require('expo-image-picker');
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** True when `crop()` can write an image, because `expo-image-manipulator` is installed. */
|
|
104
|
+
export const hasAvatarCropperExport = loadManipulator()?.ImageManipulator != null;
|
|
105
|
+
|
|
106
|
+
/** A photo to crop, as the picker returns it. */
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Let the user choose a photo or take one, ready to pass to `AvatarCropper`.
|
|
110
|
+
*
|
|
111
|
+
* Resolves `null` when the user cancels. Taking a photo asks for camera
|
|
112
|
+
* permission first and throws when it is refused, so the app can explain why
|
|
113
|
+
* nothing happened. Choosing from the library needs no permission. The
|
|
114
|
+
* picker's own editing is left off, because the cropper is the editor.
|
|
115
|
+
*
|
|
116
|
+
* Needs the optional `expo-image-picker`, and throws if it is not installed.
|
|
117
|
+
* In a development build the camera also needs the permission strings from
|
|
118
|
+
* that package's config plugin.
|
|
119
|
+
*/
|
|
120
|
+
export async function pickAvatarImage({
|
|
121
|
+
from = 'library',
|
|
122
|
+
quality = 1
|
|
123
|
+
} = {}) {
|
|
124
|
+
const picker = loadPicker();
|
|
125
|
+
if (!picker) {
|
|
126
|
+
throw new Error('pickAvatarImage needs expo-image-picker. Install it with: npx expo install expo-image-picker');
|
|
127
|
+
}
|
|
128
|
+
const options = {
|
|
129
|
+
mediaTypes: ['images'],
|
|
130
|
+
allowsEditing: false,
|
|
131
|
+
quality,
|
|
132
|
+
exif: false
|
|
133
|
+
};
|
|
134
|
+
let result;
|
|
135
|
+
if (from === 'camera') {
|
|
136
|
+
const permission = await picker.requestCameraPermissionsAsync();
|
|
137
|
+
if (!permission.granted) throw new Error('pickAvatarImage: camera permission was refused.');
|
|
138
|
+
result = await picker.launchCameraAsync(options);
|
|
139
|
+
} else {
|
|
140
|
+
result = await picker.launchImageLibraryAsync(options);
|
|
141
|
+
}
|
|
142
|
+
const asset = result.canceled ? undefined : result.assets?.[0];
|
|
143
|
+
return asset ? {
|
|
144
|
+
uri: asset.uri,
|
|
145
|
+
width: asset.width,
|
|
146
|
+
height: asset.height
|
|
147
|
+
} : null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
// Root
|
|
152
|
+
// ---------------------------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
const CropperContext = /*#__PURE__*/createContext(null);
|
|
155
|
+
function useCropper(part) {
|
|
156
|
+
const context = useContext(CropperContext);
|
|
157
|
+
if (!context) throw new Error(`${part} must be rendered inside <AvatarCropper>.`);
|
|
158
|
+
return context;
|
|
159
|
+
}
|
|
160
|
+
function describe(state) {
|
|
161
|
+
const zoom = `${Math.round(state.scale * 100)}% zoom`;
|
|
162
|
+
const angle = Math.round(normalizeDegrees(state.rotation));
|
|
163
|
+
return angle === 0 ? zoom : `${zoom}, turned ${angle} degrees`;
|
|
164
|
+
}
|
|
165
|
+
function AvatarCropperRoot({
|
|
166
|
+
source,
|
|
167
|
+
shape = 'circle',
|
|
168
|
+
maxZoom = 5,
|
|
169
|
+
inset = 24,
|
|
170
|
+
className,
|
|
171
|
+
children,
|
|
172
|
+
...props
|
|
173
|
+
}, ref) {
|
|
174
|
+
const x = useSharedValue(0);
|
|
175
|
+
const y = useSharedValue(0);
|
|
176
|
+
const scale = useSharedValue(1);
|
|
177
|
+
const rotation = useSharedValue(0);
|
|
178
|
+
const [canvas, setCanvasState] = useState(null);
|
|
179
|
+
const [measured, setMeasured] = useState(null);
|
|
180
|
+
const [summary, setSummary] = useState(describe(IDENTITY));
|
|
181
|
+
const {
|
|
182
|
+
uri,
|
|
183
|
+
width,
|
|
184
|
+
height
|
|
185
|
+
} = source;
|
|
186
|
+
const sourceSize = width && height && width > 0 && height > 0 ? {
|
|
187
|
+
width,
|
|
188
|
+
height
|
|
189
|
+
} : measured;
|
|
190
|
+
|
|
191
|
+
// The picker reports the size; anything else is measured once per photo.
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
setMeasured(null);
|
|
194
|
+
if (width && height) return;
|
|
195
|
+
let live = true;
|
|
196
|
+
Image.getSize(uri, (w, h) => {
|
|
197
|
+
if (live) setMeasured({
|
|
198
|
+
width: w,
|
|
199
|
+
height: h
|
|
200
|
+
});
|
|
201
|
+
}, () => {});
|
|
202
|
+
return () => {
|
|
203
|
+
live = false;
|
|
204
|
+
};
|
|
205
|
+
}, [uri, width, height]);
|
|
206
|
+
|
|
207
|
+
// A new photo starts again from the centre.
|
|
208
|
+
useEffect(() => {
|
|
209
|
+
x.value = 0;
|
|
210
|
+
y.value = 0;
|
|
211
|
+
scale.value = 1;
|
|
212
|
+
rotation.value = 0;
|
|
213
|
+
setSummary(describe(IDENTITY));
|
|
214
|
+
}, [uri, x, y, scale, rotation]);
|
|
215
|
+
const mask = canvas ? Math.max(0, Math.min(canvas.width, canvas.height) - inset * 2) : 0;
|
|
216
|
+
const frame = useMemo(() => sourceSize && mask > 0 ? {
|
|
217
|
+
mask,
|
|
218
|
+
image: baseSize(sourceSize, mask)
|
|
219
|
+
} : null, [sourceSize?.width, sourceSize?.height, mask] // eslint-disable-line react-hooks/exhaustive-deps
|
|
220
|
+
);
|
|
221
|
+
const setCanvas = useCallback(size => {
|
|
222
|
+
setCanvasState(current => current && current.width === size.width && current.height === size.height ? current : size);
|
|
223
|
+
}, []);
|
|
224
|
+
const read = useCallback(() => ({
|
|
225
|
+
x: x.value,
|
|
226
|
+
y: y.value,
|
|
227
|
+
scale: scale.value,
|
|
228
|
+
rotation: rotation.value
|
|
229
|
+
}), [x, y, scale, rotation]);
|
|
230
|
+
|
|
231
|
+
/** Animate to a state, settled against the bounds first. */
|
|
232
|
+
const animateTo = useCallback((target, timed) => {
|
|
233
|
+
if (!frame) return;
|
|
234
|
+
const next = settle(target, shape, frame, maxZoom);
|
|
235
|
+
const run = value => timed ? withTiming(value, {
|
|
236
|
+
duration: TURN_DURATION
|
|
237
|
+
}) : withSpring(value, SETTLE_SPRING);
|
|
238
|
+
x.value = run(next.x);
|
|
239
|
+
y.value = run(next.y);
|
|
240
|
+
scale.value = run(next.scale);
|
|
241
|
+
rotation.value = run(next.rotation);
|
|
242
|
+
setSummary(describe(next));
|
|
243
|
+
}, [frame, shape, maxZoom, x, y, scale, rotation]);
|
|
244
|
+
const settleNow = useCallback(() => animateTo(read(), false), [animateTo, read]);
|
|
245
|
+
const rotate = useCallback((direction = 1) => {
|
|
246
|
+
const current = read();
|
|
247
|
+
const target = nextQuarterTurn(current.rotation, direction);
|
|
248
|
+
// Turn about the mask, so what is in it stays in it.
|
|
249
|
+
animateTo(rotateAbout(current, target - current.rotation, 0, 0), true);
|
|
250
|
+
}, [animateTo, read]);
|
|
251
|
+
const reset = useCallback(() => {
|
|
252
|
+
// Unwind to the nearest whole turn rather than spinning back through
|
|
253
|
+
// every quarter turn taken to get here.
|
|
254
|
+
const turns = Math.round(rotation.value / 360) * 360;
|
|
255
|
+
animateTo({
|
|
256
|
+
...IDENTITY,
|
|
257
|
+
rotation: turns
|
|
258
|
+
}, true);
|
|
259
|
+
}, [animateTo, rotation]);
|
|
260
|
+
const zoomBy = useCallback(factor => animateTo(zoomAbout(read(), factor, 0, 0), false), [animateTo, read]);
|
|
261
|
+
const getCrop = useCallback(() => {
|
|
262
|
+
if (!frame || !sourceSize) return null;
|
|
263
|
+
return sourceCrop(settle(read(), shape, frame, maxZoom), frame, sourceSize);
|
|
264
|
+
}, [frame, sourceSize, read, shape, maxZoom]);
|
|
265
|
+
useImperativeHandle(ref, () => ({
|
|
266
|
+
getCrop,
|
|
267
|
+
rotate,
|
|
268
|
+
reset,
|
|
269
|
+
async crop({
|
|
270
|
+
size = 1024,
|
|
271
|
+
format = 'jpeg',
|
|
272
|
+
compress = 0.9
|
|
273
|
+
} = {}) {
|
|
274
|
+
const manipulator = loadManipulator()?.ImageManipulator;
|
|
275
|
+
if (!manipulator) {
|
|
276
|
+
throw new Error('AvatarCropper.crop needs expo-image-manipulator. Install it with: ' + 'npx expo install expo-image-manipulator — or use getCrop() to crop elsewhere.');
|
|
277
|
+
}
|
|
278
|
+
const rect = getCrop();
|
|
279
|
+
if (!rect || !sourceSize) {
|
|
280
|
+
throw new Error('AvatarCropper.crop: the photo has not been measured and laid out yet.');
|
|
281
|
+
}
|
|
282
|
+
// Redraw upright first. The turn and the cut both read raw pixels,
|
|
283
|
+
// and a camera photo often keeps its orientation in a flag instead.
|
|
284
|
+
const context = manipulator.manipulate(uri).resize({
|
|
285
|
+
width: sourceSize.width
|
|
286
|
+
});
|
|
287
|
+
if (rect.rotation !== 0) context.rotate(rect.rotation);
|
|
288
|
+
context.crop({
|
|
289
|
+
originX: rect.originX,
|
|
290
|
+
originY: rect.originY,
|
|
291
|
+
width: rect.width,
|
|
292
|
+
height: rect.height
|
|
293
|
+
});
|
|
294
|
+
if (size < rect.width) context.resize({
|
|
295
|
+
width: size,
|
|
296
|
+
height: size
|
|
297
|
+
});
|
|
298
|
+
const rendered = await context.renderAsync();
|
|
299
|
+
return rendered.saveAsync({
|
|
300
|
+
format,
|
|
301
|
+
compress
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}), [getCrop, rotate, reset, sourceSize, uri]);
|
|
305
|
+
const value = {
|
|
306
|
+
shape,
|
|
307
|
+
maxZoom,
|
|
308
|
+
inset,
|
|
309
|
+
source,
|
|
310
|
+
sourceSize,
|
|
311
|
+
frame,
|
|
312
|
+
canvas,
|
|
313
|
+
setCanvas,
|
|
314
|
+
x,
|
|
315
|
+
y,
|
|
316
|
+
scale,
|
|
317
|
+
rotation,
|
|
318
|
+
settleNow,
|
|
319
|
+
rotate,
|
|
320
|
+
reset,
|
|
321
|
+
zoomBy,
|
|
322
|
+
summary
|
|
323
|
+
};
|
|
324
|
+
return /*#__PURE__*/_jsx(CropperContext.Provider, {
|
|
325
|
+
value: value,
|
|
326
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
327
|
+
className: cn('flex-1', className),
|
|
328
|
+
...props,
|
|
329
|
+
children: children ?? /*#__PURE__*/_jsx(AvatarCropperCanvas, {})
|
|
330
|
+
})
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
const AvatarCropperForwarded = /*#__PURE__*/forwardRef(AvatarCropperRoot);
|
|
334
|
+
AvatarCropperForwarded.displayName = 'AvatarCropper';
|
|
335
|
+
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
// Canvas
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The photo under its mask, and the surface every gesture lands on. It fills
|
|
342
|
+
* the space it is given, and the mask is drawn as large as the shorter side
|
|
343
|
+
* allows, less the root's `inset`.
|
|
344
|
+
*/
|
|
345
|
+
function AvatarCropperCanvas({
|
|
346
|
+
className,
|
|
347
|
+
accessibilityLabel = 'Photo crop',
|
|
348
|
+
onLayout,
|
|
349
|
+
...props
|
|
350
|
+
}) {
|
|
351
|
+
const {
|
|
352
|
+
shape,
|
|
353
|
+
maxZoom,
|
|
354
|
+
source,
|
|
355
|
+
frame,
|
|
356
|
+
canvas,
|
|
357
|
+
setCanvas,
|
|
358
|
+
x,
|
|
359
|
+
y,
|
|
360
|
+
scale,
|
|
361
|
+
rotation,
|
|
362
|
+
settleNow,
|
|
363
|
+
rotate,
|
|
364
|
+
zoomBy,
|
|
365
|
+
summary
|
|
366
|
+
} = useCropper('AvatarCropper.Canvas');
|
|
367
|
+
const scrimToken = useCSSVariable('--color-background');
|
|
368
|
+
const ringToken = useCSSVariable('--color-foreground');
|
|
369
|
+
const scrim = typeof scrimToken === 'string' ? scrimToken : '#0a0a0a';
|
|
370
|
+
const ring = typeof ringToken === 'string' ? ringToken : '#fafafa';
|
|
371
|
+
|
|
372
|
+
// How many of the three gestures are under way. The photo settles once the
|
|
373
|
+
// last of them lets go, not each time one does.
|
|
374
|
+
const active = useSharedValue(0);
|
|
375
|
+
const W = canvas?.width ?? 0;
|
|
376
|
+
const H = canvas?.height ?? 0;
|
|
377
|
+
const image = frame?.image ?? {
|
|
378
|
+
width: 0,
|
|
379
|
+
height: 0
|
|
380
|
+
};
|
|
381
|
+
const mask = frame?.mask ?? 0;
|
|
382
|
+
const shown = useDerivedValue(() => {
|
|
383
|
+
const raw = {
|
|
384
|
+
x: x.value,
|
|
385
|
+
y: y.value,
|
|
386
|
+
scale: scale.value,
|
|
387
|
+
rotation: rotation.value
|
|
388
|
+
};
|
|
389
|
+
return frame ? present(raw, shape, frame, maxZoom) : raw;
|
|
390
|
+
});
|
|
391
|
+
const imageStyle = useAnimatedStyle(() => ({
|
|
392
|
+
transform: [{
|
|
393
|
+
translateX: shown.value.x
|
|
394
|
+
}, {
|
|
395
|
+
translateY: shown.value.y
|
|
396
|
+
}, {
|
|
397
|
+
rotate: `${shown.value.rotation}deg`
|
|
398
|
+
}, {
|
|
399
|
+
scale: shown.value.scale
|
|
400
|
+
}]
|
|
401
|
+
}));
|
|
402
|
+
const gesture = useMemo(() => {
|
|
403
|
+
const begin = () => {
|
|
404
|
+
'worklet';
|
|
405
|
+
|
|
406
|
+
if (active.value === 0) {
|
|
407
|
+
cancelAnimation(x);
|
|
408
|
+
cancelAnimation(y);
|
|
409
|
+
cancelAnimation(scale);
|
|
410
|
+
cancelAnimation(rotation);
|
|
411
|
+
}
|
|
412
|
+
active.value += 1;
|
|
413
|
+
};
|
|
414
|
+
const end = () => {
|
|
415
|
+
'worklet';
|
|
416
|
+
|
|
417
|
+
active.value = Math.max(0, active.value - 1);
|
|
418
|
+
if (active.value === 0) runOnJS(settleNow)();
|
|
419
|
+
};
|
|
420
|
+
/** Keep a raw value from running away while it is rubber-banded. */
|
|
421
|
+
const bound = state => {
|
|
422
|
+
'worklet';
|
|
423
|
+
|
|
424
|
+
if (!frame) return state;
|
|
425
|
+
const lowest = 1 / OVERSHOOT / 2;
|
|
426
|
+
const next = {
|
|
427
|
+
...state,
|
|
428
|
+
scale: Math.min(Math.max(state.scale, lowest), maxZoom * OVERSHOOT)
|
|
429
|
+
};
|
|
430
|
+
const limit = Math.max(image.width, image.height) * next.scale;
|
|
431
|
+
next.x = Math.min(Math.max(next.x, -limit), limit);
|
|
432
|
+
next.y = Math.min(Math.max(next.y, -limit), limit);
|
|
433
|
+
return next;
|
|
434
|
+
};
|
|
435
|
+
const write = state => {
|
|
436
|
+
'worklet';
|
|
437
|
+
|
|
438
|
+
const next = bound(state);
|
|
439
|
+
x.value = next.x;
|
|
440
|
+
y.value = next.y;
|
|
441
|
+
scale.value = next.scale;
|
|
442
|
+
rotation.value = next.rotation;
|
|
443
|
+
};
|
|
444
|
+
const current = () => {
|
|
445
|
+
'worklet';
|
|
446
|
+
|
|
447
|
+
return {
|
|
448
|
+
x: x.value,
|
|
449
|
+
y: y.value,
|
|
450
|
+
scale: scale.value,
|
|
451
|
+
rotation: rotation.value
|
|
452
|
+
};
|
|
453
|
+
};
|
|
454
|
+
const pan = Gesture.Pan().averageTouches(true).onBegin(() => {
|
|
455
|
+
'worklet';
|
|
456
|
+
|
|
457
|
+
begin();
|
|
458
|
+
}).onChange(event => {
|
|
459
|
+
'worklet';
|
|
460
|
+
|
|
461
|
+
const state = current();
|
|
462
|
+
write({
|
|
463
|
+
...state,
|
|
464
|
+
x: state.x + event.changeX,
|
|
465
|
+
y: state.y + event.changeY
|
|
466
|
+
});
|
|
467
|
+
}).onFinalize(() => {
|
|
468
|
+
'worklet';
|
|
469
|
+
|
|
470
|
+
end();
|
|
471
|
+
});
|
|
472
|
+
const pinch = Gesture.Pinch().onBegin(() => {
|
|
473
|
+
'worklet';
|
|
474
|
+
|
|
475
|
+
begin();
|
|
476
|
+
}).onChange(event => {
|
|
477
|
+
'worklet';
|
|
478
|
+
|
|
479
|
+
write(zoomAbout(current(), event.scaleChange, event.focalX - W / 2, event.focalY - H / 2));
|
|
480
|
+
}).onFinalize(() => {
|
|
481
|
+
'worklet';
|
|
482
|
+
|
|
483
|
+
end();
|
|
484
|
+
});
|
|
485
|
+
const turn = Gesture.Rotation().onBegin(() => {
|
|
486
|
+
'worklet';
|
|
487
|
+
|
|
488
|
+
begin();
|
|
489
|
+
}).onChange(event => {
|
|
490
|
+
'worklet';
|
|
491
|
+
|
|
492
|
+
write(rotateAbout(current(), event.rotationChange * 180 / Math.PI, event.anchorX - W / 2, event.anchorY - H / 2));
|
|
493
|
+
}).onFinalize(() => {
|
|
494
|
+
'worklet';
|
|
495
|
+
|
|
496
|
+
end();
|
|
497
|
+
});
|
|
498
|
+
return Gesture.Simultaneous(pan, pinch, turn);
|
|
499
|
+
}, [active, x, y, scale, rotation, settleNow, frame, image.width, image.height, maxZoom, W, H]);
|
|
500
|
+
const onAccessibilityAction = useCallback(event => {
|
|
501
|
+
switch (event.nativeEvent.actionName) {
|
|
502
|
+
case 'increment':
|
|
503
|
+
zoomBy(ZOOM_STEP);
|
|
504
|
+
break;
|
|
505
|
+
case 'decrement':
|
|
506
|
+
zoomBy(1 / ZOOM_STEP);
|
|
507
|
+
break;
|
|
508
|
+
case 'rotate':
|
|
509
|
+
rotate(1);
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
}, [zoomBy, rotate]);
|
|
513
|
+
const handleLayout = useCallback(event => {
|
|
514
|
+
const {
|
|
515
|
+
width,
|
|
516
|
+
height
|
|
517
|
+
} = event.nativeEvent.layout;
|
|
518
|
+
setCanvas({
|
|
519
|
+
width,
|
|
520
|
+
height
|
|
521
|
+
});
|
|
522
|
+
onLayout?.(event);
|
|
523
|
+
}, [setCanvas, onLayout]);
|
|
524
|
+
const outline = W > 0 && mask > 0 ? maskPath(shape, W / 2, H / 2, mask) : '';
|
|
525
|
+
return /*#__PURE__*/_jsx(GestureDetector, {
|
|
526
|
+
gesture: gesture,
|
|
527
|
+
children: /*#__PURE__*/_jsxs(View, {
|
|
528
|
+
className: cn('flex-1 overflow-hidden bg-background', className),
|
|
529
|
+
onLayout: handleLayout,
|
|
530
|
+
accessible: true,
|
|
531
|
+
accessibilityRole: "adjustable",
|
|
532
|
+
accessibilityLabel: accessibilityLabel,
|
|
533
|
+
accessibilityHint: "Drag to move the photo, pinch to zoom, and twist to turn it.",
|
|
534
|
+
accessibilityValue: {
|
|
535
|
+
text: summary
|
|
536
|
+
},
|
|
537
|
+
accessibilityActions: [{
|
|
538
|
+
name: 'increment',
|
|
539
|
+
label: 'Zoom in'
|
|
540
|
+
}, {
|
|
541
|
+
name: 'decrement',
|
|
542
|
+
label: 'Zoom out'
|
|
543
|
+
}, {
|
|
544
|
+
name: 'rotate',
|
|
545
|
+
label: 'Rotate'
|
|
546
|
+
}],
|
|
547
|
+
onAccessibilityAction: onAccessibilityAction,
|
|
548
|
+
...props,
|
|
549
|
+
children: [frame && W > 0 ? /*#__PURE__*/_jsx(Animated.View, {
|
|
550
|
+
pointerEvents: "none",
|
|
551
|
+
style: [{
|
|
552
|
+
position: 'absolute',
|
|
553
|
+
left: (W - image.width) / 2,
|
|
554
|
+
top: (H - image.height) / 2,
|
|
555
|
+
width: image.width,
|
|
556
|
+
height: image.height
|
|
557
|
+
}, imageStyle],
|
|
558
|
+
children: /*#__PURE__*/_jsx(Image, {
|
|
559
|
+
source: {
|
|
560
|
+
uri: source.uri
|
|
561
|
+
},
|
|
562
|
+
style: {
|
|
563
|
+
width: image.width,
|
|
564
|
+
height: image.height
|
|
565
|
+
},
|
|
566
|
+
resizeMode: "cover",
|
|
567
|
+
accessibilityIgnoresInvertColors: true
|
|
568
|
+
})
|
|
569
|
+
}) : null, outline ? /*#__PURE__*/_jsxs(Svg, {
|
|
570
|
+
width: W,
|
|
571
|
+
height: H,
|
|
572
|
+
style: {
|
|
573
|
+
position: 'absolute',
|
|
574
|
+
left: 0,
|
|
575
|
+
top: 0
|
|
576
|
+
},
|
|
577
|
+
pointerEvents: "none",
|
|
578
|
+
children: [/*#__PURE__*/_jsx(Path, {
|
|
579
|
+
d: `M0 0H${W}V${H}H0Z${outline}`,
|
|
580
|
+
fill: scrim,
|
|
581
|
+
fillOpacity: 0.78,
|
|
582
|
+
fillRule: "evenodd"
|
|
583
|
+
}), /*#__PURE__*/_jsx(Path, {
|
|
584
|
+
d: outline,
|
|
585
|
+
fill: "none",
|
|
586
|
+
stroke: ring,
|
|
587
|
+
strokeOpacity: 0.9,
|
|
588
|
+
strokeWidth: 1.5
|
|
589
|
+
})]
|
|
590
|
+
}) : null]
|
|
591
|
+
})
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
AvatarCropperCanvas.displayName = 'AvatarCropper.Canvas';
|
|
595
|
+
|
|
596
|
+
// ---------------------------------------------------------------------------
|
|
597
|
+
// Controls
|
|
598
|
+
// ---------------------------------------------------------------------------
|
|
599
|
+
|
|
600
|
+
/** A quarter turn per press, about the middle of the mask. */
|
|
601
|
+
function AvatarCropperRotate({
|
|
602
|
+
direction = 'clockwise',
|
|
603
|
+
variant = 'secondary',
|
|
604
|
+
size = 'icon',
|
|
605
|
+
children,
|
|
606
|
+
accessibilityLabel,
|
|
607
|
+
...props
|
|
608
|
+
}) {
|
|
609
|
+
const {
|
|
610
|
+
rotate
|
|
611
|
+
} = useCropper('AvatarCropper.Rotate');
|
|
612
|
+
const clockwise = direction === 'clockwise';
|
|
613
|
+
return /*#__PURE__*/_jsx(Button, {
|
|
614
|
+
variant: variant,
|
|
615
|
+
size: size,
|
|
616
|
+
accessibilityLabel: accessibilityLabel ?? (clockwise ? 'Rotate clockwise' : 'Rotate counterclockwise'),
|
|
617
|
+
onPress: () => rotate(clockwise ? 1 : -1),
|
|
618
|
+
...props,
|
|
619
|
+
children: children ?? /*#__PURE__*/_jsx(View, {
|
|
620
|
+
style: clockwise ? undefined : {
|
|
621
|
+
transform: [{
|
|
622
|
+
scaleX: -1
|
|
623
|
+
}]
|
|
624
|
+
},
|
|
625
|
+
children: /*#__PURE__*/_jsx(RotateSquareIcon, {
|
|
626
|
+
size: 20
|
|
627
|
+
})
|
|
628
|
+
})
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
AvatarCropperRotate.displayName = 'AvatarCropper.Rotate';
|
|
632
|
+
/** Back to the photo centred, unzoomed and upright. Labelled "Reset" unless given children. */
|
|
633
|
+
function AvatarCropperReset({
|
|
634
|
+
variant = 'ghost',
|
|
635
|
+
size = 'sm',
|
|
636
|
+
children = 'Reset',
|
|
637
|
+
...props
|
|
638
|
+
}) {
|
|
639
|
+
const {
|
|
640
|
+
reset
|
|
641
|
+
} = useCropper('AvatarCropper.Reset');
|
|
642
|
+
return /*#__PURE__*/_jsx(Button, {
|
|
643
|
+
variant: variant,
|
|
644
|
+
size: size,
|
|
645
|
+
onPress: reset,
|
|
646
|
+
...props,
|
|
647
|
+
children: children
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
AvatarCropperReset.displayName = 'AvatarCropper.Reset';
|
|
651
|
+
export const AvatarCropper = Object.assign(AvatarCropperForwarded, {
|
|
652
|
+
Canvas: AvatarCropperCanvas,
|
|
653
|
+
Rotate: AvatarCropperRotate,
|
|
654
|
+
Reset: AvatarCropperReset
|
|
655
|
+
});
|
|
656
|
+
//# sourceMappingURL=index.js.map
|