手机适应屏幕简单图片处理算法

微信画布绘制技巧
本文介绍了一种使用微信小程序画布API实现图片自适应绘制的方法。通过计算不同屏幕尺寸下的图片缩放比例,确保图片在各种设备上都能正确显示且不失真。文中详细解释了如何根据不同情况调整绘制宽度和高度,并利用偏移量来居中显示图片。
doDraw: function (path, height, width) {
let that = this;
var ctx = wx.createCanvasContext('firstCanvas')

 

let scale = width / height;
let drawWidth = 0; // 绘制图片的宽度
let drawHeight = 0; // 绘制图片的高度
// 假设高度确定
if (scale >= (360/ 640)) {
// 宽度过大:高度设置为1280
drawHeight = 640;
drawWidth = scale * 640;
let left = (drawWidth - 360) / 2; // 左边偏移量
ctx.drawImage(path, -left, 0, drawWidth, drawHeight);
} else {
// 宽度过小:宽度设置为720
drawWidth = 360;
drawHeight = 360 / scale;
let top = (drawHeight - 640) / 2; // 顶部偏移量
ctx.drawImage(path, 0, -top, drawWidth, drawHeight);
}
//模板

 

// 绘制前景图
ctx.drawImage(that.data.path, 72 / 2, 128 / 2, 576 / 2, 1024 / 2);

 

ctx.draw();

 

setTimeout(function () {
that.doCanvas();
}, 300)
},

转载于:https://www.cnblogs.com/xinhang/p/7780242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值