2012-07-10 6 views
2

그냥 자바 스크립트로 시작하고 내가 코드는 기본적으로 적어도 그게이 어떻게 생각하고 무엇을 (마우스 위치에 상자를 그립니다자바 스크립트 캔버스

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

오류 "캔버스가 null"을 얻고 null의 경우 ...)

Btw "캔버스"는 캔버스의 ID입니다. 감사합니다.

+0

onDOMready를 사용하셨습니까? – Bergi

답변

3

마지막 행은 다음과 같습니다

setInterval(draw,1000); 

없음 괄호. 그렇지 않으면 함수를 호출하고 PASSING하지 않습니다.

관련 문제