Skip to content

Commit a6011e2

Browse files
committed
feat(LFO): realize frequence effect
1 parent 33e5bac commit a6011e2

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

  • example/src/components/visualizaion
  • packages/components/visualization/LFO

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@ import React from 'react'
22
import { LFO, Knob } from '@echo-ui'
33

44
export const EchoLFO = () => {
5-
const [speed, setSpeed] = React.useState(5)
5+
const [speed, setSpeed] = React.useState(0.1)
6+
const [frequency, setFrequency] = React.useState(0.1)
67

78
return (
8-
<section className="h-32 w-2/3">
9-
<LFO speed={speed} />
9+
<section className="h-32 w-2/3 mb-20">
10+
<LFO speed={speed} frequency={frequency} />
1011

11-
<div>
12-
<Knob value={speed} onChange={setSpeed} min={0} max={10} step={0.1} sensitivity={2} />
12+
<Knob.Group size={50} trackWidth={3}>
13+
<Knob
14+
value={frequency}
15+
onChange={setFrequency}
16+
min={0}
17+
max={1}
18+
step={0.1}
19+
topLabel="Frequency"
20+
bottomLabel={frequency * 100 + '%'}
21+
/>
1322

14-
{speed}
15-
</div>
23+
<Knob
24+
value={speed}
25+
onChange={setSpeed}
26+
min={0.1}
27+
max={1}
28+
step={0.1}
29+
topLabel="Speed"
30+
bottomLabel={speed * 100 + '%'}
31+
/>
32+
</Knob.Group>
1633
</section>
1734
)
1835
}

packages/components/visualization/LFO/LFO.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as d3 from 'd3'
22
import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'
33
import { useResizeObserver } from '../../../lib/hooks/useResizeObserver'
4-
import { cn } from '../../../lib/utils'
4+
import { cn, validValue } from '../../../lib/utils'
55
import { LFOProps, LFORef } from './types'
66
import { useStyle } from './styles'
77
import {
@@ -14,24 +14,25 @@ import {
1414
LINE_COLOR,
1515
MIN,
1616
MAX,
17-
AMPLITUDE,
1817
FREQUENCY,
1918
} from './constants'
2019

2120
export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
2221
const {
2322
type = TYPE,
23+
frequency: _frequency = FREQUENCY,
2424
delay = DELAY,
25-
speed = SPEED,
26-
amplitude = AMPLITUDE,
27-
frequency = FREQUENCY,
25+
speed: _speed = SPEED,
2826
min = MIN,
2927
max = MAX,
3028
lineWidth = LINE_WIDTH,
3129
lineColor = LINE_COLOR,
3230
...restProps
3331
} = props
3432

33+
const speed = validValue(_speed, 0.1, 1)
34+
const frequency = validValue(_frequency, 0, 1)
35+
3536
const LFORef = useRef<LFORef | null>(null)
3637
const xScale = useRef<d3.ScaleLinear<number, number> | null>(null)
3738
const yScale = useRef<d3.ScaleLinear<number, number> | null>(null)
@@ -48,15 +49,15 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
4849

4950
xScale.current = d3
5051
.scaleLinear()
51-
.domain([0, 4 * Math.PI * speed])
52+
.domain([0, 4 * Math.PI * (speed * 10)])
5253
.range([0, width])
5354
yScale.current = d3.scaleLinear().domain([min, max]).range([height, 0])
5455
}
5556

5657
useEffect(() => {
5758
generateScales()
5859
generateWave()
59-
}, [speed])
60+
}, [speed, frequency])
6061

6162
const generateWave = () => {
6263
generateSineWave()
@@ -70,9 +71,9 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
7071
const svg = d3.select(LFORef.current).select('svg').attr('width', width).attr('height', height)
7172
svg.selectAll('*').remove()
7273

73-
const data = d3.range(0, 4 * Math.PI * speed, 0.01).map((x) => ({
74+
const data = d3.range(0, 4 * Math.PI * (speed * 10), 0.01).map((x) => ({
7475
x,
75-
y: Math.sin(x),
76+
y: Math.sin(x) * frequency * (max - min) * 0.5 + (max + min) / 2,
7677
}))
7778

7879
const line = d3

0 commit comments

Comments
 (0)