AttachUtil.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {
  2. roundPoint
  3. } from '../layout/LayoutUtil';
  4. import {
  5. center,
  6. delta
  7. } from './PositionUtil';
  8. /**
  9. * Calculates the absolute point relative to the new element's position
  10. *
  11. * @param {point} point [absolute]
  12. * @param {bounds} oldBounds
  13. * @param {bounds} newBounds
  14. *
  15. * @return {point} point [absolute]
  16. */
  17. export function getNewAttachPoint(point, oldBounds, newBounds) {
  18. var oldCenter = center(oldBounds),
  19. newCenter = center(newBounds),
  20. oldDelta = delta(point, oldCenter);
  21. var newDelta = {
  22. x: oldDelta.x * (newBounds.width / oldBounds.width),
  23. y: oldDelta.y * (newBounds.height / oldBounds.height)
  24. };
  25. return roundPoint({
  26. x: newCenter.x + newDelta.x,
  27. y: newCenter.y + newDelta.y
  28. });
  29. }
  30. /**
  31. * Calculates the shape's delta relative to a new position
  32. * of a certain element's bounds
  33. *
  34. * @param {djs.model.Shape} point [absolute]
  35. * @param {bounds} oldBounds
  36. * @param {bounds} newBounds
  37. *
  38. * @return {delta} delta
  39. */
  40. export function getNewAttachShapeDelta(shape, oldBounds, newBounds) {
  41. var shapeCenter = center(shape),
  42. oldCenter = center(oldBounds),
  43. newCenter = center(newBounds),
  44. shapeDelta = delta(shape, shapeCenter),
  45. oldCenterDelta = delta(shapeCenter, oldCenter);
  46. var newCenterDelta = {
  47. x: oldCenterDelta.x * (newBounds.width / oldBounds.width),
  48. y: oldCenterDelta.y * (newBounds.height / oldBounds.height)
  49. };
  50. var newShapeCenter = {
  51. x: newCenter.x + newCenterDelta.x,
  52. y: newCenter.y + newCenterDelta.y
  53. };
  54. return roundPoint({
  55. x: newShapeCenter.x + shapeDelta.x - shape.x,
  56. y: newShapeCenter.y + shapeDelta.y - shape.y
  57. });
  58. }