-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
176 lines (160 loc) · 4.35 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const Star = require('./star');
const option = Symbol('option');
const wrapper = Symbol('wrapper');
const stars = [];
let canvas;
let ctx;
let running = false;
class StarTimeLapse {
constructor (opt = {}) {
this.defaultOption = {
/* drawing */
sum: 50,
pole: -1,
poleStar: true,
radiusMin: 3,
radiusMax: 9,
/* animation */
blink: true,
run: true,
clockwise: true,
arc: 0.8,
delay: 0,
duration: 10000,
fps: 60,
/* style */
top: 0,
left: 0,
bottom: 0,
right: 0,
background: 'linear-gradient(#001, #232355)',
style: {
'z-index': -1,
opacity: 0.8
}
};
this[option] = Object.assign(this.defaultOption, opt);
/* create wrapper and background */
if (this[option].el) {
this[wrapper] = this[option].el;
this[wrapper].style.position = 'relative';
} else {
this[wrapper] = document.body;
}
const sky = document.createElement('div');
sky.style.position = 'absolute';
sky.style.top = this[option].top;
sky.style.left = this[option].left;
sky.style.bottom = this[option].bottom;
sky.style.right = this[option].right;
sky.style.background = this[option].background;
sky.style.overflow = 'hidden';
Object.keys(this[option].style).forEach(s => {
sky.style[s] = this[option].style[s];
});
this[wrapper].appendChild(sky);
/* drawing */
canvas = document.createElement('canvas');
canvas.innerHTML = 'Sorry, your browser does not support the canvas tag';
this[option].radius = getRadius(sky.clientWidth / 2, sky.clientHeight / 2);
if (this[option].pole === -1) {
this[option].pole = {
x: this[option].radius,
y: this[option].radius
};
}
this[option].offset = {
x: this[option].radius - sky.clientWidth / 2,
y: this[option].radius - sky.clientHeight / 2
};
canvas.width = this[option].radius * 2;
canvas.height = this[option].radius * 2;
canvas.style.position = 'absolute';
canvas.style.left = `-${this[option].offset.x}px`;
canvas.style.top = `-${this[option].offset.y}px`;
if (canvas.getContext) {
ctx = canvas.getContext('2d');
ctx.translate(this[option].pole.x, this[option].pole.y);
if (this[option].poleStar) drawPoleStar(ctx, this[option]);
drawStars(ctx, this[option]);
}
sky.appendChild(canvas);
if (this[option].run) this.toggle();
}
toggle () {
const interval = Math.round(1000 / this[option].fps);
const degree = 360 * interval / this[option].duration;
const { arc, clockwise } = this[option];
const animate = () => {
if (running) {
ctx.clearRect(-this[option].pole.x, -this[option].pole.y, canvas.getAttribute('width'), canvas.getAttribute('height'));
stars.forEach((s, i) => {
const atan = Math.atan2(s.y, s.x);
ctx.beginPath();
ctx.arc(0, 0, s.distance, atan, atan + Math.PI * arc, clockwise);
ctx.strokeStyle = s.color();
ctx.stroke();
ctx.closePath();
s.blink();
s.revolve(degree, clockwise);
s.draw();
});
setTimeout(() => {
window.requestAnimationFrame(animate);
}, interval);
}
};
const start = () => {
running = true;
}
const stop = () => {
running = false;
};
if (!running) {
start();
} else {
stop();
}
animate();
}
};
function getRadius (x, y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
function getPoint (x) {
return Math.round(Math.random() * x);
}
function drawPoleStar (ctx, opt) {
const x = 0;
const y = 0;
const radius = Math.ceil(Math.random() * opt.radiusMax + opt.radiusMin);
const { pole, blink } = opt;
drawStar(ctx, {
x,
y,
radius,
blink,
pole
});
}
function drawStars (ctx, opt) {
for (let i = 0; i < opt.sum; ++i) {
const x = getPoint(opt.radius * 2) - opt.pole.x;
const y = getPoint(opt.radius * 2) - opt.pole.y;
const radius = Math.ceil(Math.random() * opt.radiusMax + opt.radiusMin);
const { pole, blink } = opt;
drawStar(ctx, {
x,
y,
radius,
blink,
pole
});
}
}
function drawStar (ctx, opt) {
const star = new Star(ctx, opt);
stars.push(star);
star.draw();
}
module.exports = StarTimeLapse;