Dev Helpers
← Back

Fit artwork within a bounding box

Fit artwork within a bounding box using JavaScript.

Open this example in the interactive library

Source code

const fit = (w, h, maxW, maxH) => {
  const s = Math.min(maxW / w, maxH / h);
  return [Math.round(w * s), Math.round(h * s)];
};
console.log(fit(1200, 900, 320, 180));

Sample output

[ 240, 180 ]