Zoom into mouse position


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import fl.transitions.Tween;
  2. import fl.transitions.TweenEvent;
  3. import fl.transitions.easing.*;
  4. import fl.motion.MatrixTransformer;
  5.  
  6. const TWEEN_IN:String = "tweenIn";
  7. const TWEEN_OUT:String = "tweenOut";
  8. var tweenDirection:String;
  9.  
  10. var internalPoint:Point;
  11. var externalPoint:Point;
  12. var tw:Tween;
  13.  
  14. var square:Sprite = new Sprite();
  15. square.graphics.beginFill(0xFF0000);
  16. square.graphics.drawRect(0, 0, 100, 100);
  17.  
  18. square.x = stage.stageWidth/2 - square.width/2;
  19. square.y = stage.stageHeight/2 - square.height/2;
  20.  
  21. addChild(square);
  22.  
  23. square.addEventListener(MouseEvent.CLICK, zoomIn);
  24.  
  25. function zoomIn($e:MouseEvent):void
  26. {
  27. square.removeEventListener(MouseEvent.CLICK, zoomIn);
  28.  
  29. internalPoint = new Point(square.mouseX, square.mouseY);
  30. externalPoint = new Point(stage.mouseX, stage.mouseY);
  31.  
  32. var marker:Sprite = new Sprite();
  33. marker.graphics.beginFill(0);
  34. marker.graphics.drawCircle(0, 0, 5);
  35. marker.x = internalPoint.x;
  36. marker.y = internalPoint.y;
  37. marker.name = "marker";
  38.  
  39. square.addChild(marker);
  40.  
  41. tweenDirection = TWEEN_IN;
  42.  
  43. tw = new Tween(null, "", Elastic.easeOut, square.scaleX, 4, 1, true);
  44. tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale);
  45. tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween);
  46. }
  47.  
  48. function _syncScale($e:TweenEvent):void
  49. {
  50. square.scaleX = square.scaleY = tw.position;
  51.  
  52. var matrix:Matrix = square.transform.matrix;
  53.  
  54. MatrixTransformer.matchInternalPointWithExternal(matrix, internalPoint, externalPoint);
  55.  
  56. square.transform.matrix = matrix;
  57. }
  58.  
  59. function _cleanTween($e:TweenEvent):void
  60. {
  61. tw.removeEventListener(TweenEvent.MOTION_CHANGE, _syncScale);
  62. tw.removeEventListener(TweenEvent.MOTION_FINISH, _cleanTween);
  63.  
  64. tw = null;
  65.  
  66. if(tweenDirection == TWEEN_IN)
  67. stage.addEventListener(MouseEvent.CLICK, zoomOut);
  68. else if(tweenDirection == TWEEN_OUT)
  69. square.addEventListener(MouseEvent.CLICK, zoomIn);
  70. }
  71.  
  72. function zoomOut($e:MouseEvent):void
  73. {
  74. stage.removeEventListener(MouseEvent.CLICK, zoomOut);
  75.  
  76. externalPoint = square.localToGlobal(internalPoint);
  77. internalPoint = square.globalToLocal(externalPoint);
  78.  
  79. square.removeChild(square.getChildByName("marker"));
  80.  
  81. tweenDirection = TWEEN_OUT;
  82.  
  83. tw = new Tween(null, "", Strong.easeOut, square.scaleX, 1, 1, true);
  84. tw.addEventListener(TweenEvent.MOTION_CHANGE, _syncScale);
  85. tw.addEventListener(TweenEvent.MOTION_FINISH, _cleanTween);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.