BitmapData?
BitmapData is the new (to Flash 8) class that allows us to modify Bitmaps at runtime using ActionScript. It's easy to use once you get the hang of it, and can be incredibly useful. The best example I can think of is to make a colour picker using a Bitmap image containing millions of colours, then allowing the user to click the image and get the colour value at the _xmouse and _ymouse locations. BitmapData is also incredibly cool [fact].
Starting off
The first thing you need to do to use BitmapData is to import the class, you do so as you do other classes (geom, filters, etc):
import flash.display.BitmapData
Now you can start using it, the next thing you must do is make a new BitmapData object. You can do so like this:
var myBitmapData:BitmapData = new BitmapData(width, height, transparent?, colour)
EG
var myBitmapData:BitmapData = new BitmapData(550, 400, true, 0xFF000000)
That will create a new BitmapData the size of the default stage coloured black. The first thing you may notice is that the colour value has 8 (count 'em) numbers rather than the usual 6, this is because I set the transparent boolean to true so I must define the amount of alpha in the pixels. In this case it is full alpha and no colour (plain black), the way this works is ARGB - alpha, red, green, blue. Just set transparent variable to false and stick with the six digits for now if you don't think you'll need to use alpha in your Bitmaps yet.
BitmapData is an API just like the MovieClip API, and one main difference that you may dislike is that BitmapData doesn't come with a lineTo command (actually this may piss you off). See the examples section for my own BitmapData lineTo function =)
Commands
There are many commands for manipulating BitmapData in Flash, but I'm only going to explain the basic ones because the more advanced commands use other classes such as Rectangle, Point, etc. which haven't been explained yet, so I'll leave them for later.
myBitmapData.draw(source, matrix, colour transform, blend mode, rectangle) - For now you can just ignore all of the parameters except the source one, which is where you put the instance name of whatever you want to draw onto the Bitmap (text, video, movie clips, etc).
myBitmapData.loadBitmap(id) - Loads an object from the library onto the BitmapData
myBitmapData.attachBitmap(bitmap, depth, pixel snap ("always"/"never"/"auto"), smoothing (true/false)) - This attaches the Bitmap to a timeline (i.e. _root), at the depth you put.
myBitmapData.setPixel(_x, _y, colour) - Use this for drawing onto your Bitmap, it will set the (individual) pixel at the co-ordinate you put to the chosen colour (RGB).
myBitmapData.setPixel32(_x, _y, colour - Same as setPixel, but you use this if you're passing ARGB rather than RGB.
myBitmapData.getPixel(_x, _y) - Returns the colour value (RGB) at the chosen co-ordinate.
myBitmapData.setPixel32(_x, _y, colour - Returns the colour value (ARGB) at the chosen co-ordinate.
myBitmapData.clone() - Returns an exact copy of the Bitmap, you can use this to make something such as a webcam recorder.
myBitmapData.noise(random seed, low, high, channels, grayscale (true/false)) - Creates noise (the same as TV static)
myBitmapData.scroll(_x, _y) - Moves the Bitmap in itself, you'll have to play with this yourself to understand.
myBitmapData.dispose() - Delete your BitmapData, useful because BitmapData takes up a lot of RAM
myBitmapData.perlinNoise(baseX, baseY, octaves, random seed, stitch, fractal, channels, greyscale, offsets) - Generates a pattern onto the BitmapData. Set your baseX to the width of the BitmapData, baseY to the height, octaves is the number of octaves, random seed to a random number, stitch to true, fractal to true, channels to any number, greyscale to false and offsets to null. Try playing with the values, you can have real fun with this :)
Examples
function bitmapLine(startx:Number, starty:Number, endx:Number, endy:Number):Void {
myX=Array(), myY=Array();
x3 = (endx-startx)/1000;
y3 = (endy-starty)/1000;
for (a=1; a<1001; a++) {
startx += (x3), starty += (y3);
myX.push(startx), myY.push(starty);
}
for (a=1; a<1001; a++) {
bmp.setPixel(myX[a-1], myY[a-1], 0);
}
}
------
------
Any questions?
Sup, bitches :)
