Instructing TypeScript to recognize my ref as a canvas element is crucial for it to allow me to call getContext
.
To achieve this, I utilize the following code:
this.context = (<HTMLCanvasElement>this.$refs.canvas).getContext('2d');
Although TypeScript complies with calling getContext now, I encounter an eslint error:
Module Warning (from ./node_modules/eslint-loader/index.js):
D:\Projects\image-parser\image-parser\src\components\CanvasPanScale.vue
51:32 error Parsing error: '}' expected
✖ 1 problem (1 error, 0 warnings)
This issue actually pertains to the code a few lines below:
this.context = (<HTMLCanvasElement>this.$refs.canvas).getContext('2d');
this.context.imageSmoothingEnabled = false;
if (this.imageToDraw) {
this.drawImageToCanvas(); // <-- ESLint is angry about this closing parentheses
}
This dilemma seems to stem from being inside a .vue file rather than a .ts file, where both html elements and ts types are permitted in the same document, causing confusion due to the angle brackets? Removing the angle brackets resolves the matter.
To resolve the error, I implement the following solution:
const canvas = this.$refs.canvas as HTMLCanvasElement;
this.context = canvas.getContext('2d');
this.context.imageSmoothingEnabled = false;
if (this.imageToDraw) {
this.drawImageToCanvas();
}
However, creating a redundant variable solely for canvas treatment feels unnecessary, and I believe the initial method should not trigger ESLint errors. Does anyone have a workaround for this issue? Or am I overlooking something in my approach?