How to sync css animations with HTML5 audio ?
Last Updated :
14 Dec, 2022
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% );
}
h 2 {
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( -5 deg);
}
.mid::after{
content : "" ;
position : absolute ;
right : -8px ;
height : 130px ;
border : 10px solid blue ;
transform:rotate( 5 deg);
}
.bar{
height : 130px ;
border : 3px solid ;
width : 0px ;
display :inline- block ;
position : relative ;
}
.b 1 {
left : 20px ;
transform:rotate( -2 deg);
top : -48px ;
}
.b 2 {
left : 130px ;
transform:rotate( 2 deg);
top : -42px ;
}
.stick{
height : 130px ;
border : 3px solid brown;
width : 0px ;
display :inline- block ;
position : absolute ;
border-radius: 50px ;
top : -70px ;
}
.s 1 {
transform:rotate( -45 deg);
}
.s 2 {
left : 180px ;
transform:rotate( 45 deg);
}
</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 tap 1 {
9% {
transform:rotate( -45 deg);
height : 130px ;
}
24.6% {
transform:rotate( -45 deg);
height : 130px ;
}
39% {
transform:rotate( -45 deg);
height : 130px ;
}
54% {
transform:rotate( -45 deg);
height : 130px ;
}
68% {
transform:rotate( -45 deg);
height : 130px ;
}
87.7% {
transform:rotate( -45 deg);
height : 130px ;
}
0% {
transform:rotate( -60 deg);
height : 90px ;
}
17.3% {
transform:rotate( -60 deg);
height : 90px ;
}
31.5% {
transform:rotate( -60 deg);
height : 90px ;
}
47% {
transform:rotate( -60 deg);
height : 90px ;
}
62% {
transform:rotate( -60 deg);
height : 90px ;
}
78.4% {
transform:rotate( -60 deg);
height : 90px ;
}
95% {
transform:rotate( -60 deg);
height : 90px ;
}
}
@keyframes tap 2 {
0% {
transform:rotate( 45 deg);
height : 130px ;
}
17.3% {
transform:rotate( 45 deg);
height : 130px ;
}
31.5% {
transform:rotate( 45 deg);
height : 130px ;
}
47% {
transform:rotate( 45 deg);
height : 130px ;
}
62% {
transform:rotate( 45 deg);
height : 130px ;
}
78.4% {
transform:rotate( 45 deg);
height : 130px ;
}
95% {
transform:rotate( 45 deg);
height : 130px ;
}
9.9% {
transform:rotate( 60 deg);
height : 90px ;
}
24.6% {
transform:rotate( 60 deg);
height : 90px ;
}
39% {
transform:rotate( 60 deg);
height : 90px ;
}
54% {
transform:rotate( 60 deg);
height : 90px ;
}
68% {
transform:rotate( 60 deg);
height : 90px ;
}
87.7% {
transform:rotate( 60 deg);
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:
Similar Reads
How to sync css animations with HTML5 audio ?
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:
8 min read
How to use CSS Animations with Tailwind CSS ?
Tailwind CSS classes are used to style elements and apply animations effortlessly. Utilize Tailwind's animation utility classes to add dynamic visual effects. Combine Tailwind CSS with custom CSS animations for versatile and engaging web designs. Table of Content Tailwind CSS Animation Utility Class
3 min read
How to add controls to an audio in HTML5 ?
In this article, we will learn how to add controls to audio in HTML5. The HTML <audio> controls attribute is used to specify the control to play audio which means it allows embedding an HTML5 music player with audio controls straight into the webpage. The current HTML5 most commonly used ogg,
2 min read
How to Animate SVG with CSS?
Animating SVGs (Scalable Vector Graphics) with CSS is a powerful technique to bring vector-based images to life on websites. The SVG animations enhance user experience and add dynamic interactivity to the static visuals. Since SVGs are XML-based each element inside them can be controlled individuall
3 min read
How to make CSS Animations ?
Animation is a way using which we can create the illusion of motion by placing still images one after another in a typical format. For example, we can have a ball rising up at some instant then falling down as an animation effect. CSS provides us with some properties to control the animation effect
3 min read
How to create waves on button with CSS and HTML?
The wave effect on a button is a type of effect in which the shape of the button turns into a wave on hovering. While there are other ways to create a wave effect, a simple method is to use the keyframe property. Approach: In order to animate the button, we use keyframe to gradually set the transiti
2 min read
How to make a CTA Animation with Tailwind CSS ?
To enhance the engagement level of your website's Call-to-Action (CTA) elements we use Tailwind CSS utility classes. By directly customizing transition effects with Tailwind CSS, you can effortlessly introduce captivating animation effects to your CTAs, improving user interaction and overall user ex
2 min read
How to make an audio visualizer with HTML CANVAS API ?
In this article, we will see how to make an audio visualizer using the canvas API. Canvas API is used to draw graphics on the HTML canvas element. So with this feature, we will make an audio visualizer that responds to the audio from the microphone of the system. Audio Visualizer can be defined as a
4 min read
How to create a slideshow with HTML and CSS ?
A slideshow can be used to display text or images that continuously scroll from one slide to the other to display its content. This article shows an approach to building a slideshow with the use of only HTML and CSS. It consumes less browser memory and takes less computation power as there is no Jav
3 min read
How to Create Typewriter Animation using HTML and CSS ?
A typewriter animation simulates the appearance of text being typed out letter by letter, using only HTML and CSS. This effect is a simple yet eye-catching way to bring dynamic text to life on a website, capturing attention without the need for JavaScript. It's a great technique to make key text ele
7 min read