-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBarcodeMask.tsx
478 lines (456 loc) · 11.9 KB
/
BarcodeMask.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import React, { FC, memo } from 'react';
import { LayoutChangeEvent, StyleSheet, View, ViewStyle } from 'react-native';
import Animated, { Easing } from 'react-native-reanimated';
import { WithOuterLayoutProps } from './interfaces';
const { Value, Clock, block, cond, set, startClock, timing, eq } = Animated;
type DimensionUnit = string | number;
type EdgePosition = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
/**
*
*/
export type RunTimingFn = (
clock: Animated.Clock,
value: number,
destination: number,
duration: number
) => Animated.Node<number>;
export type OnLayoutChangeHandler = (event: LayoutChangeEvent) => void;
/**
* @name BarcodeMaskProps
* @description Props of BarcodeMask component
*/
export interface BarcodeMaskProps extends WithOuterLayoutProps {
/**
* @name width
* @type DimensionUnit
* @description Width of the Barcode Finder Area
* @default 280
*/
width?: DimensionUnit;
/**
* @name height
* @type DimensionUnit
* @description Height of the Barcode Finder Area
* @default 230
*/
height?: DimensionUnit;
/**
* @name edgeWidth
* @type number
* @description Width of corner edges
* @default 20
*/
edgeWidth?: number;
/**
* @name edgeHeight
* @type number
* @description Height of corner edges
* @default 20
*/
edgeHeight?: DimensionUnit;
/**
* @name edgeColor
* @type string
* @description Color of corner edges
* @default #fff
*/
edgeColor?: string;
/**
* @name edgeRadius
* @type number
* @description Border Radius of corner edges
* @default 0
*/
edgeRadius?: number;
/**
* @name edgeBorderWidth
* @type DimensionUnit
* @description Thickness of corner edges
* @default 4
*/
edgeBorderWidth?: DimensionUnit;
/**
* @name backgroundColor
* @type string
* @description Background color of Outer Finder Area
* @default #eee
*/
backgroundColor?: string;
/**
* @name maskOpacity
* @type number
* @description Opacity of Outer Finder Area
* @default 1
*/
maskOpacity?: number;
/**
* @name showAnimatedLine
* @type boolean
* @description Whether to show Animated Line
* @default true
*/
showAnimatedLine?: boolean;
/**
* @name startValue
* @type number
* @description Start value of Animated Line (only applicable if `showAnimatedLine` is set to `true`)
* @default 0
*/
startValue?: number;
/**
* @name destinationValue
* @type number
* @description Destination value of Animated Line (depends on`animatedLineOrientation`)
* @default Computed by the length of the respective orientation minus the `animatedLineThickness`
*/
destinationValue?: number;
/**
* @name animatedComponent
* @type Function
* @param width
* @param height
*/
animatedComponent?: (width: number, height: number) => React.ReactElement;
/**
* @name animatedLineThickness
* @type number
* @description Thickness of Animated Line
* @default 2
*/
animatedLineThickness?: number;
/**
* @name animatedLineOrientation
* @type 'vertical' | 'horizontal'
* @description Orientation that the Animated Line will be drawn
* @default 'horizontal'
*/
animatedLineOrientation?: 'vertical' | 'horizontal';
/**
* @name animatedLineColor
* @type string
* @description Color of Animated Line
* @default #fff
*/
animatedLineColor?: string;
/**
* @name animationDuration
* @type number
* @description Duration of Animated Line animation (in ms)
* @default 2000
*/
animationDuration?: number;
/**
* @name runTimingFn
* @type RunTimingFn
* @description Function to trigger the animation
* @default internal `runTiming` function
*/
runTimingFn?: RunTimingFn;
/**
* @name onLayoutChange
* @type OnLayoutChangeHandler
* @description Handler to handle LayoutChange. Useful if you want to only detect barcode inside the Finder Area.
* This will be provided from a custom hook that this library also provides.
* @default noop
*/
onLayoutChange?: OnLayoutChangeHandler;
}
const runTiming: RunTimingFn = (
clock: Animated.Clock,
value: number,
destination: number,
duration: number
) => {
const timingState: Animated.TimingState = {
finished: new Value(0),
position: new Value(value),
time: new Value(0),
frameTime: new Value(0),
};
const timingConfig: Animated.TimingConfig = {
duration,
toValue: new Value(destination),
easing: Easing.inOut(Easing.ease),
};
return block([
startClock(clock),
timing(clock, timingState, timingConfig),
cond(timingState.finished, [
set(timingState.finished, 0),
set(timingState.time, 0),
set(timingState.frameTime, 0),
set(
timingState.position,
cond(eq(timingState.position, destination), destination, value)
),
set(
timingConfig.toValue as Animated.Value<number>,
cond(eq(timingState.position, destination), value, destination)
),
]),
timingState.position,
]);
};
const noop = () => {};
export const BarcodeMask: FC<BarcodeMaskProps> = memo(
({
width,
height,
startValue,
destinationValue,
backgroundColor,
edgeBorderWidth,
edgeColor,
edgeHeight,
edgeWidth,
edgeRadius,
maskOpacity,
animatedComponent,
animatedLineColor,
animatedLineOrientation,
animatedLineThickness,
animationDuration,
showAnimatedLine,
runTimingFn,
onLayoutChange,
outerBoundingRect,
onOuterLayout,
}) => {
const edgeBorderStyle = React.useRef<
{
[position in EdgePosition]: ViewStyle;
}
>({
topRight: {
borderRightWidth: edgeBorderWidth as number,
borderTopWidth: edgeBorderWidth as number,
borderTopRightRadius: edgeRadius,
top: -(edgeBorderWidth as number),
right: -(edgeBorderWidth as number),
},
topLeft: {
borderTopWidth: edgeBorderWidth as number,
borderLeftWidth: edgeBorderWidth as number,
borderTopLeftRadius: edgeRadius,
top: -(edgeBorderWidth as number),
left: -(edgeBorderWidth as number),
},
bottomRight: {
borderBottomWidth: edgeBorderWidth as number,
borderRightWidth: edgeBorderWidth as number,
borderBottomRightRadius: edgeRadius,
bottom: -(edgeBorderWidth as number),
right: -(edgeBorderWidth as number),
},
bottomLeft: {
borderBottomWidth: edgeBorderWidth as number,
borderLeftWidth: edgeBorderWidth as number,
borderBottomLeftRadius: edgeRadius,
bottom: -(edgeBorderWidth as number),
left: -(edgeBorderWidth as number),
},
});
const _animatedLineDimension = (
dimension: DimensionUnit | undefined,
outerDimension: 'width' | 'height'
) => {
const outer = outerBoundingRect?.[outerDimension] ?? 0;
if (dimension) {
if (typeof dimension === 'number') {
return dimension * 0.9;
}
return dimension.endsWith('%')
? (Number(dimension.split('%')[0]) / 100) * (outer || 1) * 0.9
: Number(dimension.split(/\d+/)[0]) * (outer || 1) * 0.9;
}
return outer * 0.9;
};
const _animatedValue = (
dimension: DimensionUnit | undefined,
outerDimension: 'width' | 'height'
) => {
const calculatedDimension = _animatedLineDimension(
dimension,
outerDimension
);
const fullDimension = calculatedDimension / 0.9;
return fullDimension - (animatedLineThickness as number);
};
const _animatedLineStyle = () => {
if (animatedLineOrientation === 'horizontal') {
const _width = _animatedLineDimension(width, 'width');
const destination = _animatedValue(height, 'height');
return {
...styles.animatedLine,
height: animatedLineThickness,
width: _width,
backgroundColor: animatedLineColor,
top: runTimingFn!(
new Clock(),
startValue || 0,
destinationValue || destination,
animationDuration as number
),
};
}
const _height = _animatedLineDimension(height, 'height');
const destination = _animatedValue(width, 'width');
return {
...styles.animatedLine,
width: animatedLineThickness,
height: _height,
backgroundColor: animatedLineColor,
left: runTimingFn!(
new Clock(),
startValue || 0,
destinationValue || destination,
animationDuration as number
),
};
};
const _renderEdge = (edgePosition: EdgePosition) => {
const defaultStyle = {
width: edgeWidth,
height: edgeHeight,
borderColor: edgeColor,
zIndex: 2,
};
return (
<View
style={{
...defaultStyle,
...styles[edgePosition],
...edgeBorderStyle.current[edgePosition],
}}
/>
);
};
const _width = _animatedLineDimension(width, 'width') / 0.9;
const _height = _animatedLineDimension(height, 'height') / 0.9;
const _renderAnimated = () => {
if (showAnimatedLine) {
if (animatedComponent) {
return animatedComponent(_width, _height);
}
return <Animated.View style={_animatedLineStyle()} />;
}
return null;
};
return (
<View style={styles.container}>
<View
onLayout={onLayoutChange || noop}
style={{
...styles.finder,
width: _width,
height: _height,
}}
>
{_renderEdge('topLeft')}
{_renderEdge('topRight')}
{_renderEdge('bottomLeft')}
{_renderEdge('bottomRight')}
{_renderAnimated()}
</View>
<View style={styles.maskOuter} onLayout={onOuterLayout || noop}>
<View
style={{
...styles.maskRow,
...{ backgroundColor, opacity: maskOpacity, flex: 1 },
}}
/>
<View style={{ height, ...styles.maskCenter }}>
<View style={{ backgroundColor, opacity: maskOpacity, flex: 1 }} />
<View
style={{
...styles.maskInner,
width,
height,
borderRadius: edgeRadius,
}}
/>
<View style={{ backgroundColor, opacity: maskOpacity, flex: 1 }} />
</View>
<View
style={{
...styles.maskRow,
...{ backgroundColor, opacity: maskOpacity, flex: 1 },
}}
/>
</View>
</View>
);
}
);
BarcodeMask.defaultProps = {
width: 280,
height: 230,
edgeWidth: 20,
edgeHeight: 20,
edgeColor: '#fff',
edgeBorderWidth: 4,
edgeRadius: 0,
backgroundColor: '#eee',
maskOpacity: 1,
animatedLineColor: '#fff',
animatedLineOrientation: 'horizontal',
animatedLineThickness: 2,
animationDuration: 2000,
runTimingFn: runTiming,
showAnimatedLine: true,
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
...StyleSheet.absoluteFillObject,
},
finder: {
alignItems: 'center',
justifyContent: 'center',
zIndex: 1,
},
maskOuter: {
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'space-around',
},
maskInner: {
backgroundColor: 'transparent',
},
maskRow: {
width: '100%',
},
maskCenter: {
flexDirection: 'row',
display: 'flex',
},
topLeft: {
position: 'absolute',
top: 0,
left: 0,
},
topRight: {
position: 'absolute',
top: 0,
right: 0,
},
bottomLeft: {
position: 'absolute',
bottom: 0,
left: 0,
},
bottomRight: {
position: 'absolute',
bottom: 0,
right: 0,
},
animatedLine: {
position: 'absolute',
zIndex: 1,
},
});