# all files / Github/diagram-js/lib/util/ AttachUtil.js

100% Statements 13/13
100% Branches 0/0
100% Functions 2/2
100% Lines 13/13

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68

    319×
    319×
    319×

    319×

    319×

    32×
    32×
    32×
    32×
    32×

    32×

    32×

    32×

     import {
      roundPoint
    } from '../layout/LayoutUtil';

    import {
      center,
      delta
    } from './PositionUtil';

    /**
     * Calculates the absolute point relative to the new element's position
     *
     * @param {point} point [absolute]
     * @param {bounds} oldBounds
     * @param {bounds} newBounds
     *
     * @return {point} point [absolute]
     */
    export function getNewAttachPoint(point, oldBounds, newBounds) {
      var oldCenter = center(oldBounds),
          newCenter = center(newBounds),
          oldDelta = delta(point, oldCenter);

      var newDelta = {
        x: oldDelta.x * (newBounds.width / oldBounds.width),
        y: oldDelta.y * (newBounds.height / oldBounds.height)
      };

      return roundPoint({
        x: newCenter.x + newDelta.x,
        y: newCenter.y + newDelta.y
      });
    }

    /**
     * Calculates the shape's delta relative to a new position
     * of a certain element's bounds
     *
     * @param {djs.model.Shape} point [absolute]
     * @param {bounds} oldBounds
     * @param {bounds} newBounds
     *
     * @return {delta} delta
     */
    export function getNewAttachShapeDelta(shape, oldBounds, newBounds) {
      var shapeCenter = center(shape),
          oldCenter = center(oldBounds),
          newCenter = center(newBounds),
          shapeDelta = delta(shape, shapeCenter),
          oldCenterDelta = delta(shapeCenter, oldCenter);

      var newCenterDelta = {
        x: oldCenterDelta.x * (newBounds.width / oldBounds.width),
        y: oldCenterDelta.y * (newBounds.height / oldBounds.height)
      };

      var newShapeCenter = {
        x: newCenter.x + newCenterDelta.x,
        y: newCenter.y + newCenterDelta.y
      };

      return roundPoint({
        x: newShapeCenter.x + shapeDelta.x - shape.x,
        y: newShapeCenter.y + shapeDelta.y - shape.y
      });
    }