Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas drawImage returns error

I have this piece of code (The sCtx is a canvas context and the button is in a tight draw 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:

<img src=​"img/​btnStart.png">

Uncaught TypeError: Type error

Can someone tell me what I am doing wrong? I have looked at many examples and it seems like this should work.

like image 374
EToreo Avatar asked Mar 05 '26 08:03

EToreo


1 Answers

I believe you need x/y coordinates in order to tell the canvas context where to draw:

sCtx.drawImage(theImg,0,0);
like image 186
ayc Avatar answered Mar 07 '26 22:03

ayc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!