uniapp canvas 绘制海报
<template>
<view class="demo">
<canvas :style="{ width: canvasW + 'px', height: canvasH + 'px' }" canvas-id="myCanvas" id="myCanvas01"> </canvas>
<button class="btn" type="primary" v-if="isShow" @click="saveImage">保存图片到相册</button>
</view>
</template>
<script>
export default {
data() {
return {
canvasW:0,
canvasH:0,
SystemInfo:{},
goodsImg: {},
ewmImg:{},
ewmW:120,
title:'Apple/苹果 iPhone 11全新正品国行全网通4G智能手机苹果SE2',
price:'4158.00',
Oldprice:'4999.00',
name:'浪迹天涯',
isShow:false ,
}
},
async onLoad() {
this.SystemInfo = await this.getSystemInfo();
let goodsImgUrl = '/static/bjt.jpg'
let ewmImgUrl = '/static/2wm.jpg'
this.goodsImg = await this.getImageInfo(goodsImgUrl);
this.ewmImg = await this.getImageInfo(ewmImgUrl);
this.canvasW = this.SystemInfo.windowWidth;
this.canvasH = this.SystemInfo.windowHeight - 60;
if(this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this.SystemInfo.errMsg == 'getSystemInfo:ok'){
console.log('读取图片信息成功')
uni.showToast({
icon:'loading',
mask:true,
duration:10000,
title: '海报绘制中',
});
setTimeout(()=>{
var ctx = uni.createCanvasContext('myCanvas', this);
ctx.setFillStyle('#fff');
ctx.fillRect(0, 0, this.canvasW, this.canvasH)
ctx.drawImage(goodsImgUrl, 0, 0, this.canvasW, this.canvasW)
ctx.drawImage(ewmImgUrl, this.canvasW-130, this.canvasW+10, this.ewmW, this.ewmW)
ctx.setFontSize(16);
ctx.setFillStyle('#333');
let _strlineW = 0;
let _strLastIndex = 0;
let _strHeight = this.canvasW +30;
let _num = 1;
for (let i = 0; i < this.title.length; i++) {
_strlineW += ctx.measureText(this.title[i]).width;
if (_strlineW > this.canvasW-155) {
if(_num == 2 && 2){
ctx.fillText(this.title.substring(_strLastIndex, i-5)+'...', 10, _strHeight);
_strlineW = 0;
_strLastIndex = i;
_num++;
break;
}else{
ctx.fillText(this.title.substring(_strLastIndex, i), 10, _strHeight);
_strlineW = 0;
_strHeight += 20;
_strLastIndex = i;
_num++;
}
}else if (i == this.title.length - 1) {
ctx.fillText(this.title.substring(_strLastIndex, i + 1), 10, _strHeight);
_strlineW = 0;
}
}
ctx.setFontSize(16)
ctx.setFillStyle('#e31d1a')
ctx.fillText('¥'+this.price, 10, this.canvasW +75);
ctx.setFontSize(12)
ctx.setFillStyle('#b8b8b8')
ctx.fillText('原价¥'+this.Oldprice, 100, this.canvasW +75);
ctx.setFontSize(16)
ctx.setFillStyle('#333')
ctx.fillText(this.name+'推荐给你', 10, this.canvasW +100);
ctx.setFontSize(14)
ctx.setFillStyle('#b8b8b8')
ctx.fillText('长按或扫描识别二维码', 10, this.canvasW +this.ewmW);
ctx.draw(true,(ret)=>{
this.isShow = true
uni.showToast({
icon:'success',
mask:true,
title: '绘制完成',
});
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
quality: 1,
complete: function(res) {
console.log(res.tempFilePath)
uni.setStorageSync('filePath',res.tempFilePath)
} ,
})
});
},1500)
} else {
console.log('读取图片信息失败')
}
},
methods: {
getImageInfo(image) {
return new Promise((req, rej) => {
uni.getImageInfo({
src: image,
success: function(res) {
req(res)
},
});
})
},
getSystemInfo(){
return new Promise((req, rej) => {
uni.getSystemInfo({
success: function (res) {
req(res)
}
});
})
},
saveImage() {
let filePath = uni.getStorageSync('filePath')
wx.saveImageToPhotosAlbum({
filePath: filePath,
success(res) {
uni.showToast({
icon:'success',
mask:true,
title: '保存到相册了',
});
},
fail(res) {
console.log(res.errMsg)
}
})
}
}
}
</script>
<style>
.btn{
margin-left: 10px;
margin-right: 10px;
margin-bottom: 10px;
width: 94%;
}
</style>
效果图
