1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| async addCanvas(item) { this.isShow = true this.foot = 'close' uni.showLoading({ title: '正在生成海报' }) let ctx = wx.createCanvasContext('myCanvas') ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, this.canvasWidth, this.canvasHeight)
if (item.avatar) { ctx.drawImage(await this.getImgUrl(item.avatar), 0, 0, this.canvasWidth, 200) } else { ctx.drawImage(await this.getImgUrl(this.imageList[Math.floor(Math.random() * 8)]), 0, 0, this .canvasWidth, 200) }
ctx.font = `normal 20px ` ctx.fillStyle = '#000' let nameText = this.textSplit(ctx, item.name, this.canvasWidth - 20, 1) nameText.forEach(r => { ctx.fillText(r, 15, 220) })
let lineHeight = 25; if (item.description.includes('\n')) { let flag = 0; let height = 0 const arrDesc = item.description.split("\n"); let lastText = '' outerLoop: for (var i = 0; i < arrDesc.length; i++) { let text = this.textSplit(ctx, arrDesc[i], this.canvasWidth - 30, 7) interLoop: for (var j = 0; j < text.length; j++) { height = flag * lineHeight + 260 flag++; if (flag > 7) break outerLoop;
ctx.font = `${fontSize}px normal` ctx.fillStyle = '#636363' ctx.fillText(text[j], 15, height) lastText = text[j] } } if (!lastText.includes('...')) { ctx.font = `${fontSize}px normal` ctx.fillStyle = '#636363' ctx.fillText('...', 15, height) } } else { let text = this.textSplit(ctx, item.description, this.canvasWidth + 30, 7) let lastText = '' let height = 0 console.log("ctx.measureText(char).width===" + ctx.measureText('还').width); text.forEach((r, index) => { ctx.font = `${fontSize}px normal` ctx.fillStyle = '#636363' ctx.fillText(r, 15, index * lineHeight + 260) console.log(260 + index * lineHeight); height = 260 + index * lineHeight lastText = r }) if (!lastText.includes('...')) { ctx.font = `${fontSize}px normal` ctx.fillStyle = '#636363' ctx.fillText('...', 15, height + 10) } }
ctx.font = `normal 14px ` ctx.fillStyle = '#000' ctx.fillText('活动地点:', 15, 350) let textAdress = this.textSplit(ctx, item.address, this.canvasWidth - 100, 2) textAdress.forEach((r, index) => { ctx.font = `normal 14px ` ctx.fillStyle = '#636363' ctx.fillText(r, 15, 370 + index * lineHeight) })
ctx.font = `normal 14px ` ctx.fillStyle = '#000' ctx.fillText('时间:', 15, 420) ctx.font = `normal 14px ` ctx.fillStyle = '#000' ctx.fillText(item.start_date + " " + item.start_time, 15, 440)
ctx.drawImage(this.img_pop, this.canvasWidth / 2 + this.canvasWidth / 4, 340, 70, 70)
ctx.font = 'normal 12px ' ctx.fillStyle = '#9C9C9C' ctx.fillText('长按识别或保存图片', this.canvasWidth / 3, 470)
await ctx.draw(true, res => { setTimeout(() => { uni.canvasToTempFilePath({ canvasId: 'myCanvas', success: res => { this.poster = res.tempFilePath uni.hideLoading() }, fail: err => { uni.hideLoading() } }, this) }, 1000) }) },
|