Created
July 22, 2022 09:17
-
-
Save ff6347/e4da503b37aaa538a90f17c40a436eb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const FRONT_KEYS = ['y', 'x', 'c', 'v', 'b', 'n', 'm', ',']; | |
| const BACK_KEYS = ['s', 'd', 'g', 'h', 'j']; | |
| const frogs = document.querySelectorAll('.frog'); | |
| const frontFrogs = document.querySelectorAll('.frog.front'); | |
| const backFrogs = document.querySelectorAll('.frog.back'); | |
| frogs.forEach(frog => { | |
| frog.addEventListener('click', () => playNote(frog)); | |
| }); | |
| document.addEventListener('keydown', e => { | |
| const key = e.key; | |
| const frontKeyIndex = FRONT_KEYS.indexOf(key); | |
| const backKeyIndex = BACK_KEYS.indexOf(key); | |
| if (frontKeyIndex > -1) playNote(frontFrogs[frontKeyIndex]); | |
| if (backKeyIndex > -1) playNote(backFrogs[backKeyIndex]); | |
| if (e.key === 'y') { | |
| const element = document.querySelector('#C-note'); | |
| if (!element) { | |
| throw new Error(`Element not found, ${e.key}`); | |
| } | |
| playNote(element); | |
| } | |
| }); | |
| function playNote(frog) { | |
| const noteAudio = document.getElementById(frog.dataset.note); | |
| noteAudio.currentTime = 0; | |
| noteAudio.play(); | |
| frog.classList.add('active'); | |
| noteAudio.addEventListener('ended', () => { | |
| frog.classList.remove('active')} | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment