Skip to content

Commit

Permalink
feat: modeにwaveを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
imaimai17468 committed Aug 23, 2023
1 parent 6a285fe commit 87de8ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/AudioVisualizer/AudioVisualizer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const meta: Meta<typeof AudioVisualizer> = {
argTypes: {
mode: {
description: 'Mode of the visualizer',
options: ['bars', 'grid'],
options: ['bars', 'grid', 'wave'],
control: {
type: 'select',
},
Expand Down
22 changes: 21 additions & 1 deletion src/components/AudioVisualizer/AudioVisualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ const AudioVisualizer: React.FC<AudioVisualizerProps> = ({
} else if (mode === 'grid') {
const gridHeight = 10
for (let j = 0; j < barHeight / gridHeight; j++) {
ctx.fillRect(x, height - gridHeight * (j + 1), barWidth, gridHeight)
ctx.fillRect(
x,
height - gridHeight * (j + 1),
barWidth,
gridHeight,
)
ctx.strokeStyle = bgColor
ctx.beginPath()
ctx.moveTo(x, height - gridHeight * (j + 1))
Expand All @@ -81,6 +86,21 @@ const AudioVisualizer: React.FC<AudioVisualizerProps> = ({
x += barWidth
}

if (mode === 'wave') {
ctx.beginPath()
ctx.moveTo(0, height)
for (let i = 0; i < frequencyData.length; i++) {
ctx.lineTo(
(i / frequencyData.length) * width,
height - (frequencyData[i] / 255) * height,
)
}
ctx.lineTo(width, height)
ctx.closePath()
ctx.fillStyle = barColor
ctx.fill()
}

requestAnimationFrame(() => draw(analyser))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/AudioVisualizer/AudioVisualizer.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type AudioVisualizerProps = {
mode: 'bars' | 'grid'
mode: 'bars' | 'grid' | 'wave'
bgColor?: string;
barColor?: string;
width?: string;
Expand Down

0 comments on commit 87de8ab

Please sign in to comment.