react 中Wavesurfer绘制音频波形图

一、效果

在这里插入图片描述

二、使用

1、npm 安装

npm install wavesurfer.js --save

2、引入

import WaveSurfer from 'wavesurfer.js' //导入wavesurfer.js
import Timeline from 'wavesurfer.js/dist/plugin/wavesurfer.timeline.js' //导入时间轴插件

3、绘制

drawPlay=()=>{
        this.wavesurfer = WaveSurfer.create({
            container: '#audioChart',//容器
            waveColor: '#1DE3FA',//波形图颜色
            progressColor: '#159faf',//进度条颜色
            backend: 'MediaElement',
            mediaControls: false,
            audioRate: '1',//播放音频的速度
            //插件
            plugins: [
                //时间轴插件
                // Timeline.create({
                //     container: '#wave-timeline'
                // }),
                // 光标插件
                // CursorPlugin.create({
                //     showTime: true,
                //     opacity: 1,
                //     customShowTimeStyle: {
                //         backgroundColor: '#000',
                //         color: '#fff',
                //         padding: '2px',
                //         fontSize: '10px'
                //     }
                // }),
            ]
        });
        // 特别提醒:此处需要使用require(相对路径),否则会报错
        this.wavesurfer.load(require('../../static/testAudio/Last_Stop.mp3'));
    }

三、有坑

这里要加require,否则波形图不显示或者会报错!!!!

this.wavesurfer.load(require('../../static/testAudio/Last_Stop.mp3'));
要在 React 绘制热力图,你可以使用第三方的 JavaScript 库 D3.js。 以下是在 React 使用 D3.js 绘制热力图的基本步骤: 1. 安装 D3.js ``` npm install d3 ``` 2. 在 React 组件引入 D3.js ```javascript import * as d3 from 'd3'; ``` 3. 创建一个 SVG 元素,并设置它的宽度和高度 ```javascript const width = 500; const height = 500; const svg = d3.select('#heatmap') .append('svg') .attr('width', width) .attr('height', height); ``` 4. 创建一个颜色比例尺,用于将数值映射到颜色 ```javascript const colorScale = d3.scaleLinear() .domain([0, 100]) .range(['#fff', '#f00']); ``` 5. 创建一个矩阵,并将其数据绑定到 SVG 元素上 ```javascript const data = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]; const rects = svg.selectAll('rect') .data(data) .enter() .append('rect'); ``` 6. 设置每个矩阵的位置、宽度、高度和颜色 ```javascript rects.attr('x', (d, i) => i * 50) .attr('y', (d, i) => i * 50) .attr('width', 50) .attr('height', 50) .attr('fill', d => colorScale(d)); ``` 完整的代码示例: ```javascript import React, { useEffect, useRef } from 'react'; import * as d3 from 'd3'; function Heatmap() { const heatmapRef = useRef(null); useEffect(() => { const width = 500; const height = 500; const svg = d3.select(heatmapRef.current) .append('svg') .attr('width', width) .attr('height', height); const colorScale = d3.scaleLinear() .domain([0, 100]) .range(['#fff', '#f00']); const data = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]; const rects = svg.selectAll('rect') .data(data) .enter() .append('rect'); rects.attr('x', (d, i) => i * 50) .attr('y', (d, i) => i * 50) .attr('width', 50) .attr('height', 50) .attr('fill', d => colorScale(d)); }, []); return <div ref={heatmapRef} />; } export default Heatmap; ``` 这样就可以在 React 绘制一个简单的热力图了。你可以根据需要调整矩阵的数据、颜色比例尺和 SVG 元素的大小等参数,以满足具体的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值