Git Product home page Git Product logo

rythm.js's Introduction

Contribute

<โ™ซ/> Rythm.js - v2.2.5

Build Status

Demo at: https://okazari.github.io/Rythm.js/

A JavaScript library that makes your page dance.

Getting started

Install with npm:

npm install rythm.js

Or get from a CDN:

https://unpkg.com/rythm.js/
https://cdnjs.cloudflare.com/ajax/libs/rythm.js/2.2.5/rythm.min.js

Good old way

Import rythm into your page:

<script type="text/javascript" src="/path/to/rythm.min.js"></script>

Add one of the rythm css classes to indicate which element will dance:

<div class="rythm-bass"></div>

Create a rythm object and give it your audio URL then use the start function:

var rythm = new Rythm()
rythm.setMusic('path/to/sample.mp3')
rythm.start()

ES6 module

import Rythm from 'rythm.js'
const rythm = new Rythm()
rythm.setMusic('path/to/sample.mp3')
rythm.start()

API documentation

Rythm object

const rythm = new Rythm()

/* The starting scale is the minimum scale your element will take (Scale ratio is startingScale + (pulseRatio * currentPulse)).
 * Value in percentage between 0 and 1
 * Default: 0.75
 */
rythm.startingScale = value

/* The pulse ratio is be the maximum additional scale your element will take (Scale ratio is startingScale + (pulseRatio * currentPulse)).
 * Value in percentage between 0 and 1
 * Default: 0.30
 */
rythm.pulseRatio = value

/* The max value history represent the number of passed value that will be stored to evaluate the current pulse.
 * Int value, minimum 1
 * Default: 100
 */
rythm.maxValueHistory = value

/* Set the music the page will dance to
 * @audioUrl: '../example/mysong.mp3'
 */
rythm.setMusic(audioUrl)

/* Used to collaborate with other players library.
 * You can connect Rythm to an audioElement, and then control the audio with your other player
 */
rythm.connectExternalAudioElement(audioElement)

/* Adjust audio gain
 * @value: Number
 */
rythm.setGain(value)

/* Add your own rythm-class
 * @elementClass: Class that you want to link your rythm to
 * @danceType: Use any of the built-in effect or give your own function
 * @startValue: The starting frequency of your rythm
 * @nbValue: The number of frequency of your rythm
 * 1024 Frequencies, your rythm will react to the average of your selected frequencies.
 * Examples: bass 0-10 ; medium 150-40 ; high 500-100
 */
rythm.addRythm(elementClass, danceType, startValue, nbValue)

/* Plug your computer microphone to rythm.js.
 * This function returns a Promise object that is resolved when the microphone is up.
 * Require your website to be run in HTTPS
 */
rythm.plugMicrophone().then(function(){...})

// Let's dance
rythm.start()

/* Stop the party
 * @freeze: Set this to true if you want to prevent the elements to reset to their initial position
 */
rythm.stop(freeze)

Built-in classes with "pulse" effect

  • rythm-bass
  • rythm-medium
  • rythm-high

Custom-classes

You can use the addRythm function to make your own classes listen to specific frequencies. Here is how the basics classes are created:

  • addRythm('rythm-bass', 'pulse', 0, 10)
  • addRythm('rythm-medium', 'pulse', 150, 40)
  • addRythm('rythm-high', 'pulse', 500, 100)

Dance types available

For more control of theses dance types, you can give a configuration object as last argument to addRythm:

addRythm('rythm-high', 'shake', 500, 100, { direction:'left', min: 20, max: 300 })

Here are the built-in dances and their options:

  • pulse
    • min: Minimum value given to transform: scale(). Default: 0.75
    • max: Maximum value given to transform: scale(). Default: 1.25
  • jump
    • min: Minimum value given to transform: translateY(). Default: 0
    • max: Maximum value given to transform: translateY(). Default: 30
  • shake
    • min: Minimum value given to transform: translateX(). Default: -15
    • max: Maximum value given to transform: translateX(). Default: 15
    • direction: left for a right to left move, right for a left to right move. Default: right
  • twist
    • min: Minimum value given to transform: rotate(). Default: -20
    • max: Maximum value given to transform: rotate(). Default: 20
    • direction: left for a right to left move, right for a left to right move. Default: right
  • vanish
    • min: Minimum value (between 0 and 1) given to opacity. Default: 0
    • max: Maximum value (between 0 and 1) given to opacity. Default: 1
    • reverse: Boolean to reverse the effect. Default: false (Higher the pulse is, the more visible it will be)
  • borderColor
    • from: Array of integer between 0 and 255 corresponding to a RGB color. Default: [0,0,0]
    • to: Array of integer between 0 and 255 corresponding to a RGB color. Default: [255,255,255]
  • color
    • from: Array of integer between 0 and 255 corresponding to a RGB color. Default: [0,0,0]
    • to: Array of integer between 0 and 255 corresponding to a RGB color. Default: [255,255,255]
  • radius
    • min: Minimum value given to border-radius. Default: 0
    • max: Maximum value given to border-radius. Default: 25
    • reverse: Boolean to make effect from max to min. Default: false
  • blur
    • min: Minimum value given to filter: blur(). Default: 0
    • max: Maximum value given to filter: blur(). Default: 8
    • reverse: Boolean to make effect from max to min. Default: false
  • swing
    • curve: Whether the element should curve up or down. Default: down
    • direction: Whether the element should swing right or left. Default: right
    • radius: How far the element will swing. Default: 20
  • kern
    • min: Minimum value given to letter-spacing. Default: 0
    • max: Maximum value given to letter-spacing. Default: 25
    • reverse: Boolean to make effect from max to min. Default: false
  • neon
    • from: Array of integer between 0 and 255 corresponding to a RGB color. Default: [0,0,0]
    • to: Array of integer between 0 and 255 corresponding to a RGB color. Default: [255,255,255]
  • borderWidth
    • min: Minimum value given to border-width. Default: 0
    • max: Maximum value given to border-width. Default: 5
  • fontSize
    • min: Minimum value given to font-width. Default: 0.8
    • max: Maximum value given to font-width. Default: 1.2
  • tilt
    • min: Minimum value given to tilt. Default: 20
    • max: Maximum value given to tilt. Default: 25
    • reverse: Boolean to make effect from max to min. Default: false
  • fontColor
    • from: Array of integer between 0 and 255 corresponding to a RGB color. Default: [0,0,0]
    • to: Array of integer between 0 and 255 corresponding to a RGB color. Default: [255,255,255]

To see each visual effect, you can go to the Demo.

Custom dance type

If you want to use your own dance type, you need to give an object as the 2nd argument of addRythm instead of a built in dance key.

This object must have two properties:

  • dance: The custom function to make elements dance
  • reset: The associated custom function that will be called to reset element style
/* The custom function signature is
 * @elem: The HTML element target you want to apply your effect to
 * @value: The current pulse ratio (percentage between 0 and 1)
 * @options: The option object user can give as last argument of addRythm function
 */
const pulse = (elem, value, options = {}) => {
  const max = options.max || 1.25
  const min = options.min || 0.75
  const scale = (max - min) * value
  elem.style.transform = `scale(${min + scale})`
}

/* The reset function signature is
 * @elem: The element to reset
 */
const resetPulse = elem => {
  elem.style.transform = ''
}

addRythm('my-css-class', { dance: pulse, reset: resetPulse }, 150, 40)

Features

  • Your HTML can dance by using any of the available dance types.
  • You can use custom functions to build you own dance type (and if it looks awesome! Feel free to make a PR ;) )

Contribute

Any pull request will be appreciated. You can start coding on this project following these steps:

  • Fork the project
  • Clone your repository
  • Run npm install
  • Run npm start in the main folder to launch a development web server
  • Enjoy the rythm

Adding new dance type

In v2.2.x adding a new dance type is pretty easy:

  • Create a new file in src\dances
  • This file must export your custom dance type function
  • This file must export a reset function

For example, here is the content of jump.js file:

/* The function signature is
 * @elem: The HTML element target you want to apply your effect to
 * @value: The current pulse ratio (percentage between 0 and 1)
 * @options: The option object user can give as last argument of addRythm function
 */
export default (elem, value, options = {}) => {
  const max = options.max || 30
  const min = options.min || 0
  const jump = (max - min) * value
  elem.style.transform = `translateY(${-jump}px)`
}

/* The reset function signature is
 * @elem: The element to reset
 */
export const reset = elem => {
  elem.style.transform = ''
}
  • Import it and register it into the constructor of Dancer.js file:
import jump, { reset as resetJump } from './dances/jump.js'
class Dancer {
  constructor() {
    this.registerDance('jump', jump, resetJump)
  }
}
  • Commit it and create a PR. Then look at everyone enjoying your contribution :) !

License: GNU GPL

Author: @OkazariBzh

rythm.js's People

Contributors

acailly avatar ajaymathur avatar anant-k-singh avatar antoniojl avatar bplouzennec avatar ccocoual avatar egoist avatar haykokoryun avatar hongmaoxiao avatar jabster28 avatar jesm avatar johnwquarles avatar joseph-allen avatar kageroucf avatar kpensec avatar ldd avatar lim4349 avatar maici avatar maryzam avatar mathieu-pousse avatar nesvand avatar nicopennec avatar o-sewell avatar okazari avatar oropiko avatar sdras avatar sergehardy avatar slashgear avatar sunilkumarc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rythm.js's Issues

SVG : Color dance.

Make SVG color dance using the fill css property.
Same API than color dance.

Improve demo page UX

Some people don't find easily how the demo page work.

Maybe adding an arrow that pulse to get attention from visitors ?

Add generic function handler as first argument to .addRythm

It would be nice to let who uses your lib a kind of "free will" to manipulate the dom as he/she feels like:

rythm.addRythm(elementClass, danceType, startValue, nbValue);
rythm.addRythm(functionHandler, danceType, startValue, nbValue);

rythm.addRythm(function(event) {

    // event contains the current config and 
    // a .value which change from 0 to 1 based to the current rythm.
    myElement.style.opacity = event.value;

}, danceType, startValue, nbValue);

audioelement is not defined

Hello, thank you for this powerful project!

I used the process found that when I use the connectExternalAudioElement method, it will prompt audioelement is not defined, seemingly because the compressed package variable name is not caused.

image

how to set volume for songs?

i use the following script to set volume, but it obviously won't work w/ this:

<script>

	var audio = document.currentScript.parentElement;

	audio.volume = 0.3;

	</script>

Any way to do this?

Add an option to choose if on stop, elements should reset to their original position or not

Currently, if you stop, it will stop the music and the element will not reset to their position.
I feel that the user should be able to choose the behavior.
I consider adding this into the library itself

This would required some changes like :

  • A way to provide the option or a "stopAndReset" function
  • Each dance would need a reset function that could be called to reset elements.

any way to catch the audio is finished play?

any way to catch the audio is finished play?
so we can move out rythm class,add new rythm class or change another css when new audio been loaded to rythem JS.
thank you very much.

get error when trying to run it

I get this error when trying to run it :

yarn start v0.27.5
warning package.json: License should be a valid SPDX license expression

Export umd build

To work with bundler and not polluting the global environment

You can check out Rollup for reference or I can make a PR as well ;)

Rhythm

Was there a reason for misspelling rhythm? Just curious

Uncaught (in promise) DOMException: Failed to load because no supported source was found

Hello, I tried to use this library into an angular 2 project but I get the following error:
Uncaught (in promise) DOMException: Failed to load because no supported source was found

At the app.component,ts of my app I added the following commands:

this.rythm = new Rythm();
this.rythm.setMusic('../assets/rythm.mp3');
this.rythm.start() 

Unfortunately, I'm not able to give you more information because I don't know which is the problem and I didn't find something to help me.

Has anyone the same problem or does anybody has any idea about this error?
Thank you!

CORS error on remote mp3

Hello, I'm getting the following error when fetching audio sources from remote servers with the following code:

rythm.setMusic('https://archive.org/download/testmp3testfile/mpthreetest.mp3');

MediaElementAudioSource outputs zeroes due to CORS access restrictions for https://archive.org/download/testmp3testfile/mpthreetest.mp3

Any thoughts on how to resolve this?

Does not work in iOS Safari

Iโ€™m not sure if you have considered mobile or even Safari but starting the demo initially rotates the Github and Release Notes buttons but no music plays nor does everything dance like it does on a desktop (Chrome).

Recognize youtube audio stream

I'm using youtube iframe API in my project. How can I intercept the sound from the player and process it with rythm? Is it possible? Thank you!

SyntaxError

I've got this SyntaxError and why?

SyntaxError: export declarations may only appear at top level of a module rythm.js:1

Able to use with other player?

Thanks for the great work, i am using orher music player called APlayer. Can I add the rythm into the this player? It means the audio come from other player.

Or can you give me some tips how to add it? Thanks again

Add a minified version

Build up the code into a minified version.

Will also probably output built files into a /dist folder

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.