@wq-hook/volcano-react 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -678,10 +678,10 @@ interface AudioWaveVisualizerProps {
678
678
  color?: string | [string, string];
679
679
  /** 波形条数 */
680
680
  bars?: number;
681
- /** 高度(px) */
682
- height?: number;
683
- /** 宽度(px) */
684
- width?: number;
681
+ /** 高度:数字(像素)或 CSS 字符串(如 "60px"、"100%"、"5rem") */
682
+ height?: number | string;
683
+ /** 宽度:数字(像素)或 CSS 字符串(如 "100%"、"80%"、"300px") */
684
+ width?: number | string;
685
685
  /** 自定义类名 */
686
686
  className?: string;
687
687
  /** 自定义样式 */
package/dist/index.d.ts CHANGED
@@ -678,10 +678,10 @@ interface AudioWaveVisualizerProps {
678
678
  color?: string | [string, string];
679
679
  /** 波形条数 */
680
680
  bars?: number;
681
- /** 高度(px) */
682
- height?: number;
683
- /** 宽度(px) */
684
- width?: number;
681
+ /** 高度:数字(像素)或 CSS 字符串(如 "60px"、"100%"、"5rem") */
682
+ height?: number | string;
683
+ /** 宽度:数字(像素)或 CSS 字符串(如 "100%"、"80%"、"300px") */
684
+ width?: number | string;
685
685
  /** 自定义类名 */
686
686
  className?: string;
687
687
  /** 自定义样式 */
package/dist/index.js CHANGED
@@ -1865,10 +1865,40 @@ var AudioWaveVisualizer = ({
1865
1865
  styleObj
1866
1866
  }) => {
1867
1867
  const canvasRef = (0, import_react5.useRef)(null);
1868
+ const containerRef = (0, import_react5.useRef)(null);
1868
1869
  const requestRef = (0, import_react5.useRef)(null);
1870
+ const isWidthResponsive = typeof width === "string";
1871
+ const isHeightResponsive = typeof height === "string";
1872
+ const [canvasWidth, setCanvasWidth] = (0, import_react5.useState)(() => {
1873
+ return typeof width === "number" ? width : 200;
1874
+ });
1875
+ const [canvasHeight, setCanvasHeight] = (0, import_react5.useState)(() => {
1876
+ return typeof height === "number" ? height : 60;
1877
+ });
1878
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1879
+ (0, import_react5.useEffect)(() => {
1880
+ if (!containerRef.current) return;
1881
+ if (!isWidthResponsive && !isHeightResponsive) return;
1882
+ const container = containerRef.current;
1883
+ const resizeObserver = new ResizeObserver((entries) => {
1884
+ for (const entry of entries) {
1885
+ const { width: containerWidth, height: containerHeight } = entry.contentRect;
1886
+ if (isWidthResponsive) {
1887
+ setCanvasWidth(Math.floor(containerWidth * dpr));
1888
+ }
1889
+ if (isHeightResponsive) {
1890
+ setCanvasHeight(Math.floor(containerHeight * dpr));
1891
+ }
1892
+ }
1893
+ });
1894
+ resizeObserver.observe(container);
1895
+ return () => {
1896
+ resizeObserver.disconnect();
1897
+ };
1898
+ }, [isWidthResponsive, isHeightResponsive, dpr]);
1869
1899
  const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
1870
1900
  const textColor = Array.isArray(color) ? color[0] : color;
1871
- const draw = () => {
1901
+ const draw = (0, import_react5.useCallback)(() => {
1872
1902
  const canvas = canvasRef.current;
1873
1903
  if (!canvas) return;
1874
1904
  const ctx = canvas.getContext("2d");
@@ -1889,9 +1919,10 @@ var AudioWaveVisualizer = ({
1889
1919
  ctx.strokeStyle = fillStyle;
1890
1920
  if (style === "bar" && frequencyData) {
1891
1921
  const barWidth = w / bars;
1892
- const step = Math.floor(frequencyData.length / bars);
1893
1922
  for (let i = 0; i < bars; i++) {
1894
- const value = frequencyData[i * step] || 0;
1923
+ const halfLength = frequencyData.length / 2;
1924
+ const index = Math.floor(i / bars * halfLength);
1925
+ const value = frequencyData[index] || 0;
1895
1926
  const percent = value / 255;
1896
1927
  const barHeight = h * percent;
1897
1928
  ctx.fillRect(i * barWidth, h - barHeight, barWidth - 2, barHeight);
@@ -1929,7 +1960,7 @@ var AudioWaveVisualizer = ({
1929
1960
  if (isPlaying && !isPaused) {
1930
1961
  requestRef.current = requestAnimationFrame(draw);
1931
1962
  }
1932
- };
1963
+ }, [isPlaying, isPaused, frequencyData, timeDomainData, style, color, bars]);
1933
1964
  (0, import_react5.useEffect)(() => {
1934
1965
  if (isPlaying && !isPaused) {
1935
1966
  requestRef.current = requestAnimationFrame(draw);
@@ -1946,26 +1977,36 @@ var AudioWaveVisualizer = ({
1946
1977
  frequencyData,
1947
1978
  timeDomainData,
1948
1979
  style,
1949
- color
1980
+ color,
1981
+ draw
1950
1982
  ]);
1983
+ const actualCanvasWidth = isWidthResponsive ? canvasWidth : width;
1984
+ const actualCanvasHeight = isHeightResponsive ? canvasHeight : height;
1951
1985
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1952
1986
  "div",
1953
1987
  {
1988
+ ref: containerRef,
1954
1989
  className,
1955
1990
  style: {
1956
1991
  display: "inline-flex",
1957
1992
  flexDirection: "column",
1958
1993
  gap: "4px",
1959
- width,
1994
+ width: isWidthResponsive ? width : width,
1995
+ height: isHeightResponsive ? height : height,
1960
1996
  ...styleObj
1961
1997
  },
1962
1998
  children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1963
1999
  "canvas",
1964
2000
  {
1965
2001
  ref: canvasRef,
1966
- width,
1967
- height,
1968
- style: { display: "block" }
2002
+ width: actualCanvasWidth,
2003
+ height: actualCanvasHeight,
2004
+ style: {
2005
+ display: "block",
2006
+ // 响应式模式下,CSS 尺寸为 100%;固定模式下使用像素值
2007
+ width: isWidthResponsive ? "100%" : actualCanvasWidth,
2008
+ height: isHeightResponsive ? "100%" : actualCanvasHeight
2009
+ }
1969
2010
  }
1970
2011
  )
1971
2012
  }
package/dist/index.mjs CHANGED
@@ -1805,7 +1805,7 @@ function useStreamTTS({
1805
1805
  }
1806
1806
 
1807
1807
  // src/components/AudioWaveVisualizer.tsx
1808
- import { useEffect as useEffect4, useRef as useRef5 } from "react";
1808
+ import { useEffect as useEffect4, useRef as useRef5, useState as useState5, useCallback as useCallback5 } from "react";
1809
1809
  import { jsx } from "react/jsx-runtime";
1810
1810
  var AudioWaveVisualizer = ({
1811
1811
  isPlaying,
@@ -1821,10 +1821,40 @@ var AudioWaveVisualizer = ({
1821
1821
  styleObj
1822
1822
  }) => {
1823
1823
  const canvasRef = useRef5(null);
1824
+ const containerRef = useRef5(null);
1824
1825
  const requestRef = useRef5(null);
1826
+ const isWidthResponsive = typeof width === "string";
1827
+ const isHeightResponsive = typeof height === "string";
1828
+ const [canvasWidth, setCanvasWidth] = useState5(() => {
1829
+ return typeof width === "number" ? width : 200;
1830
+ });
1831
+ const [canvasHeight, setCanvasHeight] = useState5(() => {
1832
+ return typeof height === "number" ? height : 60;
1833
+ });
1834
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
1835
+ useEffect4(() => {
1836
+ if (!containerRef.current) return;
1837
+ if (!isWidthResponsive && !isHeightResponsive) return;
1838
+ const container = containerRef.current;
1839
+ const resizeObserver = new ResizeObserver((entries) => {
1840
+ for (const entry of entries) {
1841
+ const { width: containerWidth, height: containerHeight } = entry.contentRect;
1842
+ if (isWidthResponsive) {
1843
+ setCanvasWidth(Math.floor(containerWidth * dpr));
1844
+ }
1845
+ if (isHeightResponsive) {
1846
+ setCanvasHeight(Math.floor(containerHeight * dpr));
1847
+ }
1848
+ }
1849
+ });
1850
+ resizeObserver.observe(container);
1851
+ return () => {
1852
+ resizeObserver.disconnect();
1853
+ };
1854
+ }, [isWidthResponsive, isHeightResponsive, dpr]);
1825
1855
  const progressBackground = Array.isArray(color) ? `linear-gradient(90deg, ${color[0]}, ${color[1]})` : color;
1826
1856
  const textColor = Array.isArray(color) ? color[0] : color;
1827
- const draw = () => {
1857
+ const draw = useCallback5(() => {
1828
1858
  const canvas = canvasRef.current;
1829
1859
  if (!canvas) return;
1830
1860
  const ctx = canvas.getContext("2d");
@@ -1845,9 +1875,10 @@ var AudioWaveVisualizer = ({
1845
1875
  ctx.strokeStyle = fillStyle;
1846
1876
  if (style === "bar" && frequencyData) {
1847
1877
  const barWidth = w / bars;
1848
- const step = Math.floor(frequencyData.length / bars);
1849
1878
  for (let i = 0; i < bars; i++) {
1850
- const value = frequencyData[i * step] || 0;
1879
+ const halfLength = frequencyData.length / 2;
1880
+ const index = Math.floor(i / bars * halfLength);
1881
+ const value = frequencyData[index] || 0;
1851
1882
  const percent = value / 255;
1852
1883
  const barHeight = h * percent;
1853
1884
  ctx.fillRect(i * barWidth, h - barHeight, barWidth - 2, barHeight);
@@ -1885,7 +1916,7 @@ var AudioWaveVisualizer = ({
1885
1916
  if (isPlaying && !isPaused) {
1886
1917
  requestRef.current = requestAnimationFrame(draw);
1887
1918
  }
1888
- };
1919
+ }, [isPlaying, isPaused, frequencyData, timeDomainData, style, color, bars]);
1889
1920
  useEffect4(() => {
1890
1921
  if (isPlaying && !isPaused) {
1891
1922
  requestRef.current = requestAnimationFrame(draw);
@@ -1902,26 +1933,36 @@ var AudioWaveVisualizer = ({
1902
1933
  frequencyData,
1903
1934
  timeDomainData,
1904
1935
  style,
1905
- color
1936
+ color,
1937
+ draw
1906
1938
  ]);
1939
+ const actualCanvasWidth = isWidthResponsive ? canvasWidth : width;
1940
+ const actualCanvasHeight = isHeightResponsive ? canvasHeight : height;
1907
1941
  return /* @__PURE__ */ jsx(
1908
1942
  "div",
1909
1943
  {
1944
+ ref: containerRef,
1910
1945
  className,
1911
1946
  style: {
1912
1947
  display: "inline-flex",
1913
1948
  flexDirection: "column",
1914
1949
  gap: "4px",
1915
- width,
1950
+ width: isWidthResponsive ? width : width,
1951
+ height: isHeightResponsive ? height : height,
1916
1952
  ...styleObj
1917
1953
  },
1918
1954
  children: /* @__PURE__ */ jsx(
1919
1955
  "canvas",
1920
1956
  {
1921
1957
  ref: canvasRef,
1922
- width,
1923
- height,
1924
- style: { display: "block" }
1958
+ width: actualCanvasWidth,
1959
+ height: actualCanvasHeight,
1960
+ style: {
1961
+ display: "block",
1962
+ // 响应式模式下,CSS 尺寸为 100%;固定模式下使用像素值
1963
+ width: isWidthResponsive ? "100%" : actualCanvasWidth,
1964
+ height: isHeightResponsive ? "100%" : actualCanvasHeight
1965
+ }
1925
1966
  }
1926
1967
  )
1927
1968
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wq-hook/volcano-react",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Volcano Engine ASR & TTS React Hooks",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",