uniapp 小程序 使用canvas 绘制海报

182 阅读2分钟

微信截图_20240321172101.png

点击下载二维码海报

<template>  
<view class="container">  
<view class="qr-box">  
<view class="img-box">  
<image :src="qrImg" class="qr-img"></image>  
</view>  
</view>  
<button @click="downQRimg" class="btn">  
<image src="@/static/imgs/download.png" style="margin-right:8rpx;width: 48rpx;height: 48rpx;"></image>  
下载邀请码  
</button>  
<canvas v-if="showCanvas" canvas-id="canvas" ref="canvas" style="position:absolute;right:-100%;width: 100%;height: 100vh;"></canvas>  
</view>  
</template>
// import {downLoadQrImg} from "@/utils";  
import QR from '@/common/weapp-qrcode.js'  
  
export default {  
data() {  
return {  
qrImg: '',  
showCanvas: true  
}  
},  
components: {},  
onLoad(options) {  
this.qrImg = QR.drawImg(options.url, {  
typeNumber: 4,  
errorCorrectLevel: 'M',  
size: 500  
})  
},  
computed: {},  
created() {  
},  
mounted() {  
},  
watch: {},  
methods: {  
// 保存二维码  
downQRimg() {  
// downLoadQrImg(this.qrImg)  
this.drawCanvas()  
},  
getBase64Img(src) {  
let base64 = src.replace(/^data:image\/\w+;base64,/, "")  
let filePath = wx.env.USER_DATA_PATH + `/hym_pay123_qrcode.png`  
return new Promise((resolve, reject) => {  
uni.getFileSystemManager().writeFile({  
filePath,  
data: base64,  
encoding: 'base64',  
success: res => {  
resolve(filePath)  
},  
fail: err => {  
reject(err)  
}  
})  
})  
},  
async drawCanvas() {  
this.showCanvas = true  
const ctx = uni.createCanvasContext('canvas', this)  
const {windowWidth, windowHeight} = uni.getSystemInfoSync()  
  
ctx.beginPath()  
ctx.setFillStyle('#F5F5F5')  
ctx.fillRect(0, 0, windowWidth, windowHeight)  
  
ctx.setFillStyle('#fff')  
ctx.fillRect(16, 132, windowWidth - 32, 380)  
  
ctx.setTextAlign('center')  
ctx.setFillStyle('#202020')  
ctx.setFontSize(18)  
ctx.fillText('让门店享受数字化红利', windowWidth / 2, 210)  
  
ctx.setLineDash([5,10])  
ctx.setStrokeStyle('#EDEDED')  
ctx.moveTo(20,238)  
ctx.lineTo(windowWidth - 20, 238)  
ctx.stroke()  
  
const base64 = await this.getBase64Img(this.qrImg)  
ctx.drawImage(base64, (windowWidth - 173) / 2, 260, 173, 173)  
  
ctx.setFontSize(16)  
ctx.fillText('- 微信扫描二维码 -', windowWidth / 2, 480)  
  
ctx.draw(true, () => {  
uni.canvasToTempFilePath({  
canvasId: 'canvas',  
success: res => {  
uni.saveImageToPhotosAlbum({  
filePath: res.tempFilePath,  
success: () => {  
uni.showToast({  
title: '保存成功',  
icon: 'success'  
})  
},  
fail: () => {  
uni.showToast({  
title: '保存失败',  
icon: 'none'  
})  
},  
complete: () => {  
this.showCanvas = false  
}  
})  
},  
fail: err => {  
console.log('绘制失败', err)  
}  
}, this);  
}, this)  
  
}  
}  
}  
</script>
.qr-box {  
background: #fff;  
border-radius: 36rpx;  
margin: 64rpx 32rpx 48rpx;  
padding: 56rpx 0 60rpx;  
text-align: center;  
font-size: 36rpx;  
font-family: PingFang SC-Regular, PingFang SC;  
font-weight: 400;  
color: #202020;  
line-height: 52rpx;  
  
.second {  
position: relative;  
padding-bottom: 56rpx;  
border-bottom: 2rpx dashed #EDEDED;  
}  
  
.second:before {  
content: '';  
position: absolute;  
bottom: -18rpx;  
left: -18rpx;  
width: 36rpx;  
height: 36rpx;  
border-radius: 50%;  
background: #f5f5f5;  
}  
  
.second:after {  
content: '';  
position: absolute;  
bottom: -18rpx;  
right: -18rpx;  
width: 36rpx;  
height: 36rpx;  
border-radius: 50%;  
background: #f5f5f5;  
}  
  
view:last-child {  
font-size: 32rpx;  
margin-top: 32rpx;  
}  
}  
  
.img-box {  
margin: 56rpx auto 0;  
padding: 14rpx 14rpx 0;  
border: 2rpx solid #00000014;  
border-radius: 15rpx;  
display: inline-block;  
  
image {  
width: 300rpx;  
height: 300rpx;  
}  
}  
  
.btn {  
width: calc(100% - 64rpx);  
height: 96rpx;  
line-height: 96rpx;  
background: linear-gradient(270deg, #FF9835 0%, #FF8935 100%);  
font-size: 32rpx;  
font-family: PingFang SC-Regular, PingFang SC;  
font-weight: 400;  
color: #FFFFFF;  
border-radius: 24rpx;  
display: flex;  
align-items: center;  
justify-content: center;  
}  
  
  
</style>