Posted By

LeeProbert on 03/13/10


Tagged

textmate bitmapData glass PixelBender magnifying


Versions (?)

Magnifying Glass class (requires PixelBender file)


 / Published in: Other
 

  1. package swingpants.effect
  2. {
  3. /**
  4. * Magnifgying Glass
  5. * @author Jon Howard - www.swingpants.com
  6. * 26th Feb 2010
  7. */
  8.  
  9. import flash.geom.Point;
  10. import flash.geom.Rectangle
  11.  
  12.  
  13. import flash.display.Shader;
  14. import flash.display.Sprite;
  15. import flash.events.Event;
  16. import flash.filters.ShaderFilter;
  17. import flash.net.URLLoader;
  18. import flash.net.URLLoaderDataFormat;
  19. import flash.net.URLRequest;
  20.  
  21. import flash.display.BitmapData
  22. import flash.display.Bitmap
  23.  
  24.  
  25. public class MagnifyingGlass extends Sprite
  26. {
  27. private const ZERO_POINT:Point=new Point()
  28. private var loader:URLLoader;
  29. private var shader:Shader;
  30. private var sphereFilter:ShaderFilter;
  31.  
  32. private var inited:Boolean=false
  33.  
  34. private var refrac:Number=0.909 //Refraction - how much the 'light' is bent through the magnifying glass 0=Lots, 1=None
  35. private var radius:Number = 55 //Radius of Magnifying Glass
  36.  
  37. private var source_pic:BitmapData
  38.  
  39. private var canvas:BitmapData
  40. private var canvas_container:Bitmap
  41.  
  42. private var mag_glass_posn:Point=new Point()
  43.  
  44. /**
  45. * CONSTRUCTOR
  46. */
  47. public function MagnifyingGlass(refraction:Number=0.5, radius:Number=100, mag_glass_posn:Point=null, source_pic:BitmapData=null)
  48. {
  49. trace("[MagnifyingGlass] - CONSTRUCTOR")
  50. this.refrac = refraction
  51.  
  52. this.radius = radius
  53. canvas = new BitmapData(radius * 2, radius * 2, false, 0x0) //Make draw canvas same size as glass
  54.  
  55. if (mag_glass_posn) this.mag_glass_posn = mag_glass_posn
  56.  
  57. if (source_pic) this.source_pic = source_pic
  58. else
  59. {
  60. this.source_pic=new BitmapData(512,512)//If nothing passed then need a stand in
  61. }
  62.  
  63. this.buttonMode = false
  64. this.mouseEnabled = false
  65. this.mouseChildren=false
  66.  
  67.  
  68. LoadedShader(); //Should really embed the shader
  69. }
  70.  
  71. /**
  72. * LoadedShader - Load the PixelBender filter
  73. */
  74. private function LoadedShader():void
  75. {
  76. loader = new URLLoader();
  77. loader.dataFormat = URLLoaderDataFormat.BINARY;
  78. loader.addEventListener(Event.COMPLETE, loadCompleteHandler);
  79. loader.load(new URLRequest("pixelbender/spherize2.pbj"));
  80. }
  81.  
  82. /**
  83. * Add pixelbdner filter to shader
  84. * @param event
  85. */
  86. private function loadCompleteHandler(event:Event):void
  87. {
  88. shader = new Shader(loader.data);
  89. init()
  90. }
  91.  
  92.  
  93. /**
  94. * init - initialiser
  95. */
  96. private function init():void
  97. {
  98.  
  99. canvas_container = new Bitmap(canvas)
  100. addChild(canvas_container)
  101.  
  102. //Init the canvas container
  103. canvas_container.x = -radius
  104. canvas_container.y = -radius
  105.  
  106. sphereFilter = new ShaderFilter(shader);
  107.  
  108. inited = true
  109.  
  110. updateShader()
  111.  
  112. }
  113.  
  114. /**
  115. * UpdateShader()
  116. */
  117. private function updateShader():void
  118. {
  119. //Copy the pixels underneath the magnifying glass
  120. canvas.copyPixels(source_pic, new Rectangle(mag_glass_posn.x-radius, mag_glass_posn.y-radius, radius * 2, radius * 2), ZERO_POINT)
  121.  
  122. //Init the shader and apply as shaderFilter to canvas_container
  123. shader.data.refractionIndex.value = [refrac];
  124. shader.data.radius.value = [radius];
  125. shader.data.center.value = [radius, radius];
  126.  
  127. canvas_container.filters = [sphereFilter];
  128. }
  129.  
  130.  
  131. /**
  132. * update - Need to run this every frame (or at least everytime an update is needed
  133. */
  134. public function update():void
  135. {
  136. if(inited)updateShader()
  137. }
  138.  
  139. /**
  140. * SETTER: sourcePicture
  141. * @param val The bitmapdata object to set as the source picture
  142. */
  143. public function set sourcePicture(val:BitmapData):void
  144. {
  145. source_pic=val
  146. }
  147.  
  148. /**
  149. * SETTER: magnifyingGlassPosition - Need to make sure that the class knows where to position the mag glass
  150. * @param val Point position of mag glass
  151. */
  152. public function set magnifyingGlassPosition(val:Point):void
  153. {
  154. mag_glass_posn=val
  155. }
  156.  
  157. /**
  158. * SETTER: refraction - Set the spherize refraction
  159. */
  160. public function set refraction(val:Number):void
  161. {
  162. refrac=val
  163. }
  164.  
  165. }
  166. }

Report this snippet  

You need to login to post a comment.