4/14/2011

Pixelation Transition

Here's my attempt at a pixelizing image transition. Well... the basics.

Click to transition in and out.

in the following code, pic is an image on stage. (requires the TweenLite tween library) import flash.display.BitmapData; import flash.display.Bitmap; import flash.display.DisplayObject; import flash.geom.Matrix; import com.greensock.TweenLite; import flash.events.MouseEvent; import flash.display.Sprite; var bmd:BitmapData; var bm:Bitmap; var transHolder:Sprite; var scale:Number=1; stage.addEventListener(MouseEvent.CLICK,mosai); function mosai(e:MouseEvent){ if(pic.visible){ mosaicTrans(pic,0); }else{ mosaicTrans(pic,1); } } function mosaicTrans(pic:DisplayObject,sc:Number){ transHolder=new Sprite(); transHolder.x=pic.x; transHolder.y=pic.y; var w:int=int(pic.width*scale); var h:int=int(pic.height*scale); if(w<1||h<1){ w=1; h=1; } bmd=new BitmapData(w,h); bm=new Bitmap(bmd); transHolder.addChild(bm); addChild(transHolder); if(sc==1){ TweenLite.to(this,1,{scale:sc,onUpdate:rescale,onComplete:inDone}); }else{ pic.visible=false; TweenLite.to(this,1,{scale:sc,onUpdate:rescale}); } } function inDone():void{ pic.visible=true; if(contains(transHolder)){removeChild(transHolder);} } function rescale():void{ var w:int=int(pic.width*scale); var h:int=int(pic.height*scale); if(w>0&&h>0){ var scaleMatrix:Matrix=new Matrix(); scaleMatrix.scale(scale,scale); bmd=new BitmapData(w,h); bmd.draw(pic,scaleMatrix); bm.bitmapData=bmd; bm.width=pic.width; bm.height=pic.height; }else{ if(contains(transHolder)){removeChild(transHolder);} } }

No comments:

Post a Comment