-
Notifications
You must be signed in to change notification settings - Fork 17
/
BurstAndMoveEmitter.js
130 lines (111 loc) · 3.42 KB
/
BurstAndMoveEmitter.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
// @flow
import React, { Component } from 'react';
import { Animated, Dimensions, Easing } from 'react-native';
import type { VectorType } from './entities/Vector';
import { Vector } from './entities/Vector';
import { fromAngle, toRadians, add } from './utils/vector-helpers';
import BaseEmitter from './BaseEmitter';
import { Particle } from './entities/Particle';
import type { ParticleConfig, BaseEmitterType } from './BaseEmitter';
const { width, height } = Dimensions.get('window');
type IBurstAndMoveEmitter = BaseEmitterType & {
finalPoint: VectorType,
radius: number,
burstTime: number,
waitTime: number,
moveTime: number,
onCalculate?: () => ParticleConfig[],
onAnimate?: (Animated.Value, Animated.Value) => void
};
export interface IBurstAndMoveEmitterState {
particles: Array<Vector>[];
}
export default class BurstAndMoveEmitter extends Component<
IBurstAndMoveEmitter,
IBurstAndMoveEmitterState
> {
static defaultProps = {
finalPoint: Vector(width, height),
burstTime: 300,
waitTime: 1000,
moveTime: 1000
};
emitter: BaseEmitter;
_storeEmitterRef: BaseEmitter => void;
constructor(props: IBurstAndMoveEmitter) {
super(props);
this._calculate = this._calculate.bind(this);
this._animateParticle = this._animateParticle.bind(this);
this._storeEmitterRef = emitter => (this.emitter = emitter);
}
render() {
const { children, ...props } = this.props;
return (
<BaseEmitter
{...props}
onCalculate={this._calculate}
ref={this._storeEmitterRef}
onAnimate={this._animateParticle}
>
{children}
</BaseEmitter>
);
}
_calculate = (initialPosition: VectorType, particlesCounter: number) => {
const { numberOfParticles, radius, finalPoint, emissionRate } = this.props;
const particles: ParticleConfig[] = [];
const rate = Math.min(numberOfParticles, emissionRate);
for (let i = 0; i < rate; i++) {
// Generate a random magnitude lower than or equals the radius
const magnitude = Math.round(Math.random() * radius);
// Generate a random angle between 0 and 360
const angle = Math.round(Math.random() * 360);
// Calculate a vector based on the angle and magnitude.
const burstPoint = add(
initialPosition,
fromAngle(toRadians(angle), magnitude)
);
// first step - Emit new particles
const particle = Particle(
Vector(0, 0),
Vector(0, 0),
particlesCounter + i,
initialPosition
);
const path: VectorType[] = [initialPosition, burstPoint, finalPoint];
particles.push({
particle,
path
});
}
return particles;
};
_animateParticle = (path, transformValue, opacityValue) => {
const { burstTime, moveTime, waitTime } = this.props;
return Animated.parallel([
Animated.sequence([
Animated.timing(transformValue, {
toValue: 1,
duration: burstTime,
easing: Easing.out(Easing.quad),
useNativeDriver: true
}),
Animated.timing(transformValue, {
toValue: 2,
duration: moveTime,
delay: waitTime,
easing: Easing.in(Easing.quad),
useNativeDriver: true
}),
Animated.timing(opacityValue, {
toValue: 0,
duration: moveTime * 0.5,
useNativeDriver: true
})
])
]);
};
start() {
this.emitter.start();
}
}