Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions web/src/lib/cameraFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export interface CameraFrame {
left: number;
top: number;
width: number;
height: number;
}

export function getContainedFrame(
containerWidth: number,
containerHeight: number,
aspect: number,
): CameraFrame {
if (containerWidth <= 0 || containerHeight <= 0 || !Number.isFinite(aspect) || aspect <= 0) {
return { left: 0, top: 0, width: 0, height: 0 };
}

const containerAspect = containerWidth / containerHeight;
const width = containerAspect > aspect ? containerHeight * aspect : containerWidth;
const height = containerAspect > aspect ? containerHeight : containerWidth / aspect;
return {
left: (containerWidth - width) / 2,
top: (containerHeight - height) / 2,
width,
height,
};
}

export function cropToAspect(width: number, height: number, aspect: number): CameraFrame {
if (width <= 0 || height <= 0 || !Number.isFinite(aspect) || aspect <= 0) {
return { left: 0, top: 0, width: 0, height: 0 };
}

if (Math.abs((width / height) / aspect - 1) <= 0.01) {
return { left: 0, top: 0, width, height };
}

if (width / height > aspect) {
const cropWidth = height * aspect;
return { left: (width - cropWidth) / 2, top: 0, width: cropWidth, height };
}

const cropHeight = width / aspect;
return { left: 0, top: (height - cropHeight) / 2, width, height: cropHeight };
}

export function streamSize(aspect: number, longSide: number) {
const safeAspect = Number.isFinite(aspect) && aspect > 0 ? aspect : 4 / 3;
const shortSide = longSide / Math.max(safeAspect, 1 / safeAspect);
return {
width: { ideal: safeAspect >= 1 ? longSide : shortSide },
height: { ideal: safeAspect >= 1 ? shortSide : longSide },
aspectRatio: { ideal: safeAspect },
};
}
14 changes: 12 additions & 2 deletions web/src/pages/WallCamera.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@
min-height: 0;
}

.viewfinder {
position: absolute;
overflow: hidden;
}

.video {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: contain;
object-fit: cover;
}

.ghost {
position: absolute;
object-fit: cover;
inset: 0;
width: 100%;
height: 100%;
object-fit: fill;
pointer-events: none;
}

Expand Down
Loading
Loading