async creatCanvas() {
let that = this
//创建canvas对象
this.canvas = uni.createCanvasContext('myCanvas', this)
//canvas中的插入的图片不能是网络地址只能是下载到本地的
let url = this.bannerList[0]
let imgUrl = await this.urlTofile(url) // 产品图
this.canvas.fillStyle = '#ffffff' // 设置橙色背景
this.canvas.fillRect(0, 0, 440, 1132)
//插入背景图 第2 3 4 5参数单位是px的所以我们要做适配 rpx转换为px 可以自定义方法 也可以使用uniapp中的方法
this.canvas.drawImage(imgUrl, 2, 2, 440, 350)
// uni.getImageInfo({
// src: '../../static/share.jpg',
// success(image) {
// this.canvas.drawImage(image, 230,
// 360,
// 90,
// 90);
// }
// });
// 假设你已有一个 image 对象,并且已经加载了二维码图片
// var image = new Image();
// image.src = '../../static/share.jpg';
// image.onload = function() {
// // 等待图片加载完成后再绘制
// this.canvas.drawImage(image, 230,
// 360,
// 90,
// 90);
// };
// let imgUrl1 = await this.urlTofile('@/static/share.png') // 产品图
// debugger
let image1 = that.urImage
this.canvas.drawImage(
imgUrl,
230,
360,
90,
90
)
//将二维码插入到canvas中
this.canvas.font = '12px Arial'
this.canvas.fillStyle = 'black'
this.canvas.fillText(this.goods.name, 10, 380)
this.canvas.fillStyle = 'red'
this.canvas.fillText(this.goods.price + '元', 15, 410)
//成功之后
this.canvas.draw(
true,
setTimeout(() => {
const that = this // 在setTimeout外面保存this引用
uni.canvasToTempFilePath({
x: 0,
y: 0,
fileType: 'png',
quality: 1,
canvasId: 'myCanvas',
success: function(res) {
// 使用保存的that引用来访问组件实例
that.imgUrl = res.tempFilePath
},
fail: function(err) {
console.error(err)
},
},
this
) // 将this作为第二个参数传递给uni.canvasToTempFilePath
}, 1000)
)
},

2***@qq.com
- 发布:2024-03-18 17:16
- 更新:2024-03-18 18:42
- 阅读:485
2 个回复
JXWang
有报错吗,可否上传一下完整的代码
喜欢技术的前端 - QQ---445849201
使用uni.downloadFile 下载到本地