The BitmapData ClassThe BitmapData class is designed to be able to load and manipulate bitmaps at runtime. It is important that when using this class to import it first at the beginning of your script, like this: import flash.display.BitmapData; To instantiate a new instance of the BitmapData class, use this code as a template: var myBitmapData:BitmapData = new BitmapData(width,height,transparent,fillColor);
PropertiesheightAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.height; Description: A numerical value representing the height of the bitmap in pixels. Example: This example will trace the height of a bitmap: import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myBitmapData.height); //output: 30 rectangleAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.rectangle; Description: A Rectangle object holding position and dimension data about a given bitmap. The top (y) and left (x) are zero. Example: This example will trace the information stored in the rectangle property: import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myB.rectangle); //output: (x=0, y=0, w=20, h=30) transparentAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.transparent; Description: A Boolean value representing the state of transparency in a bitmap. Example: This example will trace transparency in a given bitmap: import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30,true); trace(myBitmapData.transparent); //output: true widthAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.height; Description: A numerical value representing the width of the bitmap in pixels. Example: This example will trace the width of a bitmap: import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myBitmapData.width); //output: 20 MethodsapplyFilterAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.applyFilter(bitmap, rec, point, filter); Parameters:
Returns: Number If the filter has been applied successfully, the number 0 will be returned; otherwise a negative number will be returned. Description: This method will apply a defined filter to the designated bitmap. Example: This example will attach an image residing in the library and apply a Blur filter to it: import flash.display.BitmapData; import flash.geom.Point; import flash.filters.BlurFilter; var point:Point = new flash.geom.Point(0,0); var blur:BlurFilter = new flash.filters.BlurFilter(5, 5, 100); var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //apply the filter myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, point, blur); //attach the image this.attachBitmap(myBitmapData, 1); cloneAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.clone(); Returns: BitmapData This method will return an exact duplicate of the BitmapData object it is being called on. Description: This method will create an exact copy of the bitmap image it is being called on. Example: This example will create a generic bitmap image, create a copy, and attach that copy: import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create the copy var copyBitmap:BitmapData = myBitmapData.clone(); //attach the bitmap this.attachBitmap(copyBitmap,1); colorTransformAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.colorTransform(rec, transformObj); Parameters:
Description: This method will apply a defined ColorTransform object to the designated bitmap. Example: This example will create a red rectangle that we will convert to a green rectangle with a ColorTransform object: import flash.display.BitmapData; import flash.geom.ColorTransform; //create a red rectangle var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create the color transform var myTransform:ColorTransform = new ColorTransform(100,100,100,1,255,150,100,1); //apply the transform myBitmapData.colorTransform(myBitmapData.rectangle,myTransform); //attach the bitmap this.attachBitmap(myBitmapData,1); copyChannelAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.copyChannel(bitmap, rec, point, sourceChannel, destChannel); Parameters:
Description: This method will copy a channel from one BitmapData instance to another. Here are the available channels:
Example: This example will create a red rectangle and then create a blue rectangle. After that, the blue channel will be copied from the second rectangle into the first, making it a purple rectangle. Then the rectangle will be displayed using the attachBitmap method: import flash.display.BitmapData; import flash.geom.Point; //create the point var point:Point = new Point(0,0); //create a red rectangle var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create a second BitmapData object var myBitmapData2:BitmapData = new BitmapData(100,100,false,0x0000ff); //copy the blue channel over myBitmapData.copyChannel(myBitmapData2,myBitmapData.rectangle, point, 4, 4); //attach the bitmap this.attachBitmap(myBitmapData,1); copyPixelsAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.copyPixels(bitmap, rec, point, aBitmap, aPoint, merge); Parameters:
Description: This method will copy over a selection of pixels from one BitmapData object to another: Example: This example will load two bitmaps that currently reside in the library (both 400x300 in size) and copy the one on the top half into the other to display: import flash.display.BitmapData; import flash.geom.*; //create the rectangle to only grab the top half var rec:Rectangle = new Rectangle(0,0,400,150); //top left var point:Point = new Point(0,0); //create the first bitmap object var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //create a second BitmapData object var myBitmapData2:BitmapData = BitmapData.loadBitmap("bigBen"); //copy the top half of the second rectangle over myBitmapData.copyPixels(myBitmapData2, rec, point); //attach the bitmap this.attachBitmap(myBitmapData,1); disposeAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.dispose(); Description: This method will free up all memory in the instance of the BitmapData object. Example: This example will create a generic bitmap image, attach it to the root timeline so it is visible, and then set up an onMouseDown event so that when a user clicks the stage, the rectangle will be gone, and the memory will have been freed: import flash.display.BitmapData; //create the bitmap object var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //attach the bitmap this.attachBitmap(myBitmapData,1); //when the user clicks the mouse, clear the memory this.onMouseDown = function():Void{ myBitmapData.dispose(); } drawAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.draw(bitmap, matrix, transformObj, blendMode, clipRec, smooth); Parameters:
Description: Using vector rendering, this method will draw an image based on bitmap and other parameters you pass it. Example: This example will create a red square as the first bitmap. Then it will load in a bitmap from the library. The first bitmap will automatically display when tested, and when a user clicks the mouse, the second bitmap will be drawn in its place. import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //load the second bitmap from the Library var myBitmapData2:BitmapData = BitmapData.loadBitmap("bigBen"); //display the first bitmap this.attachBitmap(myBitmapData,1); //when a user clicks the stage, draw the second one this.onMouseDown = function(){ myBitmapData.draw(myBitmapData2); } fillRectAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.fillRect(rec, color); Parameters:
Description: This method will fill a defined Rectangle with the ARGB (0xAARRGGBB) color passed to it. Example: This example will create a generic bitmap image with no defined color. It will then fill the entire rectangle with a semitransparent red color. Then the bitmap will be attached to the main timeline: import flash.display.BitmapData; //create the bitmap object var myBitmapData:BitmapData = new BitmapData(100,100); //fill the rectangle myBitmapData.fillRect(myBitmapData.rectangle, 0x88ff0000); //attach the bitmap this.attachBitmap(myBitmapData,1); floodFillAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.floodFill(x, y, color); Parameters:
Description: This method will carry out a flood-fill operation beginning at the designated coordinates. Example: This example will first import the necessary class file. Then we will create an initial color to work with later in the example. After that, we create a new bitmap, set its color to a light gray, and then attach it to the root timeline. Finally, we create an event that is triggered each time the user clicks the stage and that will incrementally increase the startColor variable; then, using the floodFill method, it changes the color of the rectangle to gradually appear more red: import flash.display.BitmapData; //create the initial color var startColor:Number = 0x00110000; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x00bbbbbb); //attach the bitmap this.attachBitmap(myBitmapData, 1); //incrament the color and fill the rectangle this.onMouseDown = function():Void{ startColor += 0x00100000; myBitmapData.floodFill(0, 0, startColor); } generateFilterRectAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.generateFilterRect(rec, filter); Parameters:
Returns: Rectangle The Rectangle object returned will represent the necessary dimensions and placement to fully display the bitmap when the given filter is applied. Description: This method will generate the Rectangle necessary to accommodate the bitmap if it is applied to the given filter. Example: This example will generate the necessary Rectangle to use if a BlurFilter is applied to a 400x300 image: import flash.display.BitmapData; import flash.filters.BlurFilter; //create the filter var blur:BlurFilter = new flash.filters.BlurFilter(5, 5, 100); //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //generate the rectangle trace(myBitmapData.generateFilterRect(myBitmapData.rectangle, blur)); //output: (x=-18, y=-18, w=436, h=336) getColorBoundsRectAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.getColorBoundsRect(maskColor, color, findColor, filter); Parameters:
Returns: Rectangle The Rectangle object returned will represent the minimum size rectangle to encompass all pixels of the bitmap that contain the given color. Description: This method will search the bitmap image for the given hexadecimal color and return a bounding rectangle capable of encompassing all pixels with that color. Example: This example will generate the necessary Rectangle to encompass a color at a given pixel (10,10): import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //generate the rectangle trace(myBitmapData.getColorBoundsRect (0xffffffff, myBitmapData.getPixel32(10,10), true)); //output: (x=8, y=10, w=32, h=34) getPixelAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.getPixel(x, y); Returns: Number The RGB value of a given pixel. Parameters:
Description: This method will return the RGB value of a pixel at coordinate (x,y). Example: This example will load a bitmap from the library, then return the color of the pixel at coordinate (100,100). After that, we will use the fillRect method to completely color the bitmap with the selected color. And finally, the block of color will be displayed. import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //grab the color of the pixel at (100,100) var tempColor:Number = myBitmapData.getPixel(100,100); //fill the rectangle with that color myBitmapData.fillRect(myBitmapData.rectangle, tempColor); //display the color block this.attachBitmap(myBitmapData, 1); getPixel32Availability: FP:8, AS:1.0 Generic Template: myBitmapData.getPixel32(x, y); Returns: Number The ARGB value of a given pixel. Parameters:
Description: This method will return the ARGB value of a pixel at coordinate (x,y). Example: This example will load a bitmap from the library, then return the color of the pixel at coordinate (100,100). After that, we use the fillRect method to completely color the bitmap with the selected color. And finally, the block of color will be displayed. import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //grab the color of the pixel at (100,100) var tempColor:Number = myBitmapData.getPixel32(100,100); //fill the rectangle with that color myBitmapData.fillRect(myBitmapData.rectangle, tempColor); //display the color block this.attachBitmap(myBitmapData, 1); hitTestAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.hitTest(point, alpha, obj, bitmapPoint, alphaThreshhold); Parameters:
Returns: Boolean true if the hit is within designated area, otherwise false. Description: This method will return a Boolean value answering whether a hit has occurred in a designated area of a bitmap. Example: This example will first get the necessary classes we need. Then a bitmap will be created with a red square being drawn. Next, the bitmap will be attached to the root timeline. And finally, we use the onMouseMove event to monitor whether the user moves the mouse over the rectangle: import flash.display.BitmapData; import flash.geom.Point; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x00FF0000); //attach the bitmap this.attachBitmap(myBitmapData, 1); //monitor the mouse position this.onMouseMove = function():Void{ if(myBitmapData.hitTest(new Point(), 255, new Point(_xmouse, _ymouse))) { trace("x - " + _xmouse + " y - " + _ymouse); } } loadBitmapAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.loadBitmap(id); Parameters:
Returns: BitmapData The BitmapData information for the loading bitmap. Description: This method will load a bitmap from the library into a BitmapData object at runtime. Example: This example will load a bitmap from the library and display it in the root timeline. import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library this.attachBitmap(myBitmapData, 1); mergeAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.merge(bitmap, rec, point, red, green, blue, alpha); Parameters:
Description: This method will join two BitmapData objects and combine their channels based on the amount of multiplication given. Example: This example will create a red and a blue bitmap, then combine them and increase the blue by 255. Then the bitmap will be displayed as purple: import flash.display.BitmapData; import flash.geom.Point; //create the point var point:Point = new Point(0,0); //create both bitmaps var myBitmapData:BitmapData = new BitmapData(100,100,false, 0xff0000); var myBitmapData2:BitmapData = new BitmapData(100,100,false, 0x0000ff); //merge the 2 and increase the blue myBitmapData.merge(myBitmapData2, myBitmapData.rectangle, point, 0, 0, 255, 0); //display the bitmap this.attachBitmap(myBitmapData, 1); noiseAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.noise(seed, low, high, channels, grayScale); Parameters:
Description: This method will fill a bitmap with pixels representing random noise (static). Example: This example will create a small rectangle of constantly changing static: import flash.display.BitmapData; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100,100); //constantly update the noise this.onEnterFrame = function(){ myBitmapData.noise(Math.random()*100, 1, 255, 1, true); } //display the static this.attachBitmap(myBitmapData,1); paletteMapAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.paletteMap(bitmap, rec, point, redArray, greenArray, blueArray, alphaArray); Parameters:
Description: This method will re-map each of the four channels in a given bitmap using four arrays each containing 256 values. Example: This example will first import the necessary class files. Then we will create a variable to hold an initial height that we will use later. Next, we create the bitmap as a 100x100 red square. Then the bitmap is attached to the root timeline. Finally, we use the onMouseDown event to create blue, horizontal stripes down the red square:
perlinNoiseAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.perlinNoise(baseX, baseY, octaves, seed, stitch, fractal, channels, grayscale, offSet); Parameters:
Description: This method will create a more advanced form of noise than the noise method does. Images made with perlinNoise are often used for things such as fire or water. Example: This example will create a small lightning effect in a BitmapData object: import flash.display.BitmapData; //create the bitmap var myBitmapData:BitmapData = new BitmapData(200, 200); //make lightning this.onEnterFrame = function(){ myBitmapData.perlinNoise(50, 50, 5, Math.random()*100, false, false, 8, false); } //display the lightning this.attachBitmap(myBitmapData,1); pixelDissolveAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.pixelDissolve(bitmap, rec, point, seed, numPixels, fillColor); Returns: Number This number represents the next random seed that should be used. Parameters:
Description: This method will render a pixel dissolve effect on the given bitmap using a second bitmap. Example: This example will load two bitmaps from the library, display the first, then continually dissolve in the second one.
scrollAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.scroll(x, y); Parameters:
Description: This method will scroll an image by the pixel amount given. Example: This example will load a bitmap from the library, display that bitmap, and finally scroll it by (1,1) continually creating a color stream effect. import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //display the first bitmap this.attachBitmap(myBitmapData,1); //continually scroll the image this.onEnterFrame = function():Void{ myBitmapData.scroll(1,1); } setPixelAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.setPixel(x, y, color); Parameters:
Description: This method will set the RGB value of a given pixel. Example: This example will load a bitmap from the library, display that bitmap, and create an event that allows the user to color every pixel the mouse touches to a random color. import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //display the first bitmap this.attachBitmap(myBitmapData,1); //continually scroll the image this.onMouseMove = function():Void{ myBitmapData.setPixel(_xmouse, _ymouse, Math.random()*0x1000000); } setPixel32Availability: FP:8, AS:1.0 Generic Template: myBitmapData.setPixel32(x, y, color); Parameters:
Description: This method will set the ARGB value of a given pixel. Example: This example will load a bitmap from the library and make a copy in a second BitmapData object. Then two variables will be created to hold the height and width of the bitmap. After that, the bitmap will be displayed. Then two more variables will be created to help walk through the image. And finally, an onEnterFrame event will be used to completely flip the bitmap both vertically and horizontally, pixel by pixel. import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); var myBitmapData2:BitmapData = myBitmapData.clone() var w:Number = myBitmapData.width; var h:Number = myBitmapData.height; //display the first bitmap this.attachBitmap(myBitmapData,1); var column:Number = 0; var row:Number = 0; //continually scroll the image this.onEnterFrame = function():Void{ if(column>w){ column=0; row++; } myBitmapData.setPixel32(column, row, myBitmapData2.getPixel32(w-column,h-row)); column++; } thresholdAvailability: FP:8, AS:1.0 Generic Template: myBitmapData.threshold (bitmap, rec, point, operation, threshold, color, mask, copy); Returns: Number This number is the number of pixels that were changed. Parameters:
Description: This method will test different pixel values in a given bitmap and decide whether to swap them out with a given ARGB color. Example: This example loads a bitmap image from the library and then displays it. After that, an event will be set up so that when a user selects a pixel by clicking it, every pixel in the bitmap will be compared to that pixel and colored blue if the threshold test succeeds.
|