uniapp项目的结构
<canvas style="width: 750rpx; height: 1100rpx;" canvas-id="firstCanvas" id="firstCanvas"></canvas>
<view>
<u-button class="custom-style" @click="saveImg">保存</u-button>
</view>
uniap项目js部分
onLoad(params) {
this.classData.class_id = params.classId
this.classData.school_id = params.schoolId
if (params.classId && params.schoolId) {
let classId = {
class_id: params.classId,
school_id: params.schoolId,
type: 1
}
getClassQRcodeApi(classId).then(res => {
if (res.status == 200) {
this.codeImg = res.data.qr_url
this.school_name = res.data.school_name;
this.classes_name = res.data.classes_name
const dialogRect = {
width: 750,
height: 750
}
let canvasCtx = uni.createCanvasContext('firstCanvas', this);
canvasCtx.clearRect(0, 0, dialogRect.width, dialogRect.height)
canvasCtx.setFillStyle('#f3af1e')
canvasCtx.fillRect(0, 0, dialogRect.width, dialogRect.height)
canvasCtx.setFillStyle('#fff')
canvasCtx.setFontSize(18)
canvasCtx.setTextAlign('center')
canvasCtx.fillText(this.school_name, 180, 40)
canvasCtx.fillText(this.classes_name, 180, 80)
canvasCtx.drawImage(this.codeImg, 60, 120, 264, 270)
canvasCtx.drawImage('/static/img/codeImg.jpg', 0, 420, 375, 120)
canvasCtx.draw(true);
uni.canvasToTempFilePath({
canvasId: 'firstCanvas',
success: (res) => {
const tempFilePath = res.tempFilePath;
if (uni.getSystemInfoSync().platform === 'ios' || uni.getSystemInfoSync().platform === 'android') {
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
uni.showToast({
title: '图片已保存',
icon: 'success'
});
},
fail: (err) => {
uni.showToast({
title: '保存图片失败',
icon: 'none'
});
}
});
} else if (uni.getSystemInfoSync().platform === 'h5') {
const a = document.createElement('a');
a.href = tempFilePath;
a.download = 'poster.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
uni.showToast({
title: '图片已下载',
icon: 'success'
});
}
},
fail: (err) => {
uni.showToast({
title: '生成图片失败',
icon: 'none'
});
}
});
}
})
}
},
进来就加载 canvas ,回显到页面,然后点击保存按钮到手机相册上,现在遇到的问题浏览器切换h5页面,在切换 IOS 手机屏幕,或者到其他屏幕,canvasCtx.drawImage('/static/img/codeImg.jpg', 0, 420, 375, 120)跟canvasCtx.setFillStyle('#f3af1e')
没有铺满,请问大佬们,如何修改这段代码,还有点击保存海报图片,用了 canvasCtx.draw(true);
在真机调试环境下切换到这个页面会自动保存海报图片,用户点击保存才存到手机相册里面,而不是自动保存