/ Published in: ActionScript 3
Full Background class with min. width and height
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package com.jasonhulbert.display{ import flash.events.Event; import flash.display.Sprite; import flash.display.Bitmap; import flash.display.Loader; import flash.net.URLRequest; import flash.errors.IOError; import flash.events.IOErrorEvent; import flash.display.StageScaleMode; import flash.display.StageAlign; public class FullBackground extends Sprite { var ldr:Loader; var ldrReq:URLRequest; var bmp:Bitmap; var minW:Number; var minH:Number; var stageW:Number; var stageH:Number; var scaler:Number; public function FullBackground(_imageURL:String, _minW:Number, _minH:Number) { minW = _minW; minH = _minH; ldr = new Loader ; ldrReq = new URLRequest(_imageURL); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded); ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError); addEventListener(Event.ADDED_TO_STAGE, onAdd); } private function onAdd(event:Event):void { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stage.addEventListener(Event.RESIZE, onStageResize); ldr.load(ldrReq); } private function onLoaded(event:Event):void { bmp = Bitmap(ldr.content); bmp.smoothing = true; addChild(bmp); resizeImage(); } private function onIOError(event:IOErrorEvent):void { trace("Error loading background image: " + event); } private function resizeImage():void { stageW = stage.stageWidth; stageH = stage.stageHeight; if (stageH / stageW > this.height / this.width) { scaler = this.width / this.height; this.width = stageH * scaler; this.height = stageH; } else { scaler = this.height / this.width; this.height = stageW * scaler; this.width = stageW; } this.x = (stageW / 2) - (this.width / 2); this.y = 0; } private function onStageResize(event:Event):void { resizeImage(); } } }