share a function code of canvas's self-defined content in our Mini Program to share pictures
//
drawImg: function (e) {
console.log('draw')
let val = e.currentTarget.dataset.cl
this.getClassTask(val.classId)
this.setData({
shareWork: val
})
const ctx = wx.createCanvasContext('myCanvas')
ctx.drawImage('../../../img/teacher/share_bac.png', 0, 0, 500, 400)
ctx.setFontSize(24)
ctx.setFillStyle('-sharpfff')
ctx.fillText(val.className + '', 40, 60)
ctx.setFontSize(34)
if (val.stepTaskNum === 0 && val.bookTaskNum === 0) {
ctx.fillText('', 40, 125)
} else {
if (val.stepTaskNum > 0) {
ctx.setFontSize(34)
ctx.setFillStyle('-sharpfff')
ctx.fillText('', 80, 125)
ctx.setFillStyle('-sharpffff33')
ctx.setFontSize(38)
ctx.fillText(val.stepTaskNum, 40, 125)
}
if (val.bookTaskNum > 0) {
ctx.setFontSize(34)
ctx.setFillStyle('-sharpfff')
ctx.fillText('', 80, 175)
ctx.setFillStyle('-sharpffff33')
ctx.setFontSize(38)
ctx.fillText(val.bookTaskNum, 40, 175)
}
}
ctx.setFontSize(22)
ctx.setFillStyle('-sharpfff')
ctx.fillText('>>', 40, 260)
ctx.setFontSize(24)
ctx.fillText(this.timeGet(new Date().getTime()), 360, 60)
ctx.draw()
var _this = this
setTimeout(() => {
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: 500,
height: 400,
destWidth: 500,
destHeight: 400,
canvasId: 'myCanvas',
success: function (res) {
_this.setData({
path: res.tempFilePath,
modalShare: true
})
console.log(res.tempFilePath)
}
})
}, 300)
}
you can draw text and pictures on a background
if you still have something you don't understand, you can trust me
~ ps: it's best to open and share the next tick after the picture drawing function is called, otherwise the picture will be empty
.
- draw a picture with canvas to share. two The background generates such a picture to share. In short, the thing and the picture in the lower left corner are the same picture.
Mini Program API only supports title + picture. Pictures can be drawn in custom pictures instead of default screenshots.