October 2, 2008

Fullscreen background images in Flash

Filed under: Flash, Freebies — Tags: , , , , — admin @ 12:38 am

Here’s a freebie – Flash source code to use fullscreen scaling images that maintain their aspect ration when scaling (AS2). I am using smoothImageLoad to convert the loaded png, jpg or gif to a smooth bitmap. Enjoy!

Download Source View Example

// Use Tween class to fade in the image
import mx.transitions.Tween;
import mx.transitions.easing.*;
 
var stageListener:Object=new Object();
stageListener.onResize=function(){
	position();
};
 
Stage.align="TL";
Stage.scaleMode="noScale";
Stage.addListener(stageListener);
 
// Reposition objects when the movie is resized
function position() {
	// Find new value of stage size
	var W:Number = Stage.width;
	var H:Number = Stage.height;
	// Find center of stage
	var centerX:Number = W/2;
	var centerY:Number = H/2;
	// Center main bg clip
	background_mc._x = centerX;
	background_mc._y = centerY;
	// Center target clip inside the main bg clip
	background_mc.target_mc._x = -_global.BGW/2;
	background_mc.target_mc._y = -_global.BGH/2;
	var imageRatio:Number;
	var imageXRatio = W/_global.BGW;
	var imageYRatio = H/_global.BGH;
	// Determine if the width is larger than the height
	if (imageXRatio>imageYRatio) {
		imageRatio = imageXRatio;
	} else {
		imageRatio = imageYRatio;
	}
	// Scale the image to fit fullscreen
	background_mc._xscale = background_mc._yscale=imageRatio*100;
}
 
// Function for smoothing resizable bitmaps
function smoothImageLoad(imgURL, targetMovie) {
	var i = 0;
	do {
		i++;
	} while (eval("_root.smoothImageLoadTemp"+i) != undefined);
	tmc = targetMovie.createEmptyMovieClip("smoothImageLoadTemp"+i, targetMovie.getNextHighestDepth());
	tmc.createEmptyMovieClip("ti",tmc.getNextHighestDepth());
	tmc.tm = targetMovie;
	with (tmc) {
		tmcl = new MovieClipLoader();
		tmcl.onLoadComplete = function() {
			var fadeBG:Tween = new Tween(targetMovie, "_alpha", None.easeOut, 0, 100, 1, true);
			ti.onEnterFrame = function() {
				pixelData = new flash.display.BitmapData(ti._width, ti._height);
				_global.BGW = ti._width;
				_global.BGH = ti._height;
				pixelData.draw(ti);
				tm.attachBitmap(pixelData,1,true,true);
				tm.smoothImageLoadComplete();
				_parent._parent._parent.position();
				removeMovieClip(ti._parent);
			};
		};
		tmcl.loadClip(imgURL,tmc.ti);
	}
}
// Load your image
smoothImageLoad("image.jpg",background_mc.target_mc);

© 2010 Aaron Benson. All rights reserved.

Powered by WordPress