Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2160

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T02:50:08+00:00 2024-11-26T02:50:08+00:00

Project 1: JavaScript DrumKit

  • 61k


Welcome to my “Build 30 Js Projects in 30 Days” Series .This is day 1 and project 1. If you haven't read the other articles in this series please check them out first. I'll list them at the end of this article.

As mentioned in my previous article. This is the Day 1 challenge of Wes Bos Javascript30 course.

Here is the final result:
JavaScript drumkit image

As always before starting, download the starter files from here. I've made a separate article on how to download starter files, you can check it out here.

PART 1:HTML

<body>     <div class="container">          <div class="bg-wrap">              <img class="bg-image" src="background.jpg">          </div>         <table>             <tr>                 <td class="drum-key"><button value="65">A <span>Clap</span></button></td>                 <td class="drum-key"><button value="83">S <span>Hihat</span></button></td>                 <td class="drum-key"><button value="68">D <span>Kick</span></button></td>                 <td class="drum-key"><button value="70">F <span>OpenHat</span></button></td>                 <td class="drum-key"><button value="71">G <span>Boom</span></button></td>                 <td class="drum-key"><button value="72">H <span>Ride</span></button></td>                 <td class="drum-key"><button value="74">J <span>Snare</span></button></td>                 <td class="drum-key"><button value="75">K <span>Tom</span></button></td>                 <td class="drum-key"><button value="76">L <span>Tink</span></button></td>             </tr>         </table>         </div>     <script src="script.js"></script> </body>  
Enter fullscreen mode Exit fullscreen mode

The thing to notice here is that I've given value of buttons as ascii value of characters mentioned on drum keys. I've created a table with 1 row and each data cell is a button.

PART 2:CSS

Now we are going to style our project.

*{     margin: 0;     padding:0;  } body{     overflow: hidden; } .container{     position: relative; } .bg-image{     background: url('background.jpg') center;     opacity: 0.7;     position: absolute;     top:0;     bottom:0;     width:100%;     height:auto;     z-index:-1;     position: relative; } table{     margin-left: auto;     margin-right: auto;     position: absolute;     top: 55%;     z-index:2;  } table td button{     width:3.5em;     height:3em;     font-size:2rem;     color: white;     background: transparent;     border:2px solid black;     transition: all 0.06s;     margin:1rem;  } table td button span{     font-size: 1rem;     color:gold;    display: block;  } .bright{     border: 2px solid gold;     box-shadow: 0px 3px gold;     transform: scale(1.2); } 
Enter fullscreen mode Exit fullscreen mode

Here bright class will be called when a sound is being played.Now as this article focuses mostly on javascript, I'm not getting deep into css explanation part. Try to read through it and if you've any suggestions or doubts, feel free to comment below.

PART 3:JAVASCRIPT

Now we will make our project interactive.
So the idea of this project is to play sound when

  1. Key mentioned on the button is pressed on keyboard: Here we will use event listener keydown. We will use keyCode to get the key pressed by user. Then we will pass it to a function to play the corresponding sound.
  2. We click the button using cursor. Here we will get the value of button clicked by using event listener click and then we will pass it to the function to play the sound.

Initialising audio variables –>

let clap= new Audio('sounds/clap.wav'); let boom= new Audio('sounds/boom.wav'); let hihat= new Audio('sounds/hihat.wav'); let kick= new Audio('sounds/kick.wav'); let openhat= new Audio('sounds/openhat.wav'); let ride= new Audio('sounds/ride.wav'); let snare= new Audio('sounds/snare.wav'); let tink= new Audio('sounds/tink.wav'); let tom= new Audio('sounds/tom.wav'); 
Enter fullscreen mode Exit fullscreen mode

Now we will solve the cases as explained above.

let keys=document.getElementsByClassName('drum-key');  for(let key of keys){    //This will play sound when any key is pressed     key.addEventListener('keydown',(e)=>{                //******adding bright class*****          //will call button having keycode as value.          let keyboardvalue=document.querySelector(`button[value="${e.keyCode}"]`);           //if any non displayed key is pressed.          if(keyboardvalue)         keyboardvalue.classList.add('bright');         playSound(e.keyCode);          //since transitioning is already a time bound property, using another time bound function is not recommended.             //    setTimeout(()=>             //     {keyboardvalue.classList.remove('bright');             //     },500);       })                 //*******to remove bright class*******     // we will use property transitionend.         key.addEventListener('transitionend',(e1)=>{         //since transitionend triggers every time transition is made i.e. for change in borders etc. as well , we are returning if transitionend is not triggered for property transform.          if(e1.propertyName!='transform') return;         e1.target.classList.remove('bright');        })              //******when button is clicked****     key.addEventListener('click',(e2)=>{         console.log(e2.target.value);         if(e2.target.value)         {playSound(parseInt(e2.target.value));          //Value data type is string so we will convert it to integer as switch case takes integer as parameter.         e2.target.classList.add('bright');}     }) } 
Enter fullscreen mode Exit fullscreen mode

Now we will create function playSound which takes the ascii value of key as parameter and plays the corresponding sound using swich-case.

function playSound(ch){     switch(ch)         {              case 65: {clap.currentTime=0;  //if one audio is playing then same audio will not play again as function is already running. to prevet this we et currenttime=0.                                clap.play();}                      break;             case 83: hihat.currentTime=0;                       hihat.play();                      break;             case 68: kick.currentTime=0;                       kick.play();                     break;             case 70: openhat.currentTime=0;                       openhat.play();                     break;             case 71: boom.currentTime=0;                       boom.play();                     break;             case 72: ride.currentTime=0;                       ride.play();                     break;             case 74: snare.currentTime=0;                       snare.play();                     break;             case 75: tom.currentTime=0;                       tom.play();                     break;             case 76: tink.currentTime=0;                       tink.play();                     break;         } }  
Enter fullscreen mode Exit fullscreen mode

I hope you understood the whole code.

Source code: Click here

Things I learnt

1.keydown event listener.
2.transitionend event listener.
3.transform and transition.
4.Alternative to using setTimeout function with transition.
5.How to play audio again even if play() function is already running for that audio.

Previous article from this series::

Click here

Conclusion

That's it for today's project.Next project will be an Alarm Clock .

If you have any doubts or suggestions please do let me know in the comment section. I'll be more than happy to interact with you.

If you like this series and want to be a part of it, do consider following me at @prachigarg19

Thanks for reading. 🙂

beginnersjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.