Posted By

orazal on 08/29/07


Tagged

math actionscript


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

n00ge
orazal


PositionUtils


 / Published in: ActionScript
 

methods : getGridPositions, zoomFromPoint

  1. class com.orazal.tools.PositionUtils {
  2. /**
  3. * Returns x and y positions for a grid
  4. *
  5. * @param amount The total amount of items
  6. * @param limit The row or column limit before line break
  7. * @param width The item's with
  8. * @param height The item's height
  9. * @param direction Horizontal or Vertical
  10. * @returns An array with a position object with the x and y properties
  11. */
  12. public static function getGridPositions(amount:Number, limit:Number,
  13. width:Number, height:Number,
  14. direction:String):Array {
  15. // Set direction as horizontal if none is found
  16. if (!direction) {
  17. direction = "horizontal";
  18. }
  19. var pos_ar:Array = new Array();
  20. for (var i:Number = 0; i<20; i++) {
  21. switch (direction) {
  22. case "horizontal" :
  23. var column:Number = i%limit;
  24. var row:Number = Math.floor(i/limit);
  25. break;
  26. case "vertical" :
  27. var row:Number = i%limit;
  28. var column:Number = Math.floor(i/limit);
  29. break;
  30. }
  31. var xPos = column*width;
  32. var yPos = row*height;
  33. pos_ar.push({x:xPos, y:yPos});
  34. }
  35. return pos_ar;
  36. }
  37.  
  38. /**
  39. * Calculates a clip's final size and position from a provided registration point
  40. *
  41. *@param clip The movieclip to zoom
  42. *@param percent The amount to zoom
  43. *@param xOffset The percentage (in decimals) to offset horizontally from clip's center
  44. *@param yOffset The percentage (in decimals) to offset vertically from clip's center
  45. *@returns An object with the final width, height, x and y positions
  46. */
  47. public static function zoomFromPoint(clip:MovieClip, percent:Number,
  48. xOffset:Number, yOffset:Number):Object {
  49. // Convert zoom percent to decimals
  50. percent /= 100;
  51. // Get original width and height
  52. var originalW = clip._width/(clip._xscale/100);
  53. var originalH = clip._height/(clip._yscale/100);
  54. // Center register position if not provided
  55. if (!xOffset) {
  56. var xOffset:Number = 0.5;
  57. }
  58. if (!yOffset) {
  59. var yOffset:Number = 0.5;
  60. }
  61. // Determine x and y positions
  62. var centerX = clip._x+(clip._width*xOffset);
  63. var centerY = clip._y+(clip._height*yOffset);
  64. // Determine final positions
  65. var final:Object = new Object();
  66. final.width = originalW*percent;
  67. final.height = originalH*percent;
  68. final.x = centerX-(final.width*xOffset);
  69. final.y = centerY-(final.height*yOffset);
  70. return final;
  71. }
  72. }

Report this snippet  

You need to login to post a comment.