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 'Uncategorized' Category
01Sep09 Fractal Terrain Generation
programming ruby Uncategorized
1 Comment

The original idea behind the generation was in Computer Rendering of Stochastic Models, in the June 1982 issue of  Communications of the ACM.  I’m basing my code on Paul Mertz’s Generating Random Fractal Terrain.

The gist of what I’m doing is to start with a square, like this (I’ve added a grid to make it easier to see the elements):

Our map

Our map

From here, we determine the corners:

Four corners

Four corners

Each location in the grid has a “height”. In my implementation, I assign it a value from (-1 .. 1). From these four points we find the center point, then average the values of the 4 corners and add to it some random value.

The center point

The center point

This random value, h is in a range of (-1 .. 1). So if the 4 corners all had a value of 0, and our random value was .5, then the center would be .5:

0.0 0.0
0.5
0.0 0.0

From here we determine the midpoints on the sides of the square:

Midpoints

Midpoints

Notice that where we once had one (1) square, we now have 4 smaller squares.  From here, we do each square in turn in the same fashion.  However, this time the range of h has been halved — the range can be tweaked.  If you use the formula 2^(-n), to make the terrain smoother, use a n closer to 1.  Likewise, the smaller n is, the more random the terrain.

The smaller squares are each processed, yielding smaller squares.

Finally, you have a grid of values, which range from -1 < n < 1.  From this terrain, you could then assign ranges, such as n <= -.5  for water, -.5 < n <= -.3 for swamps, -.1 < n <= .2 for plains, snd so forth.  From that you could then have something like this (tiles are  from Wesnoth):

Sample Terrain

Sample Terrain

If anyone’s interested, I can post sample code.

Enhanced by Zemanta
30Jul09 z-index and events
Uncategorized
1 Comment

Today’s lesson has to do with z-index and events. I’d had a png with transparant portions that I wanted to use as a mask, so that information from underneath would not show. However, buttons, links, and hovers were not working — the top level element was taking the events and eating them. A change of z-index and they started behaving as expected.

10Apr09 After long silence
Uncategorized
0 Comments

The hiatus has been due to me having been really sick with a weird neurological disorder.  I didn’t do any coding at all for over 2 months.  I’m starting to feel better, but I still have my good days and bad.  So, there will be updates.  Hopefully before not too much longer.

11Feb09 Javascript with CSS Sprites Animation
css javascript Uncategorized web web design
3 Comments

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

06Feb09 Chibi gets an upgrade
Uncategorized
0 Comments

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.

12Sep08 Class is Class, and Instance, Instance, and never the twain shall meet
eating crow gotchas programming ruby Uncategorized
2 Comments

More about methods, it’s inspired/spurred by a comment on methods, public_methods, and private_methods by Pit Captain. It also corrects some misconceptions I had (and may have (wrongly) given others).

I’ve added a new category, “eating crow” for this and any other postings where I step back and re-evaluate my posts. This is to keep me honest, and, if y’all would, please feel free to tell me when it’s time to “eat crow”.
Continue reading ‘Class is Class, and Instance, Instance, and never the twain shall meet’

29Aug08 CSS tooltips using CSS Sprites
css gotchas Uncategorized web design
2 Comments
Thought Bubbles!

Thought Bubbles!

For a project I’m currently working on I wanted to make some “bubble” tooltips.  In order to cut down on the size/number of images, I’m using css sprites.  A quick google reveals that nobody else has (at least on the first page) put together tooltips using sprites.  The technique will work with any sort of image, so you needn’t use the “bubbles”.  So, here’s how I did it, with the caveats/gotchas. Continue reading ‘CSS tooltips using CSS Sprites’

29Aug08 Blogs I’m excited about…
Uncategorized
0 Comments

Earlier I was going through rss feeds when I noticed that a blog had been updated. I thought to myself “Oh, cool! they’ve updated.” It struck me that there are certain individual (as in written by an individual) blogs which I follow and am excited when I see posts. Here’s a partial list — bear in mind a number of blogs (most, in fact) I read through aggregators. While there are some really nifty ones there, I’ve left them out. Also I’ve left out “magazine” blogs, however cool they may be. Well, with one exception, which I miss, and I believe others miss a lot.

In no particular order, I present….. (with links to feeds):

  • Dion Hichcliffe’s Web 2.0 Blog
  • Creating Passionate Users
  • Presentation Zen
  • Software by Rob
  • Muness
  • 24slash7
  • Creative Generalist
  • Hackity.org
  • Igvita
  • Veerle’s Blog

Does anyone have feeds like these that they love to see?

17Aug08 .irbc contents
Uncategorized
2 Comments

I think that irb is one of the most useful tools in the rails toolbox. It allows me to quickly determine how something works, sample results (for putting into blogs, don’tcha know) and makes me a more effective ruby developer.

One of the nice things about irb is it’s .irbrc file where you can place startup commands.  I’d like to share mine (and ask for others to share their favorite .irbrc tools/contents.

Continue reading ‘.irbc contents’

 
Browse Archives »
  • administrivia (6)
  • books (1)
  • Computers (2)
  • css (3)
  • cygwin (1)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (19)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (6)
  • jruby (8)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (7)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (10)
  • tools (2)
  • torquebox (2)
  • Uncategorized (9)
  • UNIX (1)
  • utilities (3)
  • web (5)
  • web design (3)
 

Latest

  • cygwin and torquebox and rvm, oh my!
  • JBoss Client Jars for Messaging
  • rsh hates nohup
  • Torquebox and Cygwin: Take I
  • 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

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)
  • cygwin (1)
  • eating crow (1)
  • games (1)
  • glassfish (1)
  • gotchas (19)
  • howto (2)
  • idiocy (3)
  • javascript (4)
  • jboss (6)
  • jruby (8)
  • Just Enough Programming (7)
  • life hacking (2)
  • mini sagas (1)
  • miscellany (1)
  • monitoring (1)
  • mysql (1)
  • philosophy (4)
  • php (1)
  • programming (17)
  • rails (7)
  • rants (2)
  • refactoring (1)
  • ruby (14)
  • tips (10)
  • tools (2)
  • torquebox (2)
  • Uncategorized (9)
  • UNIX (1)
  • 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

  • June 2012 (1)
  • November 2010 (1)
  • August 2010 (1)
  • June 2010 (1)
  • 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