File tree Expand file tree Collapse file tree 2 files changed +35
-3
lines changed
Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Original file line number Diff line number Diff line change 5757 < audio data-key ="75 " src ="sounds/tom.wav "> </ audio >
5858 < audio data-key ="76 " src ="sounds/tink.wav "> </ audio >
5959
60- < script >
61-
62- </ script >
60+ < script src ="index.js "> </ script >
6361
6462
6563</ body >
Original file line number Diff line number Diff line change 1+ class Dram {
2+ constructor ( key , audio ) {
3+ this . key = key ;
4+ this . audio = audio ;
5+ this . className = 'playing' ;
6+
7+ key . addEventListener ( 'transitionend' , event => {
8+ if ( event . propertyName !== 'transform' ) { return ; }
9+ key . classList . remove ( this . className ) ;
10+ } ) ;
11+ }
12+
13+ playSound ( ) {
14+ this . audio . currentTime = 0 ;
15+ this . audio . play ( ) ;
16+ this . key . classList . add ( this . className ) ;
17+ }
18+ }
19+
20+ const keys = Array . from ( document . querySelectorAll ( '.key' ) ) ;
21+ const drams = Array . from ( document . querySelectorAll ( 'audio' ) )
22+ . reduce ( ( drams , audio ) => {
23+ const keyCode = audio . dataset . key ;
24+ const key = keys . find ( key => key . dataset . key === keyCode ) ;
25+ if ( ! key ) { return ; }
26+ drams [ keyCode ] = new Dram ( key , audio ) ;
27+ return drams ;
28+ } , { } ) ;
29+
30+ window . addEventListener ( 'keydown' , event => {
31+ const dram = drams [ event . keyCode . toString ( ) ] ;
32+ if ( ! dram ) { return ; }
33+ dram . playSound ( ) ;
34+ } ) ;
You can’t perform that action at this time.
0 commit comments