Dev Helpers
← Back

Aspect ratio reducer

Import and render <AspectRatioReducer /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/AspectRatioReducer.tsx

export default function AspectRatioReducer({
  width = 12,
  height = 8,
}: { width?: number; height?: number } = {}) {
  const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a);
  const d = gcd(Math.round(width), Math.round(height)) || 1;
  return (
    <output>
      {Math.round(width) / d}:{Math.round(height) / d}
    </output>
  );
}