Thursday, August 2, 2007

[AS3] Full browser background with smoothing

source

This will load a image and scale it to the browsers scale.

Demo:
http://gfxcomplex.com/labs/full_browser_bg_as3.html

Background image by darkmotion:
http://www.deviantart.com/deviation/46131446/

AS3 source:
http://gfxcomplex.com/labs/full_browser_bg_as3.zip

:: Added two Events and two read only public methods for preloading.
Plus new fade image in Timer.

Methods————————
BrowserBackground.bytesLoaded; returns a :Number and is read only
BrowserBackground.bytesTotal; returns a :Number and is read only

Events————————–
BrowserBackground.BACKGROUND_LOADING dispatchis a events for progress of a Loader object, as well updates the BrowserBackground.bytesLoaded and BrowserBackground.bytesTotal.

BrowserBackground.BACKGROUND_LOADED dispatchis an event upon a full and complete load of the Load object.

Sample use code.

package {
import com.gfxcomplex.display.BrowserBackground;
import flash.display.Sprite;
import flash.events.Event;
public class MainDoc extends Sprite {
private var _bg:BrowserBackground;
public function MainDoc() {
_bg = new BrowserBackground(”http://gfxcomplex.com/images/Damn_utensils_by_darkmotiondotcom.jpg”);
addChild(_bg);
_bg.addEventListener(BrowserBackground.BACKGROUND_LOADING, loading);
_bg.addEventListener(BrowserBackground.BACKGROUND_LOADED, loaded);
}
private function loaded(e:Event):void {
_bg.removeEventListener(BrowserBackground.BACKGROUND_LOADING, loading);
_bg.removeEventListener(BrowserBackground.BACKGROUND_LOADED, loaded);
}
private function loading(e:Event):void {
var bL:Number = _bg.bytesLoaded;
var bT:Number = _bg.bytesTotal;
}
}
}

Source Code::

*////////////////////////////////////////////////////
//////////////////////////////////////////////////////
////
//// Full Browser background image
////
//// by Josh Chernoff ( GFX Complex )
//// Copy right ~ josh chernoff ~ 2007
//// All rights reserved
////
////
//// May be redistributed under The
//// GNU General Public License (GPL) Agreement
////
//////////////////////////////////////////////////////
*//////////////////////////////////////////////////////*
Class constructor requires only one argument
url:String for loading the background
public class BrowserBackground(url:String);
*/package com.gfxcomplex.display{import flash.display.Sprite;
import flash.system.LoaderContext;import flash.display.StageAlign;

import flash.display.StageScaleMode;

import flash.display.Bitmap;

import flash.display.Loader;

import flash.events.Event;

import flash.events.TimerEvent;

import flash.events.ProgressEvent;

import flash.net.URLRequest;

import flash.utils.Timer;

public class BrowserBackground extends Sprite{

public static const BACKGROUND_LOADED:String = “backgroundLoaded”;

public static const BACKGROUND_LOADING:String = “backgroundLoading”;

private var _bytesLoaded:Number;

private var _bytesTotal:Number;

private var _imageHolder:Sprite;

private var _bitmap:Bitmap;

private var _loader:Loader;

private var _timer:Timer;

public function get bytesTotal():Number{

return _bytesTotal;

}

public function get bytesLoaded():Number{

return _bytesLoaded;

}

function BrowserBackground(url:String) {

var context:LoaderContext = new LoaderContext();

context.checkPolicyFile = true;

_loader = new Loader();

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);

_loader.load(new URLRequest(url), context);

}

private function completeHandler(event:Event):void {

var sW:Number = stage.stageWidth;

var sH:Number = stage.stageHeight;

_imageHolder = new Sprite();

_timer = new Timer(50, 44);

_bitmap = Bitmap(event.target.loader.content);

_bitmap.y -= _bitmap.height;

_bitmap.smoothing = true;

addChild(_imageHolder);

_imageHolder.addChild(_bitmap);

_imageHolder.width = sW;

_imageHolder.scaleY = _imageHolder.scaleX;

if (_imageHolder.height < sH) {

_imageHolder.height = sH;

_imageHolder.scaleX = _imageHolder.scaleY;

}

_imageHolder.y = sH ;

_imageHolder.alpha = 0;

_timer.addEventListener(TimerEvent.TIMER, timerListener);

_timer.start();

stage.scaleMode = StageScaleMode.NO_SCALE;

stage.stage.align = StageAlign.TOP_LEFT;

stage.addEventListener(Event.RESIZE, stageResized);

dispatchEvent(new Event(BrowserBackground.BACKGROUND_LOADED));

_loader = null;

}

private function progressHandler(event:ProgressEvent):void {

_bytesLoaded = event.bytesLoaded;

_bytesTotal = event.bytesTotal;

dispatchEvent(new Event(BrowserBackground.BACKGROUND_LOADING));

}

private function timerListener(e:TimerEvent){

_imageHolder.alpha += .025;

if(_imageHolder.alpha >= 1){

_imageHolder.alpha = 1;

_timer.stop();

_timer.removeEventListener(TimerEvent.TIMER, timerListener);

_timer = null;

}

e.updateAfterEvent();

}

private function stageResized(event:Event):void {

var sH:Number = stage.stageHeight;

_imageHolder.width = stage.stageWidth;

_imageHolder.scaleY = _imageHolder.scaleX;

if (_imageHolder.height < sH) {

_imageHolder.height = sH;

_imageHolder.scaleX = _imageHolder.scaleY;

}

_imageHolder.y = sH;

}

}
}

No comments: