Return to Snippet

Revision: 24869
at March 13, 2010 10:54 by LeeProbert


Initial Code
package swingpants.effect
{
	/**
	 * Magnifgying Glass
	 * @author Jon Howard	-	www.swingpants.com
	 * 26th Feb 2010
	 */

	import flash.geom.Point;
	import flash.geom.Rectangle


	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ShaderFilter;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	import flash.display.BitmapData
	import flash.display.Bitmap


	public class MagnifyingGlass extends Sprite
	{
		private const ZERO_POINT:Point=new Point()
		private var loader:URLLoader;
		private var shader:Shader;
		private var sphereFilter:ShaderFilter;
		
		private var inited:Boolean=false

		private var refrac:Number=0.909	//Refraction - how much the 'light' is bent through the magnifying glass 0=Lots, 1=None
		private var radius:Number = 55 //Radius of Magnifying Glass
		
		private var source_pic:BitmapData 
		
		private var canvas:BitmapData 
		private var canvas_container:Bitmap

		private var mag_glass_posn:Point=new Point()

		/**
		 * CONSTRUCTOR
		 */
		public function MagnifyingGlass(refraction:Number=0.5, radius:Number=100, mag_glass_posn:Point=null, source_pic:BitmapData=null)
		{
			trace("[MagnifyingGlass] - CONSTRUCTOR")
			this.refrac = refraction
			
			this.radius = radius
			canvas = new BitmapData(radius * 2, radius * 2, false, 0x0) //Make draw canvas same size as glass
			
			if (mag_glass_posn) this.mag_glass_posn = mag_glass_posn
			
			if (source_pic) this.source_pic = source_pic
				else 
				{
					this.source_pic=new BitmapData(512,512)//If nothing passed then need a stand in
				}
				
			this.buttonMode = false
			this.mouseEnabled = false
			this.mouseChildren=false
			
			
			LoadedShader(); //Should really embed the shader
		}
		
		/**
		 * LoadedShader	-	Load the PixelBender filter
		 */
		private function LoadedShader():void
		{
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, loadCompleteHandler);
			loader.load(new URLRequest("pixelbender/spherize2.pbj"));
		}

		/**
		 * Add pixelbdner filter to shader
		 * @param	event
		 */
		private function loadCompleteHandler(event:Event):void 
		{
			shader = new Shader(loader.data);
			init()
		}
		
		
		/**
		 * init	-	initialiser
		 */
		private function init():void
		{
			
			canvas_container = new Bitmap(canvas)
			addChild(canvas_container)
	
			//Init the canvas container
			canvas_container.x = -radius
			canvas_container.y = -radius

			sphereFilter = new ShaderFilter(shader);
			
			inited = true
			
			updateShader()

		}
		
		/**
		 * 	UpdateShader()
		 */
		private function updateShader():void
		{
			//Copy the pixels underneath the magnifying glass
			canvas.copyPixels(source_pic, new Rectangle(mag_glass_posn.x-radius, mag_glass_posn.y-radius, radius * 2, radius * 2), ZERO_POINT)

			//Init the shader and apply as shaderFilter to canvas_container
			shader.data.refractionIndex.value = [refrac];
			shader.data.radius.value = [radius];
			shader.data.center.value = [radius, radius];
			
			canvas_container.filters = [sphereFilter];
		}

		
		/**
		 * update	-	Need to run this every frame (or at least everytime an update is needed
		 */
		public function update():void
		{
			if(inited)updateShader()
		}
		
		/** 
		 * SETTER: sourcePicture
		 * @param	val	The bitmapdata object to set as the source picture
		 */
		public function set sourcePicture(val:BitmapData):void
		{
			source_pic=val
		}
		
		/** 
		 * SETTER: magnifyingGlassPosition	-	Need to make sure that the class knows where to position the mag glass
		 * @param	val	Point position of mag glass
		 */
		public function set magnifyingGlassPosition(val:Point):void
		{
			mag_glass_posn=val
		}
		
		/**
		 * SETTER: refraction	-	Set the spherize refraction
		 */
		public function set refraction(val:Number):void
		{
			refrac=val
		}

	}
}

Initial URL

                                

Initial Description

                                

Initial Title
Magnifying Glass class (requires PixelBender file)

Initial Tags
textmate

Initial Language
Other