2010-08-20 15 views
1

나는 이상한 문제에 시달리고있다 : 코드는 캔버스 요소에 액세스 할 수 없다. 다음 코드를 고려하십시오.html5 캔버스에 액세스 할 수 없습니까?

Firefox는 오류 메시지가 this.canvas is null이됩니다. 하지만 이렇게 쓰는다면 :

this.canvas = $('#canvas'); 
this.context2D = this.canvas.getContext('2d'); 

파이어 폭스는 getContext is not a method이라고 말할 것입니다. 나는 this.canvas을 들여다 보았고 익숙지 않은 개체 (그것이 어디에서 왔는지 알 수는 없지만 캔버스는 아님)를 봅니다.

그리고 파이어 폭스에만 국한되는 것은 아니며 크롬도 동일한 결과를 산출합니다. 나는 이것에 미치고있다.


편집 : 전체 HTML은 여기

<!DOCTYPE HTML> 
<html lang="en"> 
<head> 
    <title>Background test</title> 
    <script type="text/javascript" src="Scripts/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="Scripts/soundmanager2.js"></script> 
    <script type="text/javascript" src="Scripts/base.js"></script> 
    <script type="text/javascript" src="Scripts/Resources.js"></script> 
    <script type="text/javascript" src="Scripts/Actor.js"></script> 
    <script type="text/javascript" src="Scripts/Commons.js"></script> 
    <script type="text/javascript" src="Scripts/Graphics.js"></script> 
    <script type="text/javascript" src="Scripts/Utils.js"></script> 
    <script type="text/javascript"> 
     window.onload = function(){ 
      new Commons().startupCommons(); 
      new Graphics().startupGraphics(); 
     } 
    </script> 
    <style type="text/css"> 
     body { font-family: Arial,Helvetica,sans-serif;} 
     canvas {border-style:solid; border-width:5px; border-color:green;} 
    </style> 
</head> 
<body> 
    <canvas id="visualcanvas" width="800" height="600"> 
     <p> 
      Your browser does not support the canvas element. 
     </p> 
    </canvas> 
</body> 
</html> 

난 그냥 창로드를 추가하지만 문제는 지속. 이전 코드의 this은 Graphics 객체를 참조합니다.이 객체는 window.onload이 호출 될 때 호출됩니다.

+0

HTML을 볼 수 있습니까? –

+1

'이'란 무엇입니까? - –

+0

어디에서 코드를 업로드 할 수 있습니까? – Khoi

답변

4

$ 란 무엇입니까? Prototype.js입니까? jQuery라고 가정합니다.

jQuery(someselector)을 호출하면 jQuery 객체가 생성됩니다. 선택기와 일치하는 요소가 없으면 항목이없는 jQuery 객체가됩니다.

어떤 방법 으로든 캔버스 개체로 처리 할 수 ​​없습니다.

JS의 행이 실행될 때 DOM에 'canvas'라는 ID가있는 요소가 없으므로 첫 번째 코드 세트가 작동하지 않습니다. id가 'canvas'인 요소 앞에 표시되는 스크립트 요소에서 JS를 실행하고 (문서 준비 이벤트를 통해 호출하는 등의 실행을 지연시키지 않은 경우) 또는 사용자가 id 속성과 태그 이름을 혼란스럽게합니다.

+0

첫 번째 부분은 확실히 사실입니다. 두 번째 부분은 아마도 사실입니다. –

+0

html 안에 canvas 요소가 있습니다. 사실, 그것은 완벽하게 작동합니다. 그러나 자바 스크립트를 변경 한 후에 이상하게 행동하기 시작합니다. html을 변경하지 않았으므로 요소가 여전히 있습니다. – Khoi

+1

요소는 'canvas'id를 가진 요소를 찾고있는 'visualcanvas'id를가집니다. "id 속성과 태그 이름을 혼동합니다."라는 질문에서 (또는 실제로 작성한 코드와 일치하지 않는 코드를 표시하고 있습니다.) – Quentin

관련 문제