Placed in: Home arrow Programming arrow Webdesign arrow jQuery DJ Hero - CSS3 and jQuery fun

Cialis Online

jQuery DJ Hero - CSS3 and jQuery fun

Remember jTypingHero? This was a fun little game to test your typing skills based on the ever popular Guitar Hero. Lately, I came across some news about a new "Hero" game: DJ Hero. Since I really love Proof of Concepts/experimenting, I wanted to bring the vinyl to the browser. That's why I present to you: jQuery DJ Hero.

By combining CSS3 and jQuery, I created two records that you can start spinning (faster and slower) and even scratching is enabled. In a future version, this could be used together with some real sound (this version has no sound).

jQuery DJ Hero - CSS3 and jQuery fun

To achieve the desired effect, I made use of jQuery, Animate CSS Rotation and Scale jQuery Patch and the jQuery Easing library. The picture of the vinyl has been taken from stock.xchng.

Demo jQuery DJ Hero - CSS3 and jQuery fun   Download jQuery DJ Hero - CSS3 and jQuery fun

Since we're using CSS3's transform property (to rotate the vinyls), this example only works in browsers that support CSS3 (Currently, the latest versions of Safari, Firefox and Chrome support this). I've added a video for those people that don't use those browsers and still want to see how the effect looks like. Make sure to check out the demo and let's see how you can create that yourself.

Example video

Below is a small video showing the effect inside Safari 4.0 where it's fully functional.

Now let's see how we can create something like that ourselves!

HTML

This is the HTML we're going to work with. As you can see, nothing really special going on here. Just a play button, two vinyls and two needles, each with the buttons to make the records spin faster/slower.

 
<div id="content">
   <div class="center">
      <p id="playbutton"><img src="images/btn-play.png" alt="Play" id="playbtn" /></p>
   </div>
   <div id="dj">
      <div class="needle" id="needle1"></div>
      <div class="vinyl" id="vinyl1"></div>
      <div class="needle" id="needle2"></div>
      <div class="vinyl" id="vinyl2"></div>
      <div id="buttons">
         <div id="leftbtns">
            <div id="slowbutton1"><img src="images/btn-slow.png" alt="Slow" /></div>
            <div id="speedbutton1"><img src="images/btn-fast.png" alt="Fast" /></div>
         </div>
         <div id="rightbtns">
            <div id="slowbutton2"><img src="images/btn-slow.png" alt="Slow" /></div>   
            <div id="speedbutton2"><img src="images/btn-fast.png" alt="Fast" /></div>
         </div>
      </div>
   </div>
</div>

I know this isn't the most fascinating part, but we'll need it as our backbone for out page. On with the CSS!

CSS

Just like the HTML, the CSS of this page isn't very spectecular. It simply places some divisions and adds the vinyl background images (the vinyls themselves). The needles are placed on a absolute position in order to make them visible on top of the vinyls.

 
#dj { width:760px; margin:0 auto; }
.center { margin:0 auto; width:128px; }
#playbutton { text-align:center; cursor:pointer; width:128px; }
 
.vinyl { width:370px; height:370px; float:left; margin:0 5px; }
#vinyl1 { background-image:url("../images/vinyl1.png"); }
#vinyl2 { background-image:url("../images/vinyl2.png"); }
 
.needle { width:50px; height:76px; position:absolute; background-image:url("../images/needle.png"); z-index:99; }
#needle1 { margin-left:300px; }
#needle2 { margin-left:680px; }
 
#buttons { z-index:9999; clear:both; }
#buttons img { cursor:pointer; }
#buttons div { float:left; margin-top:25px; }
#leftbtns { margin-left:60px; }
#rightbtns { margin-left:120px; }

Now that we have our HTML and CSS ready, we can finally continue to our most exciting part: animating and adding functionality with jQuery.

jQuery

Before we can use our jQuery script, we'll need to include the script files to our HTML. These are the jQuery libraries that we're going to use (placed in the head section of the HTML):

 
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript" src="js/jquery-css-transform.js"></script>
<script type="text/javascript" src="js/jquery-animate-css-rotate-scale.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>

  1. First, we'll load the JavaScript API from Google.
  2. The second one is our own script, that'll load jQuery and have our functionality we need.
  3. After that, we'll use the Animate CSS Rotation and Scale jQuery Patch.
  4. The next script is needed by the previous one.
  5. Finally, we load the jQuery Easing library for smoother animations.

After we've done the HTML part, we can move on with the actual JavaScript. I've added comments in the code to explain some stuff.

Global variables

 
// Variable to store if the records are spinning
var playing = false;
  
// Variable to store if the mouse is down (to enable scratching)
var mousedown = false;

Playbutton

 
// Function to be called when the play button is clicked.
// It changes from "play" to "pause" when records are spinning
// and starts both the vinyls.
$("#playbutton").click(function() {
   checkButtons();
   if(playing) {
      // Pause
      $("#playbtn").attr("src", "images/btn-play.png");
      playing = false;
      $(".vinyl").each(function() {
         // Clear the interval from the vinyl
         // to stop spinning
         var intervalHandle = $(this).data('intervalHandle');
         clearInterval(intervalHandle);
         $(this)
            .css({ 'cursor' : 'default' })
            .stop()
            .animate({rotate: '+=40deg'}, 800, 'easeOutCubic');            
      });
   } else {
      // Play
      $("#playbtn").attr("src", "images/btn-pause.png");
      playing = true;
      $(".vinyl").each(function() {
         $(this)
            .css({ 'cursor' : 'move' })
            .data('rotationAngle', 10);
         startSpinning($(this));
      });
   }
});

Scratching

 
// Handle the "mouseDown" to enable scratching
// We can't combine "mouseDown" with "mouseMove", so we'll need
// to set a boolean (mousedown).
// We're also clearing the intervals to prevent spinning
$(".vinyl").mousedown(function(e) {
   var intervalHandle = $(this).data('intervalHandle');
   clearInterval(intervalHandle);
   mousedown = true;
}).mouseup(function() {
   mousedown = false;
   if(playing) {
      startSpinning($(this));
   }
});
 
// When mousedown is true and the records are playing,
// we can scratch the vinyls. This is where the code can be improved,
// since we only register X-movement
$(".vinyl").mousemove(function(e){
   if(mousedown && playing) {
      var intervalHandle = $(this).data('intervalHandle');
      clearInterval(intervalHandle);
      $(this).rotate(e.pageX % 360);
   }
});

Speed control buttons

 
// Handlers for each speed button (slow or faster)
$("#speedbutton1").click(function() {
   if($(this).data('isEnabled')) {
      $("#vinyl1").data('rotationAngle', $("#vinyl1").data('rotationAngle') + 10);
   }
   checkButtons();
});
 
$("#slowbutton1").click(function() {
   if($(this).data('isEnabled')) {
      $("#vinyl1").data('rotationAngle', $("#vinyl1").data('rotationAngle') - 10);
   }
   checkButtons();
});
 
// Speedbutton2 and Slowbutton2 are removed in this tutorial

Button check

 
/**
* Check if the buttons needs to be changed
**/
function checkButtons() {
   if($("#vinyl1").data('rotationAngle') == 0) {
      $("#slowbutton1")
         .data('isEnabled', false)
         .children().attr("src", "images/btn-slow-dis.png");
   } else {
      $("#slowbutton1")
         .data('isEnabled', true)
         .children().attr("src", "images/btn-slow.png");
   }
      
   if($("#vinyl1").data('rotationAngle') == 50) {
      $("#speedbutton1")
         .data('isEnabled', false)
         .children().attr("src", "images/btn-fast-dis.png");
   } else {
      $("#speedbutton1")
         .data('isEnabled', true)
         .children().attr("src", "images/btn-fast.png");
   }
   // Vinyl2 is removed in this tutorial
}

Spinning records

 
/**
* Start spinning those vinyls by the given element. Set an interval to keep the vinyl spinning
* and attach it to the element to keep a reference for clearing later.
**/
function startSpinning(element) {
   element.stop().animate({rotate: '+=40deg'}, 800, 'easeInCubic', function() {
      var intervalHandle = setInterval(
          function () {
              element.animate({rotate: '+=' + element.data('rotationAngle') + 'deg'}, 0);
          },
          25
      );
      element.data('intervalHandle', intervalHandle);
   });
}

Conclusion and Download

I know there are a couple of bugs in this script, but I think the outcome is pretty sweet. There is loads of room for improvement, so feel free to download the source and mess around with the code yourself!

Demo jQuery DJ Hero - CSS3 and jQuery fun   Download jQuery DJ Hero - CSS3 and jQuery fun

What do you think of this wicked Proof of Concept? Would you improve the script, or do you like it the way it is? Feel free to share.


Tags:  dj jquery css3 proof of concept experiment webdevelopment

Interested in this topic? You might enjoy another article I've written called

Did you like this article? Subscribe to my feed or email to keep updated on new articles.

Spread the word and submit to:
Digg!Reddit!Del.icio.us!Facebook!StumbleUpon!
Comments
Add NewSearchRSS
Marco - Fun fact   2009-11-17 08:52:17
Gravatar image Just a little addition to the article: It works on the iPhone too! You can start spinning those vinyls and spin them even faster. Sadly, scratching doesn't work :) .
not2comply - Nice work   2009-11-18 02:52:13
Gravatar image Nice work man,
Allthough I couldn't find in any way putting this work into a part of web application :lol: ,
still I'm amaze to see it worked.
Go CSS3....., and thanks for inspiring.
Marco - Thanks!   2009-11-18 08:50:45
Gravatar image Thanks a lot man! You're totally right - this is just a simple Proof of Concept just showing that you can craete something like that.

Just keep on looking for new oppertunities ;) !
1GR3 - realism   2010-01-16 22:51:34
Gravatar image it is impressive even though you could do it in flash much easier but dude, have you ever seen turntable live? it's the vinyl that spins, not light reflections or light sources... it would be more realistic if you animate just paper labels.
ps. i love those demotivational posters
Edis   2010-03-25 14:42:20
Gravatar image Really great, i am impressed,you guys really took your time to make something good here. Is there any chance to listen to a song (or a playlist)
after a user clicks the play button and the vinyls start to rotate.That would really be something, but still, great job.
GSPiash - GSP WEB   2010-04-21 13:35:09
Gravatar image This is a great job!!! But can you tell me how could i can able to auto start spinning onload the page.
fabian - OHMYGOSH   2010-10-09 22:14:42
Gravatar image This is awesome! can't wait for the version which will be able to play sound also... so that it will have a real function....

I know it's not the gentleman way to ask other people to do a great job for free, but if you finish this work, combined with a playlist like someone else over here asked for - I'd love you! :-P

Please contact me as soon as you got a functional music playing version!!!!!!

Anyway, still up2now it's a beautyful work! Big UP!
amy - cheap clothes online   2011-03-14 03:19:10
Gravatar image You have a quite different idea,It sounds really amazing !
yunenmei - gucci bags   2011-06-25 02:31:43
Gravatar image I would have been very concerned about your information,

thank you!
hirmani80 - WebHostingPad Reviews   2011-08-02 08:55:10
Gravatar image I like it very much for using the nice info in this blog and the great technology is display in this blog. I am very much satisfied by the info in this blog. This website is providing the great articles in this website. This info is quite interesting info in this website and different info in this blog. Thanks a lot for sharing the nice info in this blog. I had really like it very much
Anthony - Beeauuuutifull!   2011-08-28 18:00:17
Gravatar image Is there a way to put music on this application and have it respond to the users action, i.e. playing a song backwards and scratchin and speed ?
Anthony - Beautiful   2011-08-28 18:03:25
Gravatar image Is there a way to put music in this , ie. two seperate tracks and have it respond to the user's actions?
PhotoshopWarrior - Great   2011-10-06 08:23:26
Gravatar image What an idea man, it's completely unique and great
Read more...
Name:
Email:
  Gravatar enabled.
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Security Image
Please input the anti-spam code that you can read in the image.
Unsubscribe from e-mail notifications.
 
< Prev   Next >
Subscribe

Subscribe to Marcofolio