shift locking 22.5 degree and move to constants (#1216)

* shift locking 22.5 degree and move to constants #1171

* review SHIFT_LOCKING_ANGLE
This commit is contained in:
José Quinto 2020-04-04 12:55:22 +01:00 committed by GitHub
parent 44f871de71
commit 030954badb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 6 deletions

View file

@ -1,6 +1,7 @@
import { ExcalidrawElement } from "./types";
import { mutateElement } from "./mutateElement";
import { isLinearElement } from "./typeChecks";
import { SHIFT_LOCKING_ANGLE } from "../constants";
export function isInvisiblySmallElement(element: ExcalidrawElement): boolean {
if (isLinearElement(element)) {
@ -21,17 +22,21 @@ export function getPerfectElementSize(
const absHeight = Math.abs(height);
if (elementType === "line" || elementType === "arrow") {
if (absHeight < absWidth / 2) {
const lockedAngle =
Math.round(Math.atan(absHeight / absWidth) / SHIFT_LOCKING_ANGLE) *
SHIFT_LOCKING_ANGLE;
if (lockedAngle === 0) {
height = 0;
} else if (absWidth < absHeight / 2) {
} else if (lockedAngle === Math.PI / 2) {
width = 0;
} else {
height = absWidth * Math.sign(height);
height =
Math.round(absWidth * Math.tan(lockedAngle)) * Math.sign(height) ||
height;
}
} else if (elementType !== "selection") {
height = absWidth * Math.sign(height);
}
return { width, height };
}