api-typings icon indicating copy to clipboard operation
api-typings copied to clipboard

ctx.drawImage 方法与新版 canvas api 不一致

Open hcl2020 opened this issue 5 years ago • 4 comments

Canvas 实例通过 SelectorQuery 获取, ctx.drawImage方法的 imageResource 实际不支持字符串类型。

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)'

hcl2020 avatar Mar 30 '20 07:03 hcl2020

定义里提供的 drawImage 接口和获取路径还是旧版的,通过 wx.createCanvasContext 获取的;新版的通过 getContext 获取的暂时还没有支持,现在应该返回的是 any。实际使用的时候有遇到问题吗?

SgLy avatar Mar 30 '20 09:03 SgLy

是的,目前返回的any。 目前在使用时需要手动指定类型

let canvas = this.getCanvas() as HTMLCanvasElement & WechatMiniprogram.Canvas;
const ctx = canvas.getContext('2d')!;
const img = canvas.createImage() as CanvasImageSource;
ctx.drawImage(img, 0, 0, 100, 100);

目前官方文档已经不推荐使用 wx.createCanvasContext 接口,建议 getContext 相关的类型能够完善一下。

hcl2020 avatar Mar 30 '20 11:03 hcl2020

正在补了,wechat-miniprogram/minigame-api-typings#3 里也有跟踪

SgLy avatar Apr 03 '20 06:04 SgLy

小程序已经原生支持ts了,canvas类型有很多问题,请问有什么进展吗。

KuuBee avatar May 09 '22 08:05 KuuBee