DEV Community

haitengli
haitengli

Posted on

High-DPI HTML5 Canvas Image Grids: Portrait Crop Math, iOS 16.7MP Limits & CORS

Building a client-side collage or taste-grid tool sounds trivial until real users drop vertical 2:3 anime key visuals, tankobon manga covers, and cross-origin album sleeves into a 1:1 square grid on iOS Safari.

While shipping Anime3x3.app—a pure browser-side 2400×2400 3x3 taste grid studio for anime, manga & manhwa, video games, and music albums—three subtle HTML5 <canvas> traps came up immediately:

  1. Center-crop decapitation when 680×1000 vertical posters are squeezed into square tiles;
  2. SecurityError: The operation is insecure (tainted canvas) when mixing multiple public cover CDNs;
  3. Silent 0×0 blank PNG exports on iOS WebKit when developers oversize offscreen canvases past the 16.77 MP GPU backing-store cap.

Here is the exact math and architecture that solved all three with zero server storage.


1. Fixing Portrait Decapitation With a 20% Top-Weighted Focal Default

Standard CSS object-fit: cover (and naive canvas center-cropping) centers the crop window at (50%, 50%). On theatrical posters and anime key visuals, studios place character eyes in the upper third (18%–28% from the top) and stack production credits across the bottom fifth.

Instead of hardcoding 0.5, parameterize the source rectangle with normalized (panX, panY) focal coordinates and default vertical portraits to panY = 0.20:

export function drawCoverTile(
  ctx: CanvasRenderingContext2D,
  img: HTMLImageElement,
  x: number,
  y: number,
  cellW: number,
  cellH: number,
  panX = 0.5,
  panY = 0.2, // Top-weighted default preserves faces on 2:3 posters!
  zoom = 1.0
) {
  const baseScale = Math.max(cellW / img.naturalWidth, cellH / img.naturalHeight);
  const drawScale = baseScale * zoom;

  const sw = cellW / drawScale;
  const sh = cellH / drawScale;
  const sx = Math.max(0, img.naturalWidth - sw) * panX;
  const sy = Math.max(0, img.naturalHeight - sh) * panY;

  ctx.drawImage(img, sx, sy, sw, sh, x, y, cellW, cellH);
}
Enter fullscreen mode Exit fullscreen mode

Giving each slot lightweight Pan X, Pan Y, and Zoom (1.0×–2.2×) sliders on top of that 20% default eliminates 90% of manual cropping work while still letting users frame full-body character sheets into crisp bust portraits.


2. Staying Under iOS Safari's 16,777,216-Pixel Canvas Limit

Desktop Chrome happily allocates a 4500×4500 (20.25 MP) offscreen <canvas>, but mobile Safari enforces a strict 16,777,216 total pixel limit (4096 × 4096)—exceeding it logs a warning and returns an empty transparent blob from canvas.toBlob().

Targeting 2400px master width hits the sweet spot across every layout:

  • 3×3 Square (1:1): 2400 × 2400 px = 5.76 MP (800×800 px per tile—razor-sharp after Reddit or X recompression, 100% safe on iOS).
  • 3×3 Vertical Poster (2:3): 2400 × 3600 px = 8.64 MP (800×1200 px per tile—still well below 16.77 MP).
  • Discord Non-Nitro Export: Rendering a secondary 1440px image/webp (quality: 0.92) produces a crisp <1.4 MB file that uploads instantly on free Discord servers.

3. Auditing Public Cover CDNs for Canvas CORS Compatibility

Even if an <img> tag renders visually in the DOM, drawing it onto a <canvas> taints the bitmap unless:

  1. You set img.crossOrigin = 'anonymous' before assigning img.src; and
  2. The image host responds with Access-Control-Allow-Origin: * (or echoes your Origin) without Vary cache poisoning.

During testing across entertainment metadata APIs:

  • AniList (s4.anilist.co) and Apple iTunes (is1-ssl.mzstatic.com) both send permissive Access-Control-Allow-Origin headers natively on cover images, so client browsers composite them directly with zero proxy hops.
  • VNDB (t.vndb.org) allows CORS on its JSON search API (api.vndb.org/kana/vn), but omits Access-Control-Allow-Origin on raw cover JPEGs. Passing strictly allowlisted cover hostnames through a tiny stateless Cloudflare Worker (Cache-Control: private, no-store + Access-Control-Allow-Origin: *) keeps visual novel 3x3 exports 100% reliable without storing a single image on disk.

You can try the live studio at anime3x3.app or grab the standalone crop-math reference from the HTML5 Canvas Grid Cropping Cheatsheet.

Top comments (0)