Skip to content

Commit

Permalink
fix aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaChuk committed Jul 29, 2024
1 parent e498055 commit 5fd058a
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions web/src/lib/components/asset-viewer/editor/crop-canvas.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -460,30 +460,49 @@
newHeight = height + y - mouseY;
newWidth = width;
if (newHeight >= minSize && mouseY >= 0) {
const { newWidth: w, newHeight: h } = keepAspectRatio(newWidth, newHeight, aspectRatio);
crop.height = Math.min(h, canvas.height);
crop.y = Math.max(0, y + height - crop.height);
crop.width = Math.min(w, canvas.width);
const { newWidth: w, newHeight: h } = adjustDimensions(
newWidth,
newHeight,
aspectRatio,
canvas.width,
canvas.height,
);
crop.y = Math.max(0, y + height - h);
crop.width = w;
crop.height = h;
}
break;
}
case 'bottom': {
newHeight = mouseY - y;
newWidth = width;
if (newHeight >= minSize && mouseY <= canvas.height) {
const { newWidth: w, newHeight: h } = keepAspectRatio(newWidth, newHeight, aspectRatio);
crop.height = Math.min(h, canvas.height - y);
crop.width = Math.min(w, canvas.width);
const { newWidth: w, newHeight: h } = adjustDimensions(
newWidth,
newHeight,
aspectRatio,
canvas.width,
canvas.height - y,
);
crop.width = w;
crop.height = h;
}
break;
}
case 'top-left': {
newWidth = width + x - mouseX;
newHeight = height + y - mouseY;
if (newWidth >= minSize && newHeight >= minSize && mouseX >= 0 && mouseY >= 0) {
const { newWidth: w, newHeight: h } = keepAspectRatio(newWidth, newHeight, aspectRatio);
crop.width = Math.min(w, canvas.width);
crop.height = Math.min(h, canvas.height);
const { newWidth: w, newHeight: h } = adjustDimensions(
newWidth,
newHeight,
aspectRatio,
canvas.width,
canvas.height,
);
crop.width = w;
crop.height = h;
crop.x = Math.max(0, x + width - crop.width);
crop.y = Math.max(0, y + height - crop.height);
}
Expand All @@ -510,9 +529,15 @@
newWidth = width + x - mouseX;
newHeight = mouseY - y;
if (newWidth >= minSize && newHeight >= minSize && mouseX >= 0 && mouseY <= canvas.height) {
const { newWidth: w, newHeight: h } = keepAspectRatio(newWidth, newHeight, aspectRatio);
crop.width = Math.min(w, canvas.width);
crop.height = Math.min(h, canvas.height - y);
const { newWidth: w, newHeight: h } = adjustDimensions(
newWidth,
newHeight,
aspectRatio,
canvas.width,
canvas.height - y,
);
crop.width = w;
crop.height = h;
crop.x = Math.max(0, x + width - crop.width);
}
break;
Expand Down

0 comments on commit 5fd058a

Please sign in to comment.