/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import fl.transitions.Tween; import fl.transitions.TweenEvent; import fl.transitions.easing.*; import fl.motion.MatrixTransformer; const TWEEN_IN:String = "tweenIn"; const TWEEN_OUT:String = "tweenOut"; var tweenDirection:String; var internalPoint:Point; var externalPoint:Point; var tw:Tween; var square:Sprite = new Sprite(); square.graphics.beginFill(0xFF0000); square.graphics.drawRect(0, 0, 100, 100); square.x = stage.stageWidth/2 - square.width/2; square.y = stage.stageHeight/2 - square.height/2; addChild(square); square.addEventListener(MouseEvent.CLICK, zoomIn); function zoomIn($e:MouseEvent):void { square.removeEventListener(MouseEvent.CLICK, zoomIn); internalPoint = new Point(square.mouseX, square.mouseY); externalPoint = new Point(stage.mouseX, stage.mouseY); var marker:Sprite = new Sprite(); marker.graphics.beginFill(0); marker.graphics.drawCircle(0, 0, 5); marker.x = internalPoint.x; marker.y = internalPoint.y; marker.name = "marker"; square.addChild(marker); tweenDirection = TWEEN_IN; tw = new Tween(null, "", Elastic.easeOut, square.scaleX, 4, 1, true); tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween); } function _syncScale($e:TweenEvent):void { square.scaleX = square.scaleY = tw.position; var matrix:Matrix = square.transform.matrix; MatrixTransformer.matchInternalPointWithExternal(matrix, internalPoint, externalPoint); square.transform.matrix = matrix; } function _cleanTween($e:TweenEvent):void { tw.removeEventListener(TweenEvent.MOTION_CHANGE, _syncScale); tw.removeEventListener(TweenEvent.MOTION_FINISH, _cleanTween); tw = null; if(tweenDirection == TWEEN_IN) stage.addEventListener(MouseEvent.CLICK, zoomOut); else if(tweenDirection == TWEEN_OUT) square.addEventListener(MouseEvent.CLICK, zoomIn); } function zoomOut($e:MouseEvent):void { stage.removeEventListener(MouseEvent.CLICK, zoomOut); externalPoint = square.localToGlobal(internalPoint); internalPoint = square.globalToLocal(externalPoint); square.removeChild(square.getChildByName("marker")); tweenDirection = TWEEN_OUT; tw = new Tween(null, "", Strong.easeOut, square.scaleX, 1, 1, true); tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale); tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween);