noise-to-scroll
Version:
A JavaScript library for browsers that allows users to scroll by making noise with their microphones.
1 lines • 4.37 kB
JavaScript
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.noiseToScroll=n():e.noiseToScroll=n()}(window,(function(){return function(e){var n={};function o(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,o),r.l=!0,r.exports}return o.m=e,o.c=n,o.d=function(e,n,t){o.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:t})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,n){if(1&n&&(e=o(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(o.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var r in e)o.d(t,r,function(n){return e[n]}.bind(null,r));return t},o.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(n,"a",n),n},o.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},o.p="",o(o.s=0)}([function(e,n){var o=this;e.exports=function(e){if("Object"!==(e=e||{}).constructor.name)throw new Error("Expected parameter to be an Object, found instead: ".concat(e.constructor.name));var n,t,r,i,c,l=o,u=null,s=null;function a(e,n){l.eventsListeners[e]&&l.eventsListeners[e].forEach((function(e){e.apply(null,n)}))}function p(n){for(var o,t=n.inputBuffer.getChannelData(0),r=t.length,i=0,c=0;c<r;c++)o=t[c],Math.abs(o)>=e.clipLevel&&(this.clipping=!0,this.lastClip=window.performance.now()),i+=o*o;var u=Math.sqrt(i/r);return this.checkClipping()&&l.onClipping(),l.volume=Math.max(u,l.volume*e.averaging),l.volume}return e.clipLevel=e.clipLevel||.8,e.averaging=e.averaging||.95,e.clipLag=e.clipLag||150,e.scrollableContainer=e.scrollableContainer||window,e.scrollDebounce=e.scrollDebounce||100,e.scrollMethod=e.scrollMethod||"scrollBy",e.scrollTop=e.scrollTop||function(){return.65*window.innerHeight},e.scrollLeft=e.scrollLeft||function(){return 0},e.scrollBehavior=e.scrollBehavior||function(){return"smooth"},e.scrollOptions=e.scrollOptions||function(){return{top:e.scrollTop(),left:e.scrollLeft(),behavior:e.scrollBehavior()}},l.params=e,l.eventsListeners={},l.volume=0,l.debug=function(){e.debug&&(arguments[0]="string"==typeof arguments[0]?"[noiseToScroll debug] ".concat(arguments[0]):arguments[0],console[!0===e.debug?"debug":e.debug].apply(null,arguments))},["scrollOptions","scrollBehavior","scrollTop","scrollLeft"].forEach((function(n){if("function"!=typeof e[n]){var o=e[n];l.debug("converted param '".concat(n,"' to function returning"),o),e[n]=function(){return o}}})),l.debug("noiseToScroll initialized with params",e),l.onClipping=function(){e.scrollableContainer&&l.scroll(),a("clipping",[l.volume])},l.scroll=(t=function(){l.debug(".".concat(e.scrollMethod,"() fired with parameter"),e.scrollOptions()),e.scrollableContainer[e.scrollMethod](e.scrollOptions()),a("scroll",[]),a("noise",[l.volume])},r=e.scrollDebounce,function(){var e=this,n=arguments,o=function(){c=null,i||t.apply(e,n)},l=i&&!c;clearTimeout(c),c=setTimeout(o,r),l&&t.apply(e,n)}),l.detect=function(){return new Promise((function(e,n){try{new(window.AudioContext||window.webkitAudioContext)}catch(e){return n("audioContext not supported")}if(!navigator.mediaDevices||!navigator.mediaDevices.getUserMedia)return n("mediaDevices not supported");e(l)})).then((function(){l.debug("browser is supporting AudioContext and mediaDevices")})).catch((function(e){throw l.debug("browser doesn't support noiseToScroll",e),e}))},l.start=function(){return l.detect().then((function(){return(n=new(window.AudioContext||window.webkitAudioContext)).resume().then((function(){return navigator.mediaDevices.getUserMedia({audio:!0})})).then((function(o){return u=n.createMediaStreamSource(o),s=function(n){var o=n.createScriptProcessor(512);return o.onaudioprocess=p,o.clipping=!1,o.lastClip=0,o.connect(n.destination),o.checkClipping=function(){return!!this.clipping&&(this.lastClip+e.clipLag<window.performance.now()&&(this.clipping=!1),this.clipping)},l.stop=function(){o.disconnect(),o.onaudioprocess=null},o}(n),u.connect(s),l}))}))},l.on=function(e,n){return l.eventsListeners[e]||(l.eventsListeners[e]=[]),l.eventsListeners[e].push(n),l},l}}])}));