Image Trail
componentry/image-trailFreeComponent
Component for image-trail
Live preview
live · rendered by the registry
Rendered by componentry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://componentry.dev/r/image-trail.jsonSource
1"use client"23import React, { useEffect, useRef } from "react"4import { Expo, gsap, Power1, Quint } from "gsap"56interface ImageTrailProps {7 images: string[]8 imageWidth?: number9 imageHeight?: number10 threshold?: number11 duration?: number12}1314export function ImageTrail({15 images = [],16 imageWidth = 200,17 imageHeight = 200,18 threshold = 50,19 duration = 1.6,20}: ImageTrailProps) {21 const contentRef = useRef<HTMLDivElement | null>(null)22 const imagesRef = useRef<HTMLImageElement[]>([])23 const mousePos = useRef({ x: 0, y: 0 })24 const cacheMousePos = useRef({ x: 0, y: 0 })25 const lastMousePos = useRef({ x: 0, y: 0 })26 const zIndexVal = useRef(1)27 const imgPosition = useRef(0)28 const parentSize = useRef({ width: 0, height: 0 })2930 useEffect(() => {31 if (contentRef.current) {32 imagesRef.current = Array.from(contentRef.current.querySelectorAll("img"))33 }3435 const handleMouseMove = (e: MouseEvent) => {36 const rect = contentRef.current?.getBoundingClientRect()37 if (rect) {38 mousePos.current = {39 x: e.clientX - rect.left,40 y: e.clientY - rect.top,41 }42 }43 }4445 calcParentSize()46 if (imagesRef.current.length === 0) {47 return48 }4950 window.addEventListener("mousemove", handleMouseMove)51 window.addEventListener("resize", calcParentSize)5253 requestAnimationFrame(renderImages)5455 return () => {56 window.removeEventListener("mousemove", handleMouseMove)57 window.removeEventListener("resize", calcParentSize)58 }59 // eslint-disable-next-line react-hooks/exhaustive-deps60 }, [])6162 const calcParentSize = () => {63 const rect = contentRef.current?.getBoundingClientRect()64 if (rect) {65 parentSize.current = { width: rect.width, height: rect.height }66 }67 }6869 const lerp = (a: number, b: number, n: number) => (1 - n) * a + n * b7071 const getMouseDistance = () => {72 const dx = mousePos.current.x - lastMousePos.current.x73 const dy = mousePos.current.y - lastMousePos.current.y74 return Math.hypot(dx, dy)75 }7677 const renderImages = () => {78 const distance = getMouseDistance()7980 cacheMousePos.current.x = lerp(81 cacheMousePos.current.x,82 mousePos.current.x,83 0.184 )85// … truncated
More from componentry
All 59 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Gradientanimated-gradient | componentry | Components | Free | no deps | |
| ASCII Effectascii-effect | componentry | Components | Free | no deps | |
| Aurora Flowaurora-flow | componentry | Components | Free | no deps | |
| auth-modalauth-modal | componentry | Components | Free | no deps | |
| border-beamborder-beam | componentry | Components | Free | no deps | |
| circuit-boardcircuit-board | componentry | Components | Free | no deps | |
| closing-plasmaclosing-plasma | componentry | Components | Free | no deps | |
| collection-surfercollection-surfer | componentry | Components | Free | no deps |
