-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
148 lines (120 loc) · 3.93 KB
/
home.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
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext('2d');
// for intro motion
let mouseMoved = false;
const pointer = {
x: .5 * window.innerWidth,
y: .5 * window.innerHeight,
}
const params = {
pointsNumber: 40,
widthFactor: .3,
mouseThreshold: .6,
spring: .4,
friction: .5
};
const trail = new Array(params.pointsNumber);
for (let i = 0; i < params.pointsNumber; i++) {
trail[i] = {
x: pointer.x,
y: pointer.y,
dx: 0,
dy: 0,
}
}
window.addEventListener("click", e => {
updateMousePosition(e.pageX, e.pageY);
});
window.addEventListener("mousemove", e => {
mouseMoved = true;
updateMousePosition(e.pageX, e.pageY);
});
window.addEventListener("touchmove", e => {
mouseMoved = true;
updateMousePosition(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
});
function updateMousePosition(eX, eY) {
pointer.x = eX;
pointer.y = eY;
}
setupCanvas();
update(0);
window.addEventListener("resize", setupCanvas);
function update(t) {
// for intro motion
if (!mouseMoved) {
pointer.x = (.5 + .3 * Math.cos(.002 * t) * (Math.sin(.005 * t))) * window.innerWidth;
pointer.y = (.5 + .2 * (Math.cos(.005 * t)) + .1 * Math.cos(.01 * t)) * window.innerHeight;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
trail.forEach((p, pIdx) => {
const prev = pIdx === 0 ? pointer : trail[pIdx - 1];
const spring = pIdx === 0 ? .4 * params.spring : params.spring;
p.dx += (prev.x - p.x) * spring;
p.dy += (prev.y - p.y) * spring;
p.dx *= params.friction;
p.dy *= params.friction;
p.x += p.dx;
p.y += p.dy;
});
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(trail[0].x, trail[0].y);
for (let i = 1; i < trail.length - 1; i++) {
const xc = .5 * (trail[i].x + trail[i + 1].x);
const yc = .5 * (trail[i].y + trail[i + 1].y);
ctx.quadraticCurveTo(trail[i].x, trail[i].y, xc, yc);
ctx.lineWidth = params.widthFactor * (params.pointsNumber - i);
ctx.stroke();
}
ctx.lineTo(trail[trail.length - 1].x, trail[trail.length - 1].y);
ctx.strokeStyle = "white";
ctx.stroke();
window.requestAnimationFrame(update);
}
function setupCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
const spans = document.querySelectorAll('p span');
const numLetters = spans.length;
spans.forEach(function(span, i) {
const mappedIndex = i - (numLetters / 2)
span.style.animationDelay = (mappedIndex * 0.25) + 's';
});
const generateGlowButtons = () => {
document.querySelectorAll(".glow-button").forEach((button) => {
let gradientElem = button.querySelector('.gradient');
if(!gradientElem) {
gradientElem = document.createElement("div");
gradientElem.classList.add("gradient");
button.appendChild(gradientElem);
}
button.addEventListener("pointermove", (e) => {
const rect = button.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
gsap.to(button, {
"--pointer-x": `${x}px`,
"--pointer-y": `${y}px`,
duration: 0.6,
});
gsap.to(button, {
"--button-glow": chroma
.mix(
getComputedStyle(button)
.getPropertyValue("--button-glow-start")
.trim(),
getComputedStyle(button).getPropertyValue("--button-glow-end").trim(),
x / rect.width
)
.hex(),
duration: 0.2,
});
});
});
}
// Set variables on loaded
document.addEventListener('DOMContentLoaded', generateGlowButtons);
// Set variables on resize
window.addEventListener('resize', generateGlowButtons);