Skip to content

Commit cba60ef

Browse files
committed
opt(LFO): optimize the gap of delay line and sine wave
1 parent bcce1fc commit cba60ef

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

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

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const EchoLFO = () => {
88

99
return (
1010
<section className="h-32 w-2/3 mb-20">
11-
<LFO speed={speed} frequency={frequency} delay={delay} lineWidth={5} />
11+
<LFO speed={speed} frequency={frequency} delay={delay} />
1212

1313
<Knob.Group size={50} trackWidth={3} min={0} max={1} step={0.1}>
1414
<Knob

packages/components/visualization/LFO/LFO.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,35 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
7171
const svg = d3.select(LFORef.current).select('svg').attr('width', width).attr('height', height)
7272
svg.selectAll('*').remove()
7373

74+
const data = d3.range(0, 4 * Math.PI * (speed * 10), 0.01).map((x) => {
75+
return {
76+
x,
77+
y: Math.sin(x) * frequency * (max - min) * 0.5 + (max + min) / 2,
78+
}
79+
})
80+
7481
if (delay > 0) {
82+
svg
83+
.append('circle')
84+
.attr('cx', delay)
85+
.attr('cy', height / 2)
86+
.attr('r', lineWidth / 2)
87+
.attr('fill', lineColor)
88+
7589
svg
7690
.append('line')
7791
.attr('x1', 0)
7892
.attr('y1', height / 2)
79-
.attr('x2', delay)
93+
.attr('x2', delay + 1)
8094
.attr('y2', height / 2)
8195
.attr('stroke', lineColor)
8296
.attr('stroke-width', lineWidth)
8397
}
8498

85-
const data = d3.range(0, 4 * Math.PI * (speed * 10), 0.01).map((x) => {
86-
return {
87-
x,
88-
y: Math.sin(x) * frequency * (max - min) * 0.5 + (max + min) / 2,
89-
}
90-
})
91-
9299
const line = d3
93100
.line<{ x: number; y: number }>()
94101
.x((d) => xScale.current!(d.x))
95102
.y((d) => yScale.current!(d.y))
96-
.curve(d3.curveBasis)
97103

98104
svg
99105
.append('path')
@@ -102,8 +108,7 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
102108
.attr('stroke', lineColor)
103109
.attr('stroke-width', lineWidth)
104110
.attr('d', line)
105-
// -1 to fix the gap between the line and the delay line
106-
.attr('transform', `translate(${delay - 0.8}, 0)`)
111+
.attr('transform', `translate(${delay}, 0)`)
107112
}
108113

109114
const { base, svg } = useStyle()

0 commit comments

Comments
 (0)