Echo Stack
A word focuses in at the center while outline copies of it unfold above and below and keep drifting, like the one clear line on a spinning drum.
Source
components/kuchi/echo-stack.tsx
1"use client";23import { loadFont } from "@remotion/google-fonts/Inter";4import { useId } from "react";5import { Easing, interpolate, useCurrentFrame, useVideoConfig } from "remotion";67const { fontFamily: INTER } = loadFont("normal", {8 weights: ["100", "900"],9 subsets: ["latin"],10});1112export type EchoStackDirection = "up" | "down";1314export interface EchoStackProps {15 text?: string;16 fontSize?: number;17 fontWeight?: number;18 fontFamily?: string;19 color?: string;20 echoColor?: string;21 echoes?: number;22 echoOpacity?: number;23 strokeWidth?: number;24 direction?: EchoStackDirection;25 cycle?: number;26 speed?: number;27 className?: string;28}2930export const echoStackDefaults = {31 text: "Bold.",32 fontSize: 170,33 fontWeight: 800,34 fontFamily: INTER,35 color: "#fafafa",36 echoColor: "",37 echoes: 3,38 echoOpacity: 0.35,39 strokeWidth: 1.5,40 direction: "up",41 cycle: 60,42 speed: 1,43} satisfies Required<Omit<EchoStackProps, "className">>;4445/** Frame where the entrance has finished; the drift keeps running after it. */46export const echoStackLength = 26;4748/** Distance between echo rows, in em. */49export const echoStackPitch = 1.05;5051/** Echoes closer to the center than this, in pitches, hide behind the word. */52const HIDDEN = 0.55;53const MAIN = [0, 14] as const;54const UNFOLD = [8, 26] as const;55const FADE_IN = [8, 24] as const;56const START_SPREAD = 0.6;57const MAX_BLUR = 12;5859const CLAMP = {60 extrapolateLeft: "clamp",61 extrapolateRight: "clamp",62} as const;6364const easeOutCubic = Easing.out(Easing.cubic);6566function finite(value: number | undefined, fallback: number) {67 return typeof value === "number" && Number.isFinite(value) ? value : fallback;68}6970function smoothstep(x: number) {71 const c = Math.min(1, Math.max(0, x));72 return c * c * (3 - 2 * c);73}7475/** Maps a composition frame onto the 30 fps timeline, scaled by `speed`. */76export function getEchoStackTime(77 frame: number,78 options: { fps?: number; speed?: number } = {},79) {80 const fps = Math.max(1, finite(options.fps, 30));81 const speed = Math.max(0, finite(options.speed, 1));82 return (Math.max(0, finite(frame, 0)) * 30 * speed) / fps;83}8485/** The main word focusing in. Blur is in reference px. */86export function getEchoStackMain(t: number) {87 const k = interpolate(t, [...MAIN], [0, 1], {88 ...CLAMP,89 easing: easeOutCubic,90 });91 return { blur: MAX_BLUR * (1 - k), opacity: k, scale: 1.04 - 0.04 * k };92}9394/** Echo visibility at a distance from the center, in pitches, before `echoOpacity`. */95export function getEchoStackFade(distance: number, echoes: number) {96 const d = Math.abs(distance);97 if (d < HIDDEN) return 0;98 if (d < 1) return smoothstep((d - HIDDEN) / (1 - HIDDEN));99 const reach = Math.max(1, echoes + 0.5);100 return Math.max(0, 1 - (d - 1) / (reach - 1));101}102103export interface EchoStackRow {104 /** Signed distance from the center in pitches, negative above. */105 offset: number;106 /** Row position in pitches after the unfold, negative above. */107 position: number;108 opacity: number;109}110111export function getEchoStackRows(112 t: number,113 options: {114 echoes?: number;115 echoOpacity?: number;116 cycle?: number;117 direction?: EchoStackDirection;118 } = {},119): EchoStackRow[] {120 const echoes = Math.max(0, Math.round(finite(options.echoes, 3)));121 const echoOpacity = Math.max(0, finite(options.echoOpacity, 0.35));122 const cycle = Math.max(1, finite(options.cycle, 60));123 const sign = options.direction === "down" ? 1 : -1;124 const phase = sign * ((t / cycle) % 1);125 const spread = interpolate(t, [...UNFOLD], [START_SPREAD, 1], {126 ...CLAMP,127 easing: easeOutCubic,128 });129 const fadeIn = interpolate(t, [...FADE_IN], [0, 1], {130 ...CLAMP,131 easing: easeOutCubic,132 });133 const rows: EchoStackRow[] = [];134 const reach = echoes + 2;135 for (let k = -reach; k <= reach; k++) {136 const offset = k + phase;137 const opacity = echoOpacity * fadeIn * getEchoStackFade(offset, echoes);138 if (opacity <= 0) continue;139 rows.push({ offset, position: offset * spread, opacity });140 }141 return rows;142}143144export function EchoStack({145 text = echoStackDefaults.text,146 fontSize = echoStackDefaults.fontSize,147 fontWeight = echoStackDefaults.fontWeight,148 fontFamily = echoStackDefaults.fontFamily,149 color = echoStackDefaults.color,150 echoColor = echoStackDefaults.echoColor,151 echoes = echoStackDefaults.echoes,152 echoOpacity = echoStackDefaults.echoOpacity,153 strokeWidth = echoStackDefaults.strokeWidth,154 direction = echoStackDefaults.direction,155 cycle = echoStackDefaults.cycle,156 speed = echoStackDefaults.speed,157 className,158}: EchoStackProps) {159 const frame = useCurrentFrame();160 const { fps, height } = useVideoConfig();161 const unit = height / 720;162 const size = fontSize * unit;163 const t = getEchoStackTime(frame, { fps, speed });164 const main = getEchoStackMain(t);165 const rows = getEchoStackRows(t, { echoes, echoOpacity, cycle, direction });166 const pitch = echoStackPitch * size;167 const outlineId = `echo-stack-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;168169 const type = {170 fontFamily,171 fontSize: size,172 lineHeight: 1,173 fontWeight,174 fontVariationSettings: `"wght" ${fontWeight}`,175 whiteSpace: "pre",176 } as const;177178 return (179 <div180 className={className}181 style={{182 position: "absolute",183 inset: 0,184 display: "flex",185 alignItems: "center",186 justifyContent: "center",187 overflow: "hidden",188 }}189 >190 {/* Outline from the filled silhouette: the variable font's overlapping191 contours would show through a text stroke. */}192 <svg width={0} height={0} style={{ position: "absolute" }} aria-hidden>193 <defs>194 <filter id={outlineId}>195 <feMorphology196 in="SourceAlpha"197 operator="erode"198 radius={strokeWidth * unit}199 result="inner"200 />201 <feComposite202 in="SourceAlpha"203 in2="inner"204 operator="out"205 result="ring"206 />207 <feFlood floodColor={echoColor || color} />208 <feComposite in2="ring" operator="in" />209 </filter>210 </defs>211 </svg>212 <div style={{ position: "relative" }}>213 {rows.map((row, i) => (214 <div215 key={i}216 aria-hidden217 style={{218 ...type,219 position: "absolute",220 left: "50%",221 top: "50%",222 color: echoColor || color,223 filter: `url(#${outlineId})`,224 opacity: row.opacity,225 transform: `translate(-50%, calc(-50% + ${row.position * pitch}px))`,226 }}227 >228 {text}229 </div>230 ))}231 <div232 style={{233 ...type,234 position: "relative",235 color,236 opacity: main.opacity,237 filter:238 main.blur > 0.05 ? `blur(${main.blur * unit}px)` : undefined,239 transform: `scale(${main.scale})`,240 }}241 >242 {text}243 </div>244 </div>245 </div>246 );247}









