I’ve started a new ruby blog. Whereas this one tends to longer posts (and not just ruby), the other is intended for short, tight pieces which focus on a single idea. In general they will be in the format of “mini sagas“. I’m shooting for daily updates on it.
One of the issues with gif animations is being able to stop and start them at will. This post examines a method of using javascript to control CSS Sprites for Animation.
We’re going to show how one can create the following animation using javascript and sprites and what benefits you might receive:
The gist of this method is to use css background images, which are swapped out via javascript to show and control the animation. In a filesize test using an animation with 9 frames, the sprites actually require less memory than a comparable animated gif:
| File | Size | Description |
|---|---|---|
| Individual frames as separate PNG’s | 95421 | Baseline — this is the sum of the sizes of the 9 frame files. |
| anim.gif | 43463 | Animated gif version |
| knot.png | 81701 | 24 bit PNG, sprites. Notice that this is smaller than the sum of the 9 files, but quite larger than the animated gif. |
| knot-grey.png | 49708 | Greyscale version of the png sprites. This is much smaller and approaches the size of the gif animation. |
| knot.gif | 40853 | Gif version of the sprites. This is smaller than the anmated version. |
| knot-idx.png | 38281 | Indexed version of the png sprites. Smallest yet. |
I think that between the added control, decreased number of files to download (a single file containing sprites can be used in many places), and the potential for reduced file size, that this is a viable method for producing animations.
The javascript code is library agnostic. Had I tied it to one library or another, I could have made it shorter, but I think that the benefits of being agnostic outweigh the potential byte savings.
// Except as otherwise noted, the following code is copyright 2008, 2009 // by Matthew Williams // // Usage and description of the script may be found at: // http://matthewkwilliams.com/ // // In order to use it, you need the following: // + A sprite file // + CSS entries for the locations of the sprites. // These are named FOO-frame-n where foo is the name of the animation, // and n is the frame. // + A block entity in which the animation will be placed. // This entity needs the following css rules since we're using the css // background trick: // + width // + height // // + A sample use would be as follows: // <script src="sprite-animator.js" type="text/javascript"><!--mce:0--></script> // // <script type="text/javascript"><!--mce:1--></script> // <button onclick="gearanim.start(); return false;">Start</button> // <button onclick="gearanim.stop(); return false;">Stop</button> // This object does the updating of the sprite animation function SpriteUpdater(instance) { var obj = instance; this.updateSprite = function() { var elem = document.getElementById(obj.id + '-sprite-container'); var a = elem.className.split('-'); var num = parseInt(a[a.length - 1]); var newNum = num; if (num >= obj.numFrames) { newNum = obj.loop ? 1 : obj.numFrames; } else { newNum = newNum + 1; } if(newNum != num) elem.className = obj.id + '-frame-' + newNum; else clearInterval(obj.intervalID); }; } // This object handles the animation // + id = the id of the block // + file = the file which contains the sprites // + loop = true/false whether it loops or not // + delay = what is the delay between frames? function SpriteAnimator(id, file, loop, delay) { // this function and the next are from css-toolbox.js // created by Patrick Hunlock :: http://www.hunlock.com // and originally found at the JavaScript Source: // http://javascript.internet.com function getCSSRule(ruleName, deleteFlag) { if (document.styleSheets) { for (var i=0; i '; element.innerHTML = insertion + element.innerHTML; }; // add css rules for the sprite container this.updateCSS = function() { var rule = addCSSRule('#' + this.id + '-sprite-container'); rule.style.backgroundImage = 'url("' + file + '")'; rule.style.backgroundRepeat = 'none'; rule.style.padding = '0'; rule.style.margin = '0'; rule.style.width = '100%'; rule.style.height = '100%'; }; // stop the animation this.stop = function() { if (this.intervalID != false) { clearInterval(this.intervalID); } }; // start the animation this.start = function(){ updater = new SpriteUpdater(this); this.intervalID = setInterval(updater.updateSprite,this.delay); }; this.id = id; this.file = file; this.loop = loop; this.delay = delay; this.updateHTML(); this.updateCSS(); this.numFrames = this.countFrames(); this.intervalID = false; }
Sample CSS looks like this:
.knot-frame-1{background-position: 0px 0px;} .knot-frame-2{background-position: -138px 0px;} .knot-frame-3{background-position: -276px 0px;} .knot-frame-4{background-position: -414px 0px;} .knot-frame-5{background-position: -552px 0px;} .knot-frame-6{background-position: -690px 0px;} .knot-frame-7{background-position: -828px 0px;} .knot-frame-8{background-position: -966px 0px;} .knot-frame-9{background-position: -1104px 0px;}
Here’s the javascript source file:
sprite-animator
I’m going to have to send my “big” laptop off for servicing, so I upgraded chibi, or as my daughter calls it, “Baby yaptop”. I upgraded the ram to 2gb — it was only $27 with tax. I had held off on this because I didn’t want to “void the warranty”. I found out today that increasing the ram wouldn’t do so, which was good because I was going to do it anyway. I also replaced the 2gb sd card with an 8gb one. I’d recently put eeebuntu on it — I’ve really been enjoying it.
However, since I added the extra ram, it’s running a *lot* faster. I’m thinking that Linux is doing a lot less behind the scenes in terms of juggling memory & is able to cache a lot better than it had. Things that might take a second or more previously take almost no time at all.
I should have done this a long time ago.
The end result is that I’ve got a very usable system now, even if it’s “only” got a 900mhz processor.
Are you limited in what software you can use at work? This article details how to have a web based tool suite for mysql. It currently has the following tools:
- AjaxMyTop — a php implementation of mytop (think top for mysql) which runs in a browser.
- rbdb — a phpmyadmin work-alike in progress. It’s the result of the 2008 Rails Rumble contest.
So you’ve noticed that both a php and a ruby application are included. Pretty spiffy, eh?
The magick partly lies in the container — I’m using GlassFish v3 prelude. Another piece is Quercus, a Java implementation of PHP 5. JRuby is used for ruby.
Continue reading ‘Web Based Portable mysql tool suite’
- administrivia (6)
- books (1)
- Computers (2)
- css (3)
- eating crow (1)
- games (1)
- glassfish (1)
- gotchas (16)
- howto (2)
- idiocy (3)
- javascript (4)
- jboss (4)
- jruby (6)
- Just Enough Programming (7)
- life hacking (2)
- mini sagas (1)
- miscellany (1)
- monitoring (1)
- mysql (1)
- philosophy (4)
- php (1)
- programming (17)
- rails (6)
- rants (2)
- refactoring (1)
- ruby (14)
- tips (9)
- tools (2)
- Uncategorized (9)
- utilities (3)
- web (5)
- web design (3)




