当前位置: 首页 > news >正文

vue+canvas音频可视化

1.代码 

<template><div class="subGuide"><canvas id="canvas"></canvas><br><audio id="audio" src="./audio.mp3" controls></audio></div>
</template><script>
export default {name: 'subGuide',data() {return {}},mounted() {const audioEle = document.querySelector('audio')const cvs = document.querySelector('canvas')const ctx = cvs.getContext('2d')function initCvs() {cvs.width = (window.innerWidth / 2) * devicePixelRatiocvs.height = (window.innerHeight / 3) * devicePixelRatio}initCvs()let isInit = falselet dateArray = nulllet analyser = nullaudioEle.addEventListener('play', function (e) {if (isInit) returnconst audCtx = new AudioContext()const source = audCtx.createMediaElementSource(audioEle)analyser = audCtx.createAnalyser()analyser.fftSize = 512dateArray = new Uint8Array(256)source.connect(analyser)analyser.connect(audCtx.destination)isInit = true})function draw() {requestAnimationFrame(draw)const { width, height } = cvsctx.clearRect(0, 0, width, height)if (!isInit) returnanalyser.getByteFrequencyData(dateArray)const len = dateArray.length / 2.5ctx.fillStyle = '#266fff'const barWidth = width / len / 2for (let i = 0; i < len; i++) {const data = dateArray[i]const barHeight = (data / 255) * heightconst x1 = i * barWidth + width / 2const x2 = width / 2 - (i + 1) * barWidthconst y = height - barHeightctx.fillRect(x1, y, barWidth - 2, barHeight)ctx.fillRect(x2, y, barWidth - 2, barHeight)}}draw()},methods: {}
}
</script><style lang="scss" scoped></style>

2.效果

音频可视化

http://www.lryc.cn/news/406895.html

相关文章:

  • 俊昭stm32笔记
  • W30-python03-pytest+selenium+allure访问百度网站实例
  • 如何在 Debian 8 上安装和使用 PostgreSQL 9.4
  • 【微信小程序】微信小程序设置本地背景图片在真机无法显示的解决方案
  • Arthas在线诊断案例实战整理
  • 使用 XRDP 远程linux主机
  • 学习小型gpt源码(自用)
  • @Transactional使用的注意事项
  • 快手可灵视频生成大模型全方位测评
  • 【JavaScript】`Map` 数据结构
  • Ubuntu22.04使用NVM安装多版本Node.js和版本切换
  • 基于C51和OLED12864实现Goole小恐龙
  • 【Docker】CentOS7环境下的安装
  • 浏览器的最大并发数(http1.1)
  • Android 开发中px、dpi 和 dp三个单位的介绍
  • zookeeper开启SASL权限认证
  • mysql一个小问题引发的思考-mysql类型转换-查询缓存 及 MYSQL查询缓存以及自动选择不使用查询缓存的情况
  • css更改图片颜色
  • 通过POST请求往Elastic批量插入数据
  • JAW:一款针对客户端JavaScript的图形化安全分析框架
  • 错误解决 error CS0117: ‘Buffer‘ does not contain a definition for ‘BlockCopy‘
  • ICMPv6与DHCPv6之网络工程师软考中级
  • 【HTML — 构建网络】HTML 入门
  • javafx的ListView代入项目的使用
  • 基于ABAP OLE技术实现对服务器文件进行读写操作
  • 求教Postgresql在jdbc处理bit(1)字段的预处理解决方案
  • 微信小程序-自定义tabBar
  • vue3+element-plus 实现动态菜单和动态路由的渲染
  • GO-学习-03-基本数据类型
  • 高并发场景下,系统的保护机制