Canvas drawImage returns an error

I have this piece of code (sCtx is the canvas context and the button is in a tight loop):

function Button(src, onClick)
{
   this.loaded = false;
   this.image = new Image();
   this.image.src = src;

   this.onClick = onClick;
}

Button.prototype.draw = function()
{
   if(!this.image.complete)
      return;

   var theImg = this.image;

   console.log(theImg);
   sCtx.drawImage(theImg);
}

When I run the code (in Chrome), I get this output:

btnStart.png

Dug TypeError: Type Error

Can someone tell me what I'm doing wrong? I looked at a lot of examples, and it looks like this should work.

+3
source share
1 answer

I believe that you need x / y coordinates to indicate the canvas context where to draw:

sCtx.drawImage(theImg,0,0);
+5
source

Source: https://habr.com/ru/post/1774601/


All Articles