Skip to content

Ramblings

Musings of Matt Williams
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'web' Category
11Feb09 Javascript with CSS Sprites Animation
Uncategorized css javascript web web design
1 Comment

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:

Animated Gif

Animated Gif

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 &gt;= 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

05Feb09 Web Based Portable mysql tool suite
glassfish jruby mysql php tools utilities web
0 Comments

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’

02Dec08 The “A” stands for Asynchronous
javascript web
0 Comments

So, I have a piece of code which goes out and updates a list.  I’m using Ajax.Updater from Prototype.js, to get data back from the server. In my javascript, however, I’m immediately after kicking off the Ajax.Updater, doing processing based on the results from the update. And they’re out of sync, of course.

It just so happens that Ajax.Updater is not recognizing onSuccess. So I have to rewrite — not a big deal, more amused with myself for forgetting the ‘A’.

So, I need to use Ajax.Request.

28Aug08 CSS “width” gotcha
css gotchas web web design
0 Comments

I found out something interesting today.  Per the W3C, the total “width” that a block element uses is:

If ‘left’ or ‘right’ are given as ‘auto’, their computed value is 0. The following constraints must hold between the other properties:

‘margin-left’ + ‘border-left-width’ + ‘padding-left’ + ‘width’ + ‘padding-right’ + ‘border-right-width’ + ‘margin-right’ = width of containing block

This made life interesting while working on a css sprite (more on that next).  I kept wondering why it bled into the next sprite….

I had thought it was the total width of the element.  Foolish me.  Now I know and……

13Aug08 Drag-n-drop
javascript web
0 Comments

Despite reports to the contrary, scriptaculous sortables do not work inside a table, nor a tbody. (as of rails 2.1)

 
Browse Archives »
  • 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)
 

Latest

  • Rails & JRuby in a Jar
  • Fractal Terrain Generation
  • Quick thought on programming and distractions
  • Using jnp as a JBoss heartbeat
  • z-index and events
  • JBoss port confusion
  • SSL Joys
  • After long silence
  • New Ruby Blog
  • Javascript with CSS Sprites Animation

Flickr

layout_newm3headerTerrain Testa

Blogroll

  • Development Blog
  • Documentation
  • Plugins
  • Suggest Ideas
  • Support Forum
  • Themes
  • WordPress Planet

Search

Browse by Category

  • 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)

Browse by Tag

  • 1.2.6
  • 2.1
  • administrivia
  • autotest
  • books
  • controller
  • css
  • feed-normalizer
  • feeds
  • gotchas
  • idiocy
  • irb
  • Java
  • javascript
  • jboss
  • jruby
  • just enough programming
  • mini sagas
  • open-uri
  • philosophy
  • php
  • pragmatism
  • programming
  • quotations
  • rails
  • rants
  • reading
  • restful_authentication
  • rspec
  • rss
  • ruby
  • rubygems
  • scriptaculous
  • setup
  • simplicity
  • sprites
  • statemachine
  • tips
  • treetop
  • utilities
  • web
  • web design
  • websense
  • yaml
  • zentest

Browse by Month

  • March 2010 (1)
  • September 2009 (1)
  • August 2009 (2)
  • July 2009 (2)
  • May 2009 (1)
  • April 2009 (1)
  • February 2009 (4)
  • January 2009 (2)
  • December 2008 (2)
  • November 2008 (5)
  • October 2008 (3)
  • September 2008 (12)
  • August 2008 (28)
 
 
  • Blog
  • About
  • Chibi
  • Ruby Blender
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed