computeFitToCanvas
Computes how a source image is scaled and placed to fill a canvas’s height, with equal margins on the left and right.
The scaled width is rounded to the nearest integer that leaves an EVEN remainder, so the two margins are exactly equal — an asymmetric pillarbox is visible at a glance and looks like a mistake. Where both neighbors qualify, the one closer to the true scaled width wins, so the aspect error stays below one part in a thousand.
Worked example, the mobile store case: a 1344x2992 frame onto a 900x1600 canvas scales by 1600/2992 to 718.72 wide. 719 would leave a 181px remainder, which cannot split evenly, so 718 is chosen (margin 91) over 720 (margin 90) because 718 is nearer 718.72.
Import:
import { computeFitToCanvas } from 'obsidian-integration-testing';Signature:
function computeFitToCanvas(params: ComputeFitToCanvasParams): FitToCanvasGeometryParameters:
| Parameter | Type | Description |
|---|---|---|
params | ComputeFitToCanvasParams | The source and canvas dimensions. |
Returns: FitToCanvasGeometry — The scaled size, the offsets, and the side margin.
Links to this page: