This project demonstrates a dynamic SVG-based dragon animation using React. It leverages modern React hooks like useRef, useState, and useEffect to create a responsive, interactive animation that reacts to user input.
- Responsive SVG Animation: A dragon that animates dynamically based on user input or auto-animation mode.
- Interactive Controls: Buttons to toggle animation, cursor movement, and auto-animation.
- Performance Optimization:
useReffor Elements: Prevents unnecessary re-renders by storing elements (elems) inuseRef.- Efficient Event Listeners: Proper handling of
resizeandpointermoveevents with cleanup during unmount to prevent memory leaks. - One-Time Initialization: Elements (
elemsRef) are initialized only once outside ofuseEffectto ensure state preservation between renders.
- React: For building the UI and handling state management.
- SVG: To render and animate vector graphics.
- JavaScript/TypeScript: Leveraging TypeScript for type safety and robust development.
-
Using
useReffor Elements
const elemsRef = useRef<{ use: SVGUseElement | null; x: number; y: number }[]>([]); -
One-Time Initialization of Elements
if (elemsRef.current.length === 0) { for (let i = 0; i < N; i++) { elemsRef.current[i] = { use: null, x: window.innerWidth / 2, y: 0 }; // Prepend elements conditionally if (i === 1) prepend("Cabeza", i); else if (i === 8 || i === 14) prepend("Aletas", i); else prepend("Espina", i); } }
-
Event Listener Cleanup
useEffect(() => { window.addEventListener("resize", resize); if (allowCursorMovement) { window.addEventListener("pointermove", pointerMoveHandler); } return () => { window.removeEventListener("resize", resize); window.removeEventListener("pointermove", pointerMoveHandler); cancelAnimationFrame(animationFrameIdRef.current); }; }, [allowCursorMovement]);
Prerequisites
Node.js (v14+ recommended)
npm or yarn package manager
Installation 1- Clone the repository:
git clone https://github.com/RyomenDev/DragonQuest.git
2- Navigate to the project directory:
cd DragonQuest
3- Install dependencies:
npm install
Run the Application Start the development server:
npm run dev
