All files / player-browser/src/media bga-camera-fit.ts

100% Statements 6/6
100% Branches 6/6
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                            4x 3x 3x 3x 3x    
/** A rectangle in the skin's coordinates. */
export interface BGACameraRect {
  readonly x: number;
  readonly y: number;
  readonly w: number;
  readonly h: number;
}
 
/**
 * Where a camera picture of `width` x `height` goes in the BGA rect `rect`: as large as fits whole, centered, so a
 * 16:9 webcam in a square BGA frame keeps its proportions with black bands above and below. A picture with no size yet
 * fills the rect.
 */
export function fitBGACameraRect(width: number, height: number, rect: BGACameraRect): BGACameraRect {
  if (!(width > 0) || !(height > 0) || !(rect.w > 0) || !(rect.h > 0)) return rect;
  const scale = Math.min(rect.w / width, rect.h / height);
  const w = width * scale;
  const h = height * scale;
  return { x: rect.x + (rect.w - w) / 2, y: rect.y + (rect.h - h) / 2, w, h };
}