The following example shows how to create a Fish Eye effect to an image using Actionscript 3.0 and Flash CS3 or Flash CS4.
First we need to import an image in the Flash library and then assign a class for Actionscript export, I have used such Oldwoman.
In this way we can "attach" the image from the library to the Stage with Actionscript 3.0.
Finally, just apply the code that I created.
Of course,it is not mandatory to use an image from the library, we can also load it from the outside.
Main.as
Source files:Code:package { import flash.display.*; import flash.text.*; import flash.events.*; import flash.filters.*; import flash.geom.*; public class Main extends MovieClip { private var old_woman:BitmapData; private var new_bitmap:Bitmap; private var bit_data:BitmapData; private var circle:BitmapData; private var circleBitmap:Bitmap; private const DIAMETER:int=200; private const INTENSITY:Number=1; public function Main() { addEventListener(Event.ADDED_TO_STAGE,init); } private function init(evt:Event):void { removeEventListener(Event.ADDED_TO_STAGE,init); createFishEye(); } private function createFishEye():void { old_woman=new OldWoman(0,0); bit_data=old_woman.clone(); new_bitmap=new Bitmap(old_woman); new_bitmap.smoothing=true; circle=createCircle(DIAMETER); circleBitmap=new Bitmap(circle); circleBitmap.visible=false; applyCircle(); addChild(new_bitmap); addChild(circleBitmap); stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoving); } private function createCircle(diameter:int):BitmapData { var circle:BitmapData=new BitmapData(diameter,diameter,false,0xFF808080); var center:Number=diameter/2; var radius:Number=center; for(var cy:int=0;cy < diameter;cy++) { var newY:int=cy-center; for(var cx:int=0;cx < diameter;cx++) { var newX:int=cx-center; var distance:Number=Math.sqrt(newX*newX+newY*newY); if(distance<radius) { var t:Number=Math.pow(Math.sin(Math.PI/2*distance/radius),INTENSITY); var dx:Number=newX*(t-1)/diameter; var dy:Number=newY*(t-1)/diameter; var blue:uint=0x80+dx*0xFF; var green:uint=0x80+dy*0xFF; circle.setPixel(cx,cy,green << 8 | blue); } } } return circle; } private function applyCircle():void { var dmf:DisplacementMapFilter=new DisplacementMapFilter(circle,new Point(stage.mouseX-DIAMETER/2,stage.mouseY-DIAMETER/2), BitmapDataChannel.BLUE,BitmapDataChannel.GREEN,DIAMETER,DIAMETER, DisplacementMapFilterMode.CLAMP); old_woman.applyFilter(bit_data,bit_data.rect,new Point(),dmf); } private function onMouseMoving(evt:MouseEvent):void { circleBitmap.x=mouseX-DIAMETER/2; circleBitmap.y=mouseY-DIAMETER/2; applyCircle(); evt.updateAfterEvent(); } } }
Bookmarks