-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (97 loc) · 2.8 KB
/
index.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
/*
*** TRAVELING THROUGH SPACE ***
An attempt of writing a space travel animation
*/
var canvas = document.getElementById('starfield');
var flr = Math.floor;
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
var halfw = canvas.width / 2,
halfh = canvas.height * 0.98,
step = 2,
warpZ = 12,
speed = 0.075;
var stampedDate = new Date();
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0,0, canvas.width, canvas.height);
function rnd(num1, num2) {
return flr(Math.random() * num2 * 2) + num1;
}
function getColor() {
return 'hsla(200,100%, ' + rnd(50,100) + '%, 1)';
}
var star = function() {
var v = vec3.fromValues(rnd(0 - halfw,halfw),rnd(0 - halfh,halfh), rnd(1, warpZ));
this.x = v[0];
this.y = v[1];
this.z = v[2];
this.color = getColor();
this.reset = function() {
v = vec3.fromValues(rnd(0 - halfw,halfw),rnd(0 - halfh,halfh), rnd(1, warpZ));
this.x = v[0];
this.y = v[1];
this.color = getColor();
vel = this.calcVel();
}
this.calcVel = function() {
return vec3.fromValues(0, 0, 0 - speed);
};
var vel = this.calcVel();
this.draw = function() {
vel = this.calcVel();
v = vec3.add(vec3.create(), v, vel);
var x = v[0] / v[2];
var y = v[1] / v[2];
var x2 = v[0] / (v[2] + speed * 0.50);
var y2 = v[1] / (v[2] + speed * 0.50);
ctx.strokeStyle = this.color;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x2, y2);
ctx.stroke();
if (x < 0 - halfw || x > halfw || y < 0 - halfh || y > halfh) {
this.reset();
}
};
}
var starfield = function() {
var numOfStars = 2048;
var stars = [];
function _init() {
for(var i = 0, len = numOfStars; i < len; i++) {
stars.push(new star());
}
}
_init();
this.draw = function() {
ctx.translate(halfw, halfh);
for(var i = 0, len = stars.length; i < len; i++) {
var currentStar = stars[i];
currentStar.draw();
}
};
}
var mStarField = new starfield();
function draw() {
// make 5 seconds
var millSeconds = 1000 * 10;
var currentTime = new Date();
speed = 0.025;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.fillRect(0,0, canvas.width, canvas.height);
mStarField.draw();
window.requestAnimationFrame(draw);
}
draw();
window.onresize = function() {
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
halfw = canvas.width / 2;
halfh = canvas.height * 0.98;
};
var danger = document.getElementsByClassName('danger');
for (var i=0; i<danger.length; i++) {
danger[i].classList.toggle("paused");
}