首页在html设置canvas
<view class="demo">
<canvas :style="{ width: canvasW + 'px',backgroundImage: 'url('+agent_poster+')', height: canvasH + 'px' }" canvas-id="myCanvas" id="myCanvas"></canvas>
</view>
Vue代码 代码可根据具体情况删除,图片可以替换 这个也是借鉴别人,就当做自己的笔记了,运行正常,已测试
<script>
export default {
components: {},
data() {
return {
canvasW:0,
canvasH:0,
SystemInfo:{},
goodsImg: {},
ewmImg:{},
ewmW:120,
title:'COCOSILIYA抖音爆款男女士鎏金沙香水持久自然淡香流沙金香水',
price:'158.00',
Oldprice:'350.00',
name:'天涯过客',
agent_poster:'https://zhibijiaoyu.com/uploads/20220703/ad476b0dd0877cf9feda351cdf79a922.jpg'
}
},
async onLoad() {
this.SystemInfo = await this.getSystemInfo();
this.goodsImg = await this.getImageInfo('https://zhibijiaoyu.com/uploads/20220703/ad476b0dd0877cf9feda351cdf79a922.jpg');
this.ewmImg = await this.getImageInfo('https://img.cdn.aliyun.dcloud.net.cn/guide/uniapp/gh_33446d7f7a26_430.jpg');
this.canvasW = this.SystemInfo.windowWidth;
this.canvasH = this.goodsImg.height + this.ewmW + 50;
console.log(this.canvasW,'canvasW')
console.log(this.canvasH,'canvasH')
if(this.goodsImg.errMsg == 'getImageInfo:ok' && this.ewmImg.errMsg == 'getImageInfo:ok' && this.SystemInfo.errMsg == 'getSystemInfo:ok'){
console.log('ok')
uni.showToast({
icon:'loading',
mask:true,
duration:1000,
title: '海报绘制中',
});
setTimeout(()=>{
var ctx = uni.createCanvasContext('myCanvas', this);
ctx.setFillStyle('#fff');
ctx.fillRect(0, 0, this.canvasW, this.canvasH)
ctx.drawImage(this.goodsImg.path, 0, 0, this.canvasW, this.canvasW)
ctx.drawImage(this.ewmImg.path, 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)=>{
console.log(ret)
uni.showToast({
icon:'success',
mask:true,
title: '绘制完成',
});
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
quality: 1,
complete: function(res) {
console.log(res)
} ,
})
});
},1500)
}else{
console.log('err')
}
},
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)
}
});
})
},
},
}
</script>