@@ -7,32 +7,28 @@ import { useStyle } from './styles'
77import {
88 TYPE ,
99 DELAY ,
10- SPEED ,
1110 WIDTH ,
1211 HEIGHT ,
1312 LINE_WIDTH ,
1413 LINE_COLOR ,
15- MIN ,
16- MAX ,
14+ AMPLITUDE ,
1715 FREQUENCY ,
1816} from './constants'
1917
2018export const LFO = forwardRef < LFORef , LFOProps > ( ( props , ref ) => {
2119 const {
2220 type = TYPE ,
2321 frequency : _frequency = FREQUENCY ,
22+ amplitude : _anplitude = AMPLITUDE ,
2423 delay : _delay = DELAY ,
25- speed : _speed = SPEED ,
26- min = MIN ,
27- max = MAX ,
2824 lineWidth = LINE_WIDTH ,
2925 lineColor = LINE_COLOR ,
3026 ...restProps
3127 } = props
3228
3329 const frequency = validValue ( _frequency , 0 , 1 )
34- const speed = validValue ( _speed , 0.1 , 1 )
35- const delay = validValue ( _delay , 1 , 100 )
30+ const amplitude = validValue ( _anplitude , 0 , 1 )
31+ const delay = validValue ( _delay , 0 , 100 )
3632
3733 const LFORef = useRef < LFORef | null > ( null )
3834 const xScale = useRef < d3 . ScaleLinear < number , number > | null > ( null )
@@ -43,7 +39,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
4339 useEffect ( ( ) => {
4440 generateScales ( )
4541 generateWave ( )
46- } , [ speed , frequency , delay , type , lineWidth , lineColor ] )
42+ } , [ frequency , amplitude , delay , type , lineWidth , lineColor ] )
4743
4844 const dimensions = useResizeObserver (
4945 LFORef ,
@@ -56,7 +52,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
5652 true ,
5753 {
5854 frequency,
59- speed ,
55+ amplitude ,
6056 delay,
6157 } ,
6258 )
@@ -66,9 +62,9 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
6662
6763 xScale . current = d3
6864 . scaleLinear ( )
69- . domain ( [ 0 , 4 * Math . PI * ( speed * 10 ) ] )
65+ . domain ( [ 0 , 4 * Math . PI * ( frequency * 10 ) ] )
7066 . range ( [ 0 , width ] )
71- yScale . current = d3 . scaleLinear ( ) . domain ( [ min , max ] ) . range ( [ height , 0 ] )
67+ yScale . current = d3 . scaleLinear ( ) . domain ( [ 0 , 1 ] ) . range ( [ height , 0 ] )
7268 }
7369
7470 const generateWave = ( ) => {
@@ -79,6 +75,17 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
7975
8076 svg . selectAll ( '*' ) . remove ( )
8177
78+ if ( frequency === 0 ) {
79+ svg
80+ . append ( 'line' )
81+ . attr ( 'x1' , 0 )
82+ . attr ( 'y1' , height / 2 )
83+ . attr ( 'x2' , width )
84+ . attr ( 'y2' , height / 2 )
85+ . attr ( 'stroke' , lineColor )
86+ . attr ( 'stroke-width' , lineWidth )
87+ }
88+
8289 if ( delay > 0 ) {
8390 svg
8491 . append ( 'circle' )
@@ -104,8 +111,8 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
104111 const generateSineWave = ( svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ) => {
105112 if ( type !== 'sine' || ! svg ) return
106113
107- const data = d3 . range ( 0 , 4 * Math . PI * ( speed * 10 ) , 0.01 ) . map ( ( x ) => {
108- const y = Math . sin ( x ) * frequency * ( max - min ) * 0.5 + ( max + min ) / 2
114+ const data = d3 . range ( 0 , 4 * Math . PI * ( frequency * 10 ) , 0.01 ) . map ( ( x ) => {
115+ const y = Math . sin ( x ) * amplitude * 0.5 + 0.5
109116 return { x, y }
110117 } )
111118
@@ -127,12 +134,10 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
127134 const generateSquareWave = ( svg : d3 . Selection < d3 . BaseType , unknown , null , undefined > ) => {
128135 if ( type !== 'square' || ! svg ) return
129136
130- const center = ( max + min ) / 2
131- const halfHeight = ( ( max - min ) * frequency ) / 2
132-
133- const data = [ { x : 0 , y : center } ] . concat (
134- d3 . range ( 0 , 4 * Math . PI * ( speed * 10 ) , 0.01 ) . map ( ( x ) => {
135- const y = Math . floor ( x / Math . PI ) % 2 === 0 ? center + halfHeight : center - halfHeight
137+ const halfHeight = 0.5 * amplitude
138+ const data = [ { x : 0 , y : 0.5 } ] . concat (
139+ d3 . range ( 0 , 4 * Math . PI * ( frequency * 10 ) , 0.01 ) . map ( ( x ) => {
140+ const y = Math . floor ( x / Math . PI ) % 2 === 0 ? 0.5 + halfHeight : 0.5 - halfHeight
136141 return { x, y }
137142 } ) ,
138143 )
0 commit comments