App Icon Generator — JavaScript source
Turn one image into the full web app icon set — favicon, apple-touch-icon and manifest sizes as crisp PNGs. Letterboxed contain-fit, optional white background — runs entirely in your browser.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* app-icon-generator — contain-fit, centered export geometry for the
* 16/32/180/192/512 web icon set.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `app-icon-generator` tool.
* Ported from src/tools/AppIconGenerator.tsx.
*
* Why this file exists: the island's interactive half — createImageBitmap
* decode, canvas drawImage, canvas.toBlob PNG encode — is browser-bound and
* stays there. What ports cleanly is the deterministic geometry that decides
* where the source image lands on every square canvas: same inputs, same
* rect, in any language.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
/** The standard size set every web app is asked for (the island's SIZES). */
export const DEFAULT_SIZES = [16, 32, 180, 192, 512];
/**
* One export target: the square canvas edge and the centered contain-fit
* rect the source image is drawn into.
*
* @typedef {Object} IconRect
* @property {number} size Canvas edge (px × px) this rect was fitted into.
* @property {number} x Top-left offset: centers the fitted image.
* @property {number} y
* @property {number} w Fitted image size, at least 1×1.
* @property {number} h
*/
/**
* The deterministic half of the tool: per export size, the contain-fit rect
* (scale = min(px/srcW, px/srcH), clamped to ≥ 1) plus the centered draw
* offset — exactly the geometry the island feeds ctx.drawImage before
* canvas.toBlob PNG-encodes each square. Bitmap decode and PNG encode stay
* with the host.
*
* @param {number} srcW Source image width in px.
* @param {number} srcH Source image height in px.
* @param {number[]} [sizes] Export sizes; defaults to DEFAULT_SIZES.
* @returns {IconRect[]}
*/
export function iconSetPlan(srcW, srcH, sizes = DEFAULT_SIZES) {
return sizes.map((px) => {
const scale = Math.min(px / srcW, px / srcH);
const w = Math.max(1, Math.round(srcW * scale));
const h = Math.max(1, Math.round(srcH * scale));
return {
size: px,
x: Math.round((px - w) / 2), // letterbox on transparency
y: Math.round((px - h) / 2),
w,
h,
};
});
}
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 →