-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.html
More file actions
131 lines (113 loc) · 2.93 KB
/
example.html
File metadata and controls
131 lines (113 loc) · 2.93 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Tween Example</title>
<style type="text/css">
html, body {
background: #1A1B1B;
margin: 0;
}
#ui {
position: absolute;
right: 20px;
top: 20px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="ui">
<button id="toggle">Toggle</button>
</div>
<script src="tween.js"></script>
<script>
var BACKGROUND_COLOR = '#1A1B1B';
var DOT_COLOR = '#F6F7EA';
var EQUATIONS = [
'Linear',
'Elastic',
'Bounce',
'Back',
'Sine',
'Circ',
'Expo',
'Quad',
'Cubic',
'Quart',
'Quint'
];
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var dots = [];
var height = canvas.height = window.innerHeight;
var width = canvas.width = window.innerWidth;
function setup() {
var space = width * 0.8;
var start = (width - space) / 2;
var total = EQUATIONS.length * 3;
var step = space / total;
context.fillStyle = BACKGROUND_COLOR;
context.fillRect(0, 0, width, height);
EQUATIONS.forEach(function(name, index) {
x = start + (step / 2) + index * 3 * step;
dots.push(
makeDot(x, Tween[name].in),
makeDot(x + step, Tween[name].out),
makeDot(x + step * 2, Tween[name].inOut)
);
});
initUI();
update();
}
function initUI() {
var toggle = document.getElementById('toggle');
toggle.addEventListener('click', function() {
dots.forEach(function(dot) {
if (!dot.tween.paused) {
dot.tween.pause();
} else {
dot.tween.play();
}
});
});
}
function makeDot(x, ease) {
var dot = {
radius: 5,
ease: ease,
x: x,
y: height * 0.2
};
dot.animate = animate.bind(dot);
dot.animate();
return dot;
}
function animate() {
var position = height * (this.y > height/2 ? 0.2 : 0.8);
this.tween =
Tween.to(this, 1.5, { y: position })
.wait(0.0) // optional delay
.ease(this.ease) // optional easing function
.start(function() {}) // add start callback
.step(function() {}) // add step callback
.done(this.animate); // add done callback
}
function update() {
requestAnimationFrame(update);
context.fillStyle = BACKGROUND_COLOR;
context.globalAlpha = 0.3;
context.fillRect(0, 0, width, height);
context.beginPath();
for (var dot, i = 0, n = dots.length; i < n; i++) {
dot = dots[i];
context.moveTo(dot.x + dot.radius, dot.y);
context.arc(dot.x, dot.y, dot.radius, 0, Math.PI * 2);
}
context.globalAlpha = 1.0;
context.fillStyle = DOT_COLOR;
context.fill();
}
setup();
</script>
</body>
</html>