mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
* change resize math to absolute instead of delta * typings * small change for width on rotation * apply absolute resize to other sides * revert&change math.ts * polish, polish, polish * refactor with offset * eliminate nextX * rename to offsetPointer * fix curved lines * prefer arrow function * remove unused variables/comments for now Co-authored-by: daishi <daishi@axlight.com>
This commit is contained in:
parent
bd32a26653
commit
8efe0b7d05
3 changed files with 205 additions and 254 deletions
88
src/math.ts
88
src/math.ts
|
@ -56,32 +56,92 @@ export function rotate(
|
|||
];
|
||||
}
|
||||
|
||||
export function adjustXYWithRotation(
|
||||
const adjustXYWithRotation = (
|
||||
side: "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se",
|
||||
position: { x: number; y: number },
|
||||
x: number,
|
||||
y: number,
|
||||
angle: number,
|
||||
deltaX: number,
|
||||
deltaY: number,
|
||||
angle: number,
|
||||
) {
|
||||
let { x, y } = position;
|
||||
) => {
|
||||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
deltaX /= 2;
|
||||
deltaY /= 2;
|
||||
if (side === "e" || side === "ne" || side === "se") {
|
||||
x -= (deltaX / 2) * (1 - Math.cos(angle));
|
||||
y -= (deltaX / 2) * -Math.sin(angle);
|
||||
x += deltaX * (1 - cos);
|
||||
y += deltaX * -sin;
|
||||
}
|
||||
if (side === "s" || side === "sw" || side === "se") {
|
||||
x -= (deltaY / 2) * Math.sin(angle);
|
||||
y -= (deltaY / 2) * (1 - Math.cos(angle));
|
||||
x += deltaY * sin;
|
||||
y += deltaY * (1 - cos);
|
||||
}
|
||||
if (side === "w" || side === "nw" || side === "sw") {
|
||||
x += (deltaX / 2) * (1 + Math.cos(angle));
|
||||
y += (deltaX / 2) * Math.sin(angle);
|
||||
x += deltaX * (1 + cos);
|
||||
y += deltaX * sin;
|
||||
}
|
||||
if (side === "n" || side === "nw" || side === "ne") {
|
||||
x += (deltaY / 2) * -Math.sin(angle);
|
||||
y += (deltaY / 2) * (1 + Math.cos(angle));
|
||||
x += deltaY * -sin;
|
||||
y += deltaY * (1 + cos);
|
||||
}
|
||||
return { x, y };
|
||||
}
|
||||
};
|
||||
|
||||
export const resizeXYWidthHightWithRotation = (
|
||||
side: "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se",
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
height: number,
|
||||
offsetX: number,
|
||||
offsetY: number,
|
||||
angle: number,
|
||||
xPointer: number,
|
||||
yPointer: number,
|
||||
offsetPointer: number,
|
||||
sidesWithSameLength: boolean,
|
||||
) => {
|
||||
// center point for rotation
|
||||
const cx = x + width / 2;
|
||||
const cy = y + height / 2;
|
||||
|
||||
// rotation with current angle
|
||||
const [rotatedX, rotatedY] = rotate(xPointer, yPointer, cx, cy, -angle);
|
||||
|
||||
let scaleX = 1;
|
||||
let scaleY = 1;
|
||||
if (side === "e" || side === "ne" || side === "se") {
|
||||
scaleX = (rotatedX - offsetPointer - x) / width;
|
||||
}
|
||||
if (side === "s" || side === "sw" || side === "se") {
|
||||
scaleY = (rotatedY - offsetPointer - y) / height;
|
||||
}
|
||||
if (side === "w" || side === "nw" || side === "sw") {
|
||||
scaleX = (x + width - offsetPointer - rotatedX) / width;
|
||||
}
|
||||
if (side === "n" || side === "nw" || side === "ne") {
|
||||
scaleY = (y + height - offsetPointer - rotatedY) / height;
|
||||
}
|
||||
|
||||
let nextWidth = width * scaleX;
|
||||
let nextHeight = height * scaleY;
|
||||
if (sidesWithSameLength) {
|
||||
nextWidth = nextHeight = Math.max(nextWidth, nextHeight);
|
||||
}
|
||||
|
||||
return {
|
||||
width: nextWidth,
|
||||
height: nextHeight,
|
||||
...adjustXYWithRotation(
|
||||
side,
|
||||
x - offsetX,
|
||||
y - offsetY,
|
||||
angle,
|
||||
width - nextWidth,
|
||||
height - nextHeight,
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
export const getPointOnAPath = (point: Point, path: Point[]) => {
|
||||
const [px, py] = point;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue