@@ -15,6 +15,8 @@ import {
1515 FREQUENCY ,
1616} from './constants'
1717
18+ type DataItem = { x : number ; y : number }
19+
1820export const LFO = forwardRef < LFORef , LFOProps > ( ( props , ref ) => {
1921 const {
2022 type = TYPE ,
@@ -72,9 +74,9 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
7274
7375 const { width, height } = dimensions . current
7476 const svg = d3 . select ( LFORef . current ) . select ( 'svg' ) . attr ( 'width' , width ) . attr ( 'height' , height )
75-
7677 svg . selectAll ( '*' ) . remove ( )
7778
79+ // Draw the center line if the amplitude is 0
7880 if ( frequency === 0 ) {
7981 svg
8082 . append ( 'line' )
@@ -86,6 +88,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
8688 . attr ( 'stroke-width' , lineWidth )
8789 }
8890
91+ // Draw the delay line
8992 if ( delay > 0 ) {
9093 svg
9194 . append ( 'circle' )
@@ -104,21 +107,23 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
104107 . attr ( 'stroke-width' , lineWidth )
105108 }
106109
107- generateSineWave ( svg )
108- generateSquareWave ( svg )
109- generateTriangleWave ( svg )
110- }
111-
112- const generateSineWave = ( svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ) => {
113- if ( type !== 'sine' || ! svg ) return
114-
115- const data = d3 . range ( 0 , 4 * Math . PI * ( frequency * 10 ) , 0.01 ) . map ( ( x ) => {
116- const y = Math . sin ( x ) * amplitude * 0.5 + 0.5
117- return { x, y }
118- } )
110+ let data : DataItem [ ] = [ ]
111+ switch ( type ) {
112+ case 'sine' :
113+ data = generateSineWaveData ( svg )
114+ break
115+ case 'square' :
116+ data = generateSquareWaveData ( svg )
117+ break
118+ case 'triangle' :
119+ data = generateTriangleWaveData ( svg )
120+ break
121+ default :
122+ break
123+ }
119124
120125 const line = d3
121- . line < { x : number ; y : number } > ( )
126+ . line < DataItem > ( )
122127 . x ( ( d ) => xScale . current ! ( d . x ) )
123128 . y ( ( d ) => yScale . current ! ( d . y ) )
124129
@@ -132,8 +137,23 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
132137 . attr ( 'transform' , `translate(${ delay } , 0)` )
133138 }
134139
135- const generateSquareWave = ( svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ) => {
136- if ( type !== 'square' || ! svg ) return
140+ const generateSineWaveData = (
141+ svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ,
142+ ) : { x : number ; y : number } [ ] => {
143+ if ( type !== 'sine' || ! svg ) return [ ]
144+
145+ const data = d3 . range ( 0 , 4 * Math . PI * ( frequency * 10 ) , 0.01 ) . map ( ( x ) => {
146+ const y = Math . sin ( x ) * amplitude * 0.5 + 0.5
147+ return { x, y }
148+ } )
149+
150+ return data
151+ }
152+
153+ const generateSquareWaveData = (
154+ svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ,
155+ ) : DataItem [ ] => {
156+ if ( type !== 'square' || ! svg ) return [ ]
137157
138158 const centerY = 0.5
139159 const halfHeight = centerY * amplitude
@@ -144,23 +164,13 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
144164 } ) ,
145165 )
146166
147- const line = d3
148- . line < { x : number ; y : number } > ( )
149- . x ( ( d ) => xScale . current ! ( d . x ) )
150- . y ( ( d ) => yScale . current ! ( d . y ) )
151-
152- svg
153- . append ( 'path' )
154- . datum ( data )
155- . attr ( 'fill' , 'none' )
156- . attr ( 'stroke' , lineColor )
157- . attr ( 'stroke-width' , lineWidth )
158- . attr ( 'd' , line )
159- . attr ( 'transform' , `translate(${ delay } , 0)` )
167+ return data
160168 }
161169
162- const generateTriangleWave = ( svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ) => {
163- if ( type !== 'triangle' || ! svg ) return
170+ const generateTriangleWaveData = (
171+ svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ,
172+ ) : DataItem [ ] => {
173+ if ( type !== 'triangle' || ! svg ) return [ ]
164174
165175 const centerY = 0.5
166176 const data = d3 . range ( 0 , 4 * Math . PI * ( frequency * 10 ) , 0.01 ) . map ( ( x ) => {
@@ -177,19 +187,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
177187 return { x, y }
178188 } )
179189
180- const line = d3
181- . line < { x : number ; y : number } > ( )
182- . x ( ( d ) => xScale . current ! ( d . x ) )
183- . y ( ( d ) => yScale . current ! ( d . y ) )
184-
185- svg
186- . append ( 'path' )
187- . datum ( data )
188- . attr ( 'fill' , 'none' )
189- . attr ( 'stroke' , lineColor )
190- . attr ( 'stroke-width' , lineWidth )
191- . attr ( 'd' , line )
192- . attr ( 'transform' , `translate(${ delay } , 0)` )
190+ return data
193191 }
194192
195193 const { base, svg } = useStyle ( )
0 commit comments