|
| 1 | +import * as React from 'react' |
| 2 | +import { useTrail, useChain, useSprings, animated, useSpringRef } from '@react-spring/web' |
| 3 | + |
| 4 | +import styles from './styles.module.css' |
| 5 | + |
| 6 | +const COORDS = [ |
| 7 | + [50, 30], |
| 8 | + [90, 30], |
| 9 | + [50, 50], |
| 10 | + [60, 60], |
| 11 | + [70, 60], |
| 12 | + [80, 60], |
| 13 | + [90, 50], |
| 14 | +] |
| 15 | + |
| 16 | +const STROKE_WIDTH = 0.5 |
| 17 | + |
| 18 | +const OFFSET = STROKE_WIDTH / 2 |
| 19 | + |
| 20 | +const MAX_WIDTH = 150 + OFFSET * 2 |
| 21 | +const MAX_HEIGHT = 100 + OFFSET * 2 |
| 22 | + |
| 23 | +export default function App() { |
| 24 | + const gridApi = useSpringRef() |
| 25 | + |
| 26 | + const gridSprings = useTrail(16, { |
| 27 | + ref: gridApi, |
| 28 | + from: { |
| 29 | + x2: 0, |
| 30 | + y2: 0, |
| 31 | + }, |
| 32 | + to: { |
| 33 | + x2: MAX_WIDTH, |
| 34 | + y2: MAX_HEIGHT, |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + const boxApi = useSpringRef() |
| 39 | + |
| 40 | + const [boxSprings] = useSprings(7, i => ({ |
| 41 | + ref: boxApi, |
| 42 | + from: { |
| 43 | + scale: 0, |
| 44 | + }, |
| 45 | + to: { |
| 46 | + scale: 1, |
| 47 | + }, |
| 48 | + delay: i * 200, |
| 49 | + config: { |
| 50 | + mass: 2, |
| 51 | + tension: 220, |
| 52 | + }, |
| 53 | + })) |
| 54 | + |
| 55 | + useChain([gridApi, boxApi], [0, 1], 1500) |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className={styles['background-container']}> |
| 59 | + <div className={styles.container}> |
| 60 | + <svg viewBox={`0 0 ${MAX_WIDTH} ${MAX_HEIGHT}`}> |
| 61 | + <g> |
| 62 | + {gridSprings.map(({ x2 }, index) => ( |
| 63 | + <animated.line |
| 64 | + x1={0} |
| 65 | + y1={index * 10 + OFFSET} |
| 66 | + x2={x2} |
| 67 | + y2={index * 10 + OFFSET} |
| 68 | + key={index} |
| 69 | + strokeWidth={STROKE_WIDTH} |
| 70 | + stroke="currentColor" |
| 71 | + /> |
| 72 | + ))} |
| 73 | + {gridSprings.map(({ y2 }, index) => ( |
| 74 | + <animated.line |
| 75 | + x1={index * 10 + OFFSET} |
| 76 | + y1={0} |
| 77 | + x2={index * 10 + OFFSET} |
| 78 | + y2={y2} |
| 79 | + key={index} |
| 80 | + strokeWidth={STROKE_WIDTH} |
| 81 | + stroke="currentColor" |
| 82 | + /> |
| 83 | + ))} |
| 84 | + </g> |
| 85 | + {boxSprings.map(({ scale }, index) => ( |
| 86 | + <animated.rect |
| 87 | + key={index} |
| 88 | + width={10} |
| 89 | + height={10} |
| 90 | + fill="currentColor" |
| 91 | + style={{ |
| 92 | + transformOrigin: `${5 + OFFSET * 2}px ${5 + OFFSET * 2}px`, |
| 93 | + transform: `translate(${COORDS[index][0] + OFFSET}px, ${COORDS[index][1] + OFFSET}px)`, |
| 94 | + scale, |
| 95 | + }} |
| 96 | + /> |
| 97 | + ))} |
| 98 | + </svg> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ) |
| 102 | +} |
0 commit comments