-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Animations.js
163 lines (145 loc) · 3.73 KB
/
Animations.js
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
import React, {Component} from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
Animated,
Easing,
} from 'react-native';
import ModelView from 'react-native-gl-model-view';
const AnimatedModelView = Animated.createAnimatedComponent(ModelView);
export default class Animations extends Component {
constructor() {
super();
this.state = {
turns: 0,
zoom: -3,
rotateX: new Animated.Value(0),
rotateZ: new Animated.Value(0),
translateZ: new Animated.Value(-20),
uiPosition: new Animated.Value(50),
};
Object.keys(this.state).forEach(key => {
if (this.state[key] instanceof Animated.Value)
this.state[key].__makeNative();
});
}
zoom = action => {
let {zoom, translateZ} = this.state;
this.state.zoom += action;
Animated.timing(translateZ, {
toValue: zoom,
useNativeDriver: true,
duration: 300,
}).start();
};
goCrazy = () => {
let {rotateZ, rotateX, translateZ} = this.state;
const crazy = (value, toValue) =>
Animated.timing(value, {
toValue,
useNativeDriver: true,
duration: Math.random() * 10000,
easing: Easing.elastic(4),
});
Animated.parallel([
crazy(rotateX, Math.random() * 1000),
crazy(translateZ, -2 - Math.random() * 3),
crazy(rotateZ, Math.random() * 1000),
]).start();
};
turnAround = () => {
let {turns, rotateZ} = this.state;
this.state.turns += 1;
Animated.timing(rotateZ, {
toValue: turns * 180,
useNativeDriver: true,
duration: 500,
}).start();
};
componentDidMount() {
Animated.parallel([
Animated.sequence([
Animated.timing(this.state.translateZ, {
toValue: this.state.zoom,
useNativeDriver: true,
duration: 3500,
easing: Easing.elastic(1),
}),
Animated.timing(this.state.uiPosition, {
toValue: 0,
useNativeDriver: true,
duration: 300,
}),
]),
Animated.timing(this.state.rotateX, {
toValue: 270,
useNativeDriver: true,
duration: 5000,
easing: Easing.elastic(5),
}),
]).start();
}
renderButton(label, method) {
return (
<TouchableOpacity onPress={method}>
<Text style={styles.button}>{label}</Text>
</TouchableOpacity>
);
}
render() {
let {rotateZ, rotateX, translateZ, uiPosition} = this.state;
return (
<View style={styles.container}>
<AnimatedModelView
model={{
uri: 'demon.model',
}}
texture={{
uri: 'demon.png',
}}
tint={{r: 1.0, g: 1.0, b: 1.0, a: 1.0}}
animate
flipTexture={false}
scale={0.01}
translateZ={translateZ}
rotateX={rotateX}
rotateZ={rotateZ}
style={styles.view}
/>
<Animated.View
style={[styles.buttons, {transform: [{translateY: uiPosition}]}]}>
{this.renderButton('zoom in', this.zoom.bind(this, 0.8))}
{this.renderButton('zoom out', this.zoom.bind(this, -0.8))}
{this.renderButton('turn around', this.turnAround.bind(this))}
{this.renderButton('go crazy', this.goCrazy.bind(this))}
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'transparent',
},
view: {
flex: 1,
backgroundColor: '#fff',
},
buttons: {
height: 50,
backgroundColor: 'transparent',
flexDirection: 'row',
justifyContent: 'space-around',
},
button: {
padding: 5,
borderWidth: 1,
borderColor: '#aaa',
borderRadius: 5,
textAlign: 'center',
fontSize: 12,
},
});