Skip to content

Commit 42d200e

Browse files
committed
feat(LFO): update sine line
1 parent 63a02f4 commit 42d200e

3 files changed

Lines changed: 56 additions & 5 deletions

File tree

example/src/components/visualizaion/EchoLFO.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { LFO } from '@echo-ui'
22

33
export const EchoLFO = () => {
44
return (
5-
<section className="h-32">
5+
<section className="h-32 w-2/3">
66
<LFO />
77
</section>
88
)

packages/components/visualization/LFO/LFO.tsx

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { forwardRef, useImperativeHandle, useRef } from 'react'
1+
import * as d3 from 'd3'
2+
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'
23
import { useResizeObserver } from '../../../lib/hooks/useResizeObserver'
34
import { cn } from '../../../lib/utils'
45
import { LFOProps, LFORef } from './types'
@@ -9,12 +10,62 @@ export const LFO = forwardRef<LFORef, LFOProps>((props, ref) => {
910
const { lineWidth = LINE_WIDTH, lineColor = LINE_COLOR, ...restProps } = props
1011

1112
const LFORef = useRef<LFORef | null>(null)
13+
const xScale = useRef<d3.ScaleLinear<number, number> | null>(null)
14+
const yScale = useRef<d3.ScaleLinear<number, number> | null>(null)
1215

1316
useImperativeHandle(ref, () => LFORef.current as LFORef)
1417

15-
const dementions = useResizeObserver(LFORef, WIDTH, HEIGHT, () => {})
18+
const dimensions = useResizeObserver(LFORef, WIDTH, HEIGHT, () => {
19+
generateScales()
20+
generateLine()
21+
})
1622

17-
console.log(dementions)
23+
const generateScales = () => {
24+
const { width, height } = dimensions.current
25+
26+
xScale.current = d3
27+
.scaleLinear()
28+
.domain([0, 4 * Math.PI])
29+
.range([0, width])
30+
31+
yScale.current = d3.scaleLinear().domain([-1, 1]).range([height, 0])
32+
}
33+
34+
const generateLine = () => {
35+
const { width, height } = dimensions.current
36+
37+
const svg = d3.select(LFORef.current).select('svg').attr('width', width).attr('height', height)
38+
39+
svg.selectAll('*').remove()
40+
41+
const data = d3.range(0, 4 * Math.PI, 0.01).map((x) => ({
42+
x,
43+
y: Math.sin(x),
44+
}))
45+
46+
const line = d3
47+
.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])
60+
61+
svg
62+
.append('path')
63+
.datum(data)
64+
.attr('fill', 'none')
65+
.attr('stroke', lineColor)
66+
.attr('stroke-width', lineWidth)
67+
.attr('d', line)
68+
}
1869

1970
const { base, svg } = useStyle()
2071

packages/components/visualization/LFO/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export const WIDTH = 360
22
export const HEIGHT = 100
33

44
export const LINE_COLOR = 'var(--echo-primary)'
5-
export const LINE_WIDTH = 2
5+
export const LINE_WIDTH = 3

0 commit comments

Comments
 (0)