Mozilla

Making the Dino roar – syncing audio and CSS transitions

It started with Brian King setting up our Google+ page using this round MDN logo by John Slater. I thought this looks cool and reminded me of the famous MGM intro so I wondered if I could turn it into an intro for our video tutorials (not sure if we will do that though). And, some photoshop and sound work later and with a sprinkle of HTML5 audio and CSS transitions, here we are (source on GitHub):

I started with the sound. If you need Creative Commons licensed sounds, Freesound is a good resource. So I took Chinese Fanfare by Nick-Nack and Roar by CGEffex and put them together in Audacity.

Saving them as OGG and MP3 gave me an audio element that I could tie into. All I needed was to listen to the timeupdate event and compare the currentTime to trigger the animations. The animations (rotation of the dino and opening and closing of the jaw) are CSS transitions triggered by classes on the parent element. The main trick was to store both the dino and the jaw inside a div and transition them separately. The jaw animation also needed a change in transformation origin as we don’t rotate the image around its center.

If you got seven minutes to spare, here is a blow-by-blow screencast explaining what is going on:

7 comments

Comments are now closed.

  1. Mardeg wrote on March 20th, 2012 at 9:13 am:

    I’m a little saddened by this, still needing javascript for synching at all almost 7 years after one of my very first comments on bugzilla was hopeful about SMIL on the audio tag:
    https://bugzilla.mozilla.org/show_bug.cgi?id=216462#c6
    Is it too late to still hope for that?

    1. Jakob wrote on March 20th, 2012 at 10:42 am:

      You don’t have to use JavaScript, you could also use CSS animations.

      1. Jakob wrote on March 20th, 2012 at 11:33 am:

        And this [1] is the proof.

        [1]: https://dl.dropbox.com/u/18835355/html/animation_syncing/anim.html

        1. Chris Heilmann wrote on March 20th, 2012 at 12:06 pm:

          Very cool. But as you state – you still use JS to make sure the syncing happens when all the things have loaded, so in essence this is the same. I used transitions as they are better supported across browsers. But nothing stops you from achieving this with animations.

  2. Josh Matthews wrote on March 20th, 2012 at 11:20 am:

    That’s fantastic.

  3. Caspy7 wrote on March 21st, 2012 at 2:43 pm:

    My first thought was to implement a song using typography/lyrics in creative ways like this one:
    http://www.youtube.com/watch?v=CJA69C6SlRk
    Doing something like this in HTML would seem like a nice proof of concept.

    Though my dream would be that it could then be created using a tool/program which wouldn’t require any code knowledge (just as that original likely was).

  4. SayaK wrote on November 24th, 2012 at 12:34 am:

    Great tutorial! I have a question. How would you change the script so the animation and sound run onclick instead of automatically when the page loads? I’m trying to do something similar where the user clicks a button to trigger the animation and audio.

Comments are closed for this article.