Recipe 7.14.
Filling a Shape with a Bitmap
Problem
You want to apply a bitmap fill to a shape.
Solution
Use the Graphics.beginBitmapFill( )
method.
Discussion
The Graphics.beginBitmapFill( ) method
enables you to apply a bitmap as a fill to a shape. The method
accepts the following parameters:
bitmap
-
A BitmapData object to use as the bitmap
fill.
matrix
-
By default the bitmap is applied with no
transform applied. You can specify a flash.geom.Matrix
object to transform the bitmap by scaling, rotating, skewing, and
translating the image.
repeat
-
A Boolean value indicating whether or not the
bitmap ought to repeat to tile fill. By default the value is
true. When the value is set to false the bitmap
edge's pixels are repeated to fill the shape.
smooth
-
A Boolean value indicating whether or not to
apply smoothing when the bitmap is scaled greater than 100 percent.
The default is false.
The following sample class loads a bitmap from a
URL, copies it to a BitmapData object, and uses that
BitmapData object as a fill for circles drawn
programmatically:
package {
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.events.Event;
public class Drawing extends Sprite {
private var _loader:Loader;
public function Drawing( ) {
_loader = new Loader( );
_loader.load(new URLRequest("http://www.rightactionscript.com/samplefiles/image2.jpg"));
_loader.contentLoaderInfo.addEventListener(Event.COMPELTE, onImageLoad);
}
private function onImageLoad(event:Event):void {
var bitmap:BitmapData = new BitmapData(_loader.width, _loader.height);
bitmap.draw(_loader, new Matrix( ));
var matrix:Matrix = new Matrix( );
matrix.scale(.1, .1);
var sampleSprite:Sprite = new Sprite( );
sampleSprite.graphics.lineStyle( );
sampleSprite.graphics.beginBitmapFill(bitmap, matrix);
sampleSprite.graphics.drawCircle(100, 100, 100);
sampleSprite.graphics.endFill( );
addChild(sampleSprite);
}
}
}
|