Skip to content

Commit

Permalink
feat: add color props
Browse files Browse the repository at this point in the history
propsから背景色とバーの色を動的に変えられるようにする
  • Loading branch information
imaimai17468 committed Aug 21, 2023
1 parent 8080680 commit 142029f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
8 changes: 0 additions & 8 deletions src/components/AudioVisualizer/AudioVisualizer.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
.story-container {
width: 500px;
height: 100px;
background-color: #fff;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}

.story-container-full {
width: 100%;
height: 100px;
background-color: #fff;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
22 changes: 16 additions & 6 deletions src/components/AudioVisualizer/AudioVisualizer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@ const meta: Meta<typeof AudioVisualizer> = {
title: 'common/AudioVisualizer',
component: AudioVisualizer,
tags: ['autodocs'],
argTypes: {},
argTypes: {
bgColor: {
control: 'color',
},
barColor: {
control: 'color',
},
}
}
export default meta
type Story = StoryObj<typeof AudioVisualizer>

export const Default: Story = {
args: {},
render: () => (
args: {
bgColor: '#fff',
barColor: '#000'
},
render: ({bgColor, barColor}) => (
<div className={styles['story-container-full']}>
<AudioVisualizer />
<AudioVisualizer bgColor={bgColor} barColor={barColor} />
</div>
),
}

export const FixedWidth: Story = {
args: {},
render: () => (
render: ({ bgColor, barColor }) => (
<div className={styles['story-container']}>
<AudioVisualizer />
<AudioVisualizer bgColor={bgColor} barColor={barColor} />
</div>
),
}
13 changes: 9 additions & 4 deletions src/components/AudioVisualizer/AudioVisualizer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { useEffect, useRef } from 'react'
import styles from './AudioVisualizer.module.css'
import { AudioVisualizerProps } from './AudioVisualizer.types'

const AudioVisualizer = () => {
const AudioVisualizer: React.FC<AudioVisualizerProps> = ({
bgColor = '#fff',
barColor = '#000'
}) => {
const containerRef = useRef<HTMLDivElement>(null)
const canvasRef = useRef<HTMLCanvasElement>(null)

Expand Down Expand Up @@ -33,7 +37,7 @@ const AudioVisualizer = () => {
draw(analyser)
})
.catch((err) => console.log('The following error occurred: ' + err))
}, [])
}, [bgColor, barColor])

const draw = (analyser: AnalyserNode) => {
if (canvasRef.current) {
Expand All @@ -46,7 +50,8 @@ const AudioVisualizer = () => {
const width = canvas.width
const height = canvas.height

ctx.clearRect(0, 0, width, height)
ctx.fillStyle = bgColor
ctx.fillRect(0, 0, width, height)

const barWidth = width / frequencyData.length
let barHeight
Expand All @@ -55,7 +60,7 @@ const AudioVisualizer = () => {
for (let i = 0; i < frequencyData.length; i++) {
barHeight = (frequencyData[i] / 255) * height

ctx.fillStyle = '#233E71'
ctx.fillStyle = barColor
ctx.fillRect(x, height - barHeight, barWidth, barHeight)

x += barWidth
Expand Down
4 changes: 4 additions & 0 deletions src/components/AudioVisualizer/AudioVisualizer.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type AudioVisualizerProps = {
bgColor?: string;
barColor?: string;
}

0 comments on commit 142029f

Please sign in to comment.