simplify properties further

This commit is contained in:
Ryan Di 2024-09-30 16:11:00 +08:00
parent 50e4a0b37d
commit bccd2bf30d
6 changed files with 14 additions and 66 deletions

View File

@ -441,13 +441,7 @@ import {
getLinkDirectionFromKey, getLinkDirectionFromKey,
} from "../element/flowchart"; } from "../element/flowchart";
import type { LocalPoint, Radians } from "../../math"; import type { LocalPoint, Radians } from "../../math";
import { import { clamp, point, pointDistance, vector } from "../../math";
clamp,
point,
pointDistance,
pointRotateRads,
vector,
} from "../../math";
import { cropElement } from "../element/cropElement"; import { cropElement } from "../element/cropElement";
const AppContext = React.createContext<AppClassProperties>(null!); const AppContext = React.createContext<AppClassProperties>(null!);
@ -9448,8 +9442,6 @@ class App extends React.Component<AppProps, AppState> {
y, y,
width, width,
height, height,
initialWidth: width,
initialHeight: height,
crop: { crop: {
x: 0, x: 0,
y: 0, y: 0,

View File

@ -255,9 +255,6 @@ const restoreElement = (
fileId: element.fileId, fileId: element.fileId,
scale: element.scale || [1, 1], scale: element.scale || [1, 1],
crop: element.crop ?? null, crop: element.crop ?? null,
initialWidth: element.initialWidth ?? element.width,
initialHeight: element.initialHeight ?? element.height,
resizeFactors: element.resizeFactors ?? [1, 1],
}); });
case "line": case "line":
// @ts-ignore LEGACY type // @ts-ignore LEGACY type

View File

@ -14,7 +14,7 @@ import {
} from "../../math"; } from "../../math";
import { updateBoundElements } from "./binding"; import { updateBoundElements } from "./binding";
import { mutateElement } from "./mutateElement"; import { mutateElement } from "./mutateElement";
import { TransformHandleType } from "./transformHandles"; import type { TransformHandleType } from "./transformHandles";
import type { import type {
ElementsMap, ElementsMap,
ExcalidrawElement, ExcalidrawElement,
@ -26,19 +26,24 @@ import {
getElementAbsoluteCoords, getElementAbsoluteCoords,
getResizedElementAbsoluteCoords, getResizedElementAbsoluteCoords,
} from "./bounds"; } from "./bounds";
import { AppClassProperties } from "../types"; import type { AppClassProperties } from "../types";
import { isInitializedImageElement } from "./typeChecks"; import { isInitializedImageElement } from "./typeChecks";
const _cropElement = ( const _cropElement = (
element: ExcalidrawImageElement, element: ExcalidrawImageElement,
image: HTMLImageElement,
transformHandle: TransformHandleType, transformHandle: TransformHandleType,
naturalWidth: number, naturalWidth: number,
naturalHeight: number, naturalHeight: number,
pointerX: number, pointerX: number,
pointerY: number, pointerY: number,
) => { ) => {
const uncroppedWidth = element.initialWidth * element.resizeFactors[0]; const uncroppedWidth =
const uncroppedHeight = element.initialHeight * element.resizeFactors[1]; element.width /
(element.crop ? element.crop.width / image.naturalWidth : 1);
const uncroppedHeight =
element.height /
(element.crop ? element.crop.height / image.naturalHeight : 1);
const naturalWidthToUncropped = naturalWidth / uncroppedWidth; const naturalWidthToUncropped = naturalWidth / uncroppedWidth;
const naturalHeightToUncropped = naturalHeight / uncroppedHeight; const naturalHeightToUncropped = naturalHeight / uncroppedHeight;
@ -154,6 +159,7 @@ export const cropElement = (
if (image && !(image instanceof Promise)) { if (image && !(image instanceof Promise)) {
const mutation = _cropElement( const mutation = _cropElement(
element, element,
image,
transformHandle, transformHandle,
image.naturalWidth, image.naturalWidth,
image.naturalHeight, image.naturalHeight,
@ -240,8 +246,9 @@ export const getUncroppedImageElement = (
if (image && !(image instanceof Promise)) { if (image && !(image instanceof Promise)) {
if (element.crop) { if (element.crop) {
const width = element.initialWidth * element.resizeFactors[0]; const width = element.width / (element.crop.width / image.naturalWidth);
const height = element.initialHeight * element.resizeFactors[1]; const height =
element.height / (element.crop.height / image.naturalHeight);
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords( const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
element, element,

View File

@ -489,9 +489,6 @@ export const newImageElement = (
status: opts.status ?? "pending", status: opts.status ?? "pending",
fileId: opts.fileId ?? null, fileId: opts.fileId ?? null,
scale: opts.scale ?? [1, 1], scale: opts.scale ?? [1, 1],
initialWidth: opts.width ?? 0,
initialHeight: opts.height ?? 0,
resizeFactors: [1, 1],
crop: opts.crop ?? null, crop: opts.crop ?? null,
}; };
}; };

View File

@ -696,13 +696,6 @@ export const resizeSingleElement = (
points: rescaledPoints, points: rescaledPoints,
}; };
// TODO: this is not the best approach
updateInternalScale(
element,
eleNewWidth / element.width,
eleNewHeight / element.height,
);
if ("scale" in element && "scale" in stateAtResizeStart) { if ("scale" in element && "scale" in stateAtResizeStart) {
mutateElement(element, { mutateElement(element, {
scale: [ scale: [
@ -757,38 +750,6 @@ export const resizeSingleElement = (
} }
}; };
const updateInternalScale = (
element: NonDeletedExcalidrawElement,
scaleX: number,
scaleY: number,
) => {
if ("type" in element && element.type === "image") {
element = element as ExcalidrawImageElement;
} else {
return;
}
// if the scales happen to be 0 (which is insanely unlikely), it will
// zero out the rolling multiplier and cause weird bugs with cropping.
// if zero is detected, just set the scales to an obnoxiously small number
if (scaleX === 0) {
scaleX = Number.EPSILON;
}
if (scaleY === 0) {
scaleY = Number.EPSILON;
}
scaleX = Math.abs(scaleX);
scaleY = Math.abs(scaleY);
mutateElement(element, {
resizeFactors: [
element.resizeFactors[0] * scaleX,
element.resizeFactors[1] * scaleY,
],
});
};
export const resizeMultipleElements = ( export const resizeMultipleElements = (
originalElements: PointerDownState["originalElements"], originalElements: PointerDownState["originalElements"],
selectedElements: readonly NonDeletedExcalidrawElement[], selectedElements: readonly NonDeletedExcalidrawElement[],

View File

@ -141,12 +141,6 @@ export type ExcalidrawImageElement = _ExcalidrawElementBase &
/** X and Y scale factors <-1, 1>, used for image axis flipping */ /** X and Y scale factors <-1, 1>, used for image axis flipping */
scale: [number, number]; scale: [number, number];
/** the image's dimension after initialization */
initialWidth: number;
initialHeight: number;
/** how much the image has been resized with respect the dimension at creation */
resizeFactors: [number, number];
crop: { crop: {
x: number; x: number;
y: number; y: number;