2015-07-07 20:34:09 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-07-07 20:34:09 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-07-07 20:34:09 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
var {Animated, PanResponder, StyleSheet} = ReactNative;
|
2015-07-07 20:34:09 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class AnExTilt extends React.Component<Object, any> {
|
2015-07-07 20:34:09 +00:00
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
panX: new Animated.Value(0),
|
|
|
|
opacity: new Animated.Value(1),
|
|
|
|
burns: new Animated.Value(1.15),
|
|
|
|
};
|
|
|
|
this.state.tiltPanResponder = PanResponder.create({
|
|
|
|
onStartShouldSetPanResponder: () => true,
|
|
|
|
onPanResponderGrant: () => {
|
|
|
|
Animated.timing(this.state.opacity, {
|
|
|
|
toValue: this.state.panX.interpolate({
|
2018-05-11 20:32:37 +00:00
|
|
|
inputRange: [-300, 0, 300], // pan is in pixels
|
|
|
|
outputRange: [0, 1, 0], // goes to zero at both edges
|
2015-07-07 20:34:09 +00:00
|
|
|
}),
|
2018-05-11 20:32:37 +00:00
|
|
|
duration: 0, // direct tracking
|
2015-07-07 20:34:09 +00:00
|
|
|
}).start();
|
|
|
|
},
|
|
|
|
onPanResponderMove: Animated.event(
|
2018-05-11 20:32:37 +00:00
|
|
|
[null, {dx: this.state.panX}], // panX is linked to the gesture
|
2015-07-07 20:34:09 +00:00
|
|
|
),
|
|
|
|
onPanResponderRelease: (e, gestureState) => {
|
|
|
|
var toValue = 0;
|
|
|
|
if (gestureState.dx > 100) {
|
|
|
|
toValue = 500;
|
|
|
|
} else if (gestureState.dx < -100) {
|
|
|
|
toValue = -500;
|
|
|
|
}
|
|
|
|
Animated.spring(this.state.panX, {
|
2018-05-11 20:32:37 +00:00
|
|
|
toValue, // animate back to center or off screen
|
|
|
|
velocity: gestureState.vx, // maintain gesture velocity
|
2015-07-07 20:34:09 +00:00
|
|
|
tension: 10,
|
|
|
|
friction: 3,
|
|
|
|
}).start();
|
|
|
|
this.state.panX.removeAllListeners();
|
2018-05-11 20:32:37 +00:00
|
|
|
var id = this.state.panX.addListener(({value}) => {
|
|
|
|
// listen until offscreen
|
2015-07-07 20:34:09 +00:00
|
|
|
if (Math.abs(value) > 400) {
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.panX.removeListener(id); // offscreen, so stop listening
|
2015-07-07 20:34:09 +00:00
|
|
|
Animated.timing(this.state.opacity, {
|
2018-05-11 20:32:37 +00:00
|
|
|
toValue: 1, // Fade back in. This unlinks it from tracking this.state.panX
|
2015-07-07 20:34:09 +00:00
|
|
|
}).start();
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.panX.setValue(0); // Note: stops the spring animation
|
2015-07-07 20:34:09 +00:00
|
|
|
toValue !== 0 && this._startBurnsZoom();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_startBurnsZoom() {
|
2018-05-11 20:32:37 +00:00
|
|
|
this.state.burns.setValue(1); // reset to beginning
|
2015-07-07 20:34:09 +00:00
|
|
|
Animated.decay(this.state.burns, {
|
2018-05-11 20:32:37 +00:00
|
|
|
velocity: 1, // sublte zoom
|
|
|
|
deceleration: 0.9999, // slow decay
|
2015-07-07 20:34:09 +00:00
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillMount() {
|
2015-07-07 20:34:09 +00:00
|
|
|
this._startBurnsZoom();
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
render(): React.Node {
|
2015-07-07 20:34:09 +00:00
|
|
|
return (
|
|
|
|
<Animated.View
|
|
|
|
{...this.state.tiltPanResponder.panHandlers}
|
2018-05-11 20:32:37 +00:00
|
|
|
style={[
|
|
|
|
styles.tilt,
|
|
|
|
{
|
|
|
|
opacity: this.state.opacity,
|
|
|
|
transform: [
|
|
|
|
{
|
|
|
|
rotate: this.state.panX.interpolate({
|
|
|
|
inputRange: [-320, 320],
|
|
|
|
outputRange: ['-15deg', '15deg'],
|
|
|
|
}),
|
|
|
|
}, // interpolate string "shapes"
|
|
|
|
{translateX: this.state.panX},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}>
|
2015-07-07 20:34:09 +00:00
|
|
|
<Animated.Image
|
|
|
|
pointerEvents="none"
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
transform: [
|
2018-05-11 20:32:37 +00:00
|
|
|
{
|
|
|
|
translateX: this.state.panX.interpolate({
|
|
|
|
inputRange: [-3, 3], // small range is extended by default
|
|
|
|
outputRange: [2, -2],
|
|
|
|
}), // parallax
|
2015-07-07 20:34:09 +00:00
|
|
|
},
|
2018-05-11 20:32:37 +00:00
|
|
|
{
|
|
|
|
scale: this.state.burns.interpolate({
|
|
|
|
inputRange: [1, 3000],
|
|
|
|
outputRange: [1, 1.25],
|
|
|
|
}), // simple multiplier
|
2015-07-07 20:34:09 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}}
|
2017-02-13 09:20:04 +00:00
|
|
|
source={require('./trees.jpg')}
|
2015-07-07 20:34:09 +00:00
|
|
|
/>
|
|
|
|
</Animated.View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
tilt: {
|
|
|
|
overflow: 'hidden',
|
|
|
|
height: 200,
|
|
|
|
marginBottom: 4,
|
|
|
|
backgroundColor: 'rgb(130, 130, 255)',
|
|
|
|
borderColor: 'rgba(0, 0, 0, 0.2)',
|
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 20,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = AnExTilt;
|