-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathline-waves.tsx
More file actions
328 lines (285 loc) · 9.12 KB
/
Copy pathline-waves.tsx
File metadata and controls
328 lines (285 loc) · 9.12 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"use client"
import { Mesh, Program, Renderer, Triangle } from "ogl"
import { useEffect, useRef, useState } from "react"
interface LineWavesProps {
speed?: number
innerLineCount?: number
outerLineCount?: number
warpIntensity?: number
rotation?: number
edgeFadeWidth?: number
colorCycleSpeed?: number
brightness?: number
color1?: string
color2?: string
color3?: string
enableMouseInteraction?: boolean
mouseInfluence?: number
}
function hexToVec3(hex: string): [number, number, number] {
const h = hex.replace("#", "")
return [
parseInt(h.slice(0, 2), 16) / 255,
parseInt(h.slice(2, 4), 16) / 255,
parseInt(h.slice(4, 6), 16) / 255,
]
}
const vertexShader = `
attribute vec2 uv;
attribute vec2 position;
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position, 0, 1);
}
`
const fragmentShader = `
precision highp float;
uniform float uTime;
uniform vec3 uResolution;
uniform float uSpeed;
uniform float uInnerLines;
uniform float uOuterLines;
uniform float uWarpIntensity;
uniform float uRotation;
uniform float uEdgeFadeWidth;
uniform float uColorCycleSpeed;
uniform float uBrightness;
uniform vec3 uColor1;
uniform vec3 uColor2;
uniform vec3 uColor3;
uniform vec2 uMouse;
uniform float uMouseInfluence;
uniform bool uEnableMouse;
#define HALF_PI 1.5707963
float hashF(float n) {
return fract(sin(n * 127.1) * 43758.5453123);
}
float smoothNoise(float x) {
float i = floor(x);
float f = fract(x);
float u = f * f * (3.0 - 2.0 * f);
return mix(hashF(i), hashF(i + 1.0), u);
}
float displaceA(float coord, float t) {
float result = sin(coord * 2.123) * 0.2;
result += sin(coord * 3.234 + t * 4.345) * 0.1;
result += sin(coord * 0.589 + t * 0.934) * 0.5;
return result;
}
float displaceB(float coord, float t) {
float result = sin(coord * 1.345) * 0.3;
result += sin(coord * 2.734 + t * 3.345) * 0.2;
result += sin(coord * 0.189 + t * 0.934) * 0.3;
return result;
}
vec2 rotate2D(vec2 p, float angle) {
float c = cos(angle);
float s = sin(angle);
return vec2(p.x * c - p.y * s, p.x * s + p.y * c);
}
void main() {
vec2 coords = gl_FragCoord.xy / uResolution.xy;
coords = coords * 2.0 - 1.0;
coords = rotate2D(coords, uRotation);
float halfT = uTime * uSpeed * 0.5;
float fullT = uTime * uSpeed;
float mouseWarp = 0.0;
if (uEnableMouse) {
vec2 mPos = rotate2D(uMouse * 2.0 - 1.0, uRotation);
float mDist = length(coords - mPos);
mouseWarp = uMouseInfluence * exp(-mDist * mDist * 4.0);
}
float warpAx = coords.x + displaceA(coords.y, halfT) * uWarpIntensity + mouseWarp;
float warpAy = coords.y - displaceA(coords.x * cos(fullT) * 1.235, halfT) * uWarpIntensity;
float warpBx = coords.x + displaceB(coords.y, halfT) * uWarpIntensity + mouseWarp;
float warpBy = coords.y - displaceB(coords.x * sin(fullT) * 1.235, halfT) * uWarpIntensity;
vec2 fieldA = vec2(warpAx, warpAy);
vec2 fieldB = vec2(warpBx, warpBy);
vec2 blended = mix(fieldA, fieldB, mix(fieldA, fieldB, 0.5));
float fadeTop = smoothstep(uEdgeFadeWidth, uEdgeFadeWidth + 0.4, blended.y);
float fadeBottom = smoothstep(-uEdgeFadeWidth, -(uEdgeFadeWidth + 0.4), blended.y);
float vMask = 1.0 - max(fadeTop, fadeBottom);
float tileCount = mix(uOuterLines, uInnerLines, vMask);
float scaledY = blended.y * tileCount;
float nY = smoothNoise(abs(scaledY));
float ridge = pow(
step(abs(nY - blended.x) * 2.0, HALF_PI) * cos(2.0 * (nY - blended.x)),
5.0
);
float lines = 0.0;
for (float i = 1.0; i < 3.0; i += 1.0) {
lines += pow(max(fract(scaledY), fract(-scaledY)), i * 2.0);
}
float pattern = vMask * lines;
float cycleT = fullT * uColorCycleSpeed;
float lum = (pattern + lines * ridge) * (cos(blended.y + cycleT * 0.5) * 0.5 + 1.0);
vec3 baseColor = (uColor1 + uColor2 + uColor3) / 3.0;
vec3 col = lum * baseColor * uBrightness;
float alpha = clamp(length(col), 0.0, 1.0);
gl_FragColor = vec4(col, alpha);
}
`
export default function LineWaves({
speed = 0.3,
innerLineCount = 32.0,
outerLineCount = 36.0,
warpIntensity = 1.0,
rotation = -45,
edgeFadeWidth = 0.0,
colorCycleSpeed = 1.0,
brightness = 0.2,
color1 = "#ffffff",
color2 = "#ffffff",
color3 = "#ffffff",
enableMouseInteraction = true,
mouseInfluence = 2.0,
}: LineWavesProps) {
const containerRef = useRef<HTMLDivElement>(null)
const [reduceMotion, setReduceMotion] = useState(false)
useEffect(() => {
if (typeof window === "undefined") return
const mq = window.matchMedia("(prefers-reduced-motion: reduce)")
const update = () => setReduceMotion(mq.matches)
update()
mq.addEventListener("change", update)
return () => mq.removeEventListener("change", update)
}, [])
useEffect(() => {
if (!containerRef.current) return
if (reduceMotion) return
const container = containerRef.current
const renderer = new Renderer({ alpha: true, premultipliedAlpha: false })
const gl = renderer.gl
gl.clearColor(0, 0, 0, 0)
const currentMouse = [0.5, 0.5]
let targetMouse = [0.5, 0.5]
function handleMouseMove(e: MouseEvent) {
const rect = gl.canvas.getBoundingClientRect()
targetMouse = [
(e.clientX - rect.left) / rect.width,
1.0 - (e.clientY - rect.top) / rect.height,
]
}
function handleMouseLeave() {
targetMouse = [0.5, 0.5]
}
const geometry = new Triangle(gl)
const rotationRad = (rotation * Math.PI) / 180
const program: Program = new Program(gl, {
vertex: vertexShader,
fragment: fragmentShader,
uniforms: {
uTime: { value: 0 },
uResolution: {
value: [
gl.canvas.width,
gl.canvas.height,
gl.canvas.width / gl.canvas.height,
],
},
uSpeed: { value: speed },
uInnerLines: { value: innerLineCount },
uOuterLines: { value: outerLineCount },
uWarpIntensity: { value: warpIntensity },
uRotation: { value: rotationRad },
uEdgeFadeWidth: { value: edgeFadeWidth },
uColorCycleSpeed: { value: colorCycleSpeed },
uBrightness: { value: brightness },
uColor1: { value: hexToVec3(color1) },
uColor2: { value: hexToVec3(color2) },
uColor3: { value: hexToVec3(color3) },
uMouse: { value: new Float32Array([0.5, 0.5]) },
uMouseInfluence: { value: mouseInfluence },
uEnableMouse: { value: enableMouseInteraction },
},
})
function resize() {
renderer.setSize(container.offsetWidth, container.offsetHeight)
program.uniforms.uResolution.value = [
gl.canvas.width,
gl.canvas.height,
gl.canvas.width / gl.canvas.height,
]
}
window.addEventListener("resize", resize)
resize()
const mesh = new Mesh(gl, { geometry, program })
container.appendChild(gl.canvas)
if (enableMouseInteraction) {
// Listen on window so mouse interaction still works when the canvas is
// behind other content (e.g. fixed-position background usage).
window.addEventListener("mousemove", handleMouseMove)
window.addEventListener("mouseleave", handleMouseLeave)
}
let animationFrameId: number | null = null
let visible = true
const io = new IntersectionObserver(
(entries) => {
for (const e of entries) {
visible = e.isIntersecting
if (visible && animationFrameId === null) {
animationFrameId = requestAnimationFrame(update)
}
}
},
{ threshold: 0 }
)
io.observe(container)
function update(time: number) {
if (!visible) {
animationFrameId = null
return
}
animationFrameId = requestAnimationFrame(update)
program.uniforms.uTime.value = time * 0.001
if (enableMouseInteraction) {
currentMouse[0] += 0.05 * (targetMouse[0] - currentMouse[0])
currentMouse[1] += 0.05 * (targetMouse[1] - currentMouse[1])
program.uniforms.uMouse.value[0] = currentMouse[0]
program.uniforms.uMouse.value[1] = currentMouse[1]
} else {
program.uniforms.uMouse.value[0] = 0.5
program.uniforms.uMouse.value[1] = 0.5
}
renderer.render({ scene: mesh })
}
animationFrameId = requestAnimationFrame(update)
return () => {
if (animationFrameId !== null) cancelAnimationFrame(animationFrameId)
io.disconnect()
window.removeEventListener("resize", resize)
if (enableMouseInteraction) {
window.removeEventListener("mousemove", handleMouseMove)
window.removeEventListener("mouseleave", handleMouseLeave)
}
container.removeChild(gl.canvas)
gl.getExtension("WEBGL_lose_context")?.loseContext()
}
}, [
speed,
innerLineCount,
outerLineCount,
warpIntensity,
rotation,
edgeFadeWidth,
colorCycleSpeed,
brightness,
color1,
color2,
color3,
enableMouseInteraction,
mouseInfluence,
reduceMotion,
])
if (reduceMotion) {
return (
<div
ref={containerRef}
className="h-full w-full bg-[radial-gradient(ellipse_at_center,rgba(255,255,255,0.06),transparent_60%)]"
aria-hidden
/>
)
}
return <div ref={containerRef} className="h-full w-full" />
}