-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
136 lines (110 loc) · 3.79 KB
/
script.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
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(other) {
this.x += other.x;
this.y += other.y;
}
distance(other) {
return Math.sqrt(this.distanceSquared(other));
}
distanceSquared(other) {
const dx = this.x - other.x;
const dy = this.y - other.y;
return dx * dx + dy * dy;
}
}
class Point {
constructor(position, velocity, color) {
this.position = position;
this.velocity = velocity;
this.color = color;
}
}
const interval = 30;
const radius = 4;
const lineWidth = 3;
const maxDistance = 120;
const maxSpeed = 25;
const points = []
onresize = () => {
const canvas = document.getElementById("background");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
}
function init() {
const canvas = document.getElementById("background");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const amount = canvas.width * canvas.height / 15000;
for (let i = 0; i < amount; i++) {
points.push(new Point(
new Vector(Math.random() * canvas.width, Math.random() * canvas.height),
new Vector(
(Math.random() * 2 - 1) * interval / 1000 * maxSpeed,
(Math.random() * 2 - 1) * interval / 1000 * maxSpeed),
'hsl(' + 360 * Math.random() + ',30%,40%)'
));
}
draw();
setInterval(() => {
update();
draw();
}, interval);
const quote = document.getElementById("quote");
const quotes = JSON.parse(quote.dataset.quotes);
quote.removeAttribute("data-quotes");
setInterval(() => {
quote.innerText = quotes[Math.floor(Math.random() * quotes.length)];
}, 10 * 1000);
}
function update() {
const canvas = document.getElementById("background");
const width = canvas.width;
const height = canvas.height;
for (const point of points) {
point.position.add(point.velocity);
if (point.position.x < 0 || point.position.x >= width) {
point.velocity.x *= -1;
point.position.x = Math.min(Math.max(point.position.x, 0), width);
}
if (point.position.y < 0 || point.position.y >= height) {
point.velocity.y *= -1;
point.position.y = Math.min(Math.max(point.position.y, 0), height);
}
}
}
function draw() {
const canvas = document.getElementById("background");
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < points.length; i++) {
for (let j = 0; j < i; j++) {
const from = points[i];
const to = points[j];
if (from.position.distanceSquared(to.position) > maxDistance * maxDistance)
continue;
const relative = (maxDistance - from.position.distance(to.position)) / maxDistance;
// ctx.globalAlpha = relative;
ctx.lineWidth = lineWidth * relative;
const gradient = ctx.createLinearGradient(from.position.x, from.position.y, to.position.x, to.position.y);
gradient.addColorStop(0, from.color);
gradient.addColorStop(1, to.color);
ctx.strokeStyle = gradient;
ctx.beginPath();
ctx.moveTo(from.position.x, from.position.y);
ctx.lineTo(to.position.x, to.position.y);
ctx.stroke();
}
}
// ctx.globalAlpha = 1;
for (const point of points) {
ctx.beginPath();
ctx.fillStyle = point.color;
ctx.arc(point.position.x, point.position.y, radius, 0, Math.PI * 2, true);
ctx.fill();
}
}