-
Notifications
You must be signed in to change notification settings - Fork 0
/
xo.js
130 lines (104 loc) · 2.99 KB
/
xo.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
// Particle Animation
var PARTICLE_NUM = 500;
var PARTICLE_BASE_RADIUS = 0.5;
var FL = 500;
var DEFAULT_SPEED = 2;
var BOOST_SPEED = 300;
var canvas;
var canvasWidth, canvasHeight;
var context;
var centerX, centerY;
var mouseX, mouseY;
var speed = DEFAULT_SPEED;
var targetSpeed = DEFAULT_SPEED;
var particles = [];
window.addEventListener('load', function () {
canvas = document.getElementById('space');
var resize = function () {
canvasWidth = canvas.width = window.innerWidth;
canvasHeight = canvas.height = window.innerHeight;
centerX = canvasWidth * 0.5;
centerY = canvasHeight * 0.5;
context = canvas.getContext('2d');
context.fillStyle = 'rgb(255, 255, 255)';
};
window.addEventListener('resize', resize);
resize();
mouseX = centerX;
mouseY = centerY;
for (var i = 0, p; i < PARTICLE_NUM; i++) {
particles[i] = randomizeParticle(new Particle());
particles[i].z -= 500 * Math.random();
}
document.addEventListener('mousemove', function (e) {
mouseX = e.clientX;
mouseY = e.clientY;
}, false);
document.addEventListener('mousedown', function (e) {
targetSpeed = BOOST_SPEED;
}, false);
document.addEventListener('mouseup', function (d) {
targetSpeed = DEFAULT_SPEED;
}, false);
setInterval(loop, 1000 / 60);
}, false);
function loop() {
context.save();
context.fillStyle = 'rgb(0, 0, 0)';
context.fillRect(0, 0, canvasWidth, canvasHeight);
context.restore();
speed += (targetSpeed - speed) * 0.01;
var p;
var cx, cy;
var rx, ry;
var f, x, y, r;
var pf, px, py, pr;
var a, a1, a2;
var halfPi = Math.PI * 0.5;
var atan2 = Math.atan2;
var cos = Math.cos;
var sin = Math.sin;
context.beginPath();
for (var i = 0; i < PARTICLE_NUM; i++) {
p = particles[i];
p.pastZ = p.z;
p.z -= speed;
if (p.z <= 0) {
randomizeParticle(p);
continue;
}
cx = centerX - (mouseX - centerX) * 1.25;
cy = centerY - (mouseY - centerY) * 1.25;
rx = p.x - cx;
ry = p.y - cy;
f = FL / p.z;
x = cx + rx * f;
y = cy + ry * f;
r = PARTICLE_BASE_RADIUS * f;
pf = FL / p.pastZ;
px = cx + rx * pf;
py = cy + ry * pf;
pr = PARTICLE_BASE_RADIUS * pf;
a = atan2(py - y, px - x);
a1 = a + halfPi;
a2 = a - halfPi;
context.moveTo(px + pr * cos(a1), py + pr * sin(a1));
context.arc(px, py, pr, a1, a2, true);
context.lineTo(x + r * cos(a2), y + r * sin(a2));
context.arc(x, y, r, a2, a1, true);
context.closePath();
}
context.fill();
}
function randomizeParticle(p) {
p.x = Math.random() * canvasWidth;
p.y = Math.random() * canvasHeight;
p.z = Math.random() * 1500 + 500;
return p;
}
function Particle(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.pastZ = 0;
}