Most online photo calendar builders follow the same heavy architecture: upload twelve 8 MB family photos to cloud object storage, enqueue a server-side rendering job, wait for an email link, and hope your private photos actually get purged after thirty days.
That server round-trip is completely unnecessary. Modern browsers have enough local compute to decode high-resolution JPEGs, compute exact object-fit: cover crops onto 300 DPI print plates, assemble rolling twelve-month grids, and emit a print-ready PDF blob entirely inside browser RAM—with zero family-photo uploads.
The hard engineering constraint is not CPU speed. It is GPU and backing-store memory management, especially on mobile Safari.
Preventing Mobile Safari GPU OOM Across 12 Print Pages
At 300 DPI, a single DIN A4 page measures 2480 x 3508 px (and US Letter measures 2550 x 3300 px). An uncompressed RGBA <canvas> backing store of that size occupies roughly 34.8 MB in memory.
If a naive React or Vue app mounts twelve preview canvases simultaneously—or keeps twelve offscreen canvases alive while assembling a 12-page calendar—total uncompressed pixel buffers blow past 415 MB before you even count the decoded source camera photos. On iOS Safari, where per-tab canvas memory limits are notoriously strict, the WebKit process gets killed silently by the OS.
The architectural fix is a single-offscreen-canvas sequential pipeline:
- Allocate exactly one offscreen canvas sized to the target 300 DPI paper box.
- For each month
0..11, decode only that month's user photo withcreateImageBitmap(file)(which automatically honors smartphone EXIF orientation). - Paint the cropped photo and calendar grid onto the shared canvas.
- Immediately encode that frame via
canvas.toBlob(cb, 'image/jpeg', 0.92)into a compact ~400–800 KB compressedUint8Array. - Call
bitmap.close()to free the decoded photo texture immediately, then move to the next month. - After month 12 finishes, shrink
canvas.width = 0; canvas.height = 0;to release the GPU surface before concatenating the final PDF byte stream.
async function encodeMonthSequentially(months, paperPx, dpi) {
const canvas = document.createElement('canvas');
canvas.width = paperPx.width;
canvas.height = paperPx.height;
const ctx = canvas.getContext('2d', { alpha: false });
const encodedPages = [];
for (const month of months) {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (month.photoFile) {
const bitmap = await createImageBitmap(month.photoFile);
drawCoverImage(ctx, bitmap, month.photoBoxPx, month.focalPoint);
bitmap.close(); // Release decoded camera bitmap immediately
}
drawCalendarGrid(ctx, month.year, month.monthIndex, paperPx, dpi);
const blob = await new Promise((res) =>
canvas.toBlob(res, 'image/jpeg', 0.92)
);
encodedPages.push(new Uint8Array(await blob.arrayBuffer()));
}
canvas.width = 0;
canvas.height = 0;
return encodedPages;
}
Peak pixel memory stays bounded to one canvas plus one decoded photo—under ~60 MB instead of half a gigabyte.
Exact object-fit: cover Cropping Math on 300 DPI Canvas
HTML5 Canvas drawImage() stretches images unless you calculate source crop coordinates manually. To replicate CSS object-fit: cover with an optional focal-point bias (focalX, focalY in [0, 1], defaulting to 0.5 center), compare source and destination aspect ratios:
function drawCoverImage(ctx, img, dest, focal = { x: 0.5, y: 0.5 }) {
const srcRatio = img.width / img.height;
const dstRatio = dest.w / dest.h;
let sx = 0, sy = 0, sw = img.width, sh = img.height;
if (srcRatio > dstRatio) {
// Source is wider than slot: crop horizontally
sw = img.height * dstRatio;
sx = (img.width - sw) * focal.x;
} else {
// Source is taller than slot: crop vertically
sh = img.width / dstRatio;
sy = (img.height - sh) * focal.y;
}
ctx.save();
ctx.beginPath();
ctx.rect(dest.x, dest.y, dest.w, dest.h);
ctx.clip();
ctx.drawImage(img, sx, sy, sw, sh, dest.x, dest.y, dest.w, dest.h);
ctx.restore();
}
Evaluating this math directly at 300 DPI coordinates ensures portraits and landscapes fill the top photo half cleanly without soft upscaling artifacts or squashed faces.
Supporting Rolling 12-Month Academic and Fiscal Years
A calendar engine hard-coded to Jan..Dec breaks for two huge real-world workflows:
- US Academic Year: August 2026 – July 2027 (matching K–12 and university terms).
- Japan Fiscal & School Year: April 2027 – March 2028 (matching Japanese corporate budgets and school calendars).
Model every calendar book as a rolling sequence generated from (startYear, startMonth) rather than a single integer year:
function buildRollingMonths(startYear, startMonth1Based, count = 12) {
return Array.from({ length: count }, (_, offset) => {
const zeroBased = (startMonth1Based - 1) + offset;
return {
year: startYear + Math.floor(zeroBased / 12),
monthIndex: zeroBased % 12, // 0 = Jan, 11 = Dec
};
});
}
Print Margins and Binder Punch Allowances
Before computing the photo frame height and the calendar cell rows, subtract a 12 mm safe print margin on all four edges so desktop printers never clip outer rules. When users plan to spiral-bind or three-hole-punch the finished sheets, reserve an optional +10 mm binder punch margin (22 mm total on the bound edge—top for hanging wall calendars, left for ring binders).
This exact local-first architecture drives the photo calendar preset, the rolling August–July academic calendar layout, and the Japanese April-start fiscal calendar maker. Every crop, date grid, and 300 DPI JPEG plate stays strictly in browser memory—keeping private family photos off remote servers while surviving mobile Safari's memory ceiling.
Top comments (0)