Rythm.js
v2.2.5 - A lot of new dances !
演示:https://okazari.github.io/rythm.js/
一個使您的頁面舞蹈的JavaScript庫。
使用NPM安裝:
npm install rythm.js或從CDN中獲得:
https://unpkg.com/rythm.js/
https://cdnjs.cloudflare.com/ajax/libs/rythm.js/2.2.6/rythm.min.js
將RYTHM導入您的頁面:
< script type =" text/javascript " src =" /path/to/rythm.min.js " > </ script >添加RYTHM CSS課程之一,以指示哪些元素會跳舞:
< div class =" rythm-bass " > </ div >創建一個RYTHM對象並給予您的音頻URL,然後使用“開始”功能:
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 ) 您可以使用addRythm函數來使自己的課程收聽特定的頻率。這是創建基礎類的方式:
addRythm('rythm-bass', 'pulse', 0, 10)addRythm('rythm-medium', 'pulse', 150, 40)addRythm('rythm-high', 'pulse', 500, 100)有關這些舞蹈類型的更多控制權,您可以將配置對像作為最後一個參數addRythm :
addRythm ( 'rythm-high' , 'shake' , 500 , 100 , { direction : 'left' , min : 20 , max : 300 } )這是內置的舞蹈及其選擇:
transform: scale() 。默認值: 0.75transform: scale() 。默認值: 1.25transform: translateY() 。默認值: 0transform: translateY() 。默認值: 30transform: translateX() 。默認值: -15transform: translateX() 。默認值: 15left向左移動, right移動,以左至右移動。默認值: righttransform: rotate() 。默認值: -20transform: rotate() 。默認值: 20left向左移動, right移動,以左至右移動。默認值: rightopacity 。默認值: 0opacity 。默認值: 1false (脈衝越高,它將越明顯)[0,0,0][255,255,255][0,0,0][255,255,255]border-radius的最小值。默認值: 0border-radius最大值。默認值: 25falsefilter: blur() 。默認值: 0filter: blur() 。默認值: 8falseup down 。默認值: downright擺動left默認值: right20letter-spacing最小值。默認值: 0letter-spacing最大值。默認值: 25false[0,0,0][255,255,255]border-width的最小值。默認值: 0border-width的最大值。默認值: 5font-width最小值。默認值: 0.8font-width最大值。默認值: 1.2tilt的最低值。默認值: 20tilt給出的最大值。默認值: 25false[0,0,0][255,255,255]要查看每個視覺效果,您可以轉到演示。
如果您想使用自己的舞蹈類型,則需要將對像作為addRythm的第二個論點,而不是內置的舞蹈鍵。
該對象必須具有兩個屬性:
/* 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 )任何拉的請求將不勝感激。您可以按以下步驟開始在此項目上進行編碼:
npm installnpm start以啟動開發Web服務器在v2.2.2中,添加新的舞蹈類型非常容易:
srcdances中創建一個新文件例如,這是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 )
}
}許可證:GNU GPL
作者:@okazaribzh