"canvasToTempFilePath:fail Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas or VideoFrame)'"
提供的值不是类型”(CSSImageValue或HTMLImageElement或SVGImageElement或HTMLVideoElement或HTMLCanvasElement或ImageBitmap或OffsetCanvas或VideoFrame)
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
有可能是在绘制 Canvas 时使用了错误的参数类型,例如将一个字符串或数字传入作为绘制对象,而不是 Canvas 对象本身。需要仔细检查代码并排除这些问题。
async drawImage() {
let self = this;
//这里是重点 新版本的type 2d 获取方法
const query = wx.createSelectorQuery();
const canvasObj = await new Promise((resolve, reject) => {
query
.select("#" + this.id)
.fields({ node: true, size: true })
.exec(async (res) => {
resolve(res[0].node);
});
});
console.log(canvasObj);
wx.canvasToTempFilePath(
{
//fileType: 'jpg',
//canvasId: 'posterCanvas', //之前的写法
canvas: canvasObj, //现在的写法 Object类型
success: (res) => {
console.log(res, '图片=========》');
// self.setData({ canClose: true });
//保存图片
},
fail(res) {
console.log(res);
},
},
this
);
},
贴代码。
方法如下:
const query = wx.createSelectorQuery();
const canvasObj = await new Promise((resolve, reject) => {
query
.select("#" + this.id)
.fields({ node: true, size: true })
.exec(async (res) => {
resolve(res[0].node);
});
});
可以参考下这篇文章:https://developers.weixin.qq.com/community/develop/article/doc/000242073903a04e082ab595b52013