Skip to content

Commit 33e5bac

Browse files
committed
feat(LFO): update props and realize speed effects
1 parent 42d200e commit 33e5bac

4 files changed

Lines changed: 66 additions & 23 deletions

File tree

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import { LFO } from '@echo-ui'
1+
import React from 'react'
2+
import { LFO, Knob } from '@echo-ui'
23

34
export const EchoLFO = () => {
5+
const [speed, setSpeed] = React.useState(5)
6+
47
return (
58
<section className="h-32 w-2/3">
6-
<LFO />
9+
<LFO speed={speed} />
10+
11+
<div>
12+
<Knob value={speed} onChange={setSpeed} min={0} max={10} step={0.1} sensitivity={2} />
13+
14+
{speed}
15+
</div>
716
</section>
817
)
918
}

packages/components/visualization/LFO/LFO.tsx

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
import * as d3 from 'd3'
2-
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'
2+
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'
33
import { useResizeObserver } from '../../../lib/hooks/useResizeObserver'
44
import { cn } from '../../../lib/utils'
55
import { LFOProps, LFORef } from './types'
66
import { useStyle } from './styles'
7-
import { WIDTH, HEIGHT, LINE_WIDTH, LINE_COLOR } from './constants'
7+
import {
8+
TYPE,
9+
DELAY,
10+
SPEED,
11+
WIDTH,
12+
HEIGHT,
13+
LINE_WIDTH,
14+
LINE_COLOR,
15+
MIN,
16+
MAX,
17+
AMPLITUDE,
18+
FREQUENCY,
19+
} from './constants'
820

921
export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
10-
const { lineWidth = LINE_WIDTH, lineColor = LINE_COLOR, ...restProps } = props
22+
const {
23+
type = TYPE,
24+
delay = DELAY,
25+
speed = SPEED,
26+
amplitude = AMPLITUDE,
27+
frequency = FREQUENCY,
28+
min = MIN,
29+
max = MAX,
30+
lineWidth = LINE_WIDTH,
31+
lineColor = LINE_COLOR,
32+
...restProps
33+
} = props
1134

1235
const LFORef = useRef<LFORef | null>(null)
1336
const xScale = useRef<d3.ScaleLinear<number, number> | null>(null)
@@ -17,46 +40,45 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
1740

1841
const dimensions = useResizeObserver(LFORef, WIDTH, HEIGHT, () => {
1942
generateScales()
20-
generateLine()
43+
generateWave()
2144
})
2245

2346
const generateScales = () => {
2447
const { width, height } = dimensions.current
2548

2649
xScale.current = d3
2750
.scaleLinear()
28-
.domain([0, 4 * Math.PI])
51+
.domain([0, 4 * Math.PI * speed])
2952
.range([0, width])
53+
yScale.current = d3.scaleLinear().domain([min, max]).range([height, 0])
54+
}
55+
56+
useEffect(() => {
57+
generateScales()
58+
generateWave()
59+
}, [speed])
3060

31-
yScale.current = d3.scaleLinear().domain([-1, 1]).range([height, 0])
61+
const generateWave = () => {
62+
generateSineWave()
3263
}
3364

34-
const generateLine = () => {
65+
const generateSineWave = () => {
66+
if (type !== 'sine') return
67+
3568
const { width, height } = dimensions.current
3669

3770
const svg = d3.select(LFORef.current).select('svg').attr('width', width).attr('height', height)
38-
3971
svg.selectAll('*').remove()
4072

41-
const data = d3.range(0, 4 * Math.PI, 0.01).map((x) => ({
73+
const data = d3.range(0, 4 * Math.PI * speed, 0.01).map((x) => ({
4274
x,
4375
y: Math.sin(x),
4476
}))
4577

4678
const line = d3
4779
.line<{ x: number; y: number }>()
48-
.x((d) => xScale(d.x))
49-
.y((d) => yScale(d.y))
50-
51-
const xScale = d3
52-
.scaleLinear()
53-
.domain([0, 4 * Math.PI])
54-
.range([0, width])
55-
56-
const yScale = d3
57-
.scaleLinear()
58-
.domain([-1, 1])
59-
.range([height - 4, 4])
80+
.x((d) => xScale.current!(d.x))
81+
.y((d) => yScale.current!(d.y))
6082

6183
svg
6284
.append('path')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
export const WIDTH = 360
22
export const HEIGHT = 100
33

4+
export const TYPE = 'sine'
5+
export const FREQUENCY = 0.5
6+
export const AMPLITUDE = 0.5
7+
export const DELAY = 0
8+
export const SPEED = 10
9+
export const MIN = -10
10+
export const MAX = 10
11+
412
export const LINE_COLOR = 'var(--echo-primary)'
513
export const LINE_WIDTH = 3

packages/components/visualization/LFO/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export interface LFOProps extends React.HTMLAttributes<HTMLDivElement> {
22
frequency?: number
33
amplitude?: number
4+
min?: number
5+
max?: number
6+
delay?: number
7+
speed?: number
48
type?: 'sine' | 'square' | 'triangle'
59
lineColor?: string
610
lineWidth?: number

0 commit comments

Comments
 (0)