Open In App

How to sync css animations with HTML5 audio ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, you will learn to synchronize CSS Animations with audio to create dynamic good websites. This is basically used in online games or animated stories to create some kind of audio effects. 

We will first analyze the audio and ultimately sync it with the CSS animation property.

Approach: To explain the sync of audio, we are going to consider an example of a drum sound

  • Analyzing the sound
  • Designing the drum
  • Syncing them together

1. Analyzing the sound:

To add the audio to the web page, we need to use the HTML <audio/> tag. We will use the preload property to load the audio before the DOM.

<audio id="drum" src="path_to_audio" preload="auto">
    ... Your browser does not support the audio element.
</audio>

Audio used: Bongos_Drum

We need to figure out the point of time where the sound actually beats and to make movements in the animation at the right time. We can use any software that creates a visualization of the audio. Software like audacity or twisted wave audio editor can help you do this job. We are using twisted wave audio editor which is an online editor which looks like the following.

 

The timeline in the software will help you to get the point of time at which animation should be done. We need to open the required audio file in this software. Once we get the time in seconds convert them to percentages to get the @keyframes values.

Result: 

seconds

0s

0.183s

0.329s

0.486s

0.636s

0.790s

0.953s

1.094s

1.266s

%

0

9.9

17.3

24.6

31.5

39

47

54

62

——–

——-

——-

——-

——-

——-

——-

——-

——-

——

seconds

1.439s

1.606s

1.783s

1.927s

 

%

68

78.4

87.7

95

2. Designing the drum: We will design a drum with two sticks and make an animation of sticks beating the drum.

To design the drum we are using the following HTML structure.

 

In this figure, we have a parent with id= “drum”. It consists of three HTML div with class top, mid and bottom indicating the different portions of the drum, a <audio> tag, and two sticks with class s1 and s2.

HTML Code:

HTML




<!DOCTYPE html>
<html>
  
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>How to Sync CSS Animations with HTML5 Audio ?</h2>
    <h3>A Drum Sound with CSS Animation</h3>
    <div id='drum'>   
        <div class="top">
            <div class="stick s1"></div>
            <div class="stick s2"></div>
        </div>
        <div class="mid"></div>
        <div class="bottom">
            <div class="bar b1"></div>
            <div class="bar b2"></div>
        </div>
        <audio src=
           controls>
        </audio>
    </div>
</body>
</html>


Output:

 

CSS Code: Adding some styles to the above HTML code.

CSS




<style>
    #drum{
            width:50%;
            margin:auto;
            position:relative;
            margin-top:100px;
    }
    audio{
            position:relative;
            left:50%;
            transform:translate(-50%);
    }
    h2{
            text-align:center;
            margin:50px
    }
    .top{
            border:5px solid blue;
            width:200px;
            margin:auto;
            height:100px;
            border-radius:100%;
            background:white;
            box-shadow:0px 2px 10px black inset;
            position:relative
    }
    .mid{
            width:180px;
            background:blue;
            height:150px;
            margin:auto;
            position:relative;
            top:-60px;
            z-index:-1;
    }
    .bottom{
            width:180px;
            margin:auto;
            height:100px;
            border-radius:100%;
            background:blue;
            position:relative;
            top:-120px;
            box-shadow:0px 25px 20px black;
    }
    .mid::before{
            content:"";
            position:absolute;
            left:-8px;
            height:130px;
            border:10px solid blue;
            transform:rotate(-5deg);
    }
    .mid::after{
            content:"";
            position:absolute;
            right:-8px;
            height:130px;
            border:10px solid blue;
            transform:rotate(5deg);
    }
    .bar{
            height:130px;
            border:3px solid;
            width:0px;
            display:inline-block;
            position:relative;
    }
    .b1{
            left:20px;
            transform:rotate(-2deg);
            top:-48px;
    }
    .b2{
            left:130px;
            transform:rotate(2deg);
            top:-42px;
    }
    .stick{
            height:130px;
            border:3px solid brown;
            width:0px;
            display:inline-block;
            position:absolute;
            border-radius:50px;
            top:-70px;
    }
    .s1{
            transform:rotate(-45deg);
    }
    .s2{
            left:180px;
            transform:rotate(45deg);
    }
</style>


3. Sync with audio:

Animation Type: Sticks beating the drum on click of the audio play button

JavaScript Code: To do this we need JavaScript which adds two classes .active1 and .active2 to the sticks HTML div with classes s1 and s2 to start the animation.

Javascript




<script>
        var audio = document.getElementsByTagName('audio')[0];
        audio.onplay=()=>{
            document.getElementsByClassName('stick')[0].classList.add('active1');
            document.getElementsByClassName('stick')[1].classList.add('active2');
            console.log("hi")
        }
        audio.onpause=()=>{
            document.getElementsByClassName('stick')[0].classList.remove('active1');
            document.getElementsByClassName('stick')[1].classList.remove('active2');
        }
</script>


We will add this code in the above HTML itself. It is selecting the HTML audio tag by adding the onplay classes and removing onpause.

The .active1 and .active2 classes add the animation like the following syntax

.active1{
     animation : tap1 2.1s ;
}
.active2{
     animation : tap2 2.1s ;
}

The tap1 and tap2 are the animation names for the two sticks in the figure. The duration of the audio is around 2.1s and we will be playing the animation for that many seconds.

CSS Code: To move the sticks we are rotating stick s1 between -45o and -60o and stick s2 between 45o and 60o with a bit of adjustment in their lengths using @keyframes.

CSS




@keyframes tap1{
     9%{
           transform:rotate(-45deg);
           height:130px;
     }
     24.6%{
           transform:rotate(-45deg);
           height:130px;
     }
     39%{
           transform:rotate(-45deg);
           height:130px;
     }
     54%{
           transform:rotate(-45deg);
           height:130px;
     }
     68%{
           transform:rotate(-45deg);
           height:130px;
     }
     87.7%{
           transform:rotate(-45deg);
           height:130px;
     }
    0%{
         transform:rotate(-60deg);
         height:90px;
    }
    17.3%{
         transform:rotate(-60deg);
         height:90px;
    }
    31.5%{
         transform:rotate(-60deg);
         height:90px;
    }
    47%{
         transform:rotate(-60deg);
         height:90px;
    }
    62%{
         transform:rotate(-60deg);
         height:90px;
    }
    78.4%{
         transform:rotate(-60deg);
         height:90px;
    }
    95%{
         transform:rotate(-60deg);
         height:90px;
    }
}
@keyframes tap2{
     0%{
           transform:rotate(45deg);
           height:130px;
     }
     17.3%{
           transform:rotate(45deg);
           height:130px;
     }
     31.5%{
           transform:rotate(45deg);
           height:130px;
     }
     47%{
           transform:rotate(45deg);
           height:130px;
     }
     62%{
           transform:rotate(45deg);
           height:130px;
     }
     78.4%{
           transform:rotate(45deg);
           height:130px;
     }
     95%{
           transform:rotate(45deg);
           height:130px;
     }
     9.9%{
           transform:rotate(60deg);
           height:90px;
     }
     24.6%{
           transform:rotate(60deg);
           height:90px;
     }
     39%{
           transform:rotate(60deg);
           height:90px;
     }
     54%{
           transform:rotate(60deg);
           height:90px;
     }
     68%{
           transform:rotate(60deg);
           height:90px;
     }
     87.7%{
           transform:rotate(60deg);
           height:90px;
     }
}


Example 1: The complete running code of the sticks beating the drum with sound.

HTML




<!DOCTYPE html>
<html>
<head>
    <style>        
        #drum{
            width:50%;
            margin:auto;
            position:relative;
            margin-top:100px;
        }
        audio{
            position:relative;
            left:50%;
            transform:translate(-50%);
        }
        h2{
            text-align:center;
            margin:50px
        }
        .top{
            border:5px solid blue;
            width:200px;
            margin:auto;
            height:100px;
            border-radius:100%;
            background:white;
            box-shadow:0px 2px 10px black inset;
            position:relative
        }
        .mid{
            width:180px;
            background:blue;
            height:150px;
            margin:auto;
            position:relative;
            top:-60px;
            z-index:-1;
        }
        .bottom{
            width:180px;
            margin:auto;
            height:100px;
            border-radius:100%;
            background:blue;
            position:relative;
            top:-120px;
            box-shadow:0px 25px 20px black;
        }
        .mid::before{
            content:"";
            position:absolute;
            left:-8px;
            height:130px;
            border:10px solid blue;
            transform:rotate(-5deg);
        }
        .mid::after{
            content:"";
            position:absolute;
            right:-8px;
            height:130px;
            border:10px solid blue;
            transform:rotate(5deg);
        }
        .bar{
            height:130px;
            border:3px solid;
            width:0px;
            display:inline-block;
            position:relative;
        }
        .b1{
            left:20px;
            transform:rotate(-2deg);
            top:-48px;
        }
        .b2{
            left:130px;
            transform:rotate(2deg);
            top:-42px;
        }
        .stick{
            height:130px;
            border:3px solid brown;
            width:0px;
            display:inline-block;
            position:absolute;
            border-radius:50px;
            top:-70px;
        }
        .s1{
            transform:rotate(-45deg);
        }
        .s2{
            left:180px;
            transform:rotate(45deg);
        
        @keyframes tap1{
            9%{
                transform:rotate(-45deg);
                height:130px;
            }
            24.6%{
                transform:rotate(-45deg);
                height:130px;
            }
            39%{
                transform:rotate(-45deg);
                height:130px;
            }
            54%{
                transform:rotate(-45deg);
                height:130px;
            }
            68%{
                transform:rotate(-45deg);
                height:130px;
            }
            87.7%{
                transform:rotate(-45deg);
                height:130px;
            }
            0%{
                transform:rotate(-60deg);
                height:90px;
            }
            17.3%{
                transform:rotate(-60deg);
                height:90px;
            }
            31.5%{
                transform:rotate(-60deg);
                height:90px;
            }
            47%{
                transform:rotate(-60deg);
                height:90px;
            }
            62%{
                transform:rotate(-60deg);
                height:90px;
            }
            78.4%{
                transform:rotate(-60deg);
                height:90px;
            }
            95%{
                transform:rotate(-60deg);
                height:90px;
            }
        }
        @keyframes tap2{
            0%{
                transform:rotate(45deg);
                height:130px;
            }
            17.3%{
                transform:rotate(45deg);
                height:130px;
            }
            31.5%{
                transform:rotate(45deg);
                height:130px;
            }
            47%{
                transform:rotate(45deg);
                height:130px;
            }
            62%{
                transform:rotate(45deg);
                height:130px;
            }
            78.4%{
                transform:rotate(45deg);
                height:130px;
            }
            95%{
                transform:rotate(45deg);
                height:130px;
            }
            9.9%{
                transform:rotate(60deg);
                height:90px;
            }
            24.6%{
                transform:rotate(60deg);
                height:90px;
            }
            39%{
                transform:rotate(60deg);
                height:90px;
            }
            54%{
                transform:rotate(60deg);
                height:90px;
            }
            68%{
                transform:rotate(60deg);
                height:90px;
            }
            87.7%{
                transform:rotate(60deg);
                height:90px;
            }
        }
    </style>
</head>
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>How to Sync CSS Animations with HTML5 Audio ?</h2>
    <h3>A Drum Sound with CSS Animation</h3>
    <div id='drum'>   
        <div class="top">
            <div class="stick s1"></div>
            <div class="stick s2"></div>
        </div>
        <div class="mid"></div>
        <div class="bottom">
            <div class="bar b1"></div>
            <div class="bar b2"></div>
        </div>
        <audio src=
           controls>
        </audio>
    </div>    
    <script>
        var audio = document.getElementsByTagName('audio')[0];
        audio.onplay=()=>{
            document.getElementsByClassName('stick')[0].classList.add('active1');
            document.getElementsByClassName('stick')[1].classList.add('active2');
            console.log("hi")
        }
        audio.onpause=()=>{
            document.getElementsByClassName('stick')[0].classList.remove('active1');
            document.getElementsByClassName('stick')[1].classList.remove('active2');
        }
    </script>
</body>
</html>
       


Output: Play the below video to listen to the audio as well.

Example 2: The following example is of syncing of the railroad crossing signal animation

HTML




<!DOCTYPE html>
<html>      
    <style>
        .signal {
            font-size: 1.8em;
            height: 400px;
            margin: 2em auto 1em;
            position: relative;
            text-align: center;
            width: 500px;
        }
        .lights,
        .lights::after {
            background-color: #400000;
            border: 0.6em solid #f00;
            border-radius: 5%;
            height: 2.6em;
            left: 0;
            position: relative;
            width: 2.6em;
        }
        .lights::after {
            content: "";
            left: 515%;
            right: 0;
            top: -0.4em;
            width: 2.6em;
            height: 2.6em;
            position: absolute;
        }
        .signal.active .lights {
            animation: r1 6.38s linear 0s 1;
        }
        .signal.active .lights::after {
            animation: r2 6.38s linear 0s 1;
        }
  
        @keyframes r1 {
            0.0% {
              background-color: #400;
            }
            1.4% {
              background-color: #400;
            }
            1.5% {
              background-color: #FF0;
            }
            8.8% {
              background-color: #400;
            }
            16.3% {
              background-color: #400;
            }
            16.4% {
              background-color: #FF0;
            }
            23.7% {
              background-color: #400;
            }
            31.7% {
              background-color: #400;
            }
            31.8% {
              background-color: #FF0;
            }
            39.0% {
              background-color: #400;
            }
            46.9% {
              background-color: #400;
            }
            47.0% {
              background-color: #FF0;
            }
            54.4% {
              background-color: #400;
            }
            62.1% {
              background-color: #400;
            }
            62.2% {
              background-color: #FF0;
            }
            69.7% {
              background-color: #400;
            }
            77.5% {
              background-color: #400;
            }
            77.6% {
              background-color: #FF0;
            }
            85.0% {
              background-color: #400;
            }
            92.4% {
              background-color: #400;
            }
            92.5% {
              background-color: #FF0;
            }
            100.0% {
              background-color: #400;
            }
        }
        @keyframes r2 {
            0.0% {
              background-color: #400;
            }
            8.8% {
              background-color: #400;
            }
            8.9% {
              background-color: #FF0;
            }
            23.7% {
              background-color: #400;
            }
            23.8% {
              background-color: #FF0;
            }
            31.7% {
              background-color: #400;
            }
            39.0% {
              background-color: #400;
            }
            39.1% {
              background-color: #FF0;
            }
            46.9% {
              background-color: #400;
            }
            54.4% {
              background-color: #400;
            }
            54.5% {
              background-color: #FF0;
            }
            62.1% {
              background-color: #400;
            }
            69.7% {
              background-color: #400;
            }
            69.8% {
              background-color: #FF0;
            }
            77.5% {
              background-color: #400;
            }
            85.0% {
              background-color: #400;
            }
            85.1% {
              background-color: #FF0;
            }
            92.4% {
              background-color: #400;
            }
            100.0% {
              background-color: #400;
            }
        }
        audio{
            margin:40px;
        }
    </style>
    <body>
        <div id="signal" class="signal">
          <h1 style="color:green">GeekforGeeks</h1>  
          <h3>How to Sync CSS Animations with HTML5 Audio ?</h3>
          <div class="lights"></div>
           <audio src=
                controls preload="auto">
                Your browser does not support the 
                <code>audio</code> element.
            </audio>
        </div>
        
        <script>
            var audio = document.getElementsByTagName('audio')[0];
            audio.onplay=()=>{
                  if (!signal.classList.contains('active')) {
                    signal.classList.add('active');
                  }
            }
            audio.onpause=()=>{
                signal.classList.remove('active');
            }
        </script>
    </body
</html>


Output: Play the below video to listen to the audio as well:



Last Updated : 14 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads