Skip to content

Instantly share code, notes, and snippets.

@scdekov
Created October 5, 2020 18:39
Show Gist options
  • Save scdekov/a815ef0e17fae3cbe1cc847f2c568f70 to your computer and use it in GitHub Desktop.
Save scdekov/a815ef0e17fae3cbe1cc847f2c568f70 to your computer and use it in GitHub Desktop.
export const moveBoxCorner = (box: BoundingBox, newCorner: Point) => {
if (box.resizingCorner === 'topLeft') {
return {
...box,
startX: newCorner.x,
startY: newCorner.y,
width: box.width + box.startX - newCorner.x,
height: box.height + box.startY - newCorner.y
}
} else if (box.resizingCorner === 'topRight') {
return {
...box,
startY: newCorner.y,
width: newCorner.x - box.startX,
height: box.height + box.startY - newCorner.y
}
} else if (box.resizingCorner === 'bottomRight') {
return {
...box,
width: newCorner.x - box.startX,
height: newCorner.y - box.startY,
}
} else if (box.resizingCorner === 'bottomLeft') {
return {
...box,
startX: newCorner.x,
width: box.width + box.startX - newCorner.x,
height: newCorner.y - box.startY
}
}
return box;
};
@avalchev94
Copy link

avalchev94 commented Oct 5, 2020

export const moveBoxCorner = (box: BoundingBox, newCorner: Point) => {
  if (box.resizingCorner === 'topLeft') {
    return {
      ...box,
      top: newCorner.y,
      left: newCorner.x,
    }
  } else if (box.resizingCorner === 'topRight') {
    return {
      ...box,
      top: newCorner.y,
      right: newCorner.x,
    }
  } else if (box.resizingCorner === 'bottomRight') {
    return {
      ...box,
      bot: newCorner.y,
      right: newCorner.x,
    }
  } else if (box.resizingCorner === 'bottomLeft') {
    return {
      ...box,
      bot: newCorner.y,
      left: newCorner.x,
    }
  }

  normalizeBox(box);
  return box;
};

function normalizeBox(box: BoundingBox) {
    if box.top < bot.bot {
        swap(box.top, bot.bot)
    }

    if box.left > box.right {
        swap(box.left, box.right)
    }
}

wild guess

@scdekov
Copy link
Author

scdekov commented Oct 5, 2020

export const normalizeBoxCoords = (box: BoundingBox): BoundingBox => {
  const normalized = {...box};
  if (normalized.width < 0) {
    normalized.startX -= normalized.width;
    normalized.width = -normalized.width
  }
  if (normalized.height < 0) {
    normalized.startY -= normalized.height;
    normalized.height = -normalized.height
  }
  return normalized;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment