Skip to content

Commit 5cc748b

Browse files
authored
Merge pull request #25 from dsznajder/feature/with-limits-and-multiply-preserve-offset
Add limit and preserveMultiplyOffset
2 parents b5d2db9 + 6ccb7aa commit 5cc748b

File tree

5 files changed

+292
-32
lines changed

5 files changed

+292
-32
lines changed

Examples/limit.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as React from "react";
2+
import { View, StyleSheet } from "react-native";
3+
import Animated from "react-native-reanimated";
4+
import { PanGestureHandler } from "react-native-gesture-handler";
5+
import { limit } from "react-native-redash";
6+
7+
const { Value, event } = Animated;
8+
9+
export default class App extends React.Component {
10+
constructor(props) {
11+
super(props);
12+
13+
this.X = new Value(0);
14+
this.Y = new Value(0);
15+
const dragX = new Value(0);
16+
const dragY = new Value(0);
17+
const panState = new Value(0);
18+
19+
this.handlePan = event([
20+
{
21+
nativeEvent: {
22+
translationX: dragX,
23+
translationY: dragY,
24+
state: panState,
25+
},
26+
},
27+
]);
28+
29+
this.X = limit(dragX, panState, -100, 100);
30+
this.Y = limit(dragY, panState, -100, 100);
31+
}
32+
33+
render() {
34+
return (
35+
<View style={styles.container}>
36+
<PanGestureHandler
37+
onGestureEvent={this.handlePan}
38+
onHandlerStateChange={this.handlePan}
39+
>
40+
<Animated.View
41+
style={[
42+
styles.box,
43+
{
44+
transform: [{ translateX: this.X }, { translateY: this.Y }],
45+
},
46+
]}
47+
/>
48+
</PanGestureHandler>
49+
</View>
50+
);
51+
}
52+
}
53+
54+
const styles = StyleSheet.create({
55+
container: {
56+
flex: 1,
57+
alignItems: "center",
58+
justifyContent: "center",
59+
backgroundColor: "#ecf0f1",
60+
padding: 8,
61+
},
62+
box: {
63+
height: 40,
64+
width: 40,
65+
backgroundColor: "red",
66+
},
67+
});

Examples/preserveMultiplyOffset.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as React from "react";
2+
import { View, StyleSheet } from "react-native";
3+
import Animated from "react-native-reanimated";
4+
import { PinchGestureHandler } from "react-native-gesture-handler";
5+
import { preserveMultiplicativeOffset } from "react-native-redash";
6+
7+
const { Value, event } = Animated;
8+
9+
export default class App extends React.Component {
10+
constructor(props) {
11+
super(props);
12+
13+
const scale = new Value(1);
14+
const scaleState = new Value(0);
15+
16+
this.handleZoom = event([
17+
{
18+
nativeEvent: {
19+
scale,
20+
state: scaleState,
21+
},
22+
},
23+
]);
24+
25+
this.scale = preserveMultiplicativeOffset(scale, scaleState);
26+
}
27+
28+
render() {
29+
return (
30+
<View style={styles.container}>
31+
<PinchGestureHandler
32+
onGestureEvent={this.handleZoom}
33+
onHandlerStateChange={this.handleZoom}
34+
>
35+
<Animated.View
36+
style={[
37+
styles.box,
38+
{
39+
transform: [{ scale: this.scale }],
40+
},
41+
]}
42+
/>
43+
</PinchGestureHandler>
44+
</View>
45+
);
46+
}
47+
}
48+
49+
const styles = StyleSheet.create({
50+
container: {
51+
flex: 1,
52+
alignItems: "center",
53+
justifyContent: "center",
54+
backgroundColor: "#ecf0f1",
55+
padding: 8,
56+
},
57+
box: {
58+
height: 40,
59+
width: 40,
60+
backgroundColor: "red",
61+
},
62+
});

README.md

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ yarn add react-native-redash
1212
```
1313

1414
```js
15-
import {atan2} from "react-native-redash";
15+
import { atan2 } from "react-native-redash";
1616

17-
atan2(y, x)
17+
atan2(y, x);
1818
```
1919

2020
## Components
@@ -29,9 +29,9 @@ Example usage:
2929

3030
```js
3131
<Interactable
32-
snapPoints={[{ x: -width }, { x: 0 }, { x: width }]}
33-
style={{...StyleSheet.absoluteFillObject, backgroundColor: "blue" }}
34-
onSnap={() => alert("oh snap!")}
32+
snapPoints={[{ x: -width }, { x: 0 }, { x: width }]}
33+
style={{ ...StyleSheet.absoluteFillObject, backgroundColor: "blue" }}
34+
onSnap={() => alert("oh snap!")}
3535
/>
3636
```
3737

@@ -52,7 +52,7 @@ Example usage:
5252
Transforms an angle in degrees in radians.
5353

5454
```js
55-
(deg: Node) => Node
55+
(deg: Node) => Node;
5656
```
5757

5858
### `toDeg(node)`
@@ -117,7 +117,7 @@ const config = {
117117
toValue: 1,
118118
easing: Easing.linear,
119119
};
120-
runTiming(clock, 0, config)
120+
runTiming(clock, 0, config);
121121
```
122122

123123
### `runDecay(clock, value, velocity, rerunDecaying)`
@@ -156,18 +156,14 @@ Example Usage:
156156
const from = {
157157
r: 197,
158158
g: 43,
159-
b: 39
159+
b: 39,
160160
};
161161
const to = {
162162
r: 225,
163163
g: 176,
164-
b: 68
164+
b: 68,
165165
};
166-
interpolateColors(
167-
x,
168-
[0, 1],
169-
[from, to]
170-
)
166+
interpolateColors(x, [0, 1], [from, to]);
171167
```
172168

173169
### `snapPoint(point, velocity, points)`
@@ -177,7 +173,7 @@ Example usage:
177173

178174
```js
179175
const snapPoints = [-width, 0, width];
180-
runSpring(clock, x, snapPoint(x, velocityX, snapPoints))
176+
runSpring(clock, x, snapPoint(x, velocityX, snapPoints));
181177
```
182178

183179
## Transformations
@@ -196,10 +192,7 @@ Example usage with `transform`.
196192
const perspective = 800;
197193
const z = new Value(100);
198194
//...
199-
transform: [
200-
{ perspective },
201-
translateZ(perspective, z)
202-
]
195+
transform: [{ perspective }, translateZ(perspective, z)];
203196
```
204197

205198
## Gestures
@@ -215,19 +208,13 @@ onScroll(contentOffset: { x?: Node; y?: Node; }) => EventNode
215208
Example usage for a vertical `ScrollView`.
216209

217210
```js
218-
<Animated.ScrollView
219-
onScroll={onScroll({ y: new Value(0) })}
220-
vertical
221-
/>
211+
<Animated.ScrollView onScroll={onScroll({ y: new Value(0) })} vertical />
222212
```
223213

224214
And for an horizontal one.
225215

226216
```js
227-
<Animated.ScrollView
228-
onScroll={onScroll({ x: new Value(0) })}
229-
horizontal
230-
/>
217+
<Animated.ScrollView onScroll={onScroll({ x: new Value(0) })} horizontal />
231218
```
232219

233220
### decay
@@ -237,9 +224,97 @@ Decorates animated value to decay after pan
237224
- [How it works](https://snack.expo.io/@dsznajder/decay)
238225
- [Example usage](./Examples/decay.tsx)
239226

227+
```js
228+
constructor(props) {
229+
const dragX = new Value(0);
230+
const panState = new Value(0);
231+
const velocityX = new Value(0);
232+
233+
this.handlePan = event([
234+
{
235+
nativeEvent: {
236+
translationX: dragX,
237+
state: panState,
238+
velocityX,
239+
},
240+
},
241+
]);
242+
243+
this.X = decay(dragX, panState, velocityX);
244+
}
245+
```
246+
240247
### preserveOffset
241248

242249
Decorates animated value to save previous offset of pan
243250

244251
- [How it works](https://snack.expo.io/@dsznajder/preserveoffset)
245252
- [Example usage](./Examples/preserveOffset.tsx)
253+
254+
```js
255+
constructor(props) {
256+
const dragX = new Value(0);
257+
const panState = new Value(0);
258+
259+
this.handlePan = event([
260+
{
261+
nativeEvent: {
262+
translationX: dragX,
263+
state: panState,
264+
},
265+
},
266+
]);
267+
268+
this.X = preserveOffset(dragX, panState);
269+
}
270+
```
271+
272+
### preserveMultiplicativeOffset
273+
274+
Decorates animated value to save previous offset of pinch
275+
276+
- [How it works](https://snack.expo.io/@dsznajder/preserveMultiplicativeOffset)
277+
- [Example usage](./Examples/preserveMultiplicativeOffset.tsx)
278+
279+
```js
280+
constructor(props) {
281+
const scale = new Value(1);
282+
const scaleState = new Value(0);
283+
284+
this.handleZoom = event([
285+
{
286+
nativeEvent: {
287+
scale,
288+
state: panState,
289+
},
290+
},
291+
]);
292+
293+
this.X = preserveMultiplicativeOffset(scale, scaleState);
294+
}
295+
```
296+
297+
### limit
298+
299+
Decorates animated value to set limits of panning
300+
301+
- [How it works](https://snack.expo.io/@dsznajder/limit)
302+
- [Example usage](./Examples/limit.tsx)
303+
304+
```js
305+
constructor(props) {
306+
const dragX = new Value(0);
307+
const panState = new Value(0);
308+
309+
this.handlePan = event([
310+
{
311+
nativeEvent: {
312+
translationX: dragX,
313+
state: panState,
314+
},
315+
},
316+
]);
317+
318+
this.X = limit(dragX, panState, -100, 100);
319+
}
320+
```

0 commit comments

Comments
 (0)