-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (111 loc) · 3.12 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
let canvas, ctx, w, h, units;
let unitCount = 100;
let hue = 0;
function init() {
canvas = document.querySelector("#canvas");
ctx = canvas.getContext("2d");
resizeReset();
createUnits();
animationLoop();
addScrollZoomEffect();
}
function resizeReset() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
ctx.fillStyle = "#222";
ctx.fillRect(0, 0, w, h);
}
function createUnits() {
units = [];
for (let i = 0; i < unitCount; i++) {
setTimeout(() => {
units.push(new Unit());
}, i * 200);
}
}
function animationLoop() {
ctx.fillStyle = "rgba(0, 0, 0, .05)";
ctx.fillRect(0, 0, w, h);
drawScene();
requestAnimationFrame(animationLoop);
}
function drawScene() {
for (let i = 0; i < units.length; i++) {
units[i].update();
units[i].draw();
}
}
function getRandomInt(min, max) {
return Math.round(Math.random() * (max - min)) + min;
}
class Unit {
constructor() {
this.reset();
this.constructed = true;
}
reset() {
this.x = Math.round(w / 2);
this.y = Math.round(h / 2);
this.sx = this.x;
this.sy = this.y;
this.angle = 60 * getRandomInt(0, 5);
this.size = 1;
this.radian = (Math.PI / 180) * (this.angle + 90);
this.speed = 2;
this.maxDistance = 30;
this.time = 0;
this.ttl = getRandomInt(180, 300);
this.hue = hue;
hue += 0.5;
}
draw() {
ctx.save();
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = `hsl(${this.hue}, 100%, 50%)`;
ctx.shadowColor = `hsl(${this.hue}, 100%, 50%)`;
ctx.shadowBlur = 5;
ctx.fill();
ctx.closePath();
ctx.restore();
}
update() {
let dx = this.sx - this.x;
let dy = this.sy - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance >= this.maxDistance) {
if (getRandomInt(0, 1)) {
this.angle += 60;
} else {
this.angle -= 60;
}
this.radian = (Math.PI / 180) * (this.angle + 90);
this.sx = this.x;
this.sy = this.y;
}
this.x += this.speed * Math.sin(this.radian);
this.y += this.speed * Math.cos(this.radian);
if (this.time >= this.ttl || this.x < 0 || this.x > w || this.y < 0 || this.y > h) {
this.reset();
}
this.time++;
}
}
function addScrollZoomEffect() {
const images = document.querySelectorAll(".image-gallery img");
let currentIndex = -1;
const imageHeight = images[0].clientHeight;
window.addEventListener("scroll", () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const newCurrentIndex = Math.min(images.length - 1, Math.floor(scrollTop / imageHeight));
if (newCurrentIndex !== currentIndex) {
if (currentIndex >= 0) {
images[currentIndex].classList.remove("zoomed");
}
images[newCurrentIndex].classList.add("zoomed");
currentIndex = newCurrentIndex;
}
});
}
window.addEventListener("DOMContentLoaded", init);
window.addEventListener("resize", resizeReset);