FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Piano/script.js at master · DukeCho38/JavaScript-Piano · GitHub
DukeCho38
/
JavaScript-Piano
Public
forked from
WebDevSimplified/JavaScript-Piano
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Piano
/
script.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
30 lines (25 loc) · 905 Bytes
Breadcrumbs
JavaScript-Piano
/
script.js
Copy path
File metadata and controls
30 lines (25 loc) · 905 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const
WHITE_KEYS
=
[
'z'
,
'x'
,
'c'
,
'v'
,
'b'
,
'n'
,
'm'
]
const
BLACK_KEYS
=
[
's'
,
'd'
,
'g'
,
'h'
,
'j'
]
const
keys
=
document
.
querySelectorAll
(
'.key'
)
const
whiteKeys
=
document
.
querySelectorAll
(
'.key.white'
)
const
blackKeys
=
document
.
querySelectorAll
(
'.key.black'
)
keys
.
forEach
(
key
=>
{
key
.
addEventListener
(
'click'
,
(
)
=>
playNote
(
key
)
)
}
)
document
.
addEventListener
(
'keydown'
,
e
=>
{
if
(
e
.
repeat
)
return
const
key
=
e
.
key
const
whiteKeyIndex
=
WHITE_KEYS
.
indexOf
(
key
)
const
blackKeyIndex
=
BLACK_KEYS
.
indexOf
(
key
)
if
(
whiteKeyIndex
>
-
1
)
playNote
(
whiteKeys
[
whiteKeyIndex
]
)
if
(
blackKeyIndex
>
-
1
)
playNote
(
blackKeys
[
blackKeyIndex
]
)
}
)
function
playNote
(
key
)
{
const
noteAudio
=
document
.
getElementById
(
key
.
dataset
.
note
)
noteAudio
.
currentTime
=
0
noteAudio
.
play
(
)
key
.
classList
.
add
(
'active'
)
noteAudio
.
addEventListener
(
'ended'
,
(
)
=>
{
key
.
classList
.
remove
(
'active'
)
}
)
}
Back
|
FazBrowse Home
|
New Git URL