2010-11-12 81 views
3

我有這樣一段代碼(該sCtx是畫布背景和按鈕是在緊張的平局循環):畫布drawImage返回錯誤

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); 
} 

當我運行的代碼(在Chrome中)我得到這個輸出:

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

Uncaught TypeError: Type error

有人能告訴我我做錯了什麼嗎?我已經看了很多例子,看起來這應該起作用。

+0

我有同樣的問題。儘管添加了x,y座標,但錯誤仍然存​​在。我該怎麼做 – 2011-08-14 07:28:05

回答

5

我相信你需要x/y座標,以告訴畫布背景繪製位置:

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

現在我覺得很愚蠢。謝謝您的幫助。 :) – EToreo 2010-11-12 21:07:30