<view class="masking" hidden="{{!textShow}}" catchtap="masking"></view>
<view class="canvas-container" hidden="{{!textShow}}" catchtap="masking">
<canvas
canvas-id="myCanvas"
class="myCanvas"
style="width: {{canvasWidth}}px; height: {{canvasHeight}}px;">
</canvas>
<view class="btn-save" catchtap="getTempFilePath">保存图片</view>
</view>
.canvas-container {
display: block;
margin: 0 auto;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.myCanvas{
margin-bottom: 50rpx;
}
.btn-save {
width: 400rpx;
height: 80rpx;
line-height: 80rpx;
background-color: #e4393c;
border-radius: 40rpx;
font-size: 36rpx;
color: #ffffff;
text-align: center;
margin: auto;
margin-top: 50rpx;
}
.masking{
position: fixed;
top: 0rpx;
right: 0rpx;
bottom: 0rpx;
left: 0rpx;
background: rgb(0, 0, 0, .5);
}
import '../../core/page_extend.js';
import common from '../../utils/common.js';
Page({
data: {
canvasWidth: 296,
canvasHeight: 426,
card_url: '',
},
onLoad: function (options) {
},
onShow: function () {
},
poster(e){
let that = this;
let { cid,istab } = e.currentTarget.dataset;
console.log(istab)
const { canvasWidth, canvasHeight} = this.data;
const ctx = wx.createCanvasContext("myCanvas", this);
this.setData({
more_index: null
})
common.post('app.php?c=coupon&a=coupon_poster_info', { cid }, (res) => {
if (res.err_code == 0) {
wx.showLoading({
title: '生成海报中...',
})
let {
backurl,
face_money,
is_all_product_text,
name,
receive_code,
store_logo,
store_name,
} = res.err_msg
let piece = 130 - ctx.measureText("¥"+ face_money).width + 10;
Promise.all([
this.getCode(backurl),
this.getCode(store_logo),
this.getCode(receive_code),
]).then(res => {
ctx.drawImage(res[0].tempFilePath, 0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = "#ff3b4b";
ctx.font = "bold 40px Arial";
ctx.textAlign = 'center';
ctx.fillText( '¥'+face_money, 155, 220);
ctx.font = "15px Arial";
ctx.fillStyle = "#ff3b4b";
ctx.textAlign = "center"
ctx.fillText(is_all_product_text, 155, 240);
ctx.drawImage(res[2].tempFilePath, 97, 280, 100, 100);
ctx.font = "18px Arial";
ctx.textAlign = "center";
ctx.fillStyle = "#fff";
ctx.fillText(store_name, 165, 65);
that.circleImg(ctx, res[1].tempFilePath, 50, 30, 25);
ctx.draw();
setTimeout(() => {
wx.hideLoading()
that.setData({
textShow: true
})
}, 1000);
})
}
}, '', (err) => {
console.error(err);
}, 'Post');
},
masking(){
this.setData({
textShow: false
})
},
circleImg: function (ctx, img, x, y, r) {
ctx.save();
var d = 2 * r;
var cx = x + r;
var cy = y + r;
ctx.beginPath();
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(img, x, y, d, d);
},
getTempFilePath: function() {
let that = this
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
success: (res) => {
that.saveImageToPhotosAlbum(res.tempFilePath)
}
})
},
saveImageToPhotosAlbum: function(imgUrl) {
let that = this;
if (imgUrl) {
wx.saveImageToPhotosAlbum({
filePath: imgUrl,
success: (res) => {
wx.showToast({
title: '保存成功',
icon: 'success',
duration: 3000
})
that.masking()
},
fail: (err) => {
console.log(err)
wx.showToast({
title: '保存失败',
icon: 'error',
duration: 3000
})
that.masking()
}
})
} else {
wx.showToast({
title: '绘制中……',
icon: 'loading',
duration: 3000
})
}
},
getCode(img) {
return new Promise((resolve, reject) => {
wx.downloadFile({
url: img,
success: (res) => {
resolve(res)
}
})
})
},
roundedRect(
ctx,
x,
y,
width,
height,
RTradius,
RBradius,
LBradius,
LTradius
) {
ctx.beginPath();
ctx.moveTo(x, y + RTradius);
ctx.lineTo(x, y + height - RTradius);
ctx.quadraticCurveTo(x, y + height, x + RTradius, y + height);
ctx.lineTo(x + width - RBradius, y + height);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width,
y + height - RBradius
);
ctx.lineTo(x + width, y + LBradius);
ctx.quadraticCurveTo(x + width, y, x + width - LBradius, y);
ctx.lineTo(x + LTradius, y);
ctx.quadraticCurveTo(x, y, x, y + LTradius);
ctx.closePath();
},
drawText(ctx, obj) {
ctx.save();
ctx.fillStyle = obj.color;
ctx.setTextBaseline("top");
if (obj.bold) {
ctx.font = `normal bold ${obj.size}px PingFangSC-Regular`;
} else {
ctx.font = `normal 100 ${obj.size}px PingFangSC-Regular`;
}
if (obj.isCenter && obj.width) {
ctx.fillText(
obj.text,
obj.x + (obj.width - ctx.measureText(obj.text).width) / 2,
obj.y
);
} else {
ctx.fillText(obj.text, obj.x, obj.y);
}
ctx.restore();
ctx.closePath();
},
measureText(text, fontSize = 10) {
let width = 0;
String(text)
.split("")
.forEach(item => {
if (/[a-zA-Z]/.test(item)) {
width += 7;
} else if (/[0-9]/.test(item)) {
width += 5.5;
} else if (/\./.test(item)) {
width += 2.7;
} else if (/-/.test(item)) {
width += 3.25;
} else if (/[\u4e00-\u9fa5]/.test(item)) {
width += 10;
} else if (/\(|\)/.test(item)) {
width += 3.73;
} else if (/\s/.test(item)) {
width += 2.5;
} else if (/%/.test(item)) {
width += 8;
} else {
width += 10;
}
});
return (width * fontSize) / 10;
},
lineation(ctx, obj) {
ctx.beginPath();
ctx.setLineWidth(1);
ctx.moveTo(obj.sx, obj.sy);
ctx.lineTo(obj.ex, obj.ey);
ctx.strokeStyle = obj.color;
ctx.stroke();
ctx.closePath();
}
})