Home > Software engineering >  Property 'clearRect' does not exist on type 'HTMLCanvasElement'
Property 'clearRect' does not exist on type 'HTMLCanvasElement'

Time:10-16

canvas has type HTMLCanvasElement | null

when i call clearRect method in canvas then i get this error:

Property 'clearRect' does not exist on type 'HTMLCanvasElement'.

this.canvas?.clearRect()

I dont know how solve this problem. Please help me.

CodePudding user response:

Because it doesn't exists on HTMLCanvasElement.

It exists on CanvasRenderingContext2D

if (this.canvas) {
  const ctx = this.canvas.getContext('2d');
  ctx.clearRect();
}
  • Related