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,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The geometry behind the avatar cropper, kept apart from the component.
|
|
3
|
+
*
|
|
4
|
+
* The mask stays fixed in the middle of the canvas and the photo moves under
|
|
5
|
+
* it, so everything here is one question asked different ways: where is the
|
|
6
|
+
* mask, measured in the photo's own frame? Clamping asks it to keep the mask
|
|
7
|
+
* on the photo — or, when the photo may be smaller than the mask, the photo
|
|
8
|
+
* inside the mask — and the export asks it to find the same square in the
|
|
9
|
+
* photo's pixels.
|
|
10
|
+
*
|
|
11
|
+
* The photo is drawn at a base size whose short side equals the mask, then
|
|
12
|
+
* moved by `x`/`y`, turned by `rotation` and scaled by `scale` about its own
|
|
13
|
+
* centre — the order React Native applies `translate`, `rotate`, `scale` in a
|
|
14
|
+
* transform. Every function runs on the UI thread inside a gesture or an
|
|
15
|
+
* animated style, so each is a worklet, and none touches React Native, which
|
|
16
|
+
* is what lets the contract tests run them in Node.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type CropShape = 'circle' | 'squircle' | 'square';
|
|
20
|
+
|
|
21
|
+
export interface Size {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Where the photo sits under the mask. Rotation is in degrees, clockwise. */
|
|
27
|
+
export interface CropState {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
scale: number;
|
|
31
|
+
rotation: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The fixed parts: the mask's side, and the photo's size at a scale of 1. */
|
|
35
|
+
export interface CropFrame {
|
|
36
|
+
mask: number;
|
|
37
|
+
image: Size;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A crop in the source's pixels, after the source is turned by `rotation`. */
|
|
41
|
+
export interface SourceCrop {
|
|
42
|
+
/** The rectangle to cut, in the turned image's pixels. Always square. */
|
|
43
|
+
originX: number;
|
|
44
|
+
originY: number;
|
|
45
|
+
width: number;
|
|
46
|
+
height: number;
|
|
47
|
+
/** Degrees to turn the source by first, clockwise, in (-180, 180]. */
|
|
48
|
+
rotation: number;
|
|
49
|
+
/** The size of the turned image the rectangle is measured in. */
|
|
50
|
+
canvas: Size;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const IDENTITY: CropState = { x: 0, y: 0, scale: 1, rotation: 0 };
|
|
54
|
+
|
|
55
|
+
/** How much a rubber-banded overshoot gives. Lower is stiffer. */
|
|
56
|
+
const RUBBER_BAND = 0.55;
|
|
57
|
+
|
|
58
|
+
/** Below this a sine or cosine is taken as zero, so a quarter turn is exact. */
|
|
59
|
+
const EPSILON = 1e-9;
|
|
60
|
+
|
|
61
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
62
|
+
'worklet';
|
|
63
|
+
return Math.min(Math.max(value, min), max);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function trig(degrees: number): { cos: number; sin: number } {
|
|
67
|
+
'worklet';
|
|
68
|
+
const radians = (degrees * Math.PI) / 180;
|
|
69
|
+
let cos = Math.cos(radians);
|
|
70
|
+
let sin = Math.sin(radians);
|
|
71
|
+
if (Math.abs(cos) < EPSILON) cos = 0;
|
|
72
|
+
if (Math.abs(sin) < EPSILON) sin = 0;
|
|
73
|
+
return { cos, sin };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** An angle folded into (-180, 180]. */
|
|
77
|
+
export function normalizeDegrees(degrees: number): number {
|
|
78
|
+
'worklet';
|
|
79
|
+
let folded = degrees % 360;
|
|
80
|
+
if (folded <= -180) folded += 360;
|
|
81
|
+
if (folded > 180) folded -= 360;
|
|
82
|
+
return folded;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The photo's size at a scale of 1: its short side exactly covers the mask. */
|
|
86
|
+
export function baseSize(source: Size, mask: number): Size {
|
|
87
|
+
'worklet';
|
|
88
|
+
const shortSide = Math.min(source.width, source.height);
|
|
89
|
+
if (shortSide <= 0 || mask <= 0) return { width: mask, height: mask };
|
|
90
|
+
const factor = mask / shortSide;
|
|
91
|
+
return { width: source.width * factor, height: source.height * factor };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Half the mask's extent along the photo's axes, in screen points.
|
|
96
|
+
*
|
|
97
|
+
* A circle reaches the same distance whichever way the photo is turned. A
|
|
98
|
+
* square turned against the photo reaches further — out to its corners — and
|
|
99
|
+
* the squircle is held to its square, which contains it, so its corners can
|
|
100
|
+
* never come off the photo either.
|
|
101
|
+
*/
|
|
102
|
+
export function maskReach(shape: CropShape, mask: number, rotation: number): number {
|
|
103
|
+
'worklet';
|
|
104
|
+
const half = mask / 2;
|
|
105
|
+
if (shape === 'circle') return half;
|
|
106
|
+
const { cos, sin } = trig(rotation);
|
|
107
|
+
return half * (Math.abs(cos) + Math.abs(sin));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The limits a crop is held to.
|
|
112
|
+
*
|
|
113
|
+
* With `cover` on, the photo always fills the mask. With it off, the photo may
|
|
114
|
+
* also be smaller than the mask, down to `minZoom` — its long side as a
|
|
115
|
+
* fraction of the mask's — and a smaller photo is kept inside the mask rather
|
|
116
|
+
* than on it.
|
|
117
|
+
*/
|
|
118
|
+
export interface CropBounds {
|
|
119
|
+
shape: CropShape;
|
|
120
|
+
maxZoom: number;
|
|
121
|
+
cover: boolean;
|
|
122
|
+
minZoom: number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The smallest scale the photo may settle at. */
|
|
126
|
+
export function minScale(bounds: CropBounds, frame: CropFrame, rotation: number): number {
|
|
127
|
+
'worklet';
|
|
128
|
+
// The base size's short side is the mask, so covering the reach on the
|
|
129
|
+
// short side covers it on the long one too.
|
|
130
|
+
const covering = maskReach(bounds.shape, 2, rotation);
|
|
131
|
+
if (bounds.cover) return covering;
|
|
132
|
+
const longSide = Math.max(frame.image.width, frame.image.height);
|
|
133
|
+
if (longSide <= 0) return covering;
|
|
134
|
+
return Math.min(covering, (bounds.minZoom * frame.mask) / longSide);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Screen offset → the same offset along the photo's axes, and back. */
|
|
138
|
+
function toImageAxes(x: number, y: number, rotation: number): { u: number; v: number } {
|
|
139
|
+
'worklet';
|
|
140
|
+
const { cos, sin } = trig(rotation);
|
|
141
|
+
return { u: cos * x + sin * y, v: -sin * x + cos * y };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function fromImageAxes(u: number, v: number, rotation: number): { x: number; y: number } {
|
|
145
|
+
'worklet';
|
|
146
|
+
const { cos, sin } = trig(rotation);
|
|
147
|
+
return { x: cos * u - sin * v, y: sin * u + cos * v };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* How far the photo can move along each of its own axes.
|
|
152
|
+
*
|
|
153
|
+
* On an axis where the photo is larger than the mask's reach, the mask has to
|
|
154
|
+
* stay on the photo. On one where it is smaller — only possible with `cover`
|
|
155
|
+
* off — the photo has to stay inside the reach instead. Both are the gap
|
|
156
|
+
* between the two half-lengths, which is why one expression covers both.
|
|
157
|
+
*/
|
|
158
|
+
function travel(state: CropState, bounds: CropBounds, frame: CropFrame): { u: number; v: number } {
|
|
159
|
+
'worklet';
|
|
160
|
+
const reach = maskReach(bounds.shape, frame.mask, state.rotation);
|
|
161
|
+
const u = (state.scale * frame.image.width) / 2 - reach;
|
|
162
|
+
const v = (state.scale * frame.image.height) / 2 - reach;
|
|
163
|
+
return bounds.cover
|
|
164
|
+
? { u: Math.max(0, u), v: Math.max(0, v) }
|
|
165
|
+
: { u: Math.abs(u), v: Math.abs(v) };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The nearest translation inside the bounds. */
|
|
169
|
+
export function clampTranslation(
|
|
170
|
+
state: CropState,
|
|
171
|
+
bounds: CropBounds,
|
|
172
|
+
frame: CropFrame
|
|
173
|
+
): { x: number; y: number } {
|
|
174
|
+
'worklet';
|
|
175
|
+
const limit = travel(state, bounds, frame);
|
|
176
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
177
|
+
return fromImageAxes(
|
|
178
|
+
clamp(along.u, -limit.u, limit.u),
|
|
179
|
+
clamp(along.v, -limit.v, limit.v),
|
|
180
|
+
state.rotation
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The state a gesture settles into: in range, and inside the bounds. */
|
|
185
|
+
export function settle(state: CropState, bounds: CropBounds, frame: CropFrame): CropState {
|
|
186
|
+
'worklet';
|
|
187
|
+
const lowest = minScale(bounds, frame, state.rotation);
|
|
188
|
+
const scale = clamp(state.scale, lowest, Math.max(lowest, bounds.maxZoom));
|
|
189
|
+
const next = { ...state, scale };
|
|
190
|
+
return { ...next, ...clampTranslation(next, bounds, frame) };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* True when every point of the mask is on the photo — when a crop needs no
|
|
195
|
+
* background at all. Always true with `cover` on, once settled.
|
|
196
|
+
*/
|
|
197
|
+
export function coversMask(state: CropState, shape: CropShape, frame: CropFrame): boolean {
|
|
198
|
+
'worklet';
|
|
199
|
+
const reach = maskReach(shape, frame.mask, state.rotation);
|
|
200
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
201
|
+
const slack = 0.5;
|
|
202
|
+
return (
|
|
203
|
+
Math.abs(along.u) <= (state.scale * frame.image.width) / 2 - reach + slack &&
|
|
204
|
+
Math.abs(along.v) <= (state.scale * frame.image.height) / 2 - reach + slack
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** An overshoot past a bound, compressed so it gives but never runs away. */
|
|
209
|
+
export function rubberBand(overshoot: number, dimension: number): number {
|
|
210
|
+
'worklet';
|
|
211
|
+
if (dimension <= 0) return 0;
|
|
212
|
+
const sign = overshoot < 0 ? -1 : 1;
|
|
213
|
+
const distance = Math.abs(overshoot);
|
|
214
|
+
return sign * (1 - 1 / ((distance * RUBBER_BAND) / dimension + 1)) * dimension;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* What to draw while a gesture is still going: the raw state, with anything
|
|
219
|
+
* past a bound rubber-banded back towards it. At rest the raw state is already
|
|
220
|
+
* settled and this returns it unchanged.
|
|
221
|
+
*/
|
|
222
|
+
export function present(state: CropState, bounds: CropBounds, frame: CropFrame): CropState {
|
|
223
|
+
'worklet';
|
|
224
|
+
const lowest = minScale(bounds, frame, state.rotation);
|
|
225
|
+
const highest = Math.max(lowest, bounds.maxZoom);
|
|
226
|
+
let scale = state.scale;
|
|
227
|
+
if (scale < lowest) scale = lowest - rubberBand(lowest - scale, lowest / 2);
|
|
228
|
+
else if (scale > highest) scale = highest + rubberBand(scale - highest, highest / 2);
|
|
229
|
+
|
|
230
|
+
const next = { ...state, scale };
|
|
231
|
+
const limit = travel(next, bounds, frame);
|
|
232
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
233
|
+
const u = clamp(along.u, -limit.u, limit.u);
|
|
234
|
+
const v = clamp(along.v, -limit.v, limit.v);
|
|
235
|
+
const offset = fromImageAxes(
|
|
236
|
+
u + rubberBand(along.u - u, frame.mask),
|
|
237
|
+
v + rubberBand(along.v - v, frame.mask),
|
|
238
|
+
state.rotation
|
|
239
|
+
);
|
|
240
|
+
return { ...next, ...offset };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Scale by `factor` about a point, given relative to the canvas centre. */
|
|
244
|
+
export function zoomAbout(state: CropState, factor: number, fx: number, fy: number): CropState {
|
|
245
|
+
'worklet';
|
|
246
|
+
return {
|
|
247
|
+
...state,
|
|
248
|
+
scale: state.scale * factor,
|
|
249
|
+
x: fx + (state.x - fx) * factor,
|
|
250
|
+
y: fy + (state.y - fy) * factor,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Turn by `degrees` about a point, given relative to the canvas centre. */
|
|
255
|
+
export function rotateAbout(state: CropState, degrees: number, ax: number, ay: number): CropState {
|
|
256
|
+
'worklet';
|
|
257
|
+
const { cos, sin } = trig(degrees);
|
|
258
|
+
const dx = state.x - ax;
|
|
259
|
+
const dy = state.y - ay;
|
|
260
|
+
return {
|
|
261
|
+
...state,
|
|
262
|
+
rotation: state.rotation + degrees,
|
|
263
|
+
x: ax + cos * dx - sin * dy,
|
|
264
|
+
y: ay + sin * dx + cos * dy,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The next quarter turn from wherever the photo is, in either direction. */
|
|
269
|
+
export function nextQuarterTurn(rotation: number, direction: 1 | -1): number {
|
|
270
|
+
'worklet';
|
|
271
|
+
const quarters = rotation / 90;
|
|
272
|
+
const nearest = Math.round(quarters);
|
|
273
|
+
// Already on a quarter turn: step a whole one. Part way: finish this one.
|
|
274
|
+
const onQuarter = Math.abs(quarters - nearest) < 1e-6;
|
|
275
|
+
const base = onQuarter ? nearest : direction > 0 ? Math.floor(quarters) : Math.ceil(quarters);
|
|
276
|
+
return (base + direction) * 90;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The square the mask covers, in the source's pixels.
|
|
281
|
+
*
|
|
282
|
+
* The export turns the source first and cuts an upright square from the
|
|
283
|
+
* result, because a turned square cannot be cut directly. Turning grows the
|
|
284
|
+
* canvas to the turned image's bounding box with the image still centred, and
|
|
285
|
+
* a point at `p` from the source's centre lands at `R(θ)·p` from the canvas
|
|
286
|
+
* centre. The mask's centre sits at `R(−θ)·(−x, −y) / scale` in the photo, so
|
|
287
|
+
* after the turn it lands at `−(x, y) / scale` — the rotation cancels, and the
|
|
288
|
+
* crop is the mask's offset scaled into pixels.
|
|
289
|
+
*/
|
|
290
|
+
export function sourceCrop(
|
|
291
|
+
state: CropState,
|
|
292
|
+
frame: CropFrame,
|
|
293
|
+
source: Size,
|
|
294
|
+
/** Keep the rectangle on the turned image. Off, it can reach past the edges. */
|
|
295
|
+
onImage = true
|
|
296
|
+
): SourceCrop {
|
|
297
|
+
'worklet';
|
|
298
|
+
const rotation = normalizeDegrees(state.rotation);
|
|
299
|
+
const { cos, sin } = trig(rotation);
|
|
300
|
+
// Both platforms round the turned canvas down to whole pixels.
|
|
301
|
+
const canvas = {
|
|
302
|
+
width: Math.floor(Math.abs(source.width * cos) + Math.abs(source.height * sin) + 1e-6),
|
|
303
|
+
height: Math.floor(Math.abs(source.width * sin) + Math.abs(source.height * cos) + 1e-6),
|
|
304
|
+
};
|
|
305
|
+
// Screen points per source pixel, at the current scale.
|
|
306
|
+
const pointsPerPixel = (frame.image.width / source.width) * state.scale;
|
|
307
|
+
const full = Math.max(1, Math.floor(frame.mask / pointsPerPixel));
|
|
308
|
+
const side = onImage ? Math.max(1, Math.min(full, canvas.width, canvas.height)) : full;
|
|
309
|
+
const centreX = canvas.width / 2 - state.x / pointsPerPixel;
|
|
310
|
+
const centreY = canvas.height / 2 - state.y / pointsPerPixel;
|
|
311
|
+
const originX = Math.round(centreX - side / 2);
|
|
312
|
+
const originY = Math.round(centreY - side / 2);
|
|
313
|
+
return {
|
|
314
|
+
originX: onImage ? clamp(originX, 0, canvas.width - side) : originX,
|
|
315
|
+
originY: onImage ? clamp(originY, 0, canvas.height - side) : originY,
|
|
316
|
+
width: side,
|
|
317
|
+
height: side,
|
|
318
|
+
rotation,
|
|
319
|
+
canvas,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Superellipse exponent for the squircle. Four is soft; five reads as an app icon. */
|
|
324
|
+
const SQUIRCLE_EXPONENT = 5;
|
|
325
|
+
const SQUIRCLE_STEPS = 128;
|
|
326
|
+
|
|
327
|
+
/** The mask's outline as an SVG path, centred on (`cx`, `cy`). */
|
|
328
|
+
export function maskPath(shape: CropShape, cx: number, cy: number, mask: number): string {
|
|
329
|
+
const r = mask / 2;
|
|
330
|
+
if (shape === 'square') {
|
|
331
|
+
return `M${cx - r} ${cy - r}H${cx + r}V${cy + r}H${cx - r}Z`;
|
|
332
|
+
}
|
|
333
|
+
if (shape === 'circle') {
|
|
334
|
+
return `M${cx - r} ${cy}A${r} ${r} 0 1 0 ${cx + r} ${cy}A${r} ${r} 0 1 0 ${cx - r} ${cy}Z`;
|
|
335
|
+
}
|
|
336
|
+
const exponent = 2 / SQUIRCLE_EXPONENT;
|
|
337
|
+
let path = '';
|
|
338
|
+
for (let step = 0; step < SQUIRCLE_STEPS; step += 1) {
|
|
339
|
+
const t = (step / SQUIRCLE_STEPS) * Math.PI * 2;
|
|
340
|
+
const cos = Math.cos(t);
|
|
341
|
+
const sin = Math.sin(t);
|
|
342
|
+
const x = cx + r * Math.sign(cos) * Math.abs(cos) ** exponent;
|
|
343
|
+
const y = cy + r * Math.sign(sin) * Math.abs(sin) ** exponent;
|
|
344
|
+
path += `${step === 0 ? 'M' : 'L'}${x.toFixed(2)} ${y.toFixed(2)}`;
|
|
345
|
+
}
|
|
346
|
+
return `${path}Z`;
|
|
347
|
+
}
|