SOTER-RESIZER
Tools
Tool
Output Size
Use Case
Resize Algorithm
function drawCoverCrop(
ctx: CanvasRenderingContext2D,
source: ImageBitmap,
targetW: number,
targetH: number
) {
const sw = source.width;
const sh = source.height;
const targetRatio = targetW / targetH;
const sourceRatio = sw / sh;
let sx = 0, sy = 0;
let sWidth = sw, sHeight = sh;
if (sourceRatio > targetRatio) {
// Source too wide → crop left/right
sWidth = Math.round(sh * targetRatio);
sx = Math.round((sw - sWidth) / 2);
} else if (sourceRatio < targetRatio) {
// Source too tall → crop top/bottom
sHeight = Math.round(sw / targetRatio);
sy = Math.round((sh - sHeight) / 2);
}
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = 'high';
ctx.drawImage(source, sx, sy, sWidth, sHeight, 0, 0, targetW, targetH);
}Export
How It Works
Last updated
