Image Class with resizing and smoothing

7 2 28th 2009 § 0 comments

A class for adding images to the stage, resizing them and turning smoothing on.

// load the Image class
import 	com.drpunchlogic.IMGload;

// -- Image
// set up the var for the image
// - ( the container for the image , the location of the xml )
var I:IMGload = new IMGload( holder_mc, "sample.jpg" );

package com.drpunchlogic
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.ProgressEvent;
    import flash.events.Event;

    // tween class
    import gs.TweenMax;
    import gs.easing.*;

    public class IMGload
    {

        private var _Width:Number;
		private var _Height:Number;
		//
		private var _container:Object;
		private var _img:String;
		//
		private var _percent:Number;
		private var _loader:Loader;
		// grab preload from library
		public var preloader:PreLoader;
		// set center of container
		private var _conXcent:Number;
		private var _conYcent:Number;

        public function IMGload( container:Object, img:String ) : void
        {

		   // set the container for the object
		   _container = container;

		   // set image path
		   _img = img;

		   // set center
		   _conXcent = Math.floor( _container.width / 2 );
		   _conYcent = Math.floor( _container.height / 2 );

		 	// startloading Image
			LoadStart();
		}

		private function LoadStart() : void
		{
			// set a new var each time
			preloader = new PreLoader();
			LoadAttach( preloader );

			_loader = new Loader();
			_loader.load( new URLRequest( _img ) );

			_loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, LoadProgress );
            _loader.contentLoaderInfo.addEventListener( Event.COMPLETE, LoadFin ); 

        }

		// generic attach to center of the container
		private function LoadAttach( obj:Object ) : void
		{
			// place preloader in the clip
		   	obj.name = "imgPreload_mc";
		   	_container.addChild( obj );

		   	// position the preloader within the clip
		   	obj.x = _conXcent;
		   	obj.y = _conYcent;
			trace( _container.width + ":" + _conXcent + "\t\t" + _container.height + ":" + _conYcent);

           	// prep the preloader to ani in
		   	obj.alpha = 0;
		   	obj.scaleX = obj.scaleY = 0.3;

			// object animate in
			LoadAniIn( obj );
			trace( "\t{ " + _img + ": ADDED:\t" + obj.name + " }" );
		}

		private function LoadRemove( obj:Object ) : void
		{
			_container.removeChild( obj );
			trace( "\t{ " + _img + ": KILLED:\t" + obj.name + " }" );
		}

		private function LoadAniIn( obj:Object ) : void
		{
			trace( "\t{ " + _img + ": ANI IN:\t" + obj.name + " }" );
			// animate
			TweenMax.to(obj, 0.5, {
				scaleX:1,
				scaleY:1,
				alpha:1,
				ease:Circ.easeOut
			});
		}

		private function LoadAniOut( obj:Object ) : void
		{
			trace( "\t{ " + _img + ": ANI OUT:\t" + obj.name + " }" );
			// animate
			TweenMax.to(obj, 0.5, {
				scaleX:0.3,
				scaleY:0.3,
				alpha:0,
				ease:Circ.easeOut,
				onComplete:LoadRemove( obj )
			});	

		}

		// tracking the porgess of the load
		private function LoadProgress( e:ProgressEvent ) : void
		{
			_percent = Math.floor( e.bytesLoaded / e.bytesTotal * 100 );
			var obj = _container.getChildByName( "imgPreload_mc" );
			obj.txt.htmlText = String( "LOADING:\t" + _percent + "%" );
			trace( "\t{ " + _img + ": LOADING:\t" + _percent + "%  } " );
		}

		// now that the loading is over
        private function LoadFin(e:Event) : void
        {
            // turn on smoothing
			e.target.content.smoothing = true;

			// get width and height
			_Width = e.target.width;
			_Height = e.target.height;
			trace( "\t{ W:\t" + _Width + " H:\t" + _Height + " }" );

			// define the vars
			var desiredSize:Number ;
			var currentSize:Number ;

			// get the image object
			//var _imgObj = e.target.content;
			var _imgObj = _loader;
			_container.addChild( _imgObj );

			// check to see which side is longer
			if( _Width > _Height )
			{
				 desiredSize = _conXcent * 2;
				 currentSize = _Width;
			} else {
				 desiredSize = _conYcent * 2;
				 currentSize = _Height;
			}

			//Get the %
			var scale:Number = (desiredSize / currentSize);

			//Set the _xscale and _yscale of the loaded image
			_imgObj.scaleX = _imgObj.scaleY = scale;

			// Set the placement of the image object
			_imgObj.x = -( _conXcent );
			_imgObj.y = -( _conYcent );
			_imgObj.alpha = 0;

			TweenMax.to(_imgObj, 0.5, {
				alpha:1,
				ease:Circ.easeIn
			});
			trace( "\t{ X:\t" + _imgObj.x + " Y:\t" + _imgObj.y + " S:\t" + _imgObj.scaleX +" }" );

			// set the loader text to 100%
			var obj = _container.getChildByName( "imgPreload_mc" );
			obj.txt.htmlText = String( "LOADING: 100%" );
			trace( "\t{ " + _img + ": LOADING: COMPLETE  } " );
			// animate out
			LoadAniOut( obj );

        }
    }
}

Tagged , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What's this?

You are currently reading Image Class with resizing and smoothing at DrPunchBlog.

meta

  •  

    July 2009
    M T W T F S S
    « Apr   Aug »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Archives

  • The Uber-Mannerist ...
    By Van Arno
  • RSS mtr.me

    • ‘Up In The Air’ Producer Launches The Allegiance Shingle With IM Global-Funded ‘Money Monster’
      EXCLUSIVE: Daniel Dubiecki has launched The Allegiance Theater, a association designed to emanate a breakwater where talent and financiers can induce film and TV projects. The association gets off a belligerent with a privately-backed beyond and growth account to beget character-driven, commercially appealing stories, pronounced Dubiecki. As Jason Reitman’s […]
    • New Oils
      James and Esao both published oil paintings to their blogs recently, James’s work on tip is titled, “Pauline Prieto“, and Esao’s investigate underneath it is substantially eleven or thirteen years aged now from art propagandize and he writes about because it is called “Grumpy Study”. Article source: http://meathaus.com/2012/02/06/new-oils/ […]
    • Video Game Character Facebook Profiles
      This is a array of cinema from a Facebook profiles of video diversion characters. Obviously they’re fake, that we wish we would’ve famous before spending a half hour perplexing to find and crony Nathan Drake and Solid Snake. OMG, how cold would it be to know those dual guys in genuine life? “Wow,GW — man-crush […]
  • Translator