/* 录音 Recorder扩展,动态波形显示 https://github.com/xiangyuecn/Recorder */ (function(){ var WaveView=function(set){ return new fn(set); }; var ViewTxt="WaveView"; var fn=function(set){ var This=this; var o={ /* elem:"css selector" //自动显示到dom,并以此dom大小为显示大小 //或者配置显示大小,手动把waveviewObj.elem显示到别的地方 ,width:0 //显示宽度 ,height:0 //显示高度 以上配置二选一 */ scale:2 //缩放系数,应为正整数,使用2(3? no!)倍宽高进行绘制,避免移动端绘制模糊 ,speed:9 //移动速度系数,越大越快 ,phase:21.8 //相位,调整了速度后,调整这个值得到一个看起来舒服的波形 ,fps:20 //绘制帧率,调整后也需调整phase值 ,keep:true //当停止了input输入时,是否保持波形,设为false停止后将变成一条线 ,lineWidth:3 //线条基础粗细 //渐变色配置:[位置,css颜色,...] 位置: 取值0.0-1.0之间 ,linear1:[0,"rgba(150,96,238,1)",0.2,"rgba(170,79,249,1)",1,"rgba(53,199,253,1)"] //线条渐变色1,从左到右 ,linear2:[0,"rgba(209,130,255,0.6)",1,"rgba(53,199,255,0.6)"] //线条渐变色2,从左到右 ,linearBg:[0,"rgba(255,255,255,0.2)",1,"rgba(54,197,252,0.2)"] //背景渐变色,从上到下 }; for(var k in set){ o[k]=set[k]; }; This.set=set=o; var elem=set.elem; if(elem){ if(typeof(elem)=="string"){ elem=document.querySelector(elem); }else if(elem.length){ elem=elem[0]; }; }; if(elem){ set.width=elem.offsetWidth; set.height=elem.offsetHeight; }; var scale=set.scale; var width=set.width*scale; var height=set.height*scale; if(!width || !height){ throw new Error(ViewTxt+"无宽高"); } var thisElem=This.elem=document.createElement("div"); var lowerCss=["","transform-origin:0 0;","transform:scale("+(1/scale)+");"]; thisElem.innerHTML='
'; var canvas=This.canvas=thisElem.querySelector("canvas"); var ctx=This.ctx=canvas.getContext("2d"); canvas.width=width; canvas.height=height; This.linear1=This.genLinear(ctx,width,set.linear1); This.linear2=This.genLinear(ctx,width,set.linear2); This.linearBg=This.genLinear(ctx,height,set.linearBg,true); if(elem){ elem.innerHTML=""; elem.appendChild(thisElem); }; This._phase=0; }; fn.prototype=WaveView.prototype={ genLinear:function(ctx,size,colors,top){ var rtv=ctx.createLinearGradient(0,0,top?0:size,top?size:0); for(var i=0;i