panelui-native 0.109.0 → 0.111.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 +360 -0
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -0
- package/lib/module/components/avatar-cropper/index.js +822 -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 +120 -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 +195 -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 +347 -0
- package/src/components/avatar-cropper/index.tsx +1036 -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,822 @@
|
|
|
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, useRef, 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, coversMask, 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
|
+
/** `react-native-view-shot`'s capture function, or null when it is not installed. */
|
|
104
|
+
function loadCapture() {
|
|
105
|
+
try {
|
|
106
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
107
|
+
const mod = require('react-native-view-shot');
|
|
108
|
+
return mod?.captureRef ?? null;
|
|
109
|
+
} catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** True when `crop()` can write an image, because `expo-image-manipulator` is installed. */
|
|
115
|
+
export const hasAvatarCropperExport = loadManipulator()?.ImageManipulator != null;
|
|
116
|
+
|
|
117
|
+
/** A photo to crop, as the picker returns it. */
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Let the user choose a photo or take one, ready to pass to `AvatarCropper`.
|
|
121
|
+
*
|
|
122
|
+
* Resolves `null` when the user cancels. Taking a photo asks for camera
|
|
123
|
+
* permission first and throws when it is refused, so the app can explain why
|
|
124
|
+
* nothing happened. Choosing from the library needs no permission. The
|
|
125
|
+
* picker's own editing is left off, because the cropper is the editor.
|
|
126
|
+
*
|
|
127
|
+
* Needs the optional `expo-image-picker`, and throws if it is not installed.
|
|
128
|
+
* In a development build the camera also needs the permission strings from
|
|
129
|
+
* that package's config plugin.
|
|
130
|
+
*/
|
|
131
|
+
export async function pickAvatarImage({
|
|
132
|
+
from = 'library',
|
|
133
|
+
quality = 1
|
|
134
|
+
} = {}) {
|
|
135
|
+
const picker = loadPicker();
|
|
136
|
+
if (!picker) {
|
|
137
|
+
throw new Error('pickAvatarImage needs expo-image-picker. Install it with: npx expo install expo-image-picker');
|
|
138
|
+
}
|
|
139
|
+
const options = {
|
|
140
|
+
mediaTypes: ['images'],
|
|
141
|
+
allowsEditing: false,
|
|
142
|
+
quality,
|
|
143
|
+
exif: false
|
|
144
|
+
};
|
|
145
|
+
let result;
|
|
146
|
+
if (from === 'camera') {
|
|
147
|
+
const permission = await picker.requestCameraPermissionsAsync();
|
|
148
|
+
if (!permission.granted) throw new Error('pickAvatarImage: camera permission was refused.');
|
|
149
|
+
result = await picker.launchCameraAsync(options);
|
|
150
|
+
} else {
|
|
151
|
+
result = await picker.launchImageLibraryAsync(options);
|
|
152
|
+
}
|
|
153
|
+
const asset = result.canceled ? undefined : result.assets?.[0];
|
|
154
|
+
return asset ? {
|
|
155
|
+
uri: asset.uri,
|
|
156
|
+
width: asset.width,
|
|
157
|
+
height: asset.height
|
|
158
|
+
} : null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---------------------------------------------------------------------------
|
|
162
|
+
// Root
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
const CropperContext = /*#__PURE__*/createContext(null);
|
|
166
|
+
function useCropper(part) {
|
|
167
|
+
const context = useContext(CropperContext);
|
|
168
|
+
if (!context) throw new Error(`${part} must be rendered inside <AvatarCropper>.`);
|
|
169
|
+
return context;
|
|
170
|
+
}
|
|
171
|
+
function describe(state) {
|
|
172
|
+
const zoom = `${Math.round(state.scale * 100)}% zoom`;
|
|
173
|
+
const angle = Math.round(normalizeDegrees(state.rotation));
|
|
174
|
+
return angle === 0 ? zoom : `${zoom}, turned ${angle} degrees`;
|
|
175
|
+
}
|
|
176
|
+
function AvatarCropperRoot({
|
|
177
|
+
source,
|
|
178
|
+
shape = 'circle',
|
|
179
|
+
maxZoom = 5,
|
|
180
|
+
cover = true,
|
|
181
|
+
minZoom = 0.5,
|
|
182
|
+
background,
|
|
183
|
+
inset = 24,
|
|
184
|
+
className,
|
|
185
|
+
children,
|
|
186
|
+
...props
|
|
187
|
+
}, ref) {
|
|
188
|
+
const x = useSharedValue(0);
|
|
189
|
+
const y = useSharedValue(0);
|
|
190
|
+
const scale = useSharedValue(1);
|
|
191
|
+
const rotation = useSharedValue(0);
|
|
192
|
+
const [canvas, setCanvasState] = useState(null);
|
|
193
|
+
const [measured, setMeasured] = useState(null);
|
|
194
|
+
const [summary, setSummary] = useState(describe(IDENTITY));
|
|
195
|
+
const [stage, setStage] = useState(null);
|
|
196
|
+
const backgroundToken = useCSSVariable('--color-background');
|
|
197
|
+
const fill = background ?? (typeof backgroundToken === 'string' ? backgroundToken : '#ffffff');
|
|
198
|
+
const bounds = useMemo(() => ({
|
|
199
|
+
shape,
|
|
200
|
+
maxZoom,
|
|
201
|
+
cover,
|
|
202
|
+
minZoom
|
|
203
|
+
}), [shape, maxZoom, cover, minZoom]);
|
|
204
|
+
const {
|
|
205
|
+
uri,
|
|
206
|
+
width,
|
|
207
|
+
height
|
|
208
|
+
} = source;
|
|
209
|
+
const sourceSize = width && height && width > 0 && height > 0 ? {
|
|
210
|
+
width,
|
|
211
|
+
height
|
|
212
|
+
} : measured;
|
|
213
|
+
|
|
214
|
+
// The picker reports the size; anything else is measured once per photo.
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
setMeasured(null);
|
|
217
|
+
if (width && height) return;
|
|
218
|
+
let live = true;
|
|
219
|
+
Image.getSize(uri, (w, h) => {
|
|
220
|
+
if (live) setMeasured({
|
|
221
|
+
width: w,
|
|
222
|
+
height: h
|
|
223
|
+
});
|
|
224
|
+
}, () => {});
|
|
225
|
+
return () => {
|
|
226
|
+
live = false;
|
|
227
|
+
};
|
|
228
|
+
}, [uri, width, height]);
|
|
229
|
+
|
|
230
|
+
// A new photo starts again from the centre.
|
|
231
|
+
useEffect(() => {
|
|
232
|
+
x.value = 0;
|
|
233
|
+
y.value = 0;
|
|
234
|
+
scale.value = 1;
|
|
235
|
+
rotation.value = 0;
|
|
236
|
+
setSummary(describe(IDENTITY));
|
|
237
|
+
}, [uri, x, y, scale, rotation]);
|
|
238
|
+
const mask = canvas ? Math.max(0, Math.min(canvas.width, canvas.height) - inset * 2) : 0;
|
|
239
|
+
const frame = useMemo(() => sourceSize && mask > 0 ? {
|
|
240
|
+
mask,
|
|
241
|
+
image: baseSize(sourceSize, mask)
|
|
242
|
+
} : null, [sourceSize?.width, sourceSize?.height, mask] // eslint-disable-line react-hooks/exhaustive-deps
|
|
243
|
+
);
|
|
244
|
+
const setCanvas = useCallback(size => {
|
|
245
|
+
setCanvasState(current => current && current.width === size.width && current.height === size.height ? current : size);
|
|
246
|
+
}, []);
|
|
247
|
+
const read = useCallback(() => ({
|
|
248
|
+
x: x.value,
|
|
249
|
+
y: y.value,
|
|
250
|
+
scale: scale.value,
|
|
251
|
+
rotation: rotation.value
|
|
252
|
+
}), [x, y, scale, rotation]);
|
|
253
|
+
|
|
254
|
+
/** Animate to a state, settled against the bounds first. */
|
|
255
|
+
const animateTo = useCallback((target, timed) => {
|
|
256
|
+
if (!frame) return;
|
|
257
|
+
const next = settle(target, bounds, frame);
|
|
258
|
+
const run = value => timed ? withTiming(value, {
|
|
259
|
+
duration: TURN_DURATION
|
|
260
|
+
}) : withSpring(value, SETTLE_SPRING);
|
|
261
|
+
x.value = run(next.x);
|
|
262
|
+
y.value = run(next.y);
|
|
263
|
+
scale.value = run(next.scale);
|
|
264
|
+
rotation.value = run(next.rotation);
|
|
265
|
+
setSummary(describe(next));
|
|
266
|
+
}, [frame, bounds, x, y, scale, rotation]);
|
|
267
|
+
const settleNow = useCallback(() => animateTo(read(), false), [animateTo, read]);
|
|
268
|
+
const rotate = useCallback((direction = 1) => {
|
|
269
|
+
const current = read();
|
|
270
|
+
const target = nextQuarterTurn(current.rotation, direction);
|
|
271
|
+
// Turn about the mask, so what is in it stays in it.
|
|
272
|
+
animateTo(rotateAbout(current, target - current.rotation, 0, 0), true);
|
|
273
|
+
}, [animateTo, read]);
|
|
274
|
+
const reset = useCallback(() => {
|
|
275
|
+
// Unwind to the nearest whole turn rather than spinning back through
|
|
276
|
+
// every quarter turn taken to get here.
|
|
277
|
+
const turns = Math.round(rotation.value / 360) * 360;
|
|
278
|
+
animateTo({
|
|
279
|
+
...IDENTITY,
|
|
280
|
+
rotation: turns
|
|
281
|
+
}, true);
|
|
282
|
+
}, [animateTo, rotation]);
|
|
283
|
+
const zoomBy = useCallback(factor => animateTo(zoomAbout(read(), factor, 0, 0), false), [animateTo, read]);
|
|
284
|
+
const settled = useCallback(() => frame ? settle(read(), bounds, frame) : null, [frame, bounds, read]);
|
|
285
|
+
const getCrop = useCallback(() => {
|
|
286
|
+
const state = settled();
|
|
287
|
+
if (!state || !frame || !sourceSize) return null;
|
|
288
|
+
return sourceCrop(state, frame, sourceSize, coversMask(state, shape, frame));
|
|
289
|
+
}, [settled, frame, sourceSize, shape]);
|
|
290
|
+
useImperativeHandle(ref, () => ({
|
|
291
|
+
getCrop,
|
|
292
|
+
rotate,
|
|
293
|
+
reset,
|
|
294
|
+
async crop({
|
|
295
|
+
size = 1024,
|
|
296
|
+
format,
|
|
297
|
+
compress = 0.9
|
|
298
|
+
} = {}) {
|
|
299
|
+
const state = settled();
|
|
300
|
+
const rect = getCrop();
|
|
301
|
+
if (!state || !frame || !rect || !sourceSize) {
|
|
302
|
+
throw new Error('AvatarCropper.crop: the photo has not been measured and laid out yet.');
|
|
303
|
+
}
|
|
304
|
+
const transparent = fill === 'transparent';
|
|
305
|
+
const output = format ?? (transparent ? 'png' : 'jpeg');
|
|
306
|
+
const side = Math.min(size, rect.width);
|
|
307
|
+
if (!coversMask(state, shape, frame)) {
|
|
308
|
+
// Part of the outline is background, which the manipulator cannot
|
|
309
|
+
// paint on a phone. Draw the crop as the screen does, off to one
|
|
310
|
+
// side, and capture that instead.
|
|
311
|
+
const capture = loadCapture();
|
|
312
|
+
if (!capture) {
|
|
313
|
+
throw new Error('AvatarCropper.crop: a photo smaller than the outline is written with ' + 'react-native-view-shot. Install it with: npx expo install react-native-view-shot');
|
|
314
|
+
}
|
|
315
|
+
return new Promise((resolve, reject) => {
|
|
316
|
+
setStage({
|
|
317
|
+
state,
|
|
318
|
+
side,
|
|
319
|
+
format: output === 'jpeg' ? 'jpg' : 'png',
|
|
320
|
+
compress,
|
|
321
|
+
capture,
|
|
322
|
+
resolve: result => {
|
|
323
|
+
setStage(null);
|
|
324
|
+
resolve(result);
|
|
325
|
+
},
|
|
326
|
+
reject: error => {
|
|
327
|
+
setStage(null);
|
|
328
|
+
reject(error);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
const manipulator = loadManipulator()?.ImageManipulator;
|
|
334
|
+
if (!manipulator) {
|
|
335
|
+
throw new Error('AvatarCropper.crop needs expo-image-manipulator. Install it with: ' + 'npx expo install expo-image-manipulator — or use getCrop() to crop elsewhere.');
|
|
336
|
+
}
|
|
337
|
+
// Redraw upright first. The turn and the cut both read raw pixels,
|
|
338
|
+
// and a camera photo often keeps its orientation in a flag instead.
|
|
339
|
+
const context = manipulator.manipulate(uri).resize({
|
|
340
|
+
width: sourceSize.width
|
|
341
|
+
});
|
|
342
|
+
if (rect.rotation !== 0) context.rotate(rect.rotation);
|
|
343
|
+
context.crop({
|
|
344
|
+
originX: rect.originX,
|
|
345
|
+
originY: rect.originY,
|
|
346
|
+
width: rect.width,
|
|
347
|
+
height: rect.height
|
|
348
|
+
});
|
|
349
|
+
if (side < rect.width) context.resize({
|
|
350
|
+
width: side,
|
|
351
|
+
height: side
|
|
352
|
+
});
|
|
353
|
+
const rendered = await context.renderAsync();
|
|
354
|
+
return rendered.saveAsync({
|
|
355
|
+
format: output,
|
|
356
|
+
compress
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}), [getCrop, settled, rotate, reset, sourceSize, uri, frame, shape, fill]);
|
|
360
|
+
const value = {
|
|
361
|
+
shape,
|
|
362
|
+
bounds,
|
|
363
|
+
fill,
|
|
364
|
+
stage,
|
|
365
|
+
inset,
|
|
366
|
+
source,
|
|
367
|
+
sourceSize,
|
|
368
|
+
frame,
|
|
369
|
+
canvas,
|
|
370
|
+
setCanvas,
|
|
371
|
+
x,
|
|
372
|
+
y,
|
|
373
|
+
scale,
|
|
374
|
+
rotation,
|
|
375
|
+
settleNow,
|
|
376
|
+
rotate,
|
|
377
|
+
reset,
|
|
378
|
+
zoomBy,
|
|
379
|
+
summary
|
|
380
|
+
};
|
|
381
|
+
return /*#__PURE__*/_jsx(CropperContext.Provider, {
|
|
382
|
+
value: value,
|
|
383
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
384
|
+
className: cn('flex-1', className),
|
|
385
|
+
...props,
|
|
386
|
+
children: children ?? /*#__PURE__*/_jsx(AvatarCropperCanvas, {})
|
|
387
|
+
})
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
const AvatarCropperForwarded = /*#__PURE__*/forwardRef(AvatarCropperRoot);
|
|
391
|
+
AvatarCropperForwarded.displayName = 'AvatarCropper';
|
|
392
|
+
/**
|
|
393
|
+
* The crop drawn on its own, for capture: a square of the background with the
|
|
394
|
+
* photo placed in it exactly as the canvas places it under the outline.
|
|
395
|
+
*
|
|
396
|
+
* It sits directly under the outline, at the outline's size, beneath the
|
|
397
|
+
* canvas's own drawing of the same thing — so it is never seen, whatever
|
|
398
|
+
* surrounds the cropper. The capture reads the view's own drawing, not what is
|
|
399
|
+
* on screen above it, and scales it to the size asked for.
|
|
400
|
+
*/
|
|
401
|
+
function ExportStage({
|
|
402
|
+
job,
|
|
403
|
+
frame,
|
|
404
|
+
uri,
|
|
405
|
+
fill,
|
|
406
|
+
left,
|
|
407
|
+
top
|
|
408
|
+
}) {
|
|
409
|
+
const ref = useRef(null);
|
|
410
|
+
const started = useRef(false);
|
|
411
|
+
const side = frame.mask;
|
|
412
|
+
const {
|
|
413
|
+
width,
|
|
414
|
+
height
|
|
415
|
+
} = frame.image;
|
|
416
|
+
const {
|
|
417
|
+
state
|
|
418
|
+
} = job;
|
|
419
|
+
const capture = () => {
|
|
420
|
+
if (started.current) return;
|
|
421
|
+
started.current = true;
|
|
422
|
+
job.capture(ref.current, {
|
|
423
|
+
format: job.format,
|
|
424
|
+
quality: job.compress,
|
|
425
|
+
result: 'tmpfile',
|
|
426
|
+
width: job.side,
|
|
427
|
+
height: job.side
|
|
428
|
+
}).then(uri => job.resolve({
|
|
429
|
+
uri,
|
|
430
|
+
width: job.side,
|
|
431
|
+
height: job.side
|
|
432
|
+
})).catch(job.reject);
|
|
433
|
+
};
|
|
434
|
+
return /*#__PURE__*/_jsx(View, {
|
|
435
|
+
ref: ref,
|
|
436
|
+
collapsable: false,
|
|
437
|
+
pointerEvents: "none",
|
|
438
|
+
importantForAccessibility: "no-hide-descendants",
|
|
439
|
+
accessibilityElementsHidden: true,
|
|
440
|
+
style: {
|
|
441
|
+
position: 'absolute',
|
|
442
|
+
left,
|
|
443
|
+
top,
|
|
444
|
+
width: side,
|
|
445
|
+
height: side,
|
|
446
|
+
overflow: 'hidden',
|
|
447
|
+
backgroundColor: fill
|
|
448
|
+
},
|
|
449
|
+
children: /*#__PURE__*/_jsx(View, {
|
|
450
|
+
style: {
|
|
451
|
+
position: 'absolute',
|
|
452
|
+
left: (side - width) / 2,
|
|
453
|
+
top: (side - height) / 2,
|
|
454
|
+
width,
|
|
455
|
+
height,
|
|
456
|
+
transform: [{
|
|
457
|
+
translateX: state.x
|
|
458
|
+
}, {
|
|
459
|
+
translateY: state.y
|
|
460
|
+
}, {
|
|
461
|
+
rotate: `${state.rotation}deg`
|
|
462
|
+
}, {
|
|
463
|
+
scale: state.scale
|
|
464
|
+
}]
|
|
465
|
+
},
|
|
466
|
+
children: /*#__PURE__*/_jsx(Image, {
|
|
467
|
+
source: {
|
|
468
|
+
uri
|
|
469
|
+
},
|
|
470
|
+
style: {
|
|
471
|
+
width,
|
|
472
|
+
height
|
|
473
|
+
},
|
|
474
|
+
resizeMode: "cover",
|
|
475
|
+
onLoad: () => requestAnimationFrame(capture),
|
|
476
|
+
onError: () => job.reject(new Error('AvatarCropper.crop: the photo failed to load.'))
|
|
477
|
+
})
|
|
478
|
+
})
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// ---------------------------------------------------------------------------
|
|
483
|
+
// Canvas
|
|
484
|
+
// ---------------------------------------------------------------------------
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The photo under its mask, and the surface every gesture lands on. It fills
|
|
488
|
+
* the space it is given, and the mask is drawn as large as the shorter side
|
|
489
|
+
* allows, less the root's `inset`.
|
|
490
|
+
*/
|
|
491
|
+
function AvatarCropperCanvas({
|
|
492
|
+
className,
|
|
493
|
+
accessibilityLabel = 'Photo crop',
|
|
494
|
+
onLayout,
|
|
495
|
+
...props
|
|
496
|
+
}) {
|
|
497
|
+
const {
|
|
498
|
+
shape,
|
|
499
|
+
bounds,
|
|
500
|
+
fill,
|
|
501
|
+
stage,
|
|
502
|
+
source,
|
|
503
|
+
frame,
|
|
504
|
+
canvas,
|
|
505
|
+
setCanvas,
|
|
506
|
+
x,
|
|
507
|
+
y,
|
|
508
|
+
scale,
|
|
509
|
+
rotation,
|
|
510
|
+
settleNow,
|
|
511
|
+
rotate,
|
|
512
|
+
zoomBy,
|
|
513
|
+
summary
|
|
514
|
+
} = useCropper('AvatarCropper.Canvas');
|
|
515
|
+
const scrimToken = useCSSVariable('--color-background');
|
|
516
|
+
const ringToken = useCSSVariable('--color-foreground');
|
|
517
|
+
const scrim = typeof scrimToken === 'string' ? scrimToken : '#0a0a0a';
|
|
518
|
+
const ring = typeof ringToken === 'string' ? ringToken : '#fafafa';
|
|
519
|
+
|
|
520
|
+
// How many of the three gestures are under way. The photo settles once the
|
|
521
|
+
// last of them lets go, not each time one does.
|
|
522
|
+
const active = useSharedValue(0);
|
|
523
|
+
const W = canvas?.width ?? 0;
|
|
524
|
+
const H = canvas?.height ?? 0;
|
|
525
|
+
const image = frame?.image ?? {
|
|
526
|
+
width: 0,
|
|
527
|
+
height: 0
|
|
528
|
+
};
|
|
529
|
+
const mask = frame?.mask ?? 0;
|
|
530
|
+
const shown = useDerivedValue(() => {
|
|
531
|
+
const raw = {
|
|
532
|
+
x: x.value,
|
|
533
|
+
y: y.value,
|
|
534
|
+
scale: scale.value,
|
|
535
|
+
rotation: rotation.value
|
|
536
|
+
};
|
|
537
|
+
return frame ? present(raw, bounds, frame) : raw;
|
|
538
|
+
});
|
|
539
|
+
const imageStyle = useAnimatedStyle(() => ({
|
|
540
|
+
transform: [{
|
|
541
|
+
translateX: shown.value.x
|
|
542
|
+
}, {
|
|
543
|
+
translateY: shown.value.y
|
|
544
|
+
}, {
|
|
545
|
+
rotate: `${shown.value.rotation}deg`
|
|
546
|
+
}, {
|
|
547
|
+
scale: shown.value.scale
|
|
548
|
+
}]
|
|
549
|
+
}));
|
|
550
|
+
const gesture = useMemo(() => {
|
|
551
|
+
const begin = () => {
|
|
552
|
+
'worklet';
|
|
553
|
+
|
|
554
|
+
if (active.value === 0) {
|
|
555
|
+
cancelAnimation(x);
|
|
556
|
+
cancelAnimation(y);
|
|
557
|
+
cancelAnimation(scale);
|
|
558
|
+
cancelAnimation(rotation);
|
|
559
|
+
}
|
|
560
|
+
active.value += 1;
|
|
561
|
+
};
|
|
562
|
+
const end = () => {
|
|
563
|
+
'worklet';
|
|
564
|
+
|
|
565
|
+
active.value = Math.max(0, active.value - 1);
|
|
566
|
+
if (active.value === 0) runOnJS(settleNow)();
|
|
567
|
+
};
|
|
568
|
+
/** Keep a raw value from running away while it is rubber-banded. */
|
|
569
|
+
const bound = state => {
|
|
570
|
+
'worklet';
|
|
571
|
+
|
|
572
|
+
if (!frame) return state;
|
|
573
|
+
// A guard against running away, not a bound: `present` does the bounding.
|
|
574
|
+
const lowest = 0.05;
|
|
575
|
+
const next = {
|
|
576
|
+
...state,
|
|
577
|
+
scale: Math.min(Math.max(state.scale, lowest), bounds.maxZoom * OVERSHOOT)
|
|
578
|
+
};
|
|
579
|
+
const limit = Math.max(image.width, image.height) * next.scale;
|
|
580
|
+
next.x = Math.min(Math.max(next.x, -limit), limit);
|
|
581
|
+
next.y = Math.min(Math.max(next.y, -limit), limit);
|
|
582
|
+
return next;
|
|
583
|
+
};
|
|
584
|
+
const write = state => {
|
|
585
|
+
'worklet';
|
|
586
|
+
|
|
587
|
+
const next = bound(state);
|
|
588
|
+
x.value = next.x;
|
|
589
|
+
y.value = next.y;
|
|
590
|
+
scale.value = next.scale;
|
|
591
|
+
rotation.value = next.rotation;
|
|
592
|
+
};
|
|
593
|
+
const current = () => {
|
|
594
|
+
'worklet';
|
|
595
|
+
|
|
596
|
+
return {
|
|
597
|
+
x: x.value,
|
|
598
|
+
y: y.value,
|
|
599
|
+
scale: scale.value,
|
|
600
|
+
rotation: rotation.value
|
|
601
|
+
};
|
|
602
|
+
};
|
|
603
|
+
const pan = Gesture.Pan().averageTouches(true).onBegin(() => {
|
|
604
|
+
'worklet';
|
|
605
|
+
|
|
606
|
+
begin();
|
|
607
|
+
}).onChange(event => {
|
|
608
|
+
'worklet';
|
|
609
|
+
|
|
610
|
+
const state = current();
|
|
611
|
+
write({
|
|
612
|
+
...state,
|
|
613
|
+
x: state.x + event.changeX,
|
|
614
|
+
y: state.y + event.changeY
|
|
615
|
+
});
|
|
616
|
+
}).onFinalize(() => {
|
|
617
|
+
'worklet';
|
|
618
|
+
|
|
619
|
+
end();
|
|
620
|
+
});
|
|
621
|
+
const pinch = Gesture.Pinch().onBegin(() => {
|
|
622
|
+
'worklet';
|
|
623
|
+
|
|
624
|
+
begin();
|
|
625
|
+
}).onChange(event => {
|
|
626
|
+
'worklet';
|
|
627
|
+
|
|
628
|
+
write(zoomAbout(current(), event.scaleChange, event.focalX - W / 2, event.focalY - H / 2));
|
|
629
|
+
}).onFinalize(() => {
|
|
630
|
+
'worklet';
|
|
631
|
+
|
|
632
|
+
end();
|
|
633
|
+
});
|
|
634
|
+
const turn = Gesture.Rotation().onBegin(() => {
|
|
635
|
+
'worklet';
|
|
636
|
+
|
|
637
|
+
begin();
|
|
638
|
+
}).onChange(event => {
|
|
639
|
+
'worklet';
|
|
640
|
+
|
|
641
|
+
write(rotateAbout(current(), event.rotationChange * 180 / Math.PI, event.anchorX - W / 2, event.anchorY - H / 2));
|
|
642
|
+
}).onFinalize(() => {
|
|
643
|
+
'worklet';
|
|
644
|
+
|
|
645
|
+
end();
|
|
646
|
+
});
|
|
647
|
+
return Gesture.Simultaneous(pan, pinch, turn);
|
|
648
|
+
}, [active, x, y, scale, rotation, settleNow, frame, image.width, image.height, bounds, W, H]);
|
|
649
|
+
const onAccessibilityAction = useCallback(event => {
|
|
650
|
+
switch (event.nativeEvent.actionName) {
|
|
651
|
+
case 'increment':
|
|
652
|
+
zoomBy(ZOOM_STEP);
|
|
653
|
+
break;
|
|
654
|
+
case 'decrement':
|
|
655
|
+
zoomBy(1 / ZOOM_STEP);
|
|
656
|
+
break;
|
|
657
|
+
case 'rotate':
|
|
658
|
+
rotate(1);
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
}, [zoomBy, rotate]);
|
|
662
|
+
const handleLayout = useCallback(event => {
|
|
663
|
+
const {
|
|
664
|
+
width,
|
|
665
|
+
height
|
|
666
|
+
} = event.nativeEvent.layout;
|
|
667
|
+
setCanvas({
|
|
668
|
+
width,
|
|
669
|
+
height
|
|
670
|
+
});
|
|
671
|
+
onLayout?.(event);
|
|
672
|
+
}, [setCanvas, onLayout]);
|
|
673
|
+
const outline = W > 0 && mask > 0 ? maskPath(shape, W / 2, H / 2, mask) : '';
|
|
674
|
+
return /*#__PURE__*/_jsx(GestureDetector, {
|
|
675
|
+
gesture: gesture,
|
|
676
|
+
children: /*#__PURE__*/_jsxs(View, {
|
|
677
|
+
className: cn('flex-1 overflow-hidden bg-background', className),
|
|
678
|
+
onLayout: handleLayout,
|
|
679
|
+
accessible: true,
|
|
680
|
+
accessibilityRole: "adjustable",
|
|
681
|
+
accessibilityLabel: accessibilityLabel,
|
|
682
|
+
accessibilityHint: "Drag to move the photo, pinch to zoom, and twist to turn it.",
|
|
683
|
+
accessibilityValue: {
|
|
684
|
+
text: summary
|
|
685
|
+
},
|
|
686
|
+
accessibilityActions: [{
|
|
687
|
+
name: 'increment',
|
|
688
|
+
label: 'Zoom in'
|
|
689
|
+
}, {
|
|
690
|
+
name: 'decrement',
|
|
691
|
+
label: 'Zoom out'
|
|
692
|
+
}, {
|
|
693
|
+
name: 'rotate',
|
|
694
|
+
label: 'Rotate'
|
|
695
|
+
}],
|
|
696
|
+
onAccessibilityAction: onAccessibilityAction,
|
|
697
|
+
...props,
|
|
698
|
+
children: [stage && frame && W > 0 ? /*#__PURE__*/_jsx(ExportStage, {
|
|
699
|
+
job: stage,
|
|
700
|
+
frame: frame,
|
|
701
|
+
uri: source.uri,
|
|
702
|
+
fill: fill,
|
|
703
|
+
left: (W - mask) / 2,
|
|
704
|
+
top: (H - mask) / 2
|
|
705
|
+
}) : null, !bounds.cover && frame && W > 0 && fill !== 'transparent' ? /*#__PURE__*/_jsx(View, {
|
|
706
|
+
pointerEvents: "none",
|
|
707
|
+
style: {
|
|
708
|
+
position: 'absolute',
|
|
709
|
+
left: (W - mask) / 2,
|
|
710
|
+
top: (H - mask) / 2,
|
|
711
|
+
width: mask,
|
|
712
|
+
height: mask,
|
|
713
|
+
backgroundColor: fill
|
|
714
|
+
}
|
|
715
|
+
}) : null, frame && W > 0 ? /*#__PURE__*/_jsx(Animated.View, {
|
|
716
|
+
pointerEvents: "none",
|
|
717
|
+
style: [{
|
|
718
|
+
position: 'absolute',
|
|
719
|
+
left: (W - image.width) / 2,
|
|
720
|
+
top: (H - image.height) / 2,
|
|
721
|
+
width: image.width,
|
|
722
|
+
height: image.height
|
|
723
|
+
}, imageStyle],
|
|
724
|
+
children: /*#__PURE__*/_jsx(Image, {
|
|
725
|
+
source: {
|
|
726
|
+
uri: source.uri
|
|
727
|
+
},
|
|
728
|
+
style: {
|
|
729
|
+
width: image.width,
|
|
730
|
+
height: image.height
|
|
731
|
+
},
|
|
732
|
+
resizeMode: "cover",
|
|
733
|
+
accessibilityIgnoresInvertColors: true
|
|
734
|
+
})
|
|
735
|
+
}) : null, outline ? /*#__PURE__*/_jsxs(Svg, {
|
|
736
|
+
width: W,
|
|
737
|
+
height: H,
|
|
738
|
+
style: {
|
|
739
|
+
position: 'absolute',
|
|
740
|
+
left: 0,
|
|
741
|
+
top: 0
|
|
742
|
+
},
|
|
743
|
+
pointerEvents: "none",
|
|
744
|
+
children: [/*#__PURE__*/_jsx(Path, {
|
|
745
|
+
d: `M0 0H${W}V${H}H0Z${outline}`,
|
|
746
|
+
fill: scrim,
|
|
747
|
+
fillOpacity: 0.78,
|
|
748
|
+
fillRule: "evenodd"
|
|
749
|
+
}), /*#__PURE__*/_jsx(Path, {
|
|
750
|
+
d: outline,
|
|
751
|
+
fill: "none",
|
|
752
|
+
stroke: ring,
|
|
753
|
+
strokeOpacity: 0.9,
|
|
754
|
+
strokeWidth: 1.5
|
|
755
|
+
})]
|
|
756
|
+
}) : null]
|
|
757
|
+
})
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
AvatarCropperCanvas.displayName = 'AvatarCropper.Canvas';
|
|
761
|
+
|
|
762
|
+
// ---------------------------------------------------------------------------
|
|
763
|
+
// Controls
|
|
764
|
+
// ---------------------------------------------------------------------------
|
|
765
|
+
|
|
766
|
+
/** A quarter turn per press, about the middle of the mask. */
|
|
767
|
+
function AvatarCropperRotate({
|
|
768
|
+
direction = 'clockwise',
|
|
769
|
+
variant = 'secondary',
|
|
770
|
+
size = 'icon',
|
|
771
|
+
children,
|
|
772
|
+
accessibilityLabel,
|
|
773
|
+
...props
|
|
774
|
+
}) {
|
|
775
|
+
const {
|
|
776
|
+
rotate
|
|
777
|
+
} = useCropper('AvatarCropper.Rotate');
|
|
778
|
+
const clockwise = direction === 'clockwise';
|
|
779
|
+
return /*#__PURE__*/_jsx(Button, {
|
|
780
|
+
variant: variant,
|
|
781
|
+
size: size,
|
|
782
|
+
accessibilityLabel: accessibilityLabel ?? (clockwise ? 'Rotate clockwise' : 'Rotate counterclockwise'),
|
|
783
|
+
onPress: () => rotate(clockwise ? 1 : -1),
|
|
784
|
+
...props,
|
|
785
|
+
children: children ?? /*#__PURE__*/_jsx(View, {
|
|
786
|
+
style: clockwise ? undefined : {
|
|
787
|
+
transform: [{
|
|
788
|
+
scaleX: -1
|
|
789
|
+
}]
|
|
790
|
+
},
|
|
791
|
+
children: /*#__PURE__*/_jsx(RotateSquareIcon, {
|
|
792
|
+
size: 20
|
|
793
|
+
})
|
|
794
|
+
})
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
AvatarCropperRotate.displayName = 'AvatarCropper.Rotate';
|
|
798
|
+
/** Back to the photo centred, unzoomed and upright. Labelled "Reset" unless given children. */
|
|
799
|
+
function AvatarCropperReset({
|
|
800
|
+
variant = 'ghost',
|
|
801
|
+
size = 'sm',
|
|
802
|
+
children = 'Reset',
|
|
803
|
+
...props
|
|
804
|
+
}) {
|
|
805
|
+
const {
|
|
806
|
+
reset
|
|
807
|
+
} = useCropper('AvatarCropper.Reset');
|
|
808
|
+
return /*#__PURE__*/_jsx(Button, {
|
|
809
|
+
variant: variant,
|
|
810
|
+
size: size,
|
|
811
|
+
onPress: reset,
|
|
812
|
+
...props,
|
|
813
|
+
children: children
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
AvatarCropperReset.displayName = 'AvatarCropper.Reset';
|
|
817
|
+
export const AvatarCropper = Object.assign(AvatarCropperForwarded, {
|
|
818
|
+
Canvas: AvatarCropperCanvas,
|
|
819
|
+
Rotate: AvatarCropperRotate,
|
|
820
|
+
Reset: AvatarCropperReset
|
|
821
|
+
});
|
|
822
|
+
//# sourceMappingURL=index.js.map
|