js模拟css的object-fit属性,作用于canvas中drawImage的图片处理

图片样式经常会用到的一个css属性object-fit,它可以让图片合理的在容器框内展示,但是很多业务场景,比如canvas绘制时,无法支持图片的这个css属性,所以我们来模拟一个object-fit 基础款

/**
 * 计算图片裁剪或者摆放位置
 * @param {*} type  contain, cover 暂时只兼容这两个模式
 * @param {*} containerWidth  容器宽度
 * @param {*} containerHeight  容器高度
 * @param {*} imgWidth   图片宽度
 * @param {*} imgHeight  图片高度
 * @return {*} canvas drawImage的所有入参
 */
function getObjectFitSize(
    type = "cover",
    containerWidth,
    containerHeight,
    imgWidth,
    imgHeight
) {
    let radio = 1, // 容器与图片的比例
        sx = 0, // 开始剪切的 x 坐标位置。
        sy = 0, // 开始剪切的 y 坐标位置。
        swidth = imgWidth, // 被剪切图像的宽度。
        sheight = imgHeight, // 被剪切图像的高度。
        x = 0, // 在画布上放置图像的 x 坐标位置。
        y = 0, // 在画布上放置图像的 y 坐标位置。
        width = containerWidth, // 要使用的图像的宽度(伸展或缩小图像)。
        height = containerHeight; // 要使用的图像的高度(伸展或缩小图像)。
    let cWHRatio = containerWidth / containerHeight;
    let iWHRatio = imgWidth / imgHeight;
    if (type === "cover") {
        // cover模式,需要裁剪
        if (iWHRatio >= cWHRatio) {
            // 横图,高先匹配,裁剪宽度
            radio = containerHeight / imgHeight;
            sx = (imgWidth - containerWidth / radio) / 2;
            swidth = containerWidth / radio;
            sheight = imgHeight;
        } else {
            // 竖图,宽先匹配,裁剪高度
            radio = containerWidth / imgWidth;
            sy = (imgHeight - containerHeight / radio) / 2;
            swidth = imgWidth;
            sheight = containerHeight / radio;
        }
    } else if (type === "contain") {
        if (iWHRatio >= cWHRatio) {
            // 横图,宽先匹配,高度自适应
            radio = containerWidth / imgWidth;
            y = (containerHeight - imgHeight * radio) / 2;
            height = imgHeight * radio;
        } else {
            // 竖图,高先匹配,宽度自适应
            radio = containerHeight / imgHeight;
            x = (containerWidth - imgWidth * radio) / 2;
            width = imgWidth * radio;
        }
    }
    return {
        sx,
        sy,
        swidth,
        sheight,
        x,
        y,
        width,
        height,
    };
}
// getObjectFitSize返回的参数可以直接作为canvas drawImage方法的入参
// 例子
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const img = document.getElementById("img");
const { sx,sy,swidth,sheight,x,y,width,height } = getObjectFitSize('cover',100,100,10,10)
ctx.drawImage(sx,sy,swidth,sheight,x,y,width,height;

应用案例:canvas处理图片

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值