Yet another Bitmap class: BitmapTiler

I love having a bit of free time!

Problem: You want to create a tileable bitmap by drawing stuff to it. “Tileable” meaning that the if you put a bunch of copies of them together in a grid, the tops will match up with the bottoms, and lefts with rights, seamlessly. “By drawing stuff to it” means using BitmapData.draw.

Solution: Use BitmapTiler.

BitmapTiler is a wrapper for BitmapData, with one method, draw, which matches the signature for BitmapData.draw. OK, almost matches it. BitmapData.draw takes an IBitmapDrawable as a source parameter, which can be a DisplayObject or another BitmapData. BitmapTiler.draw just takes a DisplayObject.

When you draw an object to a BitmapData using BitmapTiler, it checks to see if the object extends beyond the left edge of the bitmap. If so, it draws it again, one screen off to the right. Same with right to left. Same with top to bottom, bottom to top. If it extends off a corner, it redraws it correctly in each corner. The result is that the bitmap’s edges will all match up with the opposite edges.

This is useful for Graphics.beginBitmapFill(). You generate a tileable bitmap and use that as a bitmap fill and draw a shape as large as you want. That’s what the following demo does. First it shows the bitmap that is created, then draws a larger square using beginBitmapFill with that bitmap.

[kml_flashembed movie=”http://www.bit-101.com/blog/wp-content/uploads/2008/03/bitmaptiler.swf” width=”500″ height=”610″/]

Not the most awesome demonstration, but shows it works.

Get the class here:

http://www.bit-101.com/blog/wp-content/uploads/2008/03/BitmapTiler.as

Some of that code is pretty brute force stuff. I’m not convinced there isn’t a more elegant way to do it, but again, it seems to work, so good enough for now. 🙂

This entry was posted in Flash. Bookmark the permalink.

4 Responses to Yet another Bitmap class: BitmapTiler

  1. kp says:

    not sure if i need to integrate this with the BigAssCanvas class. do you really NEED a 10,000 x 10,000 TILEABLE bitmap???

  2. Erik says:

    I think I am missing something. What is the difference with beginBitmapFill?

    beginBitmapFill tiles things automatically.

    Greetz Erik

  3. kp says:

    Yes, it tiles things by placing multiple copies of the bitmap side by side. It’s up to you to create a bitmap that will look good when tiled. Most of the time you will see a distinct seam where the two tiles meet. This class helps you make bitmaps that will have no visible seams when they are tiled.

  4. Marc says:

    If you click it fast enough, it’s like the intro to Saved by the Bell. Very cool.

Leave a Reply