Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(screenshot-editor): add aspect-ratio-aware image layout math

Richie McIlroy0100bb67d814
diff --git a/apps/desktop/src/routes/screenshot-editor/layout.ts b/apps/desktop/src/routes/screenshot-editor/layout.ts
index 1a2784516..38bd30607 100644
--- a/apps/desktop/src/routes/screenshot-editor/layout.ts
+++ b/apps/desktop/src/routes/screenshot-editor/layout.ts
@@ -1,25 +1,124 @@
-import type { XY } from "~/utils/tauri";
+import type { AspectRatio, XY } from "~/utils/tauri";
export const SCREEN_MAX_PADDING = 0.4;
+const roundBaseDimension = (value: number) =>
+ Math.max((Math.ceil(value) + 1) & ~1, 2);
+
+const roundAutoBaseDimension = (value: number) => (Math.floor(value) + 1) & ~1;
+
+function getAspectRatioValue(aspectRatio: AspectRatio) {
+ switch (aspectRatio) {
+ case "wide":
+ return 16 / 9;
+ case "vertical":
+ return 9 / 16;
+ case "square":
+ return 1;
+ case "classic":
+ return 4 / 3;
+ case "tall":
+ return 3 / 4;
+ }
+}
+
+function getBaseSize(
+ cropWidth: number,
+ cropHeight: number,
+ paddingFactor: number,
+ aspectRatio: AspectRatio | null,
+) {
+ if (aspectRatio === null) {
+ const scale = 1 + paddingFactor * 2;
+ return {
+ width: roundAutoBaseDimension(cropWidth * scale),
+ height: roundAutoBaseDimension(cropHeight * scale),
+ };
+ }
+
+ const cropAspect = cropWidth / cropHeight;
+ const targetAspect = getAspectRatioValue(aspectRatio);
+ const padding = Math.max(cropWidth, cropHeight) * paddingFactor * 2;
+
+ if (cropAspect > targetAspect) {
+ const width = cropWidth + padding;
+ const height = width / targetAspect;
+ return {
+ width: roundBaseDimension(width),
+ height: roundBaseDimension(height),
+ };
+ }
+
+ const height = cropHeight + padding;
+ const width = height * targetAspect;
+ return {
+ width: roundBaseDimension(width),
+ height: roundBaseDimension(height),
+ };
+}
+
export function calculateImageTransform(
frameSize: { width: number; height: number },
imageSize: { width: number; height: number },
padding: number,
crop: { position: XY<number>; size: XY<number> } | null,
+ aspectRatio: AspectRatio | null,
) {
const cropWidth = crop?.size.x ?? imageSize.width;
const cropHeight = crop?.size.y ?? imageSize.height;
+
+ if (
+ frameSize.width <= 0 ||
+ frameSize.height <= 0 ||
+ cropWidth <= 0 ||
+ cropHeight <= 0
+ ) {
+ return {
+ offset: { x: 0, y: 0 },
+ size: {
+ width: Math.max(frameSize.width, 0),
+ height: Math.max(frameSize.height, 0),
+ },
+ };
+ }
+
const croppedAspect = cropWidth / cropHeight;
const outputAspect = frameSize.width / frameSize.height;
const paddingFactor = (padding / 100.0) * SCREEN_MAX_PADDING;
+ const baseSize = getBaseSize(
+ cropWidth,
+ cropHeight,
+ paddingFactor,
+ aspectRatio,
+ );
+ const outputScale = Math.min(
+ frameSize.width / Math.max(baseSize.width, 1),
+ frameSize.height / Math.max(baseSize.height, 1),
+ );
+
+ if (aspectRatio === null) {
+ const offsetX = cropWidth * paddingFactor * outputScale;
+ const offsetY = cropHeight * paddingFactor * outputScale;
+
+ return {
+ offset: { x: offsetX, y: offsetY },
+ size: {
+ width: Math.max(frameSize.width - offsetX * 2, 1),
+ height: Math.max(frameSize.height - offsetY * 2, 1),
+ },
+ };
+ }
+
const cropBasis = Math.max(cropWidth, cropHeight);
const maxPadding = Math.max(
Math.min((frameSize.width - 1) / 2, (frameSize.height - 1) / 2),
0,
);
- const paddingPixels = Math.min(cropBasis * paddingFactor, maxPadding);
+ const paddingPixels = Math.min(
+ cropBasis * paddingFactor * outputScale,
+ maxPadding,
+ );
const availableWidth = Math.max(frameSize.width - 2 * paddingPixels, 1);
const availableHeight = Math.max(frameSize.height - 2 * paddingPixels, 1);
@@ -53,6 +152,7 @@ export function getImageRect(
imageSize: { width: number; height: number } | null,
padding: number,
crop: { position: XY<number>; size: XY<number> } | null,
+ aspectRatio: AspectRatio | null,
) {
if (!imageSize) {
return {
@@ -68,6 +168,7 @@ export function getImageRect(
imageSize,
padding,
crop,
+ aspectRatio,
);
return {