@istmx/ui 0.1.8 → 0.1.9
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/README.md +41 -1
- package/bin/istmx.mjs +2 -2
- package/package.json +1 -1
- package/registry/button.tsx +4 -7
- package/registry/text-reveal.tsx +307 -0
- package/registry/image-effects-card.tsx +0 -197
package/README.md
CHANGED
|
@@ -17,7 +17,6 @@ Add the practical controls:
|
|
|
17
17
|
|
|
18
18
|
```sh
|
|
19
19
|
npx @istmx/ui add button
|
|
20
|
-
npx @istmx/ui add image-effects-card
|
|
21
20
|
```
|
|
22
21
|
|
|
23
22
|
Or use your preferred package manager:
|
|
@@ -132,11 +131,52 @@ Other options include `direction`, `interval`, `speed`, `scale`, and effect
|
|
|
132
131
|
settings for blur, fade, slide, and wave. The full component implementation is
|
|
133
132
|
in your `components/ui/animated-text.tsx` file.
|
|
134
133
|
|
|
134
|
+
## Text Reveal
|
|
135
|
+
|
|
136
|
+
Install the editable source with:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
npx @istmx/ui add text-reveal
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`TextReveal` reveals plain text line by line and character by character. It
|
|
143
|
+
supports a viewport-triggered entrance by default, or reversible scroll-linked
|
|
144
|
+
animation by passing a Motion value from `useScroll`.
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
import TextReveal from '@/components/ui/text-reveal'
|
|
148
|
+
|
|
149
|
+
export function Quote() {
|
|
150
|
+
return (
|
|
151
|
+
<TextReveal
|
|
152
|
+
text={'Every word arrives softly,\none character at a time.'}
|
|
153
|
+
characterStagger={0.028}
|
|
154
|
+
lineStagger={0.2}
|
|
155
|
+
duration={0.65}
|
|
156
|
+
waveDistance={8}
|
|
157
|
+
waveOvershoot={6}
|
|
158
|
+
sidewaysDistance={1.5}
|
|
159
|
+
blurAmount={6}
|
|
160
|
+
opacityFrom={0.08}
|
|
161
|
+
opacityPeak={0.72}
|
|
162
|
+
scaleFrom={0.96}
|
|
163
|
+
scalePeak={1.025}
|
|
164
|
+
/>
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Tune `characterStagger`, `lineStagger`, `duration`, `waveDistance`,
|
|
170
|
+
`waveOvershoot`, `sidewaysDistance`, `blurAmount`, opacity, and scale props to
|
|
171
|
+
shape the reveal. The component keeps words together across natural line wraps
|
|
172
|
+
and provides a single screen-reader text string.
|
|
173
|
+
|
|
135
174
|
## CLI options
|
|
136
175
|
|
|
137
176
|
```sh
|
|
138
177
|
npx @istmx/ui add animated-text --no-install
|
|
139
178
|
npx @istmx/ui add animated-text --overwrite
|
|
179
|
+
npx @istmx/ui add text-reveal
|
|
140
180
|
npx @istmx/ui add image-accordion
|
|
141
181
|
npx @istmx/ui add image-trail
|
|
142
182
|
npx @istmx/ui add infinite-image-canvas
|
package/bin/istmx.mjs
CHANGED
|
@@ -6,13 +6,13 @@ import { access, mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
|
6
6
|
import path from 'node:path'
|
|
7
7
|
import { fileURLToPath } from 'node:url'
|
|
8
8
|
|
|
9
|
-
const VERSION = '0.1.
|
|
9
|
+
const VERSION = '0.1.9'
|
|
10
10
|
const COMPONENTS = {
|
|
11
11
|
button: { fileName: 'button.tsx' },
|
|
12
12
|
'animated-text': { fileName: 'animated-text.tsx' },
|
|
13
|
+
'text-reveal': { fileName: 'text-reveal.tsx' },
|
|
13
14
|
'infinite-image-canvas': { fileName: 'infinite-image-canvas.tsx' },
|
|
14
15
|
'image-trail': { fileName: 'image-trail.tsx' },
|
|
15
|
-
'image-effects-card': { fileName: 'image-effects-card.tsx' },
|
|
16
16
|
'image-accordion': { fileName: 'image-accordion.tsx' },
|
|
17
17
|
'dock-navigation': {
|
|
18
18
|
fileName: 'dock-navigation.tsx',
|
package/package.json
CHANGED
package/registry/button.tsx
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef, useState } from 'react'
|
|
4
|
-
import type {
|
|
5
|
-
AnchorHTMLAttributes,
|
|
6
|
-
ButtonHTMLAttributes,
|
|
7
|
-
ReactNode,
|
|
8
|
-
} from 'react'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
9
5
|
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
|
|
6
|
+
import type { HTMLMotionProps } from 'motion/react'
|
|
10
7
|
import { cn } from '@/lib/utils'
|
|
11
8
|
|
|
12
9
|
export type ButtonVariant = 'primary' | 'secondary' | 'outline' | 'ghost' | 'soft'
|
|
@@ -29,12 +26,12 @@ type SharedButtonProps = {
|
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
type NativeButtonProps = SharedButtonProps &
|
|
32
|
-
Omit<
|
|
29
|
+
Omit<HTMLMotionProps<'button'>, keyof SharedButtonProps> & {
|
|
33
30
|
href?: never
|
|
34
31
|
}
|
|
35
32
|
|
|
36
33
|
type ButtonAnchorProps = SharedButtonProps &
|
|
37
|
-
Omit<
|
|
34
|
+
Omit<HTMLMotionProps<'a'>, keyof SharedButtonProps | 'href'> & {
|
|
38
35
|
href: string
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { motion, useReducedMotion, useTransform } from 'motion/react'
|
|
4
|
+
import type { MotionValue } from 'motion/react'
|
|
5
|
+
import { cn } from '@/lib/utils'
|
|
6
|
+
|
|
7
|
+
export type TextRevealProps = {
|
|
8
|
+
text: string
|
|
9
|
+
className?: string
|
|
10
|
+
characterStagger?: number
|
|
11
|
+
lineStagger?: number
|
|
12
|
+
duration?: number
|
|
13
|
+
waveDistance?: number
|
|
14
|
+
waveOvershoot?: number
|
|
15
|
+
sidewaysDistance?: number
|
|
16
|
+
blurAmount?: number
|
|
17
|
+
opacityFrom?: number
|
|
18
|
+
opacityPeak?: number
|
|
19
|
+
scaleFrom?: number
|
|
20
|
+
scalePeak?: number
|
|
21
|
+
scrollProgress?: MotionValue<number>
|
|
22
|
+
once?: boolean
|
|
23
|
+
viewportAmount?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function ScrollRevealCharacter({
|
|
27
|
+
character,
|
|
28
|
+
progress,
|
|
29
|
+
start,
|
|
30
|
+
end,
|
|
31
|
+
waveDistance,
|
|
32
|
+
waveOvershoot,
|
|
33
|
+
sidewaysDistance,
|
|
34
|
+
blurAmount,
|
|
35
|
+
opacityFrom,
|
|
36
|
+
opacityPeak,
|
|
37
|
+
scaleFrom,
|
|
38
|
+
scalePeak,
|
|
39
|
+
}: {
|
|
40
|
+
character: string
|
|
41
|
+
progress: MotionValue<number>
|
|
42
|
+
start: number
|
|
43
|
+
end: number
|
|
44
|
+
waveDistance: number
|
|
45
|
+
waveOvershoot: number
|
|
46
|
+
sidewaysDistance: number
|
|
47
|
+
blurAmount: number
|
|
48
|
+
opacityFrom: number
|
|
49
|
+
opacityPeak: number
|
|
50
|
+
scaleFrom: number
|
|
51
|
+
scalePeak: number
|
|
52
|
+
}) {
|
|
53
|
+
const characterProgress = useTransform(progress, [start, end], [0, 1])
|
|
54
|
+
const opacity = useTransform(
|
|
55
|
+
characterProgress,
|
|
56
|
+
[0, 0.55, 1],
|
|
57
|
+
[opacityFrom, opacityPeak, 1],
|
|
58
|
+
)
|
|
59
|
+
const y = useTransform(
|
|
60
|
+
characterProgress,
|
|
61
|
+
[0, 0.55, 1],
|
|
62
|
+
[waveDistance, -waveOvershoot, 0],
|
|
63
|
+
)
|
|
64
|
+
const x = useTransform(
|
|
65
|
+
characterProgress,
|
|
66
|
+
[0, 0.55, 1],
|
|
67
|
+
[-sidewaysDistance, sidewaysDistance * 0.5, 0],
|
|
68
|
+
)
|
|
69
|
+
const scale = useTransform(
|
|
70
|
+
characterProgress,
|
|
71
|
+
[0, 0.55, 1],
|
|
72
|
+
[scaleFrom, scalePeak, 1],
|
|
73
|
+
)
|
|
74
|
+
const filter = useTransform(
|
|
75
|
+
characterProgress,
|
|
76
|
+
[0, 0.55, 1],
|
|
77
|
+
[`blur(${blurAmount}px)`, `blur(${blurAmount * 0.35}px)`, 'blur(0px)'],
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<motion.span
|
|
82
|
+
style={{ opacity, x, y, scale, filter }}
|
|
83
|
+
className="inline-block whitespace-pre"
|
|
84
|
+
>
|
|
85
|
+
{character}
|
|
86
|
+
</motion.span>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function TextReveal({
|
|
91
|
+
text,
|
|
92
|
+
className,
|
|
93
|
+
characterStagger = 0.024,
|
|
94
|
+
lineStagger = 0.24,
|
|
95
|
+
duration = 0.7,
|
|
96
|
+
waveDistance = 5,
|
|
97
|
+
waveOvershoot = waveDistance * 0.75,
|
|
98
|
+
sidewaysDistance = waveDistance * 0.12,
|
|
99
|
+
blurAmount = 7,
|
|
100
|
+
opacityFrom = 0.08,
|
|
101
|
+
opacityPeak = 0.72,
|
|
102
|
+
scaleFrom = 0.96,
|
|
103
|
+
scalePeak = 1.025,
|
|
104
|
+
scrollProgress,
|
|
105
|
+
once = true,
|
|
106
|
+
viewportAmount = 0.4,
|
|
107
|
+
}: TextRevealProps) {
|
|
108
|
+
const shouldReduceMotion = useReducedMotion()
|
|
109
|
+
const lines = text.split('\n')
|
|
110
|
+
const stagger = Math.max(0, characterStagger)
|
|
111
|
+
const lineDelay = Math.max(0, lineStagger)
|
|
112
|
+
const animationDuration = Math.max(0, duration)
|
|
113
|
+
const wave = Math.max(0, waveDistance)
|
|
114
|
+
const wavePeak = Math.max(0, waveOvershoot)
|
|
115
|
+
const sideways = Math.max(0, sidewaysDistance)
|
|
116
|
+
const blur = Math.max(0, blurAmount)
|
|
117
|
+
const startOpacity = Math.min(1, Math.max(0, opacityFrom))
|
|
118
|
+
const middleOpacity = Math.min(1, Math.max(startOpacity, opacityPeak))
|
|
119
|
+
const startScale = Math.max(0.01, scaleFrom)
|
|
120
|
+
const middleScale = Math.max(0.01, scalePeak)
|
|
121
|
+
const lineStartDelays: number[] = []
|
|
122
|
+
let nextLineDelay = 0
|
|
123
|
+
|
|
124
|
+
for (const line of lines) {
|
|
125
|
+
lineStartDelays.push(nextLineDelay)
|
|
126
|
+
nextLineDelay +=
|
|
127
|
+
Math.max(0, Array.from(line).length - 1) * stagger +
|
|
128
|
+
animationDuration +
|
|
129
|
+
lineDelay
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const characterCount = lines.reduce(
|
|
133
|
+
(total, line) => total + Array.from(line).length,
|
|
134
|
+
0,
|
|
135
|
+
)
|
|
136
|
+
const scrollCharacterWeight = Math.max(stagger, 0.001)
|
|
137
|
+
const scrollLineWeight = Math.max(0, lineDelay)
|
|
138
|
+
const totalScrollWeight =
|
|
139
|
+
characterCount * scrollCharacterWeight +
|
|
140
|
+
Math.max(0, lines.length - 1) * scrollLineWeight
|
|
141
|
+
let scrollWeight = 0
|
|
142
|
+
|
|
143
|
+
if (scrollProgress && !shouldReduceMotion) {
|
|
144
|
+
return (
|
|
145
|
+
<p className={cn('text-foreground', className)}>
|
|
146
|
+
<span className="sr-only">{text.replace(/\n/g, ' ')}</span>
|
|
147
|
+
{lines.map((line, lineIndex) => {
|
|
148
|
+
const words = line.match(/\s+|\S+/gu) ?? []
|
|
149
|
+
const content = words.map((word, wordIndex) => {
|
|
150
|
+
if (/^\s+$/u.test(word)) {
|
|
151
|
+
scrollWeight += Array.from(word).length * scrollCharacterWeight
|
|
152
|
+
return (
|
|
153
|
+
<span
|
|
154
|
+
key={`${wordIndex}-${word}`}
|
|
155
|
+
className="whitespace-pre-wrap"
|
|
156
|
+
>
|
|
157
|
+
{word}
|
|
158
|
+
</span>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const characters = Array.from(word).map((character) => {
|
|
163
|
+
const start = scrollWeight / totalScrollWeight
|
|
164
|
+
scrollWeight += scrollCharacterWeight
|
|
165
|
+
const end = scrollWeight / totalScrollWeight
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<ScrollRevealCharacter
|
|
169
|
+
key={`${start}-${character}`}
|
|
170
|
+
character={character}
|
|
171
|
+
progress={scrollProgress}
|
|
172
|
+
start={start}
|
|
173
|
+
end={end}
|
|
174
|
+
waveDistance={wave}
|
|
175
|
+
waveOvershoot={wavePeak}
|
|
176
|
+
sidewaysDistance={sideways}
|
|
177
|
+
blurAmount={blur}
|
|
178
|
+
opacityFrom={startOpacity}
|
|
179
|
+
opacityPeak={middleOpacity}
|
|
180
|
+
scaleFrom={startScale}
|
|
181
|
+
scalePeak={middleScale}
|
|
182
|
+
/>
|
|
183
|
+
)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
return (
|
|
187
|
+
<span
|
|
188
|
+
key={`${wordIndex}-${word}`}
|
|
189
|
+
className="inline-block whitespace-nowrap"
|
|
190
|
+
>
|
|
191
|
+
{characters}
|
|
192
|
+
</span>
|
|
193
|
+
)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
if (lineIndex < lines.length - 1) {
|
|
197
|
+
scrollWeight += scrollLineWeight
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<span
|
|
202
|
+
key={`${lineIndex}-${line}`}
|
|
203
|
+
aria-hidden="true"
|
|
204
|
+
className="block"
|
|
205
|
+
>
|
|
206
|
+
{content}
|
|
207
|
+
</span>
|
|
208
|
+
)
|
|
209
|
+
})}
|
|
210
|
+
</p>
|
|
211
|
+
)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<motion.p
|
|
216
|
+
initial={shouldReduceMotion ? false : 'hidden'}
|
|
217
|
+
whileInView="visible"
|
|
218
|
+
viewport={{ once, amount: Math.min(1, Math.max(0, viewportAmount)) }}
|
|
219
|
+
className={cn('text-foreground', className)}
|
|
220
|
+
>
|
|
221
|
+
<span className="sr-only">{text.replace(/\n/g, ' ')}</span>
|
|
222
|
+
{lines.map((line, lineIndex) => {
|
|
223
|
+
let characterIndex = 0
|
|
224
|
+
const words = line.match(/\s+|\S+/gu) ?? []
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<span
|
|
228
|
+
key={`${lineIndex}-${line}`}
|
|
229
|
+
aria-hidden="true"
|
|
230
|
+
className="block"
|
|
231
|
+
>
|
|
232
|
+
{words.map((word, wordIndex) => {
|
|
233
|
+
if (/^\s+$/u.test(word)) {
|
|
234
|
+
characterIndex += Array.from(word).length
|
|
235
|
+
return (
|
|
236
|
+
<span
|
|
237
|
+
key={`${wordIndex}-${word}`}
|
|
238
|
+
className="whitespace-pre-wrap"
|
|
239
|
+
>
|
|
240
|
+
{word}
|
|
241
|
+
</span>
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const characters = Array.from(word).map((character) => {
|
|
246
|
+
const delay =
|
|
247
|
+
lineStartDelays[lineIndex] + characterIndex * stagger
|
|
248
|
+
characterIndex += 1
|
|
249
|
+
|
|
250
|
+
return (
|
|
251
|
+
<motion.span
|
|
252
|
+
key={`${characterIndex}-${character}`}
|
|
253
|
+
custom={delay}
|
|
254
|
+
variants={{
|
|
255
|
+
hidden: {
|
|
256
|
+
opacity: startOpacity,
|
|
257
|
+
y: wave,
|
|
258
|
+
x: -sideways,
|
|
259
|
+
scale: startScale,
|
|
260
|
+
filter: `blur(${blur}px)`,
|
|
261
|
+
},
|
|
262
|
+
visible: shouldReduceMotion
|
|
263
|
+
? {
|
|
264
|
+
opacity: 1,
|
|
265
|
+
y: 0,
|
|
266
|
+
x: 0,
|
|
267
|
+
scale: 1,
|
|
268
|
+
filter: 'blur(0px)',
|
|
269
|
+
transition: { duration: 0 },
|
|
270
|
+
}
|
|
271
|
+
: (staggerDelay: number) => ({
|
|
272
|
+
opacity: 1,
|
|
273
|
+
y: [wave, -wavePeak, 0],
|
|
274
|
+
x: [-sideways, sideways * 0.5, 0],
|
|
275
|
+
scale: [startScale, middleScale, 1],
|
|
276
|
+
filter: ['blur(' + blur + 'px)', 'blur(0px)'],
|
|
277
|
+
transition: {
|
|
278
|
+
duration: animationDuration,
|
|
279
|
+
delay: staggerDelay,
|
|
280
|
+
ease: 'easeOut',
|
|
281
|
+
},
|
|
282
|
+
}),
|
|
283
|
+
}}
|
|
284
|
+
className="inline-block whitespace-pre"
|
|
285
|
+
>
|
|
286
|
+
{character}
|
|
287
|
+
</motion.span>
|
|
288
|
+
)
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
return (
|
|
292
|
+
<span
|
|
293
|
+
key={`${wordIndex}-${word}`}
|
|
294
|
+
className="inline-block whitespace-nowrap"
|
|
295
|
+
>
|
|
296
|
+
{characters}
|
|
297
|
+
</span>
|
|
298
|
+
)
|
|
299
|
+
})}
|
|
300
|
+
</span>
|
|
301
|
+
)
|
|
302
|
+
})}
|
|
303
|
+
</motion.p>
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export default TextReveal
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import { useEffect, useId, useRef, useState } from 'react'
|
|
4
|
-
import type { CSSProperties, PointerEvent } from 'react'
|
|
5
|
-
import Image from 'next/image'
|
|
6
|
-
|
|
7
|
-
export type ImageEffect = 'pixelate' | 'rgb-split' | 'glitch' | 'ascii'
|
|
8
|
-
|
|
9
|
-
export type ImageEffectsCardProps = {
|
|
10
|
-
src: string
|
|
11
|
-
alt: string
|
|
12
|
-
effect: ImageEffect
|
|
13
|
-
title: string
|
|
14
|
-
detail?: string
|
|
15
|
-
className?: string
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const LENS_MASK =
|
|
19
|
-
'radial-gradient(circle var(--lens-radius, 94px) at var(--pointer-x, 50%) var(--pointer-y, 50%), #000 0 52%, rgba(0,0,0,.92) 62%, rgba(0,0,0,.48) 78%, transparent 100%)'
|
|
20
|
-
const ASCII = ' .,:;irsXA253hMHGS#9B&@'
|
|
21
|
-
|
|
22
|
-
function drawEffectCanvas(
|
|
23
|
-
canvas: HTMLCanvasElement,
|
|
24
|
-
image: HTMLImageElement,
|
|
25
|
-
effect: 'pixelate' | 'ascii',
|
|
26
|
-
) {
|
|
27
|
-
const bounds = canvas.getBoundingClientRect()
|
|
28
|
-
const width = Math.max(1, Math.round(bounds.width))
|
|
29
|
-
const height = Math.max(1, Math.round(bounds.height))
|
|
30
|
-
const context = canvas.getContext('2d')
|
|
31
|
-
if (!context || !image.naturalWidth || !image.naturalHeight) return
|
|
32
|
-
canvas.width = width
|
|
33
|
-
canvas.height = height
|
|
34
|
-
|
|
35
|
-
const scale = Math.max(width / image.naturalWidth, height / image.naturalHeight)
|
|
36
|
-
const cropWidth = width / scale
|
|
37
|
-
const cropHeight = height / scale
|
|
38
|
-
const sx = (image.naturalWidth - cropWidth) / 2
|
|
39
|
-
const sy = (image.naturalHeight - cropHeight) / 2
|
|
40
|
-
|
|
41
|
-
if (effect === 'pixelate') {
|
|
42
|
-
const sample = document.createElement('canvas')
|
|
43
|
-
sample.width = Math.max(1, Math.ceil(width / 10))
|
|
44
|
-
sample.height = Math.max(1, Math.ceil(height / 10))
|
|
45
|
-
const small = sample.getContext('2d')
|
|
46
|
-
if (!small) return
|
|
47
|
-
small.drawImage(image, sx, sy, cropWidth, cropHeight, 0, 0, sample.width, sample.height)
|
|
48
|
-
context.imageSmoothingEnabled = false
|
|
49
|
-
context.drawImage(sample, 0, 0, width, height)
|
|
50
|
-
context.imageSmoothingEnabled = true
|
|
51
|
-
return
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const columns = Math.max(18, Math.round(width / 8))
|
|
55
|
-
const rows = Math.max(24, Math.round(height / 8))
|
|
56
|
-
const sample = document.createElement('canvas')
|
|
57
|
-
sample.width = columns
|
|
58
|
-
sample.height = rows
|
|
59
|
-
const small = sample.getContext('2d', { willReadFrequently: true })
|
|
60
|
-
if (!small) return
|
|
61
|
-
try {
|
|
62
|
-
small.drawImage(image, sx, sy, cropWidth, cropHeight, 0, 0, columns, rows)
|
|
63
|
-
const pixels = small.getImageData(0, 0, columns, rows).data
|
|
64
|
-
context.clearRect(0, 0, width, height)
|
|
65
|
-
context.font = `bold ${Math.max(6, width / columns)}px ui-monospace, monospace`
|
|
66
|
-
context.textAlign = 'center'
|
|
67
|
-
context.textBaseline = 'middle'
|
|
68
|
-
for (let y = 0; y < rows; y += 1) {
|
|
69
|
-
for (let x = 0; x < columns; x += 1) {
|
|
70
|
-
const offset = (y * columns + x) * 4
|
|
71
|
-
const red = pixels[offset]
|
|
72
|
-
const green = pixels[offset + 1]
|
|
73
|
-
const blue = pixels[offset + 2]
|
|
74
|
-
const lightness = (red * 0.3 + green * 0.59 + blue * 0.11) / 255
|
|
75
|
-
const character = ASCII[Math.min(ASCII.length - 1, Math.floor((1 - lightness) * (ASCII.length - 1)))]
|
|
76
|
-
context.fillStyle = `rgba(${Math.min(255, red + 48)}, ${Math.min(255, green + 48)}, ${Math.min(255, blue + 48)}, .98)`
|
|
77
|
-
context.fillText(character, (x + 0.5) * (width / columns), (y + 0.5) * (height / rows))
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
} catch {
|
|
81
|
-
// Canvas text remains hidden if the remote image host blocks pixel access.
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export default function ImageEffectsCard({
|
|
86
|
-
src,
|
|
87
|
-
alt,
|
|
88
|
-
effect,
|
|
89
|
-
title,
|
|
90
|
-
detail,
|
|
91
|
-
className = '',
|
|
92
|
-
}: ImageEffectsCardProps) {
|
|
93
|
-
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
94
|
-
const [active, setActive] = useState(false)
|
|
95
|
-
const filterId = `glitch-${useId().replace(/:/g, '')}`
|
|
96
|
-
const localMaskStyle: CSSProperties = {
|
|
97
|
-
maskImage: LENS_MASK,
|
|
98
|
-
WebkitMaskImage: LENS_MASK,
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
useEffect(() => {
|
|
102
|
-
if ((effect !== 'pixelate' && effect !== 'ascii') || !active) return
|
|
103
|
-
const canvas = canvasRef.current
|
|
104
|
-
if (!canvas) return
|
|
105
|
-
let cancelled = false
|
|
106
|
-
const image = new window.Image()
|
|
107
|
-
image.crossOrigin = 'anonymous'
|
|
108
|
-
image.src = src
|
|
109
|
-
const draw = () => {
|
|
110
|
-
if (!cancelled) drawEffectCanvas(canvas, image, effect)
|
|
111
|
-
}
|
|
112
|
-
image.decode().then(draw).catch(() => {})
|
|
113
|
-
const observer = new ResizeObserver(draw)
|
|
114
|
-
observer.observe(canvas)
|
|
115
|
-
return () => {
|
|
116
|
-
cancelled = true
|
|
117
|
-
observer.disconnect()
|
|
118
|
-
}
|
|
119
|
-
}, [active, effect, src])
|
|
120
|
-
|
|
121
|
-
function handlePointerEnter(event: PointerEvent<HTMLElement>) {
|
|
122
|
-
const radius = Math.round(76 + Math.random() * 52)
|
|
123
|
-
event.currentTarget.style.setProperty('--lens-radius', `${radius}px`)
|
|
124
|
-
setActive(true)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function handlePointerMove(event: PointerEvent<HTMLElement>) {
|
|
128
|
-
const bounds = event.currentTarget.getBoundingClientRect()
|
|
129
|
-
event.currentTarget.style.setProperty('--pointer-x', `${event.clientX - bounds.left}px`)
|
|
130
|
-
event.currentTarget.style.setProperty('--pointer-y', `${event.clientY - bounds.top}px`)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const layerTransition = `pointer-events-none absolute inset-0 transition-opacity duration-200 ease-out motion-reduce:transition-none ${active ? 'opacity-100' : 'opacity-0'}`
|
|
134
|
-
|
|
135
|
-
return (
|
|
136
|
-
<figure
|
|
137
|
-
onPointerEnter={handlePointerEnter}
|
|
138
|
-
onPointerMove={handlePointerMove}
|
|
139
|
-
onPointerLeave={() => setActive(false)}
|
|
140
|
-
className={`group relative isolate aspect-[4/5] overflow-hidden rounded-xl bg-neutral-950 ${className}`}
|
|
141
|
-
>
|
|
142
|
-
<Image
|
|
143
|
-
src={src}
|
|
144
|
-
alt={alt}
|
|
145
|
-
fill
|
|
146
|
-
unoptimized
|
|
147
|
-
sizes="(max-width: 640px) 50vw, (max-width: 1024px) 25vw, 260px"
|
|
148
|
-
className="object-cover"
|
|
149
|
-
/>
|
|
150
|
-
|
|
151
|
-
{(effect === 'pixelate' || effect === 'ascii') && (
|
|
152
|
-
<canvas
|
|
153
|
-
ref={canvasRef}
|
|
154
|
-
aria-hidden="true"
|
|
155
|
-
className={`${layerTransition} h-full w-full object-cover blur-[0.4px]`}
|
|
156
|
-
style={localMaskStyle}
|
|
157
|
-
/>
|
|
158
|
-
)}
|
|
159
|
-
|
|
160
|
-
{effect === 'rgb-split' && (
|
|
161
|
-
<div className={layerTransition} style={localMaskStyle} aria-hidden="true">
|
|
162
|
-
<div className="absolute inset-0 -translate-x-[3px] scale-[1.015] bg-cover bg-center mix-blend-screen blur-[0.6px]" style={{ backgroundImage: `url("${src}")`, filter: 'sepia(1) saturate(5) hue-rotate(292deg)' }} />
|
|
163
|
-
<div className="absolute inset-0 translate-x-[3px] scale-[1.015] bg-cover bg-center mix-blend-screen blur-[0.6px]" style={{ backgroundImage: `url("${src}")`, filter: 'sepia(1) saturate(5) hue-rotate(150deg)' }} />
|
|
164
|
-
</div>
|
|
165
|
-
)}
|
|
166
|
-
|
|
167
|
-
{effect === 'glitch' && (
|
|
168
|
-
<>
|
|
169
|
-
<svg className="absolute size-0" aria-hidden="true">
|
|
170
|
-
<filter id={filterId}>
|
|
171
|
-
<feTurbulence type="fractalNoise" baseFrequency="0.018 0.12" numOctaves="1" seed="3" result="noise" />
|
|
172
|
-
<feDisplacementMap in="SourceGraphic" in2="noise" scale="24" xChannelSelector="R" yChannelSelector="G" />
|
|
173
|
-
</filter>
|
|
174
|
-
</svg>
|
|
175
|
-
<div className={layerTransition} style={localMaskStyle} aria-hidden="true">
|
|
176
|
-
<div className="absolute inset-0 scale-[1.025] bg-cover bg-center blur-[0.55px]" style={{ backgroundImage: `url("${src}")`, filter: `url(#${filterId}) saturate(1.45) hue-rotate(8deg)` }} />
|
|
177
|
-
</div>
|
|
178
|
-
</>
|
|
179
|
-
)}
|
|
180
|
-
|
|
181
|
-
<div
|
|
182
|
-
aria-hidden="true"
|
|
183
|
-
className={`pointer-events-none absolute inset-0 transition-opacity duration-200 motion-reduce:transition-none ${active ? 'opacity-100' : 'opacity-0'}`}
|
|
184
|
-
style={{
|
|
185
|
-
...localMaskStyle,
|
|
186
|
-
background: 'radial-gradient(circle var(--lens-radius, 94px) at var(--pointer-x, 50%) var(--pointer-y, 50%), transparent 62%, rgba(255,255,255,.22) 76%, rgba(255,255,255,.05) 88%, transparent 100%)',
|
|
187
|
-
filter: 'blur(5px)',
|
|
188
|
-
}}
|
|
189
|
-
/>
|
|
190
|
-
|
|
191
|
-
<figcaption className="absolute inset-x-0 bottom-0 z-10 bg-gradient-to-t from-black/80 via-black/35 to-transparent px-3 pt-12 pb-3 text-white">
|
|
192
|
-
<p className="text-[10px] font-semibold tracking-[0.14em] uppercase">{title}</p>
|
|
193
|
-
{detail ? <p className="mt-1 text-[10px] text-white/70">{detail}</p> : null}
|
|
194
|
-
</figcaption>
|
|
195
|
-
</figure>
|
|
196
|
-
)
|
|
197
|
-
}
|