SpaceUtil.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Get Resize direction given axis + offset
  3. *
  4. * @param {String} axis (x|y)
  5. * @param {Number} offset
  6. *
  7. * @return {String} (e|w|n|s)
  8. */
  9. export function getDirection(axis, offset) {
  10. if (axis === 'x') {
  11. if (offset > 0) {
  12. return 'e';
  13. }
  14. if (offset < 0) {
  15. return 'w';
  16. }
  17. }
  18. if (axis === 'y') {
  19. if (offset > 0) {
  20. return 's';
  21. }
  22. if (offset < 0) {
  23. return 'n';
  24. }
  25. }
  26. return null;
  27. }
  28. /**
  29. * Resize the given bounds by the specified delta from a given anchor point.
  30. *
  31. * @param {Bounds} bounds the bounding box that should be resized
  32. * @param {String} direction in which the element is resized (n, s, e, w)
  33. * @param {Point} delta of the resize operation
  34. *
  35. * @return {Bounds} resized bounding box
  36. */
  37. export function resizeBounds(bounds, direction, delta) {
  38. var dx = delta.x,
  39. dy = delta.y;
  40. switch (direction) {
  41. case 'n':
  42. return {
  43. x: bounds.x,
  44. y: bounds.y + dy,
  45. width: bounds.width,
  46. height: bounds.height - dy
  47. };
  48. case 's':
  49. return {
  50. x: bounds.x,
  51. y: bounds.y,
  52. width: bounds.width,
  53. height: bounds.height + dy
  54. };
  55. case 'w':
  56. return {
  57. x: bounds.x + dx,
  58. y: bounds.y,
  59. width: bounds.width - dx,
  60. height: bounds.height
  61. };
  62. case 'e':
  63. return {
  64. x: bounds.x,
  65. y: bounds.y,
  66. width: bounds.width + dx,
  67. height: bounds.height
  68. };
  69. default:
  70. throw new Error('unrecognized direction: ' + direction);
  71. }
  72. }