15 lines
No EOL
659 B
TypeScript
15 lines
No EOL
659 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.style.display = "none";
|
|
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;
|
|
} |