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

html5 canvas c,HTML5 Canvas 频谱

废话少说,直接贴出代码

HTML5音频可视化频谱跳动代码

HTML5音频可视化频谱跳动代码

audio element not supported

main.js

window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

window.onload = function() {

var audio = document.getElementById('audio');

var ctx = new AudioContext();

var analyser = ctx.createAnalyser();

var audioSrc = ctx.createMediaElementSource(audio);

// we have to connect the MediaElementSource with the analyser

audioSrc.connect(analyser);

analyser.connect(ctx.destination);

// we could configure the analyser: e.g. analyser.fftSize (for further infos read the spec)

// analyser.fftSize = 64;

// frequencyBinCount tells you how many values you'll receive from the analyser

var frequencyData = new Uint8Array(analyser.frequencyBinCount);

// we're ready to receive some data!

var canvas = document.getElementById('canvas'),

cwidth = canvas.width,

cheight = canvas.height - 2,

meterWidth = 10, //width of the meters in the spectrum

gap = 2, //gap between meters

capHeight = 2,

capStyle = '#fff',

meterNum = 800 / (10 + 2), //count of the meters

capYPositionArray = []; store the vertical position of hte caps for the preivous frame

ctx = canvas.getContext('2d'),

gradient = ctx.createLinearGradient(0, 0, 0, 300);

gradient.addColorStop(1, '#0f0');

gradient.addColorStop(0.5, '#ff0');

gradient.addColorStop(0, '#f00');

// loop

function renderFrame() {

var array = new Uint8Array(analyser.frequencyBinCount);

analyser.getByteFrequencyData(array);

var step = Math.round(array.length / meterNum); //sample limited data from the total array

ctx.clearRect(0, 0, cwidth, cheight);

for (var i = 0; i < meterNum; i++) {

var value = array[i * step];

if (capYPositionArray.length < Math.round(meterNum)) {

capYPositionArray.push(value);

};

ctx.fillStyle = capStyle;

//draw the cap, with transition effect

if (value < capYPositionArray[i]) {

ctx.fillRect(i * 12, cheight - (--capYPositionArray[i]), meterWidth, capHeight);

} else {

ctx.fillRect(i * 12, cheight - value, meterWidth, capHeight);

capYPositionArray[i] = value;

};

ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look

ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter

}

requestAnimationFrame(renderFrame);

}

renderFrame();

audio.play();

};

注:存在跨域问题在google chrome 浏览器40 以下好用,官方解释40 以上不更新,哪位朋友有好的解决方案可以 留言交流。

相关文章:

  • 宜昌哪个学校的计算机专业最好,东升学校计算机专业怎么样啊?
  • 四川计算机学校在哪,四川省联合经济计算机学校位置在哪儿
  • winpe修复计算机无法启动,PE修复系统启动故障的详细教程
  • 计算机软件制图英文,《计算机制图》计算机制图-工程制图.pdf
  • 查询其他系中比计算机科学某一学生年龄小的学生姓名和年龄.,①查询其他系中比计算机科学(CS)某一学生年龄小的学生姓名和年龄。-桃子题库网...
  • 计算机行业的最新技术,计算机行业发展空间巨大 三大必然趋势引领发展
  • 北京大学计算机李昀,2020-2021计算机与信息技术学院学生干部公示名单
  • 计算机丢失ndfapi.dll6,NdfCancelIncident function (ndfapi.h)
  • 绿洲显示服务器,《最后的绿洲》服务器连接问题怎么解决 连接异常问题官方公告...
  • 存储服务器的配置文档,存储系统、服务器配置及技术要求
  • sql服务器虚拟内存不足,解决SQL Server虚拟内存不足情况
  • 失落城堡手游获取服务器信息99,失落城堡手游更新情况介绍 游戏更新大全
  • 网站服务器年费属于什么费用,云服务器属于什么费用
  • rs232数据无线传输至服务器,智嵌UDP与RS232RS485双向数据透明传输例程(开发板做服务器)使用说明.pdf...
  • saas系统用的什么语音_为什么越来越多的人选择SaaS模式的crm客户管理系统?
  • [译]Python中的类属性与实例属性的区别
  • 2017 前端面试准备 - 收藏集 - 掘金
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • Mysql5.6主从复制
  • python 装饰器(一)
  • Redux 中间件分析
  • RxJS: 简单入门
  • ViewService——一种保证客户端与服务端同步的方法
  • 高性能JavaScript阅读简记(三)
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 实现简单的正则表达式引擎
  • 使用API自动生成工具优化前端工作流
  • 使用iElevator.js模拟segmentfault的文章标题导航
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 硬币翻转问题,区间操作
  • 运行时添加log4j2的appender
  • 自制字幕遮挡器
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • #QT(TCP网络编程-服务端)
  • (6)设计一个TimeMap
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (附源码)计算机毕业设计SSM智慧停车系统
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (四) 虚拟摄像头vivi体验
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)http-server应用
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • .a文件和.so文件
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .net 7 上传文件踩坑
  • .NET MVC之AOP
  • .net 获取url的方法
  • .NET 药厂业务系统 CPU爆高分析
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .NET建议使用的大小写命名原则