waveform
elevenlabs/waveformFreeComponent
shadcn/ui publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/elevenlabs/ui/main/apps/www/public/r/waveform.jsonSource
1"use client"23import {4 useCallback,5 useEffect,6 useMemo,7 useRef,8 useState,9 type HTMLAttributes,10} from "react"1112import { cn } from "@/lib/utils"1314export type WaveformProps = HTMLAttributes<HTMLDivElement> & {15 data?: number[]16 barWidth?: number17 barHeight?: number18 barGap?: number19 barRadius?: number20 barColor?: string21 fadeEdges?: boolean22 fadeWidth?: number23 height?: string | number24 active?: boolean25 onBarClick?: (index: number, value: number) => void26}2728export const Waveform = ({29 data = [],30 barWidth = 4,31 barHeight: baseBarHeight = 4,32 barGap = 2,33 barRadius = 2,34 barColor,35 fadeEdges = true,36 fadeWidth = 24,37 height = 128,38 onBarClick,39 className,40 ...props41}: WaveformProps) => {42 const canvasRef = useRef<HTMLCanvasElement>(null)43 const containerRef = useRef<HTMLDivElement>(null)44 const heightStyle = typeof height === "number" ? `${height}px` : height4546 useEffect(() => {47 const canvas = canvasRef.current48 const container = containerRef.current49 if (!canvas || !container) return5051 const resizeObserver = new ResizeObserver(() => {52 const rect = container.getBoundingClientRect()53 const dpr = window.devicePixelRatio || 15455 canvas.width = rect.width * dpr56 canvas.height = rect.height * dpr57 canvas.style.width = `${rect.width}px`58 canvas.style.height = `${rect.height}px`5960 const ctx = canvas.getContext("2d")61 if (ctx) {62 ctx.scale(dpr, dpr)63 renderWaveform()64 }65 })6667 const renderWaveform = () => {68 const ctx = canvas.getContext("2d")69 if (!ctx) return7071 const rect = canvas.getBoundingClientRect()72 ctx.clearRect(0, 0, rect.width, rect.height)7374 const computedBarColor =75 barColor ||76 getComputedStyle(canvas).getPropertyValue("--foreground") ||77 "#000"7879 const barCount = Math.floor(rect.width / (barWidth + barGap))80 const centerY = rect.height / 28182 for (let i = 0; i < barCount; i++) {83 const dataIndex = Math.floor((i / barCount) * data.length)84 const value = data[dataIndex] || 085 const barHeight = Math.max(baseBarHeight, value * rect.height * 0.8)86 const x = i * (barWidth + barGap)87 const y = centerY - barHeight / 28889 ctx.fillStyle = computedBarColor90 ctx.globalAlpha = 0.3 + value * 0.79192 if (barRadius > 0) {93 ctx.beginPath()94 ctx.roundRect(x, y, barWidth, barHeight, barRadius)95 ctx.fill()96// … truncated
Files it writes
More from shadcn/ui
All 50 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| audio-playeraudio-player | shadcn/ui | Components | Free | 2 deps | |
| bar-visualizerbar-visualizer | shadcn/ui | Components | Free | no deps | |
| conversationconversation | shadcn/ui | Components | Free | 1 dep | |
| conversation-barconversation-bar | shadcn/ui | Components | Free | 1 dep | |
| live-waveformlive-waveform | shadcn/ui | Components | Free | no deps | |
| matrixmatrix | shadcn/ui | Components | Free | no deps | |
| messagemessage | shadcn/ui | Components | Free | 1 dep | |
| mic-selectormic-selector | shadcn/ui | Components | Free | no deps |
