2012-07-10 64 views
2

在JavaScript剛剛開始,我得到錯誤「畫布爲空」JavaScript的畫布是空

function draw() { 

    var canvas = document.getElementById("canvas"); 
    var ctx = canvas.getContext("2d"); 
    var width,height; 

    width = canvas.width; 
    height = canvas.height; 

    ctx.fillStyle = "rgb(0,0,0)"; 
    ctx.fillRect (10, 10, width, height); 
    ctx.fillStyle = "rgb(200,0,0)"; 
    ctx.fillRect (posx, posy, 30, 30); 

} 
var posx; 
var posy; 
function getMouse(e){ 
    posx=0;posy=0; 
    var ev=(!e)?window.event:e;//Moz:IE 
    if (ev.pageX){ 
     posx=ev.pageX;posy=ev.pageY//Mozilla or compatible 
    } 
    else if(ev.clientX){ 
     posx=ev.clientX;posy=ev.clientY//IE or compatible 
    } 
    else{ 
     return false//old browsers 
    } 
     document.getElementById('mydiv').firstChild.data='X='+posx+' Y='+posy; 
} 
setInterval(draw(),1000); 

的代碼基本上是在鼠標的位置畫一個框(至少這就是它該做的...)

btw「canvas」是畫布的id。 感謝!

+0

你用onDOM了嗎? – Bergi 2012-07-10 17:25:34

回答

3

最後一行應該是:

setInterval(draw,1000); 

沒有括號。否則,你正在呼叫該功能,而不是通過它。