tldraw
Version:
A tiny little drawing editor.
41 lines (40 loc) • 1.36 kB
JavaScript
import { clamp, structuredClone } from "@tldraw/editor";
import { getUncroppedSize } from "../../../../../shapes/shared/crop.mjs";
function getTranslateCroppedImageChange(editor, shape, delta) {
if (!shape) {
throw Error("Needs to translate a cropped shape!");
}
const { crop: oldCrop } = shape.props;
if (!oldCrop) {
return;
}
const flatten = editor.inputs.getShiftKey() ? Math.abs(delta.x) < Math.abs(delta.y) ? "x" : "y" : null;
if (flatten === "x") {
delta.x = 0;
} else if (flatten === "y") {
delta.y = 0;
}
delta.rot(-shape.rotation);
const { w, h } = getUncroppedSize(shape.props, oldCrop);
const xCropSize = oldCrop.bottomRight.x - oldCrop.topLeft.x;
const yCropSize = oldCrop.bottomRight.y - oldCrop.topLeft.y;
const newCrop = structuredClone(oldCrop);
const xMinWithCrop = 1 - xCropSize;
const yMinWithCrop = 1 - yCropSize;
newCrop.topLeft.x = clamp(newCrop.topLeft.x - delta.x / w, 0, xMinWithCrop);
newCrop.topLeft.y = clamp(newCrop.topLeft.y - delta.y / h, 0, yMinWithCrop);
newCrop.bottomRight.x = newCrop.topLeft.x + xCropSize;
newCrop.bottomRight.y = newCrop.topLeft.y + yCropSize;
const partial = {
id: shape.id,
type: shape.type,
props: {
crop: newCrop
}
};
return partial;
}
export {
getTranslateCroppedImageChange
};
//# sourceMappingURL=crop_helpers.mjs.map