@istmx/ui 0.1.11 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +533 -463
- package/bin/istmx.mjs +481 -314
- package/package.json +27 -27
- package/registry/accordion.tsx +186 -0
- package/registry/ai-chat-block/ai-chat-block.tsx +456 -460
- package/registry/ai-chat-block/ai-chat-input.tsx +694 -694
- package/registry/ai-chat-block/attachment.tsx +466 -466
- package/registry/ai-chat-block/chat-code-block.tsx +50 -50
- package/registry/ai-chat-block/chat-composer.tsx +51 -51
- package/registry/ai-chat-block/chat-logo.tsx +18 -18
- package/registry/ai-chat-block/chat-message.tsx +161 -161
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -273
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -9
- package/registry/ai-chat-block/code-highlighter.tsx +342 -357
- package/registry/ai-chat-block/default-attachments.tsx +42 -42
- package/registry/ai-chat-block/index.ts +30 -30
- package/registry/ai-chat-block/message-content.tsx +287 -287
- package/registry/ai-chat-block/message-list.tsx +93 -93
- package/registry/ai-chat-block/send-button.tsx +102 -102
- package/registry/ai-chat-block/types.ts +70 -71
- package/registry/ai-chat-block/utils.ts +6 -6
- package/registry/ai-chat-block/voice-button.tsx +66 -66
- package/registry/alert-dialog.tsx +229 -0
- package/registry/alert.tsx +188 -0
- package/registry/animated-text.tsx +228 -225
- package/registry/aspect-ratio.tsx +32 -0
- package/registry/avatar-group.tsx +66 -0
- package/registry/avatar.tsx +121 -0
- package/registry/badge.tsx +52 -0
- package/registry/bento-grid.tsx +438 -433
- package/registry/button.tsx +156 -237
- package/registry/carousel.tsx +184 -0
- package/registry/dock-navigation.tsx +200 -198
- package/registry/drift-canvas.tsx +383 -0
- package/registry/drift-wall.tsx +361 -0
- package/registry/flex-carousel.tsx +1430 -0
- package/registry/guided-gallery.tsx +1506 -0
- package/registry/image-accordion.tsx +306 -302
- package/registry/image-trail.tsx +126 -121
- package/registry/infinite-image-canvas.tsx +268 -263
- package/registry/input-otp.tsx +332 -0
- package/registry/list-card.tsx +130 -0
- package/registry/mobile-menu-dock.tsx +216 -211
- package/registry/morph-slider.tsx +756 -0
- package/registry/pixel-cat.tsx +972 -1020
- package/registry/scroll-canvas-cards.tsx +643 -0
- package/registry/scroll-parallax-hero.tsx +566 -565
- package/registry/scroll-story-cards.tsx +443 -438
- package/registry/slider.tsx +163 -0
- package/registry/spotlight-grid.tsx +226 -0
- package/registry/streaming-text.tsx +269 -278
- package/registry/text-reveal.tsx +311 -307
- package/registry/user-avatar.tsx +99 -0
- package/registry/utils.ts +6 -6
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import * as React from 'react'
|
|
4
|
+
import { Renderer, Triangle, Program, Mesh, Texture } from 'ogl'
|
|
5
|
+
import { gsap } from 'gsap'
|
|
6
|
+
import { IconChevronLeft, IconChevronRight } from '@tabler/icons-react'
|
|
7
|
+
import { cn } from '@/lib/utils'
|
|
8
|
+
|
|
9
|
+
export interface MorphSliderItem {
|
|
10
|
+
image: string
|
|
11
|
+
caption?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface MorphSliderProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
15
|
+
items?: MorphSliderItem[]
|
|
16
|
+
startIndex?: number
|
|
17
|
+
transition?: 'melt' | 'ripple' | 'shear' | 'swirl'
|
|
18
|
+
duration?: number
|
|
19
|
+
ease?: string
|
|
20
|
+
intensity?: number
|
|
21
|
+
scale?: number
|
|
22
|
+
aberration?: number
|
|
23
|
+
drift?: number
|
|
24
|
+
autoplay?: boolean
|
|
25
|
+
autoplayDelay?: number
|
|
26
|
+
loop?: boolean
|
|
27
|
+
radius?: number
|
|
28
|
+
overlayColor?: string
|
|
29
|
+
showCaptions?: boolean
|
|
30
|
+
showControls?: boolean
|
|
31
|
+
showIndicators?: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const TRANSITIONS: Record<string, number> = { melt: 0, ripple: 1, shear: 2, swirl: 3 }
|
|
35
|
+
|
|
36
|
+
const DEFAULT_ITEMS: MorphSliderItem[] = [
|
|
37
|
+
{
|
|
38
|
+
image: 'https://images.unsplash.com/photo-1782977389500-dd7adad33ebe?q=80&w=1600&auto=format&fit=crop',
|
|
39
|
+
caption: 'One',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
image: 'https://images.unsplash.com/photo-1781499455083-6ccc3beb20cd?q=80&w=1600&auto=format&fit=crop',
|
|
43
|
+
caption: 'Two',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
image: 'https://images.unsplash.com/photo-1776394254711-4a0d7345269a?q=80&w=1600&auto=format&fit=crop',
|
|
47
|
+
caption: 'Three',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
image: 'https://images.unsplash.com/photo-1781242629922-6f39cc3671cd?q=80&w=1600&auto=format&fit=crop',
|
|
51
|
+
caption: 'Four',
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
const vertexShader = `
|
|
56
|
+
attribute vec2 position;
|
|
57
|
+
attribute vec2 uv;
|
|
58
|
+
varying vec2 vUv;
|
|
59
|
+
void main() {
|
|
60
|
+
vUv = uv;
|
|
61
|
+
gl_Position = vec4(position, 0.0, 1.0);
|
|
62
|
+
}
|
|
63
|
+
`
|
|
64
|
+
|
|
65
|
+
const fragmentShader = `
|
|
66
|
+
precision highp float;
|
|
67
|
+
|
|
68
|
+
uniform sampler2D tCurrent;
|
|
69
|
+
uniform sampler2D tNext;
|
|
70
|
+
uniform vec2 uResolution;
|
|
71
|
+
uniform vec2 uCurrentSize;
|
|
72
|
+
uniform vec2 uNextSize;
|
|
73
|
+
uniform float uProgress;
|
|
74
|
+
uniform float uDir;
|
|
75
|
+
uniform int uMode;
|
|
76
|
+
uniform float uIntensity;
|
|
77
|
+
uniform float uScale;
|
|
78
|
+
uniform float uAberration;
|
|
79
|
+
uniform float uDrift;
|
|
80
|
+
uniform float uTime;
|
|
81
|
+
uniform float uReduce;
|
|
82
|
+
uniform vec2 uPointer;
|
|
83
|
+
uniform vec3 uOverlay;
|
|
84
|
+
|
|
85
|
+
varying vec2 vUv;
|
|
86
|
+
|
|
87
|
+
const float PI = 3.14159265359;
|
|
88
|
+
|
|
89
|
+
float hash11(float p) {
|
|
90
|
+
p = fract(p * 0.1031);
|
|
91
|
+
p *= p + 33.33;
|
|
92
|
+
p *= p + p;
|
|
93
|
+
return fract(p);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
float hash21(vec2 p) {
|
|
97
|
+
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
|
|
98
|
+
p3 += dot(p3, p3.yzx + 33.33);
|
|
99
|
+
return fract((p3.x + p3.y) * p3.z);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
float noise(vec2 p) {
|
|
103
|
+
vec2 i = floor(p);
|
|
104
|
+
vec2 f = fract(p);
|
|
105
|
+
vec2 u = f * f * (3.0 - 2.0 * f);
|
|
106
|
+
float a = hash21(i);
|
|
107
|
+
float b = hash21(i + vec2(1.0, 0.0));
|
|
108
|
+
float c = hash21(i + vec2(0.0, 1.0));
|
|
109
|
+
float d = hash21(i + vec2(1.0, 1.0));
|
|
110
|
+
return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
float fbm(vec2 p) {
|
|
114
|
+
float v = 0.0;
|
|
115
|
+
float a = 0.5;
|
|
116
|
+
for (int i = 0; i < 5; i++) {
|
|
117
|
+
v += a * noise(p);
|
|
118
|
+
p *= 2.0;
|
|
119
|
+
a *= 0.5;
|
|
120
|
+
}
|
|
121
|
+
return v;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
mat2 rot(float a) {
|
|
125
|
+
float s = sin(a);
|
|
126
|
+
float c = cos(a);
|
|
127
|
+
return mat2(c, -s, s, c);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
vec2 coverUV(vec2 uv, vec2 res, vec2 img) {
|
|
131
|
+
float rA = res.x / max(res.y, 1.0);
|
|
132
|
+
float iA = img.x / max(img.y, 1.0);
|
|
133
|
+
vec2 s = vec2(1.0);
|
|
134
|
+
float ratio = rA / max(iA, 0.0001);
|
|
135
|
+
if (ratio > 1.0) {
|
|
136
|
+
s.y = 1.0 / ratio;
|
|
137
|
+
} else {
|
|
138
|
+
s.x = ratio;
|
|
139
|
+
}
|
|
140
|
+
return (uv - 0.5) * s + 0.5;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
void main() {
|
|
144
|
+
float p = clamp(uProgress, 0.0, 1.0);
|
|
145
|
+
float env = sin(p * PI);
|
|
146
|
+
|
|
147
|
+
vec2 uv = vUv;
|
|
148
|
+
uv += vec2(sin(uTime * 0.25 + uv.y * 4.0), cos(uTime * 0.22 + uv.x * 4.0)) * uDrift * 0.008;
|
|
149
|
+
uv = (uv - 0.5) * (1.0 - uDrift * 0.02 * sin(uTime * 0.4)) + 0.5;
|
|
150
|
+
|
|
151
|
+
vec2 uvC = uv;
|
|
152
|
+
vec2 uvN = uv;
|
|
153
|
+
float m = smoothstep(0.0, 1.0, p);
|
|
154
|
+
|
|
155
|
+
if (uReduce < 0.5) {
|
|
156
|
+
if (uMode == 3) {
|
|
157
|
+
vec2 c = uv - 0.5;
|
|
158
|
+
float r = length(c);
|
|
159
|
+
float ang = env * uIntensity * 3.5 * (1.0 - r);
|
|
160
|
+
uvC = rot(ang) * c + 0.5;
|
|
161
|
+
uvN = rot(-ang) * c + 0.5;
|
|
162
|
+
m = smoothstep(0.0, 1.0, p);
|
|
163
|
+
} else if (uMode == 1) {
|
|
164
|
+
float d = distance(uv, uPointer);
|
|
165
|
+
float ring = p * 1.6;
|
|
166
|
+
float wave = sin((d - ring) * 30.0) * env;
|
|
167
|
+
vec2 dir = normalize(uv - uPointer + 1e-4);
|
|
168
|
+
vec2 disp = dir * wave * uIntensity * 0.25;
|
|
169
|
+
uvC = uv + disp;
|
|
170
|
+
uvN = uv + disp * 0.6;
|
|
171
|
+
m = 1.0 - smoothstep(ring - 0.03, ring + 0.03, d);
|
|
172
|
+
} else if (uMode == 2) {
|
|
173
|
+
float slices = 14.0;
|
|
174
|
+
float row = floor(uv.y * slices);
|
|
175
|
+
float rnd = hash11(row);
|
|
176
|
+
vec2 disp = vec2((rnd - 0.5) * env * uIntensity * 0.6, 0.0);
|
|
177
|
+
uvC = uv + disp;
|
|
178
|
+
uvN = uv + disp;
|
|
179
|
+
float localX = uDir > 0.0 ? uv.x : 1.0 - uv.x;
|
|
180
|
+
float th = p * 1.5 - 0.25 + (rnd - 0.5) * 0.25;
|
|
181
|
+
m = 1.0 - smoothstep(th - 0.06, th + 0.06, localX);
|
|
182
|
+
} else {
|
|
183
|
+
float nn = fbm(uv * uScale + uTime * 0.03);
|
|
184
|
+
float warp = fbm(uv * uScale * 1.7 - uTime * 0.02);
|
|
185
|
+
vec2 g = vec2(nn, warp) - 0.5;
|
|
186
|
+
uvC = uv + g * uIntensity * 0.5 * p;
|
|
187
|
+
uvN = uv - g * uIntensity * 0.5 * (1.0 - p);
|
|
188
|
+
m = smoothstep(nn - 0.15, nn + 0.15, p);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
vec2 sC = coverUV(uvC, uResolution, uCurrentSize);
|
|
193
|
+
vec2 sN = coverUV(uvN, uResolution, uNextSize);
|
|
194
|
+
|
|
195
|
+
float ca = uReduce < 0.5 ? uAberration * env * 0.03 : 0.0;
|
|
196
|
+
|
|
197
|
+
vec3 colC = vec3(
|
|
198
|
+
texture2D(tCurrent, sC + vec2(ca, 0.0)).r,
|
|
199
|
+
texture2D(tCurrent, sC).g,
|
|
200
|
+
texture2D(tCurrent, sC - vec2(ca, 0.0)).b
|
|
201
|
+
);
|
|
202
|
+
vec3 colN = vec3(
|
|
203
|
+
texture2D(tNext, sN + vec2(ca, 0.0)).r,
|
|
204
|
+
texture2D(tNext, sN).g,
|
|
205
|
+
texture2D(tNext, sN - vec2(ca, 0.0)).b
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
vec3 col = mix(colC, colN, m);
|
|
209
|
+
float vig = smoothstep(1.25, 0.25, length(uv - 0.5));
|
|
210
|
+
col = mix(col, uOverlay, (1.0 - vig) * 0.28);
|
|
211
|
+
|
|
212
|
+
gl_FragColor = vec4(col, 1.0);
|
|
213
|
+
}
|
|
214
|
+
`
|
|
215
|
+
|
|
216
|
+
function makeFallbackTexture(gl: any) {
|
|
217
|
+
const size = 4
|
|
218
|
+
const data = new Uint8Array(size * size * 4)
|
|
219
|
+
for (let i = 0; i < size * size; i++) {
|
|
220
|
+
data[i * 4] = 24
|
|
221
|
+
data[i * 4 + 1] = 24
|
|
222
|
+
data[i * 4 + 2] = 28
|
|
223
|
+
data[i * 4 + 3] = 255
|
|
224
|
+
}
|
|
225
|
+
return new Texture(gl, { image: data, width: size, height: size, generateMipmaps: false })
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function hexToRgb(hex: string) {
|
|
229
|
+
let h = (hex || '#000000').replace('#', '')
|
|
230
|
+
if (h.length === 3) {
|
|
231
|
+
h = h
|
|
232
|
+
.split('')
|
|
233
|
+
.map((c) => c + c)
|
|
234
|
+
.join('')
|
|
235
|
+
}
|
|
236
|
+
const n = parseInt(h, 16)
|
|
237
|
+
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
class MorphEngine {
|
|
241
|
+
container: HTMLElement
|
|
242
|
+
items: MorphSliderItem[]
|
|
243
|
+
getOptions: () => any
|
|
244
|
+
onIndexChange: (index: number) => void
|
|
245
|
+
reducedMotion: boolean
|
|
246
|
+
current: number
|
|
247
|
+
animating: boolean
|
|
248
|
+
dragging: boolean
|
|
249
|
+
dragDir: number
|
|
250
|
+
shownIndex: number
|
|
251
|
+
tween: any
|
|
252
|
+
renderer: any
|
|
253
|
+
gl: any
|
|
254
|
+
canvas: HTMLCanvasElement
|
|
255
|
+
geometry: any
|
|
256
|
+
textures: any[]
|
|
257
|
+
sizes: [number, number][]
|
|
258
|
+
program: any
|
|
259
|
+
mesh: any
|
|
260
|
+
boundContextLost: any
|
|
261
|
+
resizeObserver: ResizeObserver
|
|
262
|
+
boundLoop: any
|
|
263
|
+
raf: number
|
|
264
|
+
|
|
265
|
+
constructor(
|
|
266
|
+
container: HTMLElement,
|
|
267
|
+
{
|
|
268
|
+
items,
|
|
269
|
+
startIndex,
|
|
270
|
+
reducedMotion,
|
|
271
|
+
getOptions,
|
|
272
|
+
onIndexChange,
|
|
273
|
+
dprCap,
|
|
274
|
+
}: {
|
|
275
|
+
items: MorphSliderItem[]
|
|
276
|
+
startIndex: number
|
|
277
|
+
reducedMotion: boolean
|
|
278
|
+
getOptions: () => any
|
|
279
|
+
onIndexChange: (index: number) => void
|
|
280
|
+
dprCap: number
|
|
281
|
+
},
|
|
282
|
+
) {
|
|
283
|
+
this.container = container
|
|
284
|
+
this.items = items
|
|
285
|
+
this.getOptions = getOptions
|
|
286
|
+
this.onIndexChange = onIndexChange
|
|
287
|
+
this.reducedMotion = reducedMotion
|
|
288
|
+
|
|
289
|
+
this.current = startIndex
|
|
290
|
+
this.animating = false
|
|
291
|
+
this.dragging = false
|
|
292
|
+
this.dragDir = 0
|
|
293
|
+
this.shownIndex = startIndex
|
|
294
|
+
this.tween = null
|
|
295
|
+
|
|
296
|
+
this.renderer = new Renderer({
|
|
297
|
+
alpha: false,
|
|
298
|
+
antialias: true,
|
|
299
|
+
dpr: Math.min(window.devicePixelRatio || 1, dprCap),
|
|
300
|
+
})
|
|
301
|
+
this.gl = this.renderer.gl
|
|
302
|
+
this.gl.clearColor(0.05, 0.05, 0.06, 1)
|
|
303
|
+
|
|
304
|
+
this.canvas = this.gl.canvas
|
|
305
|
+
this.canvas.className = 'block w-full h-full'
|
|
306
|
+
container.appendChild(this.canvas)
|
|
307
|
+
|
|
308
|
+
this.geometry = new Triangle(this.gl)
|
|
309
|
+
this.textures = this.items.map(() => makeFallbackTexture(this.gl))
|
|
310
|
+
this.sizes = this.items.map(() => [1, 1])
|
|
311
|
+
|
|
312
|
+
const opts = this.getOptions()
|
|
313
|
+
this.program = new Program(this.gl, {
|
|
314
|
+
vertex: vertexShader,
|
|
315
|
+
fragment: fragmentShader,
|
|
316
|
+
uniforms: {
|
|
317
|
+
tCurrent: { value: this.textures[this.current] },
|
|
318
|
+
tNext: { value: this.textures[this.current] },
|
|
319
|
+
uResolution: { value: [1, 1] },
|
|
320
|
+
uCurrentSize: { value: this.sizes[this.current] },
|
|
321
|
+
uNextSize: { value: this.sizes[this.current] },
|
|
322
|
+
uProgress: { value: 0 },
|
|
323
|
+
uDir: { value: 1 },
|
|
324
|
+
uMode: { value: TRANSITIONS[opts.transition] ?? 0 },
|
|
325
|
+
uIntensity: { value: opts.intensity },
|
|
326
|
+
uScale: { value: opts.scale },
|
|
327
|
+
uAberration: { value: opts.aberration },
|
|
328
|
+
uDrift: { value: opts.drift },
|
|
329
|
+
uTime: { value: 0 },
|
|
330
|
+
uReduce: { value: reducedMotion ? 1 : 0 },
|
|
331
|
+
uPointer: { value: [0.5, 0.5] },
|
|
332
|
+
uOverlay: { value: hexToRgb(opts.overlayColor) },
|
|
333
|
+
},
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
this.mesh = new Mesh(this.gl, { geometry: this.geometry, program: this.program })
|
|
337
|
+
|
|
338
|
+
this.boundContextLost = this.onContextLost.bind(this)
|
|
339
|
+
this.canvas.addEventListener('webglcontextlost', this.boundContextLost, false)
|
|
340
|
+
|
|
341
|
+
this.resizeObserver = new ResizeObserver(() => this.resize())
|
|
342
|
+
this.resizeObserver.observe(container)
|
|
343
|
+
this.resize()
|
|
344
|
+
|
|
345
|
+
this.loadTextures()
|
|
346
|
+
|
|
347
|
+
this.boundLoop = this.loop.bind(this)
|
|
348
|
+
this.raf = requestAnimationFrame(this.boundLoop)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
loadTextures() {
|
|
352
|
+
this.items.forEach((item, index) => {
|
|
353
|
+
const img = new Image()
|
|
354
|
+
img.crossOrigin = 'anonymous'
|
|
355
|
+
img.src = item.image
|
|
356
|
+
img.onload = () => {
|
|
357
|
+
const texture = new Texture(this.gl, { generateMipmaps: false })
|
|
358
|
+
texture.image = img
|
|
359
|
+
this.textures[index] = texture
|
|
360
|
+
this.sizes[index] = [img.naturalWidth || 1, img.naturalHeight || 1]
|
|
361
|
+
if (index === this.current) {
|
|
362
|
+
this.program.uniforms.tCurrent.value = texture
|
|
363
|
+
this.program.uniforms.uCurrentSize.value = this.sizes[index]
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
resize() {
|
|
370
|
+
const rect = this.container.getBoundingClientRect()
|
|
371
|
+
const w = Math.max(rect.width, 1)
|
|
372
|
+
const h = Math.max(rect.height, 1)
|
|
373
|
+
this.renderer.setSize(w, h)
|
|
374
|
+
this.program.uniforms.uResolution.value = [this.gl.canvas.width, this.gl.canvas.height]
|
|
375
|
+
this.renderer.render({ scene: this.mesh })
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
syncOptions() {
|
|
379
|
+
const opts = this.getOptions()
|
|
380
|
+
this.program.uniforms.uMode.value = TRANSITIONS[opts.transition] ?? 0
|
|
381
|
+
this.program.uniforms.uIntensity.value = opts.intensity
|
|
382
|
+
this.program.uniforms.uScale.value = opts.scale
|
|
383
|
+
this.program.uniforms.uAberration.value = opts.aberration
|
|
384
|
+
this.program.uniforms.uDrift.value = opts.drift
|
|
385
|
+
this.program.uniforms.uOverlay.value = hexToRgb(opts.overlayColor)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
loop(t: number) {
|
|
389
|
+
this.program.uniforms.uTime.value = t * 0.001
|
|
390
|
+
if (!this.dragging && !this.animating) this.syncOptions()
|
|
391
|
+
this.renderer.render({ scene: this.mesh })
|
|
392
|
+
this.raf = requestAnimationFrame(this.boundLoop)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
wrap(i: number) {
|
|
396
|
+
const n = this.items.length
|
|
397
|
+
return ((i % n) + n) % n
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
prepareNext(dir: number) {
|
|
401
|
+
const target = this.wrap(this.current + dir)
|
|
402
|
+
this.program.uniforms.tCurrent.value = this.textures[this.current]
|
|
403
|
+
this.program.uniforms.uCurrentSize.value = this.sizes[this.current]
|
|
404
|
+
this.program.uniforms.tNext.value = this.textures[target]
|
|
405
|
+
this.program.uniforms.uNextSize.value = this.sizes[target]
|
|
406
|
+
this.program.uniforms.uDir.value = dir
|
|
407
|
+
return target
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
goTo(dir: number) {
|
|
411
|
+
if (this.animating || this.dragging || this.items.length < 2) return
|
|
412
|
+
const opts = this.getOptions()
|
|
413
|
+
if (!opts.loop) {
|
|
414
|
+
const raw = this.current + dir
|
|
415
|
+
if (raw < 0 || raw > this.items.length - 1) return
|
|
416
|
+
}
|
|
417
|
+
this.syncOptions()
|
|
418
|
+
const target = this.prepareNext(dir)
|
|
419
|
+
this.animating = true
|
|
420
|
+
this.announce(target)
|
|
421
|
+
const duration = this.reducedMotion ? Math.min(opts.duration, 0.4) : opts.duration
|
|
422
|
+
this.tween = gsap.fromTo(
|
|
423
|
+
this.program.uniforms.uProgress,
|
|
424
|
+
{ value: 0 },
|
|
425
|
+
{
|
|
426
|
+
value: 1,
|
|
427
|
+
duration,
|
|
428
|
+
ease: opts.ease,
|
|
429
|
+
onComplete: () => this.commit(target),
|
|
430
|
+
},
|
|
431
|
+
)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
announce(index: number) {
|
|
435
|
+
if (index === this.shownIndex) return
|
|
436
|
+
this.shownIndex = index
|
|
437
|
+
if (this.onIndexChange) this.onIndexChange(index)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
commit(target: number) {
|
|
441
|
+
this.current = target
|
|
442
|
+
this.program.uniforms.tCurrent.value = this.textures[target]
|
|
443
|
+
this.program.uniforms.uCurrentSize.value = this.sizes[target]
|
|
444
|
+
this.program.uniforms.uProgress.value = 0
|
|
445
|
+
this.animating = false
|
|
446
|
+
this.tween = null
|
|
447
|
+
this.announce(target)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
next() {
|
|
451
|
+
this.goTo(1)
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
prev() {
|
|
455
|
+
this.goTo(-1)
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
setPointer(x: number, y: number) {
|
|
459
|
+
this.program.uniforms.uPointer.value = [x, y]
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
beginDrag() {
|
|
463
|
+
if (this.animating || this.items.length < 2) return false
|
|
464
|
+
this.dragging = true
|
|
465
|
+
this.dragDir = 0
|
|
466
|
+
this.syncOptions()
|
|
467
|
+
return true
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
drag(ndx: number) {
|
|
471
|
+
if (!this.dragging) return
|
|
472
|
+
const opts = this.getOptions()
|
|
473
|
+
const dir = ndx < 0 ? 1 : -1
|
|
474
|
+
if (!opts.loop) {
|
|
475
|
+
const raw = this.current + dir
|
|
476
|
+
if (raw < 0 || raw > this.items.length - 1) {
|
|
477
|
+
this.program.uniforms.uProgress.value = 0
|
|
478
|
+
return
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (dir !== this.dragDir) {
|
|
482
|
+
this.dragDir = dir
|
|
483
|
+
this.prepareNext(dir)
|
|
484
|
+
}
|
|
485
|
+
const progress = Math.min(Math.abs(ndx), 1)
|
|
486
|
+
this.program.uniforms.uProgress.value = progress
|
|
487
|
+
this.announce(progress > 0.5 ? this.wrap(this.current + dir) : this.current)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
endDrag() {
|
|
491
|
+
if (!this.dragging) return
|
|
492
|
+
this.dragging = false
|
|
493
|
+
const p = this.program.uniforms.uProgress.value
|
|
494
|
+
if (this.dragDir === 0) return
|
|
495
|
+
const target = this.wrap(this.current + this.dragDir)
|
|
496
|
+
const duration = this.reducedMotion ? 0.3 : 0.5
|
|
497
|
+
this.animating = true
|
|
498
|
+
if (p > 0.4) {
|
|
499
|
+
this.announce(target)
|
|
500
|
+
this.tween = gsap.to(this.program.uniforms.uProgress, {
|
|
501
|
+
value: 1,
|
|
502
|
+
duration,
|
|
503
|
+
ease: 'power2.out',
|
|
504
|
+
onComplete: () => this.commit(target),
|
|
505
|
+
})
|
|
506
|
+
} else {
|
|
507
|
+
this.announce(this.current)
|
|
508
|
+
this.tween = gsap.to(this.program.uniforms.uProgress, {
|
|
509
|
+
value: 0,
|
|
510
|
+
duration,
|
|
511
|
+
ease: 'power2.out',
|
|
512
|
+
onComplete: () => {
|
|
513
|
+
this.animating = false
|
|
514
|
+
this.tween = null
|
|
515
|
+
},
|
|
516
|
+
})
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
onContextLost(e: Event) {
|
|
521
|
+
e.preventDefault()
|
|
522
|
+
cancelAnimationFrame(this.raf)
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
destroy() {
|
|
526
|
+
cancelAnimationFrame(this.raf)
|
|
527
|
+
if (this.tween) this.tween.kill()
|
|
528
|
+
this.resizeObserver.disconnect()
|
|
529
|
+
this.canvas.removeEventListener('webglcontextlost', this.boundContextLost)
|
|
530
|
+
this.textures.forEach((tex) => {
|
|
531
|
+
if (tex && tex.texture) this.gl.deleteTexture(tex.texture)
|
|
532
|
+
})
|
|
533
|
+
if (this.program && this.program.program) this.gl.deleteProgram(this.program.program)
|
|
534
|
+
const ext = this.gl.getExtension('WEBGL_lose_context')
|
|
535
|
+
if (ext) ext.loseContext()
|
|
536
|
+
if (this.canvas.parentNode) this.canvas.parentNode.removeChild(this.canvas)
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export function MorphSlider({
|
|
541
|
+
items = DEFAULT_ITEMS,
|
|
542
|
+
startIndex = 0,
|
|
543
|
+
transition = 'melt',
|
|
544
|
+
duration = 1.1,
|
|
545
|
+
ease = 'power2.inOut',
|
|
546
|
+
intensity = 0.55,
|
|
547
|
+
scale = 2.4,
|
|
548
|
+
aberration = 0.35,
|
|
549
|
+
drift = 0.4,
|
|
550
|
+
autoplay = false,
|
|
551
|
+
autoplayDelay = 4,
|
|
552
|
+
loop = true,
|
|
553
|
+
radius = 16,
|
|
554
|
+
overlayColor = '#000000',
|
|
555
|
+
showCaptions = true,
|
|
556
|
+
showControls = true,
|
|
557
|
+
showIndicators = true,
|
|
558
|
+
className = '',
|
|
559
|
+
...props
|
|
560
|
+
}: MorphSliderProps) {
|
|
561
|
+
const containerRef = React.useRef<HTMLDivElement | null>(null)
|
|
562
|
+
const engineRef = React.useRef<MorphEngine | null>(null)
|
|
563
|
+
const [index, setIndex] = React.useState(startIndex)
|
|
564
|
+
const [hovering, setHovering] = React.useState(false)
|
|
565
|
+
|
|
566
|
+
const optsRef = React.useRef<any>(null)
|
|
567
|
+
optsRef.current = {
|
|
568
|
+
transition,
|
|
569
|
+
duration,
|
|
570
|
+
ease,
|
|
571
|
+
intensity,
|
|
572
|
+
scale,
|
|
573
|
+
aberration,
|
|
574
|
+
drift,
|
|
575
|
+
overlayColor,
|
|
576
|
+
loop,
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
React.useEffect(() => {
|
|
580
|
+
if (!containerRef.current) return undefined
|
|
581
|
+
const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
|
582
|
+
|
|
583
|
+
const engine = new MorphEngine(containerRef.current, {
|
|
584
|
+
items,
|
|
585
|
+
startIndex,
|
|
586
|
+
reducedMotion,
|
|
587
|
+
dprCap: 2,
|
|
588
|
+
getOptions: () => optsRef.current,
|
|
589
|
+
onIndexChange: setIndex,
|
|
590
|
+
})
|
|
591
|
+
engineRef.current = engine
|
|
592
|
+
setIndex(startIndex)
|
|
593
|
+
|
|
594
|
+
return () => {
|
|
595
|
+
engine.destroy()
|
|
596
|
+
engineRef.current = null
|
|
597
|
+
}
|
|
598
|
+
}, [items, startIndex])
|
|
599
|
+
|
|
600
|
+
const handleNext = React.useCallback(() => engineRef.current?.next(), [])
|
|
601
|
+
const handlePrev = React.useCallback(() => engineRef.current?.prev(), [])
|
|
602
|
+
|
|
603
|
+
React.useEffect(() => {
|
|
604
|
+
if (!autoplay || hovering) return undefined
|
|
605
|
+
const id = setTimeout(() => engineRef.current?.next(), Math.max(autoplayDelay, 1) * 1000)
|
|
606
|
+
return () => clearTimeout(id)
|
|
607
|
+
}, [autoplay, autoplayDelay, hovering, index])
|
|
608
|
+
|
|
609
|
+
React.useEffect(() => {
|
|
610
|
+
const el = containerRef.current
|
|
611
|
+
if (!el) return undefined
|
|
612
|
+
let startX = 0
|
|
613
|
+
let width = 1
|
|
614
|
+
let active = false
|
|
615
|
+
|
|
616
|
+
const onDown = (e: PointerEvent) => {
|
|
617
|
+
const rect = el.getBoundingClientRect()
|
|
618
|
+
width = rect.width || 1
|
|
619
|
+
startX = e.clientX
|
|
620
|
+
const px = (e.clientX - rect.left) / rect.width
|
|
621
|
+
const py = (e.clientY - rect.top) / rect.height
|
|
622
|
+
engineRef.current?.setPointer(px, 1 - py)
|
|
623
|
+
active = engineRef.current?.beginDrag() ?? false
|
|
624
|
+
if (active && el.setPointerCapture) {
|
|
625
|
+
try {
|
|
626
|
+
el.setPointerCapture(e.pointerId)
|
|
627
|
+
} catch {}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
const onMove = (e: PointerEvent) => {
|
|
631
|
+
if (!active) return
|
|
632
|
+
const ndx = (e.clientX - startX) / width
|
|
633
|
+
engineRef.current?.drag(ndx)
|
|
634
|
+
}
|
|
635
|
+
const onUp = () => {
|
|
636
|
+
if (!active) return
|
|
637
|
+
active = false
|
|
638
|
+
engineRef.current?.endDrag()
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
el.addEventListener('pointerdown', onDown)
|
|
642
|
+
el.addEventListener('pointermove', onMove)
|
|
643
|
+
el.addEventListener('pointerup', onUp)
|
|
644
|
+
el.addEventListener('pointercancel', onUp)
|
|
645
|
+
|
|
646
|
+
return () => {
|
|
647
|
+
el.removeEventListener('pointerdown', onDown)
|
|
648
|
+
el.removeEventListener('pointermove', onMove)
|
|
649
|
+
el.removeEventListener('pointerup', onUp)
|
|
650
|
+
el.removeEventListener('pointercancel', onUp)
|
|
651
|
+
}
|
|
652
|
+
}, [])
|
|
653
|
+
|
|
654
|
+
const onKeyDown = React.useCallback(
|
|
655
|
+
(e: React.KeyboardEvent) => {
|
|
656
|
+
if (e.key === 'ArrowRight') {
|
|
657
|
+
e.preventDefault()
|
|
658
|
+
handleNext()
|
|
659
|
+
} else if (e.key === 'ArrowLeft') {
|
|
660
|
+
e.preventDefault()
|
|
661
|
+
handlePrev()
|
|
662
|
+
}
|
|
663
|
+
},
|
|
664
|
+
[handleNext, handlePrev],
|
|
665
|
+
)
|
|
666
|
+
|
|
667
|
+
const hasCaptions = items.some((item) => item.caption)
|
|
668
|
+
|
|
669
|
+
return (
|
|
670
|
+
<div
|
|
671
|
+
className={cn(
|
|
672
|
+
'relative w-full h-full overflow-hidden bg-neutral-950 select-none touch-pan-y',
|
|
673
|
+
className,
|
|
674
|
+
)}
|
|
675
|
+
style={{ borderRadius: `${radius}px` }}
|
|
676
|
+
onMouseEnter={() => setHovering(true)}
|
|
677
|
+
onMouseLeave={() => setHovering(false)}
|
|
678
|
+
{...props}
|
|
679
|
+
>
|
|
680
|
+
<div
|
|
681
|
+
ref={containerRef}
|
|
682
|
+
className="absolute inset-0 cursor-grab active:cursor-grabbing outline-none focus-visible:ring-2 focus-visible:ring-foreground/40"
|
|
683
|
+
role="group"
|
|
684
|
+
aria-roledescription="carousel"
|
|
685
|
+
aria-label="Image morph slider"
|
|
686
|
+
tabIndex={0}
|
|
687
|
+
onKeyDown={onKeyDown}
|
|
688
|
+
/>
|
|
689
|
+
|
|
690
|
+
{showCaptions && hasCaptions && (
|
|
691
|
+
<div className="absolute left-5 bottom-5 z-10 max-w-[70%] pointer-events-none grid" aria-live="polite">
|
|
692
|
+
{items.map((item, i) =>
|
|
693
|
+
item.caption ? (
|
|
694
|
+
<span
|
|
695
|
+
key={i}
|
|
696
|
+
aria-hidden={i === index ? undefined : true}
|
|
697
|
+
className={cn(
|
|
698
|
+
'col-start-1 row-start-1 justify-self-start inline-block px-3.5 py-2 rounded-xl text-xs sm:text-sm font-semibold tracking-wide text-foreground bg-surface/60 backdrop-blur-md border border-border transition-all duration-300 ease-out',
|
|
699
|
+
i === index
|
|
700
|
+
? 'opacity-100 translate-y-0 blur-0'
|
|
701
|
+
: 'opacity-0 translate-y-3 blur-xs pointer-events-none',
|
|
702
|
+
)}
|
|
703
|
+
>
|
|
704
|
+
{item.caption}
|
|
705
|
+
</span>
|
|
706
|
+
) : null,
|
|
707
|
+
)}
|
|
708
|
+
</div>
|
|
709
|
+
)}
|
|
710
|
+
|
|
711
|
+
{showControls && (
|
|
712
|
+
<div className="absolute top-1/2 left-0 right-0 z-20 flex justify-between px-4 -translate-y-1/2 pointer-events-none">
|
|
713
|
+
<button
|
|
714
|
+
type="button"
|
|
715
|
+
className="pointer-events-auto inline-flex items-center justify-center w-10 h-10 rounded-full border border-border bg-surface/60 backdrop-blur-md text-foreground hover:bg-surface hover:scale-105 active:scale-95 transition-all cursor-pointer shadow-lg"
|
|
716
|
+
aria-label="Previous slide"
|
|
717
|
+
onClick={handlePrev}
|
|
718
|
+
>
|
|
719
|
+
<IconChevronLeft className="w-5 h-5" />
|
|
720
|
+
</button>
|
|
721
|
+
<button
|
|
722
|
+
type="button"
|
|
723
|
+
className="pointer-events-auto inline-flex items-center justify-center w-10 h-10 rounded-full border border-border bg-surface/60 backdrop-blur-md text-foreground hover:bg-surface hover:scale-105 active:scale-95 transition-all cursor-pointer shadow-lg"
|
|
724
|
+
aria-label="Next slide"
|
|
725
|
+
onClick={handleNext}
|
|
726
|
+
>
|
|
727
|
+
<IconChevronRight className="w-5 h-5" />
|
|
728
|
+
</button>
|
|
729
|
+
</div>
|
|
730
|
+
)}
|
|
731
|
+
|
|
732
|
+
{showIndicators && (
|
|
733
|
+
<div className="absolute bottom-5 right-5 z-20 flex items-center gap-1.5 p-1.5 rounded-full bg-surface/50 backdrop-blur-md border border-border" role="tablist" aria-label="Slides">
|
|
734
|
+
{items.map((_, i) => (
|
|
735
|
+
<button
|
|
736
|
+
key={i}
|
|
737
|
+
type="button"
|
|
738
|
+
role="tab"
|
|
739
|
+
aria-selected={i === index}
|
|
740
|
+
aria-label={`Go to slide ${i + 1}`}
|
|
741
|
+
className={cn(
|
|
742
|
+
'h-2 rounded-full transition-all duration-300 cursor-pointer',
|
|
743
|
+
i === index ? 'w-6 bg-foreground' : 'w-2 bg-foreground/30 hover:bg-foreground/60',
|
|
744
|
+
)}
|
|
745
|
+
onClick={() => {
|
|
746
|
+
const engine = engineRef.current
|
|
747
|
+
if (!engine || i === index) return
|
|
748
|
+
engine.goTo(i > index ? 1 : -1)
|
|
749
|
+
}}
|
|
750
|
+
/>
|
|
751
|
+
))}
|
|
752
|
+
</div>
|
|
753
|
+
)}
|
|
754
|
+
</div>
|
|
755
|
+
)
|
|
756
|
+
}
|