/
Loader.react.js
107 lines (95 loc) · 2.94 KB
/
Loader.react.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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import React from 'react';
import styles from 'components/Loader/Loader.scss';
const SMALL_RADIUS = 20;
const LARGE_RADIUS = 50;
const DURATION = 3000;
const LENGTH = 1.5 * (SMALL_RADIUS + LARGE_RADIUS) * Math.PI + 2 * (SMALL_RADIUS + LARGE_RADIUS);
const POINTS = {
A: (1.5 * SMALL_RADIUS * Math.PI) / LENGTH, // End of first loop
B: (1.5 * SMALL_RADIUS * Math.PI + SMALL_RADIUS + LARGE_RADIUS) / LENGTH, // Beginning of second loop
C: (1.5 * (SMALL_RADIUS + LARGE_RADIUS) * Math.PI + SMALL_RADIUS + LARGE_RADIUS) / LENGTH, // End of second loop
};
function getRadius(t) {
return 40 - 60 * Math.abs(t - 0.5);
}
function getPosition(t) {
if (t < POINTS.A) {
let multiplier = LENGTH / SMALL_RADIUS;
return {
x: SMALL_RADIUS + SMALL_RADIUS * Math.cos(t * multiplier + Math.PI / 2),
y: 2 * LARGE_RADIUS + SMALL_RADIUS - SMALL_RADIUS * Math.sin(t * multiplier + Math.PI / 2)
};
} else if (t < POINTS.B) {
return {
x: 2 * SMALL_RADIUS,
y: LENGTH * (POINTS.A - t) + 2 * LARGE_RADIUS + SMALL_RADIUS
};
} else if (t < POINTS.C) {
let t2 = t - POINTS.B;
let multiplier = LENGTH / LARGE_RADIUS;
return {
x: 2 * SMALL_RADIUS + LARGE_RADIUS - LARGE_RADIUS * Math.cos(t2 * multiplier),
y: LARGE_RADIUS - LARGE_RADIUS * Math.sin(t2 * multiplier)
};
} else {
return {
x: LENGTH * (POINTS.C - t) + 2 * SMALL_RADIUS + LARGE_RADIUS,
y: 2 * LARGE_RADIUS
};
}
}
export default class Loader extends React.Component {
componentDidMount() {
this.mounted = true;
this.mountTime = new Date().getTime();
requestAnimationFrame(this.animate.bind(this));
}
componentWillUnmount() {
this.mounted = false;
}
animate() {
if (!this.mounted) {
return;
}
let delta = new Date() - this.mountTime;
let t = (delta / DURATION) % 1;
let pos = getPosition(t);
let style = this.refs.dot0.style;
style.left = pos.x + 'px';
style.top = pos.y + 'px';
style.width = style.height = getRadius(t) + 'px';
t = (delta / DURATION + 0.4) % 1;
pos = getPosition(t);
style = this.refs.dot1.style;
style.left = pos.x + 'px';
style.top = pos.y + 'px';
style.width = style.height = getRadius(t) + 'px';
t = (delta / DURATION + 0.8) % 1;
pos = getPosition(t);
style = this.refs.dot2.style;
style.left = pos.x + 'px';
style.top = pos.y + 'px';
style.width = style.height = getRadius(t) + 'px';
requestAnimationFrame(this.animate.bind(this));
}
render() {
let classes = styles.loader;
if (this.props.className) {
classes += ' ' + this.props.className;
}
return (
<div className={classes}>
<div ref='dot0' />
<div ref='dot1' />
<div ref='dot2' />
</div>
);
}
}