我有这个代码:
convertToDataURLviaCanvas(url, outputFormat){
return new Promise( (resolve, reject) => {
let img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
let canvas: any = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
//callback(dataURL);
canvas = null;
resolve(dataURL);
};
img.src = url;
});
}
问题是 this.height 和 this.width 在 Visual Studio 代码中出现下划线,错误说:La propiedad 'width' no existinge en el tipo 'HTMLElement'。代码有效,但我有这个烦人的红色下划线,有没有办法通过代码删除它们,也许在画布中放置一些与任何类型不同的类型。
错误在那里是因为不能保证分配的方法将与它在其中定义的范围具有相同的上下文。this
理论上可以是任何东西。
您可以使用img.width
和img.height
代替来解决此问题this
。这会导致img
被分配给 的方法捕获onload
,并且将始终引用该对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句