const PosterCanvas = (arr2, store_name,vipjiage, price,successFn) =>{
wx.showLoading({ title: '海报生成中', mask: true });
const ctx = wx.createCanvasContext('myCanvas');
ctx.clearRect(0, 0, 0, 0);
/**
* 只能获取合法域名下的图片信息,本地调试无法获取
*
*/
wx.getImageInfo({
src: arr2[0],
success: function (res) {
console.log(res);
const WIDTH = res.width;
const HEIGHT = res.height;
ctx.drawImage(arr2[0], 0, 0, WIDTH, HEIGHT);
ctx.drawImage(arr2[1], 0, 100, WIDTH, WIDTH*1.1);
ctx.save();
let r = 50;
let d = r * 2;
let cx = 450;
let cy = 1130;
ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(arr2[2], cx, cy, d, d);
ctx.restore();
const CONTENT_ROW_LENGTH = 40;
let [contentLeng, contentArray, contentRows] = textByteLength(store_name, CONTENT_ROW_LENGTH);
if (contentRows > 2) {
contentRows =2;
let textArray=contentArray.slice(0,2);
textArray[textArray.length-1]+='……';
contentArray = textArray;
}
ctx.setTextAlign('center');
ctx.setFontSize(32);
let contentHh = 32 * 1.5;
for (let m = 0; m < contentArray.length; m++) {
ctx.fillText(contentArray[m], WIDTH / 2, 1010 + contentHh * m);
}
ctx.setTextAlign('left')
ctx.setFontSize(32);
ctx.setFillStyle('red');
ctx.fillText('会员价:', WIDTH/ 12, 1120 + contentHh);
ctx.setTextAlign('left')
ctx.setFontSize(38);
ctx.setFillStyle('red');
ctx.fillText('¥' + vipjiage, WIDTH/ 12+120, 1120 + contentHh);
ctx.setTextAlign('left')
ctx.setFontSize(32);
ctx.setFillStyle('gray');
ctx.fillText('原价:¥' + price, WIDTH/ 12, 1170 + contentHh);
ctx.draw(true, function () {
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
fileType: 'png',
destWidth: WIDTH,
destHeight: HEIGHT,
success: function (res) {
wx.hideLoading();
successFn && successFn(res.tempFilePath);
}
})
});
},
fail:function(){
wx.hideLoading();
Tips({title:'无法获取图片信息'});
}
})
}
微信小程序用canvas做分享海报
最新推荐文章于 2023-05-25 13:52:36 发布