Demonstração em: https://okazari.github.io/rythm.js/
Uma biblioteca JavaScript que faz sua página dançar.
Instale com o NPM:
npm install rythm.jsOu obtenha de um CDN:
https://unpkg.com/rythm.js/
https://cdnjs.cloudflare.com/ajax/libs/rythm.js/2.2.6/rythm.min.js
Importar ritmo para sua página:
< script type =" text/javascript " src =" /path/to/rythm.min.js " > </ script >Adicione uma das classes Rythm CSS para indicar qual elemento dançará:
< div class =" rythm-bass " > </ div >Crie um objeto de ritmo e forneça seu URL de áudio e use a função Iniciar:
var rythm = new Rythm ( )
rythm . setMusic ( 'path/to/sample.mp3' )
rythm . start ( ) import Rythm from 'rythm.js'
const rythm = new Rythm ( )
rythm . setMusic ( 'path/to/sample.mp3' )
rythm . start ( ) 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 ) Você pode usar a função addRythm para fazer com que suas próprias classes ouçam frequências específicas. Aqui está como as classes básicas são criadas:
addRythm('rythm-bass', 'pulse', 0, 10)addRythm('rythm-medium', 'pulse', 150, 40)addRythm('rythm-high', 'pulse', 500, 100) Para obter mais controle desses tipos de dança, você pode dar um objeto de configuração como último argumento ao addRythm :
addRythm ( 'rythm-high' , 'shake' , 500 , 100 , { direction : 'left' , min : 20 , max : 300 } )Aqui estão as danças internas e suas opções:
transform: scale() . Padrão: 0.75transform: scale() . Padrão: 1.25transform: translateY() . Padrão: 0transform: translateY() . Padrão: 30transform: translateX() . Padrão: -15transform: translateX() . Padrão: 15left para um movimento direito para esquerda, right o movimento esquerdo para direita. Padrão: righttransform: rotate() . Padrão: -20transform: rotate() . Padrão: 20left para um movimento direito para esquerda, right o movimento esquerdo para direita. Padrão: rightopacity . Padrão: 0opacity . Padrão: 1false (maior o pulso é, mais visível será)[0,0,0][255,255,255][0,0,0][255,255,255]border-radius . Padrão: 0border-radius . Padrão: 25falsefilter: blur() . Padrão: 0filter: blur() . Padrão: 8falseup ou down . Padrão: downright ou left . Padrão: right20letter-spacing . Padrão: 0letter-spacing . Padrão: 25false[0,0,0][255,255,255]border-width . Padrão: 0border-width . Padrão: 5font-width . Padrão: 0.8font-width . Padrão: 1.2tilt . Padrão: 20tilt . Padrão: 25false[0,0,0][255,255,255]Para ver cada efeito visual, você pode ir para a demonstração.
Se você deseja usar seu próprio tipo de dança, precisa dar um objeto como o segundo argumento de addRythm em vez de uma chave de dança embutida.
Este objeto deve ter duas propriedades:
/* 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 )Qualquer solicitação de tração será apreciada. Você pode começar a codificar neste projeto seguindo estas etapas:
npm installnpm start na pasta principal para iniciar um servidor da Web de desenvolvimentoEm v2.2.x, adicionar um novo tipo de dança é bem fácil:
srcdances Por exemplo, aqui está o conteúdo do arquivo jump.js :
/* 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 = ''
}Dancer.js : import jump , { reset as resetJump } from './dances/jump.js'
class Dancer {
constructor ( ) {
this . registerDance ( 'jump' , jump , resetJump )
}
}Licença: GNU GPL
Autor: @okazaribzh