当前位置 : 主页 > 网络编程 > JavaScript >

Crop Image

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt /** * cropW, cropH: width and height to be cropped * w, h: width and height of source image */function crop(cropW, cropH, w, h) { let k1 = cropW / w; let k2 = cropH / h; let k = k1 k2 ? k1 : k2; let fitW = Math.ceil(w * k); le
gistfile1.txt
/**
 * cropW, cropH: width and height to be cropped
 * w, h: width and height of source image
 */
function crop(cropW, cropH, w, h) {
    let k1 = cropW / w;
    let k2 = cropH / h;
    let k = k1 > k2 ? k1 : k2;
    let fitW = Math.ceil(w * k);
    let fitH = Math.ceil(h * k);
    let topLeftX = Math.ceil((fitW - cropW) / 2);
    let topLeftY = Math.ceil((fitH - cropH) / 2);
    
    console.info('fit wxh:' + fitW + 'x' + fitH + ' crop from (' + topLeftX + ',' + topLeftY + ')');
}
网友评论