通过canvas来绘制截取部分,然后保存
html部分
<canvas :style="{ width: canvasW + 'px', height: canvasH + 'px' }"
canvas-id="myCanvas"></canvas>
<view class="button" @click="save_temp"><text class="cuIcon-down"></text>保存到相册</view>
js部分
data部分:
canvasW:'',
canvasH:'',
onload部分:
this.canvasH = uni.getSystemInfoSync().screenHeight
this.canvasW = uni.getSystemInfoSync().screenWidth
methods部分:
getPersentageH(p){
p = p /100
let h = uni.getSystemInfoSync().screenHeight
return parseInt(h * p)
},
draw(){
let ctx = uni.createCanvasContext('myCanvas');
this.path 根据项目中使用到的图片进行赋值
ctx.drawImage(this.path,0,0,this.canvasW,this.canvasH);
ctx.setFillStyle('#F76260')
ctx.font=`${uni.upx2px(30)}px Arial`;
ctx.setTextAlign('center')
ctx.fillText('十五个字十五个字十五个字十五个', this.canvasW/2, this.getPersentageH(61))
ctx.setFillStyle('#FBBD08')
ctx.save()
ctx.beginPath()
ctx.arc(uni.upx2px(377), this.getPersentageH(54), uni.upx2px(61), 0, 2 * Math.PI)
ctx.clip()
ctx.fill()
ctx.drawImage(this.path1, uni.upx2px(315), this.getPersentageH(50.3),uni.upx2px(122),uni.upx2px(122))
ctx.restore()
ctx.drawImage(this.path, uni.upx2px(274), this.getPersentageH(67),uni.upx2px(204),uni.upx2px(204));
ctx.draw()
}
保存到相册:
save_temp(){
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
success: res => {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
complete: msg => console.log(msg)
})
},
fail: err => console.log(err)
})
}