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=“https://www.bit-101.com/2003/wp-content/uploads/2008/03/bitmaptiler.swf” width=“500″ height=“610″/]
Not the most awesome demonstration, but shows it works.
Get the class here:
https://www.bit-101.com/2003/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. 🙂