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