Photo Merge — JavaScript source
Combine photos side by side, stacked, or in a grid — uniform cells, adjustable gap, one PNG out. Runs entirely in your browser.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* photo-merge — merge-cell layout for side-by-side, stacked and grid merges.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `photo-merge` tool, ported from
* src/tools/PhotoMerge.tsx.
*
* Why this file exists: the live island is canvas-bound — createImageBitmap
* decodes each photo, drawImage composites it, canvas.toBlob encodes the PNG,
* and the cells are painted over a #0f172a background. Those calls consume
* pure geometry (uniform cell size, per-image fit scale, centered placement);
* this port extracts that geometry so the math is inspectable and testable
* without a browser.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
/**
* Compute the merge layout for a list of image sizes.
*
* Mirrors the deterministic half of merge() in PhotoMerge.tsx:
* - cellW/cellH are the LARGEST input width/height, so no photo is ever
* upscaled away from its native resolution.
* - row stacks n cells horizontally, column stacks them vertically, grid
* folds them into ceil(sqrt(n)) columns; the canvas is cells + (perimeter)
* gaps — cols + 1 horizontal, rows + 1 vertical.
* - each photo is scaled by min(cellW / w, cellH / h) (aspect-preserving,
* letterboxed) and centered inside its cell.
*
* Math.round rounds halves toward +infinity, exactly like the browser.
*
* @param {Array<{width: number, height: number}>} images decoded photo sizes.
* @param {number} gap gutter in pixels (the island's slider, 0-64, default 12).
* @param {'row'|'column'|'grid'} layout merge arrangement.
* @returns {{canvasWidth: number, canvasHeight: number, cols: number,
* rows: number, placements: Array<{col: number, row: number, scale: number,
* x: number, y: number, w: number, h: number}>}}
*/
export function mergeLayout(images, gap = 12, layout = 'row') {
const n = images.length;
const cellW = Math.max(...images.map((i) => i.width));
const cellH = Math.max(...images.map((i) => i.height));
const cols = layout === 'row' ? n : layout === 'column' ? 1 : Math.ceil(Math.sqrt(n));
const rows = layout === 'row' ? 1 : layout === 'column' ? n : Math.ceil(n / cols);
const placements = images.map((img, i) => {
const col = i % cols;
const row = Math.floor(i / cols);
const scale = Math.min(cellW / img.width, cellH / img.height);
const w = Math.round(img.width * scale);
const h = Math.round(img.height * scale);
return {
col,
row,
scale,
w,
h,
x: gap + col * (cellW + gap) + Math.round((cellW - w) / 2),
y: gap + row * (cellH + gap) + Math.round((cellH - h) / 2),
};
});
return {
canvasWidth: cols * cellW + (cols + 1) * gap,
canvasHeight: rows * cellH + (rows + 1) * gap,
cols,
rows,
placements,
};
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →