react-native-kenburns-view 5.1.1 → 5.2.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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) Nimila Hiranya Samarasinghe
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md CHANGED
@@ -20,7 +20,9 @@
20
20
 
21
21
  **Lightweight:** No native code or custom native modules. It uses only React Native’s built-in `Animated` API and core components, so it works with standard RN and Expo and adds minimal bundle size. Each instance uses randomized timing and pan direction so multiple images don’t move in lockstep.
22
22
 
23
- Version: 5.1.1
23
+ For the **React (web)** version of this effect, see [react-kenburns-view](https://github.com/nHiRanZ/react-kenburns-view).
24
+
25
+ Version: 5.2.0
24
26
 
25
27
  ## Supported React Native versions
26
28
 
@@ -13,6 +13,7 @@ import React, {
13
13
  useEffect,
14
14
  useImperativeHandle,
15
15
  useRef,
16
+ useState,
16
17
  } from 'react';
17
18
  import PropTypes from 'prop-types';
18
19
  import {
@@ -26,6 +27,17 @@ import {
26
27
  const DEFAULT_DURATION = 20000;
27
28
  const FILL_SCALE = 1.15;
28
29
 
30
+ function getRandomCycle(zoomStart, zoomEnd, panXAmount, panYAmount) {
31
+ return {
32
+ scaleFrom: zoomStart + Math.random() * (zoomEnd - zoomStart),
33
+ scaleTo: zoomStart + Math.random() * (zoomEnd - zoomStart),
34
+ txFrom: -panXAmount + Math.random() * 2 * panXAmount,
35
+ txTo: -panXAmount + Math.random() * 2 * panXAmount,
36
+ tyFrom: -panYAmount + Math.random() * 2 * panYAmount,
37
+ tyTo: -panYAmount + Math.random() * 2 * panYAmount,
38
+ };
39
+ }
40
+
29
41
  const KenBurnsView = forwardRef((props, ref) => {
30
42
  const {
31
43
  imageWidth,
@@ -43,14 +55,24 @@ const KenBurnsView = forwardRef((props, ref) => {
43
55
  ...rest
44
56
  } = props;
45
57
 
58
+ const w = imageWidth || 0;
59
+ const h = imageHeight || 0;
60
+ const panXAmount = w * panX;
61
+ const panYAmount = h * panY;
62
+
46
63
  const progress = useRef(new Animated.Value(0)).current;
47
64
  const animationRef = useRef(null);
65
+ const configRef = useRef({ zoomStart, zoomEnd, panXAmount, panYAmount });
66
+ configRef.current = { zoomStart, zoomEnd, panXAmount, panYAmount };
67
+
68
+ const [cycle, setCycle] = useState(() =>
69
+ getRandomCycle(zoomStart, zoomEnd, panXAmount, panYAmount),
70
+ );
71
+ const cycleRef = useRef(cycle);
72
+ cycleRef.current = cycle;
48
73
 
49
74
  const randomRef = useRef({
50
- phase: Math.random(),
51
75
  durationFactor: 0.7 + Math.random() * 0.6,
52
- panXSign: Math.random() > 0.5 ? 1 : -1,
53
- panYSign: Math.random() > 0.5 ? 1 : -1,
54
76
  }).current;
55
77
 
56
78
  const stop = useCallback(() => {
@@ -60,32 +82,37 @@ const KenBurnsView = forwardRef((props, ref) => {
60
82
  }
61
83
  }, []);
62
84
 
63
- const start = useCallback(() => {
64
- stop();
65
- progress.setValue(randomRef.phase);
66
-
85
+ const runNextCycle = useCallback(() => {
86
+ const { zoomStart: zs, zoomEnd: ze, panXAmount: pxa, panYAmount: pya } =
87
+ configRef.current;
88
+ const prev = cycleRef.current;
89
+ const nextTo = getRandomCycle(zs, ze, pxa, pya);
90
+ setCycle({
91
+ scaleFrom: prev.scaleTo,
92
+ scaleTo: nextTo.scaleTo,
93
+ txFrom: prev.txTo,
94
+ txTo: nextTo.txTo,
95
+ tyFrom: prev.tyTo,
96
+ tyTo: nextTo.tyTo,
97
+ });
98
+ progress.setValue(0);
67
99
  const d = Math.round(duration * randomRef.durationFactor);
100
+ animationRef.current = Animated.timing(progress, {
101
+ toValue: 1,
102
+ duration: d,
103
+ easing: Easing.inOut(Easing.quad),
104
+ useNativeDriver: true,
105
+ });
106
+ animationRef.current.start(({ finished }) => {
107
+ if (finished) runNextCycle();
108
+ });
109
+ }, [duration, progress, randomRef.durationFactor]);
68
110
 
69
- animationRef.current = Animated.loop(
70
- Animated.sequence([
71
- Animated.timing(progress, {
72
- toValue: 1,
73
- duration: d,
74
- easing: Easing.inOut(Easing.quad),
75
- useNativeDriver: true,
76
- }),
77
- Animated.timing(progress, {
78
- toValue: 0,
79
- duration: d,
80
- easing: Easing.inOut(Easing.quad),
81
- useNativeDriver: true,
82
- }),
83
- ]),
84
- {resetBeforeIteration: false},
85
- );
86
-
87
- animationRef.current.start();
88
- }, [duration, progress, stop, randomRef]);
111
+ const start = useCallback(() => {
112
+ stop();
113
+ progress.setValue(0);
114
+ runNextCycle();
115
+ }, [progress, runNextCycle, stop]);
89
116
 
90
117
  useImperativeHandle(
91
118
  ref,
@@ -110,24 +137,19 @@ const KenBurnsView = forwardRef((props, ref) => {
110
137
  };
111
138
  }, [autoStart, start, stop]);
112
139
 
113
- const w = imageWidth || 0;
114
- const h = imageHeight || 0;
115
- const panXAmount = w * panX * randomRef.panXSign;
116
- const panYAmount = h * panY * randomRef.panYSign;
117
-
118
140
  const translateX = progress.interpolate({
119
141
  inputRange: [0, 1],
120
- outputRange: [-panXAmount, panXAmount],
142
+ outputRange: [cycle.txFrom, cycle.txTo],
121
143
  });
122
144
 
123
145
  const translateY = progress.interpolate({
124
146
  inputRange: [0, 1],
125
- outputRange: [-panYAmount, panYAmount],
147
+ outputRange: [cycle.tyFrom, cycle.tyTo],
126
148
  });
127
149
 
128
150
  const scale = progress.interpolate({
129
151
  inputRange: [0, 1],
130
- outputRange: [zoomStart, zoomEnd],
152
+ outputRange: [cycle.scaleFrom, cycle.scaleTo],
131
153
  });
132
154
 
133
155
  const innerSize = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-kenburns-view",
3
- "version": "5.1.1",
3
+ "version": "5.2.0",
4
4
  "description": "KenBurns Image Effect for React Native",
5
5
  "main": "index.js",
6
6
  "scripts": {