/**
* 生成名片
* @param array arr2 卡片素材
* @param array strs 素材文字 数组
* @param function successFn 回调函数
*
*
*/
PosterCanvasCard: function(arr2, strs, successFn, errFun) {
let that = this;
const ctx = uni.createCanvasContext('myCanvas');
ctx.clearRect(0, 0, 0, 0);
/**
* 只能获取合法域名下的图片信息,本地调试无法获取
*
*/
uni.getImageInfo({
src: arr2[0],
success: function(res) {
// const WIDTH = res.width*1.1;
// const HEIGHT = res.height*1.125;
// let r = 180;
// let d = r * 2;
// let cx = 190;
// let cy = 450;
// ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
console.log(arr2)
let imgIconnum = 1
let duber = 1;
let fontNum = 1.5;
let cardNum = 1
let bgBase64 = `data:image/png;base64,*****`; // base64格式的图片
let logBase = `data:image/png;base64,***2`; // logo 区域图
ctx.drawImage(bgBase64, 0, 0, 355*duber + 35, 260*duber + 10); // 背景图
ctx.setFillStyle("#FFFFFF");
ctx.drawImage(logBase, 10*duber, 10*duber, 300*imgIconnum, 35*duber);
ctx.save();
ctx.restore();
const CONTENT_ROW_LENGTH = 40;
let [contentLeng, contentArray, contentRows] = that.textByteLength(strs[0], 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(24*fontNum);
ctx.setFillStyle('#FFFFFF');
let contentHh = 24 * fontNum * 1.3;
for (let m = 0; m < contentArray.length; m++) {
ctx.fillText(contentArray[m], 50*fontNum, 72*fontNum + contentHh * m);
}
ctx.setTextAlign('left')
let nameW = 31* fontNum *strs[0].length;
console.log(nameW);
ctx.setFillStyle('#FFFFFF');
ctx.setFontSize(10*fontNum);
if (strs[1].length == 1) {
ctx.fillText(strs[1][0], nameW, 28*fontNum + contentHh );
}else if(strs[1].length == 2) {
ctx.fillText(strs[1][0], nameW, 28*fontNum + contentHh );
ctx.fillText(strs[1][1], nameW, 40*fontNum + contentHh);
}
ctx.setFontSize(12*fontNum);
ctx.fillText(strs[2], 30*duber, 75*fontNum + contentHh );
ctx.fillText(strs[3], 30*duber, 90*fontNum + contentHh);
ctx.fillText(strs[4], 30*duber, 105*fontNum + contentHh);
ctx.fillText(strs[5], 30*duber, 120*fontNum + contentHh);
ctx.draw(true, function() {
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
fileType: 'png',
destWidth: 1420*cardNum,
destHeight: 1040*cardNum,
success: function(res) {
uni.hideLoading();
successFn && successFn(res.tempFilePath);
},
fail: function(err) {
uni.hideLoading();
errFun && errFun(err);
}
})
});
},
fail: function(err) {
uni.hideLoading();
that.Tips({
title: '无法获取图片信息'
});
errFun && errFun(err);
}
})
},
1、 背景图片或者添加的图片过大 可选择用base64格式
2、文字格式 : [
"姓名",
"['职位', '职位二、职位3']",
'phone',
'email',
'address'
'webUrl'
]
一定要注意 分享图片的内存 如果大于1M 就会失败 很多手机出现这种情况 就是说 这个图片上传时需要进行压缩图片