all files / lib/features/snapping/ SnapUtil.js

77.42% Statements 24/31
68% Branches 17/25
66.67% Functions 4/6
77.42% Lines 24/31
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                         18×       18× 18×   18×                               14×       14×                                                 153×   153× 89×     64× 64×                                       135×       135×         135×   135×     135×     135×   135×   135× 135×     135×  
var abs = Math.abs,
    round = Math.round;
 
 
/**
 * Snap value to a collection of reference values.
 *
 * @param  {Number} value
 * @param  {Array<Number>} values
 * @param  {Number} [tolerance=10]
 *
 * @return {Number} the value we snapped to or null, if none snapped
 */
export function snapTo(value, values, tolerance) {
  tolerance = tolerance === undefined ? 10 : tolerance;
 
  var idx, snapValue;
 
  for (idx = 0; idx < values.length; idx++) {
    snapValue = values[idx];
 
    if (abs(snapValue - value) <= tolerance) {
      return snapValue;
    }
  }
}
 
 
export function topLeft(bounds) {
  return {
    x: bounds.x,
    y: bounds.y
  };
}
 
 
export function mid(bounds, defaultValue) {
 
  Iif (!bounds || isNaN(bounds.x) || isNaN(bounds.y)) {
    return defaultValue;
  }
 
  return {
    x: round(bounds.x + bounds.width / 2),
    y: round(bounds.y + bounds.height / 2)
  };
}
 
 
export function bottomRight(bounds) {
  return {
    x: bounds.x + bounds.width,
    y: bounds.y + bounds.height
  };
}
 
 
/**
 * Retrieve the snap state of the given event.
 *
 * @param  {Event} event
 * @param  {String} axis
 *
 * @return {Boolean} the snapped state
 *
 */
export function isSnapped(event, axis) {
  var snapped = event.snapped;
 
  if (!snapped) {
    return false;
  }
 
  Eif (typeof axis === 'string') {
    return snapped[axis];
  }
 
  return snapped.x && snapped.y;
}
 
 
/**
 * Set the given event as snapped.
 *
 * This method may change the x and/or y position of the shape
 * from the given event!
 *
 * @param {Event} event
 * @param {String} axis
 * @param {Number|Boolean} value
 *
 * @return {Number} old value
 */
export function setSnapped(event, axis, value) {
  Iif (typeof axis !== 'string') {
    throw new Error('axis must be in [x, y]');
  }
 
  Iif (typeof value !== 'number' && value !== false) {
    throw new Error('value must be Number or false');
  }
 
  var delta,
      previousValue = event[axis];
 
  var snapped = event.snapped = (event.snapped || {});
 
 
  Iif (value === false) {
    snapped[axis] = false;
  } else {
    snapped[axis] = true;
 
    delta = value - previousValue;
 
    event[axis] += delta;
    event['d' + axis] += delta;
  }
 
  return previousValue;
}