11import React , { useState , useRef , useEffect } from 'react' ;
22
33const numRings = 15 ;
4- const segmentsPerRing = 50 ;
4+ const segmentsPerRing = 25 ;
55
66const AudioVisualizer = ( ) => {
77 const [ rings , setRings ] = useState ( [ ] ) ;
88 const audioContextRef = useRef ( null ) ;
99 const analyserRef = useRef ( null ) ;
1010 const animationFrameIdRef = useRef ( null ) ;
1111 const rotationRef = useRef ( 0 ) ;
12+ const [ viewportSize , setViewportSize ] = useState ( { width : window . innerWidth , height : window . innerHeight } ) ;
1213
1314 useEffect ( ( ) => {
15+ const handleResize = ( ) => {
16+ setViewportSize ( { width : window . innerWidth , height : window . innerHeight } ) ;
17+ } ;
18+ window . addEventListener ( 'resize' , handleResize ) ;
19+ return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
20+ } , [ ] ) ;
21+
22+ useEffect ( ( ) => {
23+ const maxRadius = Math . min ( viewportSize . width , viewportSize . height ) / 2 - 50 ;
1424 const initialRings = Array . from ( { length : numRings } , ( _ , i ) => {
15- const baseRadius = i * 30 + 50 ;
25+ const baseRadius = ( i / numRings ) * maxRadius + 20 ;
1626 const segments = Array . from ( { length : segmentsPerRing } , ( _ , j ) => ( {
1727 id : j ,
1828 angle : ( j / segmentsPerRing ) * 2 * Math . PI ,
@@ -57,7 +67,7 @@ const AudioVisualizer = () => {
5767 const amplitudeFactor = frequencyAmplitude / 50 ;
5868
5969 const newSegments = ring . segments . map ( segment =>
60- ( { ...segment , radius : ring . baseRadius + amplitudeFactor * 25 } ) ) ;
70+ ( { ...segment , radius : ring . baseRadius + amplitudeFactor * ( viewportSize . width / 150 ) } ) ) ;
6171
6272 return {
6373 ...ring ,
@@ -87,7 +97,7 @@ const AudioVisualizer = () => {
8797 audioContextRef . current . close ( ) ;
8898 }
8999 } ;
90- } , [ ] ) ;
100+ } , [ viewportSize ] ) ;
91101
92102 const containerStyle = {
93103 position : 'fixed' ,
@@ -114,7 +124,7 @@ const AudioVisualizer = () => {
114124 < div style = { containerStyle } >
115125 < div style = { visualizerWrapperStyle } >
116126 { rings . map ( ring => {
117- const ringDotSize = Math . round ( 3 + ring . amplitudeFactor ) ;
127+ const ringDotSize = Math . round ( 3 + ring . amplitudeFactor * ( viewportSize . width / 2000 ) ) ;
118128 return (
119129 < div key = { ring . id } >
120130 { ring . segments . map ( segment => (
0 commit comments