Frontend Graphics Reference · MIT

HTML5 Canvas Grid Cropping & CORS Export Cheatsheet

When building browser-based collage utilities—such as 9-panel anime 3x3 taste grids, manga tankobon cover charts, or 1:1 album mosaics—three subtle browser constraints cause most bugs: naive center-cropping on vertical portraits, iOS Safari canvas memory ceilings, and tainted cross-origin bitmaps.

1. Top-Weighted Portrait Focal Math (2:3 Source → 1:1 Square Tile)

Promotional posters and book jackets have an aspect ratio near 2:3 (for example 680×1000), placing character faces in the top third and credits at the bottom. A naive 50% vertical crop decapitates foreheads. Defaulting panY to 0.20 keeps eyes centered while allowing per-tile pan and zoom overrides:

function computeCoverCrop(imgW, imgH, cellW, cellH, panX = 0.5, panY = 0.2, zoom = 1.0) {
  const baseScale = Math.max(cellW / imgW, cellH / imgH);
  const scale = baseScale * zoom;
  const sw = cellW / scale;
  const sh = cellH / scale;
  const sx = Math.max(0, imgW - sw) * panX;
  const sy = Math.max(0, imgH - sh) * panY;
  return { sx, sy, sw, sh };
}
Interactive Crop Calculator (680×1000 Portrait → 800×800 Tile):
sx=0, sy=64, sw=680, sh=680

2. Safe High-DPI Canvas Dimensions Across Browsers

Grid Layout Master Canvas Total Megapixels iOS Safari (<16.77 MP Limit)
3×3 Square (1:1) 2400 × 2400 px 5.76 MP Safe (800×800 per tile)
3×3 Vertical Poster (2:3) 2400 × 3600 px 8.64 MP Safe (800×1200 per tile)
5×5 Album Chart (1:1) 2400 × 2400 px 5.76 MP Safe (480×480 per tile)

3. Preventing Tainted Canvas SecurityErrors

To call canvas.toBlob() on external cover artwork without throwing a DOMException, assign img.crossOrigin = "anonymous" before setting img.src, and ensure the upstream CDN responds with Access-Control-Allow-Origin. You can inspect a full multi-catalog implementation live at Anime3x3.app (including VNDB & Light Novel 3x3 and Video Game 3x3 presets).