All files / player-browser/src/stage viewport.ts

94.73% Statements 18/19
96.55% Branches 28/29
87.5% Functions 7/8
94.44% Lines 17/18

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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                            64x     64x 64x 64x                         2x                       39x               22x 22x 21x     39x       16x             39x               16x       4x                           10x 10x 10x    
/** Letterbox fit of a fixed design canvas into the screen. */
export interface ScaledViewport {
  x: number;
  y: number;
  scale: number;
  /** Per-axis scales when the output aspect differs from the authored coordinate canvas. */
  scaleX?: number;
  scaleY?: number;
}
 
/**
 * Scale + centring offset that fits a `designWidth x designHeight` canvas inside the screen (letterboxed /
 * pillarboxed). An output aspect ratio stretches the authored coordinates into that output canvas. Non-finite or non-positive scales fall back to 1.
 */
export function resolveScaledViewport(
  screenWidth: number,
  screenHeight: number,
  designWidth: number,
  designHeight: number,
  displayAspectRatio?: number,
): ScaledViewport {
  const outputWidth =
    displayAspectRatio !== undefined && Number.isFinite(displayAspectRatio) && displayAspectRatio > 0
      ? designHeight * displayAspectRatio
      : designWidth;
  const scale = Math.min(screenWidth / outputWidth, screenHeight / designHeight);
  const safeScale = Number.isFinite(scale) && scale > 0 ? scale : 1;
  return {
    x: (screenWidth - outputWidth * safeScale) / 2,
    y: (screenHeight - designHeight * safeScale) / 2,
    scale: safeScale,
    ...(outputWidth !== designWidth ? { scaleX: (outputWidth * safeScale) / designWidth, scaleY: safeScale } : {}),
  };
}
 
/** A rectangle in design pixels placed on the canvas through `viewport`: CSS pixels from the canvas's top-left. */
export function designRectToCanvas(
  rect: { x: number; y: number; width: number; height: number },
  viewport: ScaledViewport,
): { x: number; y: number; width: number; height: number } {
  return {
    x: viewport.x + rect.x * (viewport.scaleX ?? viewport.scale),
    y: viewport.y + rect.y * (viewport.scaleY ?? viewport.scale),
    width: rect.width * (viewport.scaleX ?? viewport.scale),
    height: rect.height * (viewport.scaleY ?? viewport.scale),
  };
}
 
/**
 * Upper bound for text rasterization density. High enough for a 3x-DPR display magnified 2.5x; beyond that the glyph
 * textures cost memory without visible gain.
 */
const MAX_TEXT_RESOLUTION = 8;
 
/**
 * Resolution design-canvas text should rasterize at so it stays crisp after the viewport scale: Pixi rasterizes `Text`
 * into a texture at `resolution` texels per design pixel, and the scene root then magnifies it by `viewportScale`.
 * Matching `viewportScale x rendererResolution` makes one texel land on one device pixel.
 */
export function resolveDesignTextResolution(viewportScale: number, rendererResolution: number): number {
  const value = viewportScale * rendererResolution;
  if (!Number.isFinite(value) || value <= 0) return 1;
  return Math.min(MAX_TEXT_RESOLUTION, Math.max(1, Math.round(value * 100) / 100));
}
 
let designTextResolution = 1;
 
/** Called by the shared scenes whenever they (re)fit the design canvas; read by the built-in skins' text helpers. */
export function setDesignTextResolution(value: number): void {
  designTextResolution = value;
}
 
export function getDesignTextResolution(): number {
  return designTextResolution;
}
 
let designPixelRatio = 1;
 
/**
 * Device pixels per design pixel on screen right now (`viewport scale × renderer resolution`, not rounded). Set by the
 * shared scenes next to {@link setDesignTextResolution}; canvas skins size their canvases from it so each canvas pixel
 * lands on exactly one screen pixel.
 */
export function setDesignPixelRatio(value: number): void {
  designPixelRatio = Number.isFinite(value) && value > 0 ? value : 1;
}
 
export function getDesignPixelRatio(): number {
  return designPixelRatio;
}
 
/**
 * Resolution (canvas pixels per design pixel) for a `width` × `height` design canvas drawn dot by dot at
 * `designPixelRatio` device pixels per design pixel: the width's device pixels, rounded, over the width — lowered so
 * the long side, whichever it is, stays within `maxEdge` pixels (a portrait stage is taller than it is wide).
 */
export function resolveCanvasPixelRatio(
  width: number,
  height: number,
  designPixelRatio: number,
  maxEdge: number,
): number {
  const ratio = Math.max(1, Math.round(width * designPixelRatio)) / width;
  const longSide = Math.max(width, height);
  return longSide * ratio > maxEdge ? maxEdge / longSide : ratio;
}