simplify code

This commit is contained in:
Ryan Di 2025-01-07 19:23:26 +08:00
parent ef20c8b9fa
commit ec39094933

View File

@ -386,10 +386,6 @@ const configExportDimension = async ({
const containPadding = cfg.fit === "contain"; const containPadding = cfg.fit === "contain";
if (cfg.x != null || cfg.x != null) {
cfg.fit = "none";
}
cfg.padding = cfg.padding ?? 0; cfg.padding = cfg.padding ?? 0;
cfg.scale = cfg.scale ?? 1; cfg.scale = cfg.scale ?? 1;
@ -441,20 +437,20 @@ const configExportDimension = async ({
// variables for target bounding box // variables for target bounding box
let [x, y, width, height] = origCanvasSize; let [x, y, width, height] = origCanvasSize;
if (cfg.fit === "contain" || cfg.widthOrHeight || cfg.maxWidthOrHeight) { x = cfg.x ?? x;
if (cfg.width != null) { y = cfg.y ?? y;
cfg.padding = Math.min( width = cfg.width ?? width;
cfg.padding, height = cfg.height ?? height;
(cfg.width - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
);
}
if (cfg.height != null) { if (cfg.fit === "contain" || cfg.widthOrHeight || cfg.maxWidthOrHeight) {
cfg.padding = Math.min( cfg.padding =
cfg.padding, cfg.padding && cfg.padding > 0
(cfg.height - DEFAULT_SMALLEST_EXPORT_SIZE) / 2, ? Math.min(
); cfg.padding,
} (width - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
(height - DEFAULT_SMALLEST_EXPORT_SIZE) / 2,
)
: 0;
if (cfg.getDimensions != null) { if (cfg.getDimensions != null) {
const ret = cfg.getDimensions(width, height); const ret = cfg.getDimensions(width, height);
@ -480,34 +476,6 @@ const configExportDimension = async ({
} }
} }
if (cfg.width != null) {
width = cfg.width;
if (cfg.padding && containPadding) {
width -= cfg.padding * 2;
}
if (cfg.height) {
height = cfg.height;
if (cfg.padding && containPadding) {
height -= cfg.padding * 2;
}
} else {
// if height not specified, scale the original height to match the new
// width while maintaining aspect ratio
height *= width / origWidth;
}
} else if (cfg.height != null) {
height = cfg.height;
if (cfg.padding && containPadding) {
height -= cfg.padding * 2;
}
// width not specified, so scale the original width to match the new
// height while maintaining aspect ratio
width *= height / origHeight;
}
if (cfg.maxWidthOrHeight != null || cfg.widthOrHeight != null) { if (cfg.maxWidthOrHeight != null || cfg.widthOrHeight != null) {
if (cfg.padding) { if (cfg.padding) {
if (cfg.maxWidthOrHeight != null) { if (cfg.maxWidthOrHeight != null) {
@ -534,31 +502,26 @@ const configExportDimension = async ({
width = ret.width; width = ret.width;
height = ret.height; height = ret.height;
cfg.scale = ret.scale ?? cfg.scale; cfg.scale = ret.scale ?? cfg.scale;
} else if ( } else if (containPadding) {
containPadding && // const whRatio = width / height;
cfg.padding &&
cfg.width == null && // console.log("cfg.padding", cfg.padding);
cfg.height == null
) { // const wRatio = (width - cfg.padding * 2) / width;
// const hRatio = (height - cfg.padding * 2) / height;
// exportScale = Math.min(wRatio, hRatio);
// width -= cfg.padding * 2;
// height -= (cfg.padding * 2) / whRatio;
const whRatio = width / height; const whRatio = width / height;
width -= cfg.padding * 2; width -= cfg.padding * 2;
height -= (cfg.padding * 2) / whRatio; height -= (cfg.padding * 2) / whRatio;
}
if ( const wRatio = width / origWidth;
(cfg.fit === "contain" && !cfg.maxWidthOrHeight) || const hRatio = height / origHeight;
(containPadding && cfg.padding) // scale the orig canvas to fit in the target frame
) { exportScale = Math.min(wRatio, hRatio);
if (cfg.fit === "contain") {
const wRatio = width / origWidth;
const hRatio = height / origHeight;
// scale the orig canvas to fit in the target frame
exportScale = Math.min(wRatio, hRatio);
} else {
const wRatio = (width - cfg.padding * 2) / width;
const hRatio = (height - cfg.padding * 2) / height;
exportScale = Math.min(wRatio, hRatio);
}
} }
x = cfg.x ?? origX; x = cfg.x ?? origX;