Zoom in to a link in a TextFlow within a scaled container in a Scroller viewport


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



Copy this code and paste it in your HTML
  1. private function moveAndZoomToLink(link:LinkElement,p:Person):void
  2. {
  3. var linkStart:int = link.getAbsoluteStart();
  4. var textFlowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition(linkStart);
  5. var linkPositionOnLine:int = linkStart-textFlowLine.absoluteStart;
  6.  
  7. var controllerIndex:int = textFlow.flowComposer.findControllerIndexAtPosition(linkStart);
  8. var controller:ContainerController = textFlow.flowComposer.getControllerAt(controllerIndex);
  9. var containerSprite:Sprite = controller.container;
  10.  
  11. var rect:Rectangle = textFlowLine.getBounds();
  12. var atomRect:Rectangle = textFlowLine.getTextLine().getAtomBounds(linkPositionOnLine+1);
  13. var nameWidth:Number = p.firstName.length*atomRect.width;
  14.  
  15. var globalAtomRect:Rectangle = new Rectangle(rect.x+(atomRect.x-atomRect.width),rect.y,nameWidth,rect.height);
  16.  
  17. var box:Shape = new Shape();
  18. box.graphics.beginFill(0xff0000,0.3);
  19. box.graphics.drawRect(globalAtomRect.x,globalAtomRect.y,globalAtomRect.width,globalAtomRect.height);
  20. containerSprite.addChild(box);
  21.  
  22. var localPosition:Point = new Point();
  23. localPosition.x = containerSprite.x+rect.x+globalAtomRect.x;
  24. localPosition.y = containerSprite.y+rect.y;
  25.  
  26. /*
  27. We're zooming in so scale the local position
  28. */
  29. var scaledX:Number = localPosition.x*Config.SHIRT_ZOOM_SCALE;
  30. var scaledY:Number = localPosition.y*Config.SHIRT_ZOOM_SCALE;
  31.  
  32. /*
  33. We don't want the shirt to move so it is out of the viewport so calculate what this range is ...
  34. */
  35.  
  36. var maxMoveX:Number = (Config.SHIRT_WIDTH*Config.SHIRT_ZOOM_SCALE)-Config.VIEWPORT_WIDTH;
  37. var maxMoveY:Number = (Config.SHIRT_HEIGHT*Config.SHIRT_ZOOM_SCALE)-Config.VIEWPORT_HEIGHT;
  38.  
  39. var newX:Number = 0-scaledX;
  40. var newY:Number = 0-scaledY;
  41.  
  42. var centeredX:Number = newX+Config.VIEWPORT_CENTER_X;
  43. var centeredY:Number = newY+Config.VIEWPORT_CENTER_Y;
  44.  
  45. /*
  46. Check the shirt hasn't moved out of bounds
  47. */
  48.  
  49. var offsetX:Number = centeredX>0? centeredX*-1 : centeredX<0-maxMoveX? (centeredX*-1)-maxMoveX : 0;
  50. var offsetY:Number = centeredY>0? centeredY*-1 : centeredY<0-maxMoveY? (centeredY*-1)-maxMoveY : 0;
  51.  
  52. /*
  53. Finally, the new position is nudged so it is at the centre of the viewport space or as near as can be.
  54. */
  55. moveToPointX = centeredX>0? 0 : centeredX<0-maxMoveX? centeredX+((centeredX*-1)-maxMoveX) : centeredX;
  56. moveToPointY = centeredY>0? 0 : centeredY<0-maxMoveY? centeredY+((centeredY*-1)-maxMoveY) : centeredY;
  57.  
  58. var viewportX:Number = Config.VIEWPORT_CENTER_X+offsetX;
  59. var viewportY:Number = Config.VIEWPORT_CENTER_Y+offsetY;
  60.  
  61. shirtMoveTo.play();
  62.  
  63. var evt:PersonSelectionEvent = new PersonSelectionEvent(PersonSelectionEvent.PERSON_SELECTION,true);
  64. evt.viewportPoint = new Point(viewportX,viewportY);
  65. evt.person = p;
  66.  
  67. dispatchEvent(evt);
  68. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.