1.0.4 • Published 5 years ago

noise-to-scroll v1.0.4

Weekly downloads
3
License
GPL-3.0-only
Repository
github
Last release
5 years ago

noise-to-scroll

A JavaScript library for browsers that allows users to scroll by making noise with their microphones.

This library uses the Web Audio API (see https://developer.mozilla.org/fr/docs/Web/API/Web_Audio_API).

Image from Gyazo

Installation

npm install noise-to-scroll

Usage

CDN

<html>
  <head>
    ...
  </head>
  <body>
    ...
    <script src="https://unpkg.com/noise-to-scroll/dist/noise-to-scroll.min.js"></script>
    <script>
        noiseToScroll({scrollableContainer: document.querySelector('#myScrollableElement')})
            .start()
            .then(() => {
                console.log('make some noise to scroll!');
            });
    </script>
  </body>
</html>

With a module bundler

import { nts } from 'noise-to-scroll'; // es6 import style or basic require

nts({scrollableContainer: document.querySelector('#myScrollableElement')})
    .start()
    .then(() => {
        console.log('make some noise to scroll!');
    });

This library is also compatible with AMD loading.

The minified javascript file in the /dist folder is built with webpack with libraryTarget set to umd (see https://webpack.js.org/configuration/output/#outputlibrarytarget).

API

Constructor noiseToScroll({options})

The options parameter is not required and has default values.

paramtypedefaultdetail
scrollableContainerHTMLElement|ObjectwindowThe HTML element (or object) that is scrollable, the method .scrollBy({options}) will be used on it for the scroll.
scrollMethodStringscrollByMethod called on the scrollableContainer.
scrollTopNumber|Functionwindow.innerHeight * 0.65Value of the top scroll.
scrollLeftNumber|Function0Value of the left scroll.
scrollBehaviorString|FunctionsmoothBehavior of the scroll.
scrollOptionsObject|Function{scrollTop, scrollLeft, scrollBehavior}Option object passed to the scrollMethod method.
scrollDebounceNumber100Number of milliseconds passed on the debounce for the scroll function.
debugBoolean|StringfalseEnable debug logs, can also pass the method to call on the console object (console[debug]).
clipLagNumber150Number of milliseconds of timeout after the end of a detected noticeable noise.
clipLevelNumber0.8Level of 'volume' on which the scroll event will trigger. 0 < clipLevel < 1

start()

Returns a Promise that is resolved if the browser supports the mediaDevices, AudioContext and if the user accepts to allow microphone on the web page.

Rejected if unsupported or user denied to allow microphone.

The Promise might not be resolved or rejected if the user doesn't answer the permission popup.

noiseToScroll()
    .start()
    .then(() => {
        console.log('browser support OK and user allowed microphone access'); // noiseToScroll is up and running
    })
    .catch((error) => {
        console.log(`unable to start noiseToScroll: ${error}`); // error contain the reason
    )};

detect()

Returns a Promise that is resolved if the browser supports mediaDevices and AudioContext.

Rejected if unsupported.

noiseToScroll()
    .detect()
    .then(() => {
        console.log('browser support OK'); // ready to start
    })
    .catch((error) => {
        console.log(`browser doesn't support: ${error}`);
    )};

on('event', listener)

Listen to the following events :

  • clipping : fired when a noise becomes noticeable according to clipLevel, no debounce, event is fired 100 times each seconds, listen carefully!
  • scroll : fired every time the scrollBy method is called on the scrollableContainer.
  • noise : similar to scroll event but the first param passed to the listener is the 'volume' that triggered the scroll.

.on() methods can be chained.

noiseToScroll()
    .on('noise', (volume) => {
        console.log(`volume ${volume} triggered the scroll!`);
    });
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago