|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Image from "next/image"; |
| 4 | +import { useState, useEffect } from "react"; |
| 5 | +import staticLogoSrc from "@/../public/logo/buggie_bugchan_logo.png"; |
| 6 | +import spriteSrc from "@/../public/logo/jsconf_bugchan_v5_minified.png"; |
| 7 | + |
| 8 | +type Props = { |
| 9 | + width: number; |
| 10 | + height: number; |
| 11 | + alt: string; |
| 12 | + fetchPriority?: "high" | "low" | "auto"; |
| 13 | +}; |
| 14 | + |
| 15 | +const FRAME_COUNT = 29; |
| 16 | +const SPRITE_WIDTH = 400; |
| 17 | +const SPRITE_HEIGHT = 11600; |
| 18 | +const ANIMATION_DURATION = 1500; |
| 19 | + |
| 20 | +export function AnimatedLogo({ |
| 21 | + width, |
| 22 | + height, |
| 23 | + alt, |
| 24 | + fetchPriority, |
| 25 | +}: Props) { |
| 26 | + const [isAnimating, setIsAnimating] = useState(false); |
| 27 | + const [isSpriteLoaded, setIsSpriteLoaded] = useState(false); |
| 28 | + const [clickCount, setClickCount] = useState(0); |
| 29 | + |
| 30 | + // detect when sprite sheet has fully loaded |
| 31 | + useEffect(() => { |
| 32 | + const img = new window.Image(); |
| 33 | + img.onload = () => { |
| 34 | + setIsSpriteLoaded(true); |
| 35 | + }; |
| 36 | + img.src = spriteSrc.src; |
| 37 | + }, []); |
| 38 | + |
| 39 | + const handleClick = () => { |
| 40 | + if (isAnimating || !isSpriteLoaded) return; |
| 41 | + |
| 42 | + setClickCount(clickCount + 1); |
| 43 | + setIsAnimating(true); |
| 44 | + setTimeout(() => { |
| 45 | + setIsAnimating(false); |
| 46 | + }, ANIMATION_DURATION); |
| 47 | + }; |
| 48 | + |
| 49 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 50 | + // when focused, allow to animate the logo by pressing Enter or Space key |
| 51 | + if (e.key === "Enter" || e.key === " ") { |
| 52 | + e.preventDefault(); |
| 53 | + handleClick(); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const isReverse = clickCount % 3 === 0; |
| 58 | + |
| 59 | + |
| 60 | + return ( |
| 61 | + <div |
| 62 | + className="logo-container" |
| 63 | + onClick={handleClick} |
| 64 | + onKeyDown={handleKeyDown} |
| 65 | + role="button" |
| 66 | + tabIndex={0} |
| 67 | + aria-label={`${alt}. Click to animate.`} |
| 68 | + aria-busy={!isSpriteLoaded} |
| 69 | + style={{ |
| 70 | + width: `${width}px`, |
| 71 | + height: `${height}px`, |
| 72 | + flexShrink: 0, |
| 73 | + cursor: isSpriteLoaded ? "pointer" : "default", |
| 74 | + }} |
| 75 | + > |
| 76 | + {/* show static logo only if sprite hasn't loaded yet */} |
| 77 | + {!isSpriteLoaded && ( |
| 78 | + <Image |
| 79 | + src={staticLogoSrc} |
| 80 | + fetchPriority={fetchPriority} |
| 81 | + alt={alt} |
| 82 | + width={width} |
| 83 | + height={height} |
| 84 | + style={{ |
| 85 | + width: `${width}px`, |
| 86 | + height: `${height}px`, |
| 87 | + objectFit: "contain", |
| 88 | + }} |
| 89 | + /> |
| 90 | + )} |
| 91 | + |
| 92 | + {/* after sprite is loaded, show sprite at frame 0 (idle) / animating */} |
| 93 | + {isSpriteLoaded && ( |
| 94 | + <div |
| 95 | + role="img" |
| 96 | + aria-label={alt} |
| 97 | + style={{ |
| 98 | + width: `${width}px`, |
| 99 | + height: `${height}px`, |
| 100 | + backgroundImage: `url(${spriteSrc.src})`, |
| 101 | + backgroundSize: `${width}px ${(SPRITE_HEIGHT / SPRITE_WIDTH) * width}px`, |
| 102 | + animation: isAnimating |
| 103 | + ? `sprite-animate ${ANIMATION_DURATION}ms steps(${FRAME_COUNT - 1}) 1 ${isReverse ? "reverse" : "normal"}` |
| 104 | + : "none", |
| 105 | + }} |
| 106 | + /> |
| 107 | + )} |
| 108 | + |
| 109 | + <style jsx>{` |
| 110 | + .logo-container { |
| 111 | + transition: transform 0.2s ease-out; |
| 112 | + } |
| 113 | + |
| 114 | + .logo-container:hover { |
| 115 | + transform: scale(1.05); |
| 116 | + } |
| 117 | + |
| 118 | + @keyframes sprite-animate { |
| 119 | + from { |
| 120 | + background-position: 0 0; |
| 121 | + } |
| 122 | + to { |
| 123 | + background-position: 0 -${((FRAME_COUNT - 1) * height)}px; |
| 124 | + } |
| 125 | + } |
| 126 | + `}</style> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
| 130 | + |
0 commit comments