Return to Snippet

Revision: 27222
at May 31, 2010 00:36 by jHulbert


Updated Code
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();
		}

	}

}

Revision: 27221
at May 30, 2010 18:55 by jHulbert


Initial Code
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 container:Sprite;
		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 {
			container = new Sprite();
			addChild(container);

			bmp = Bitmap(ldr.content);
			bmp.smoothing = true;
			container.addChild(bmp);

			resizeContainer();
		}

		private function onIOError(event:IOErrorEvent):void {
			trace("Error loading background image: " + event);
		}

		private function resizeContainer():void {
			if (stage.stageWidth < minW || stage.stageHeight < minH) {
				return;
			}

			if (stage.stageHeight / stage.stageWidth > container.height / container.width) {
				scaler = container.width / container.height;
				container.height = stage.stageHeight;
				container.width = stage.stageHeight * scaler;
			} else {
				scaler = container.height / container.width;
				container.width = stage.stageWidth;
				container.height = stage.stageWidth * scaler;
			}
			container.x = 0;
			container.y = 0;
		}

		private function onStageResize(event:Event):void {
			resizeContainer();
		}

	}

}

Initial URL


Initial Description
Full Background class with min. width and height

Initial Title
as3 Full Background Image Class

Initial Tags


Initial Language
ActionScript 3