having fun with code

Spinning Color Wheel: Mootools + Raphael + SVG

A couple of days ago I was playing at work with the RaphaelJS library to do some image rotation that we may end up using on a new project we are working on. At the end I ended up creating a spinning object with variable speed, similar to this demo I show you here.

Using RaphaelJS it is very easy to embed any image into an SVG object and rotate it. With a little bit of animation, we can set angular speeds and have some fun :)

Spinning color wheel

I have added an acceleration parameter which will make the speed changes very smooth, although it the new speed is very different than the current one, it will take some time to reach it.

See it in action: http://enekoalonso.com/research/color-wheel.php

The code

  1. var ColorWheel = new Class({
  2.   Extends: Thread,
  3.  
  4.   initialize: function(options) {
  5.     this.parent(options);
  6.     this.raphael = Raphael('wheel', 400, 400);
  7.     this.wheel = this.raphael.image('media/svg/colorwheel.svg.png', 0,0,400, 400);
  8.     this.lblSpeed = $('speed');
  9.     this.lblRPM = $('rpm');
  10.     this.lblFPS = $('fps');
  11.    
  12.     $('btnSetSpeed').addEvent('click', function(event) {
  13.       this.setSpeed($('targetSpeed').get('value'));
  14.     }.bind(this));
  15.   },
  16.  
  17.   execute: function() {
  18.     var now = new Date().getTime();
  19.     this.fps = (1000 / (now – this.lastTime)).toFixed(2);
  20.     this.lastTime = now;
  21.  
  22.     if (this.targetSpeed == 0) { // stop
  23.       this.angularSpeed *= 0.99;
  24.       if (this.angularSpeed < 0.1) this.stop();
  25.     }
  26.  
  27.     else if (this.angularSpeed < this.targetSpeed) { // accelerate
  28.       this.angularSpeed = Math.min(this.angularSpeed + this.acceleration, this.targetSpeed);
  29.     }
  30.  
  31.     else if (this.angularSpeed > this.targetSpeed) { // brake
  32.       this.angularSpeed = Math.max(this.angularSpeedthis.acceleration, this.targetSpeed);
  33.     }
  34.  
  35.     var currentRPM = 60*(this.angularSpeed/360)*(1000/|>this.options.interval);
  36.     this.lblSpeed.set('text', 'Angular speed: ' + this.angularSpeed.toFixed(2) + ' deg per frame');
  37.     this.lblRPM.set('text', 'Current speed: ' + currentRPM.toFixed(2) + ' rpm');
  38.     this.lblFPS.set('text', 'Frames: ' + this.fps + ' fps');
  39.  
  40.     this.wheel.rotate(this.angularSpeed);
  41.   },
  42.  
  43.   start: function() {
  44.     this.angularSpeed = 0;
  45.     this.setSpeed($('targetSpeed').get('value'));
  46.     this.acceleration = 0.1;
  47.     this.lastTime = new Date().getTime();
  48.     this.parent();
  49.   },
  50.  
  51.   setSpeed: function(rpm) {
  52.     if (this.stopped) this.start();
  53.     this.targetSpeed = (rpm/60)*(360)*(this.options.interval/|>1000);
  54.     console.log('setSpeed (rpm,anglespeed): ', rpm, this.targetSpeed);
  55.   }
  56. });
  57.  
  58. window.addEvent('domready', function() {
  59.   new ColorWheel({interval:1000/30}).start(); // 30 fps
  60. });

Enjoy!

PS: I took the color wheel from Wikipedia, although I couldn’t figure out how to embed an SVG graphic into a Raphael SVG container.

Related Posts:

7 Comments to Spinning Color Wheel: Mootools + Raphael + SVG

  1. February 24, 2009 at 22:27 | Permalink

    Well done. Now, with animate method it should be easier to do the same thing?

  2. August 19, 2009 at 12:41 | Permalink

    Love this example. I’ve been a web-developer professionally for a year now and i constantly look for inspiration like this. You have inspired me to try and create a full svg home-page.

    May take me a while as I’m busy as hell right now. But i will convert my homepage to svg i swear! check it in about a month! rickyh.co.uk

  3. November 12, 2010 at 18:28 | Permalink

    Not bad for version 0.5.12 of raphael
    I linked to it at
    http://www.irunmywebsite.com/raphael/raphaelsource.php

  4. Roberto Petitpas's Gravatar Roberto Petitpas
    March 3, 2011 at 12:27 | Permalink

    Eneko,
    I am from Santiago, Chile, and I am searching for a way to spin at the rpm I choose, any picture, say in jpg format for example. Just as you spinned the color wheel. Just to stare at it, no other output is desired.

    My motivation is: I recently read about some new meanings that we could grasp about a crop circle just by looking at its spinning image. There are a few videos in Youtube showing this. For an example look at http://www.youtube.com/watch?v=KwGaZDeAWqU. I do collect hundreds of crop circle images since 1980, now that I know, I got the itch to look at them spinning at different rpms.

    Since I am just a user, I wonder if you can help putting things together as per your example, but with a way to change the picture by choosing any other. I would take care of editing the pictures so they are a square and the logical center of the image is at the geometrical center of the square.

    Of course I will need some guidance of how to have it running in my PC.

    Well, thanks a lot and I hope to get your answer.
    Best regards,
    Roberto

  5. April 23, 2011 at 08:52 | Permalink

    This is great! This is a very good work. This article is just a justification of having fun with code.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation