Tiling a Pattern with Bitmap Fills
/**
* Copyright Husam.Adil ( http://wonderfl.net/user/Husam.Adil )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/QRkP
*/
package {
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.sampler.NewObjectSample;
import flash.system.LoaderContext;
public class ch35ex13 extends Sprite {
public function ch35ex13() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var l:Loader = new Loader();
l.load(new URLRequest("http://actionscriptbible.com/files/plaid.gif"),
new LoaderContext(true)); //thanks to squidfingers.com for the tile!
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
}
protected function onLoadComplete(event:Event):void {
var b:Bitmap = LoaderInfo(event.target).content as Bitmap;
if (b) {
var tile:TileBitmap = new TileBitmap(b.bitmapData);
addChild(tile);
tile.followStageSize(stage);
tile.addEventListener(MouseEvent.CLICK, onClick);
}
}
protected function onClick(event:MouseEvent):void {
//go full screen on click to see more of this beautiful plaid
stage.displayState = (stage.displayState == StageDisplayState.NORMAL)?
StageDisplayState.FULL_SCREEN : StageDisplayState.NORMAL;
}
}
}
import flash.display.*;
import flash.events.Event;
class TileBitmap extends Sprite
{
protected var srcBitmap:BitmapData;
protected var smoothing:Boolean = false;
protected var _width:Number = 0;
protected var _height:Number = 0;
public function TileBitmap(srcBitmap:BitmapData, smoothing:Boolean = false) {
super();
this.smoothing = smoothing
this.srcBitmap = srcBitmap;
redraw();
}
public function followStageSize(s:Stage):void {
s.addEventListener(Event.RESIZE,
function(event:Event):void {matchStageSize(Stage(event.target))});
matchStageSize(s);
}
protected function matchStageSize(s:Stage):void {
width = s.stageWidth;
height = s.stageHeight;
}
override public function set width(w:Number):void {
if (Math.abs(w - width) < 1) return;
_width = w;
super.width = _width;
redraw();
}
override public function get width():Number {return _width;}
override public function set height(h:Number):void {
if (Math.abs(h - height) < 1) return;
_height = h;
super.height = _height;
redraw();
}
override public function get height() : Number {return _height;}
protected function redraw():void {
scaleX = scaleY = 1;
if (srcBitmap && width > 0 && height > 0) {
graphics.clear();
graphics.beginBitmapFill(srcBitmap, null, true, smoothing);
graphics.drawRect(0, 0, Math.ceil(width), Math.ceil(height));
graphics.endFill();
}
}
}