demo.html

<style>body { background: #000; }</style>
<script src="../siriwave.js"></script>
<div id="container" style="width: 900px; height: 300px; background: #000"></div>
<script> var SW = new SiriWave({
           style: 'ios9',
              speed: 0.1,
              amplitude: 0.2,
              speedInterpolationSpeed: 0,
              frequency: 2,
              height: 424,
             width: 964,
              container: document.getElementById('container'),
              autostart: true
	});

	var ctx = new (window.AudioContext || window.webkitAudioContext)();
var analyser = ctx.createAnalyser();
analyser.connect(ctx.destination);
analyser.fftSize = 2048; var bufferLength = analyser.frequencyBinCount;
function getAverageVolume(data) {
var values = 0;
var length = data.length;
for (var i = 0; i < data.length; i++) {
values += data[i];
}
return values / data.length;
} window.navigator.getUserMedia(
{ audio: true },
function(stream) {
var input = ctx.createMediaStreamSource(stream);
console.log()
input.connect(analyser); var processor = ctx.createScriptProcessor(2048, 1, 1);
processor.connect(ctx.destination); processor.onaudioprocess = function() {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var average = getAverageVolume(array);
SW.setAmplitude(average / 140);
};
},
function(){
console.log(1)
}
); </script>

  siriwave.js

(function() {
// // Siri Wave iOS9 Style curve function SiriWave9Curve(opt) {
this.controller = opt.controller;
this.definition = opt.definition;
this.tick = 0; this._respawn();
} SiriWave9Curve.prototype._respawn = function() {
this.amplitude = 0.3 + Math.random() * 0.7;
this.seed = Math.random();
this.openClass = 2 + (Math.random()*3)|0;
}; SiriWave9Curve.prototype._ypos = function(i) {
var p = this.tick;
var y = -1 * Math.abs(Math.sin(p)) * this.controller.amplitude * this.amplitude * this.controller.cache.heightMax * Math.pow(1 / (1 + Math.pow(this.openClass * i, 2)), 2); if (Math.abs(y) < 0.001) {
this._respawn();
} return y;
}; SiriWave9Curve.prototype._draw = function(sign) {
var ctx = this.controller.ctx; this.tick += this.controller.speed * (1 - 0.5 * Math.sin(this.seed * Math.PI)); ctx.beginPath(); var xBase = this.controller.cache.width2 + (-this.controller.cache.width4 + this.seed * (this.controller.cache.width2));
var yBase = this.controller.cache.height2; var x, y;
var xInit = null; for (var i = -3; i <= 3; i += 0.01) {
x = xBase + i * this.controller.cache.width4;
y = yBase + ((sign || 1) * this._ypos(i)); xInit = xInit || x; ctx.lineTo(x, y);
} var height = Math.abs(this._ypos(0)); var gradient = ctx.createRadialGradient(xBase, yBase, height * 1.15, xBase, yBase, height * 0.3);
gradient.addColorStop(0, 'rgba(' + this.definition.color + ', 0.4)');
gradient.addColorStop(1, 'rgba(' + this.definition.color + ', 0.2)');
ctx.fillStyle = gradient; ctx.lineTo(xInit, yBase);
ctx.closePath(); ctx.fill();
}; SiriWave9Curve.prototype.draw = function() {
this._draw(-1);
this._draw(1);
}; SiriWave9Curve.prototype.definition = [
{ color: '32,133,252' },
{
color: '94,252,169'
}, {
color: '253,71,103'
},
{
color: '255,252,0'
},
{ color: '253,0,255' }
]; // Standard Curve function SiriWaveCurve(opt) {
this.controller = opt.controller;
this.definition = opt.definition;
} SiriWaveCurve.prototype._globAttenuationEquation = function(x) {
if (SiriWaveCurve.prototype._globAttenuationEquation.cache[x] == null) {
SiriWaveCurve.prototype._globAttenuationEquation.cache[x] = Math.pow(4 / (4 + Math.pow(x, 4)), 4);
}
return SiriWaveCurve.prototype._globAttenuationEquation.cache[x];
};
SiriWaveCurve.prototype._globAttenuationEquation.cache = {}; SiriWaveCurve.prototype._xpos = function(i) {
return this.controller.cache.width2 + i * this.controller.cache.width4;
}; SiriWaveCurve.prototype._ypos = function(i) {
var att = (this.controller.cache.heightMax * this.controller.amplitude) / this.definition.attenuation;
return this.controller.cache.height2 + this._globAttenuationEquation(i) * att * Math.sin(this.controller.frequency * i - this.controller.phase);
}; SiriWaveCurve.prototype.draw = function() {
var ctx = this.controller.ctx; ctx.moveTo(0,0);
ctx.beginPath();
ctx.strokeStyle = 'rgba(' + this.controller.color + ',' + this.definition.opacity + ')';
ctx.lineWidth = this.definition.lineWidth; for (var i = -2; i <= 2; i += 0.01) {
var y = this._ypos(i); if (Math.abs(i) >= 1.90) y = this.controller.cache.height2; ctx.lineTo(this._xpos(i), y);
} ctx.stroke();
}; SiriWaveCurve.prototype.definition = [
{ attenuation: -2, lineWidth: 1, opacity: 0.1 },
{ attenuation: -6, lineWidth: 1, opacity: 0.2 },
{ attenuation: 4, lineWidth: 1, opacity: 0.4 },
{ attenuation: 2, lineWidth: 1, opacity: 0.6 },
{ attenuation: 1, lineWidth: 1.5, opacity: 1 },
]; // Expose API function SiriWave(opt) {
opt = opt || {}; this.phase = 0;
this.run = false;
this.cache = {}; if (opt.container == null) {
console.warn("SiriWaveJS: no container defined, using body");
opt.container = document.body;
} this.style = opt.style || 'ios'; this.container = opt.container; this.width = opt.width || window.getComputedStyle(this.container).width.replace('px', '');
this.height = opt.height || window.getComputedStyle(this.container).height.replace('px', '');
this.ratio = opt.ratio || window.devicePixelRatio || 1; this.cache.width = this.ratio * this.width;
this.cache.height = this.ratio * this.height;
this.cache.height2 = this.cache.height / 2;
this.cache.width2 = this.cache.width / 2;
this.cache.width4 = this.cache.width / 4;
this.cache.heightMax = (this.cache.height2) - 4; // Constructor opt this.amplitude = (opt.amplitude == undefined) ? 1 : opt.amplitude;
this.speed = (opt.speed == undefined) ? 0.2 : opt.speed;
this.frequency = (opt.frequency == undefined) ? 6 : opt.frequency;
this.color = this._hex2rgb(opt.color || '#fff'); // Interpolation this.speedInterpolationSpeed = opt.speedInterpolationSpeed || 0.005;
this.amplitudeInterpolationSpeed = opt.amplitudeInterpolationSpeed || 0.05; this.cache.interpolation = {
speed: this.speed,
amplitude: this.amplitude
}; // Canvas this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.cache.width;
this.canvas.height = this.cache.height; if (opt.cover) {
this.canvas.style.width = this.canvas.style.height = '100%';
} else {
this.canvas.style.width = (this.cache.width / this.ratio) + 'px';
this.canvas.style.height = (this.cache.height / this.ratio) + 'px';
} this.curves = []; var i = 0, j = 0;
if (this.style === 'ios9') {
for (; i < SiriWave9Curve.prototype.definition.length; i++) {
for (j = 0; j < (3 * Math.random()) | 0; j++) {
this.curves.push(new SiriWave9Curve({
controller: this,
definition: SiriWave9Curve.prototype.definition[i]
}));
}
}
} else {
for (; i < SiriWaveCurve.prototype.definition.length; i++) {
this.curves.push(new SiriWaveCurve({
controller: this,
definition: SiriWaveCurve.prototype.definition[i]
}));
}
} // Start
this.container.appendChild(this.canvas);
if (opt.autostart) {
this.start();
}
} SiriWave.prototype._hex2rgb = function(hex){
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m,r,g,b) { return r + r + g + g + b + b; });
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ?
parseInt(result[1],16).toString()+','+parseInt(result[2], 16).toString()+','+parseInt(result[3], 16).toString()
: null;
}; SiriWave.prototype._interpolate = function(propertyStr) {
increment = this[ propertyStr + 'InterpolationSpeed' ]; if (Math.abs(this.cache.interpolation[propertyStr] - this[propertyStr]) <= increment) {
this[propertyStr] = this.cache.interpolation[propertyStr];
} else {
if (this.cache.interpolation[propertyStr] > this[propertyStr]) {
this[propertyStr] += increment;
} else {
this[propertyStr] -= increment;
}
}
}; SiriWave.prototype._clear = function() {
this.ctx.globalCompositeOperation = 'destination-out';
this.ctx.fillRect(0, 0, this.cache.width, this.cache.height);
this.ctx.globalCompositeOperation = 'source-over';
}; SiriWave.prototype._draw = function() {
for (var i = 0, len = this.curves.length; i < len; i++) {
this.curves[i].draw();
}
}; SiriWave.prototype._startDrawCycle = function() {
if (this.run === false) return;
this._clear(); // Interpolate values
this._interpolate('amplitude');
this._interpolate('speed'); this._draw();
this.phase = (this.phase + Math.PI * this.speed) % (2 * Math.PI); if (window.requestAnimationFrame) {
window.requestAnimationFrame(this._startDrawCycle.bind(this));
} else {
setTimeout(this._startDrawCycle.bind(this), 20);
}
}; /* API */ SiriWave.prototype.start = function() {
this.phase = 0;
this.run = true;
this._startDrawCycle();
}; SiriWave.prototype.stop = function() {
this.phase = 0;
this.run = false;
}; SiriWave.prototype.setSpeed = function(v, increment) {
this.cache.interpolation.speed = v;
}; SiriWave.prototype.setAmplitude = function(v) {
this.cache.interpolation.amplitude = Math.max(Math.min(v, 1), 0);
}; if (typeof define === 'function' && define.amd) {
define(function(){ return SiriWave; });
} else {
window.SiriWave = SiriWave;
} //
})();

  

siriWave.js的demo的更多相关文章

  1. Three.js基本 Demo

    对于新手来说,几个简单的例子非常实用,偶然发现几个不错的Demo,分享给大家! Three.js基本 Demo 1.最基本的Hello World:http://stemkoski.github.io ...

  2. 微信JS SDK Demo 官方案例[转]

    摘要: 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用 ...

  3. 微信JS SDK Demo

    微信JS-SDK 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置原文:http://www.cnblogs.com/txw1958/p/ ...

  4. 微信JS SDK Demo 官方案例

    微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享 ...

  5. 高德地图根据经纬度转换成地址JS代码demo

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. [整理]Node入门 » 一本全面的Node.js教程 - Demo实践所遇到的问题

    花了一个上午看完[转载]Node入门 » 一本全面的Node.js教程 根据里面的Demo自己手动实现过程中还是遇到了些问题,特整理在此. <1>.由于node.msi安装包已经自动添加了 ...

  7. js闭包Demo

    我们先看一个关于Javascript利用循环绑定事件的例子: 例如:一个不确定长度的列表,在鼠标经过某一条的时候改变背景.   ﹤!DOCTYPE html PUBLIC "-//W3C// ...

  8. anguar.js tutorial demo

    http://docs.angularjs.cn/tutorial angular 入门demo : PhoneCat Tutorial App 别人的DEMO(官方版):http://angular ...

  9. Vue2.0 vue-source.js jsonp demo vue跨域请求

    以调用百度的输入提示接口为例 ===================================================================================== ...

随机推荐

  1. 对django的理解

    http://www.cnblogs.com/chongdongxiaoyu/p/9403399.html https://blog.csdn.net/weixin_42134789/article/ ...

  2. js实现checkbox全选,全部选和反选效果

    效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. JDBC概念和使用

    JDBC学习:    JAVA的数据获取方式:        1 直接声明变量并赋值.                 2 Scanner类控制台输入        3 IO流(将硬盘存储中的数据读取 ...

  4. python高级(四)—— 文本和字节序列(编码问题)

    本文主要内容 字符 字节 结构体和内存视图 字符和字节之间的转换——编解码器 BOM鬼符  标准化Unicode字符串 Unicode文本排序 python高级——目录 文中代码均放在github上: ...

  5. Oracle分析函数、窗口函数简单记录汇总

    一.分析函数.窗口函数一般形式 1.分析函数的形式分析函数带有一个开窗函数over(),包含三个分析子句:分组(partition by), 排序(order by), 窗口(rows) ,他们的使用 ...

  6. Which mb sdconnect c4 worth the money?

    MB SD connect C4 with laptop v2018.5 Version avaiable now ,It is ready to work after you get it ,wor ...

  7. python量化分析系列之---5行代码实现1秒内获取一次所有股票的实时分笔数据

    python量化分析系列之---5行代码实现1秒内获取一次所有股票的实时分笔数据 最近工作太忙了,有一个星期没有更新文章了,本来这一期打算分享一些对龙虎榜数据的分析结果的,现在还没有把数据内的价值很好 ...

  8. 文献综述十六:基于UML的中小型超市管理系统分析与设计

    一.基本信息 标题:基于UML的中小型超市管理系统分析与设计 时间:2016 出版源:Journal of Xiangnan University 文件分类:uml技术系统的研究 二.研究背景 开发一 ...

  9. es第一篇:Getting Started

    es是一个近乎实时的搜索平台,这意味着从索引文档到文档可搜索,是有一点点延迟的(通常是一秒).es集群是一个或多个节点的集合,它们共同保存数据,并提供跨所有节点的联合索引和搜索功能.集群名由clust ...

  10. U盘拷贝大文件提示文件过大无法拷贝解决方案

    工具: 计算机 windows操作系统 U盘 原因:由于U盘的格式问题导致的,当期的磁盘格式是FAT32类型的,无拷贝过大的文件 方法:接下来修改U盘类型,且不格式化U盘 1.键盘win+R快捷键弹出 ...