File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 5858 < audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
5959
6060< script >
61+ // There's quite a bit of delay between keydown and when the audio plays,
62+ // but this is functional :P
63+ ( function ( ) {
64+ const playingClass = 'playing' ;
6165
66+ function playSound ( e ) {
67+ const audio = document . querySelector ( `audio[data-key='${ e . keyCode } ']` ) ;
68+ const key = document . querySelector ( `.key[data-key='${ e . keyCode } ']` ) ;
69+ if ( ! audio ) return ;
70+ audio . currentTime = 0 ; // rewind the audio to the start
71+ audio . play ( ) ;
72+ key . classList . add ( playingClass ) ;
73+ }
74+
75+ function removeTransition ( e ) {
76+ if ( e . propertyName !== 'transform' ) return ;
77+ this . classList . remove ( playingClass ) ;
78+ }
79+
80+ const keys = document . querySelectorAll ( '.key' ) ;
81+ keys . forEach ( key => key . addEventListener ( 'transitionend' , removeTransition ) ) ;
82+
83+ window . addEventListener ( 'keydown' , playSound ) ;
84+ } ) ( ) ;
6285</ script >
6386
6487
You can’t perform that action at this time.
0 commit comments