FeDirect/static/image.mts
CenTdemeern1 b408f66f9d
All checks were successful
Build & Test / build-run (push) Successful in 45s
Use hidden properly everywhere
2025-02-12 21:59:35 +01:00

15 lines
No EOL
650 B
TypeScript

export function resize(image: HTMLImageElement, width: number = 16, height: number = 16): string {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.hidden = true;
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
if (ctx === null) throw Error("Resize failed");
const w = Math.min(image.width / image.height, 1) * width;
const h = Math.min(image.height / image.width, 1) * height;
ctx.drawImage(image, (width - w) / 2, (height - h) / 2, w, h);
const url = canvas.toDataURL();
document.body.removeChild(canvas);
return url;
}