2012-10-16 2 views
0

텍스트 파일을 만들면 픽셀로 렌더링된다는 생각이 들었습니다. 첫 번째 테스트가 성공합니다. 나는 그것에 대해 하나의 픽셀을 가졌다. 이제 더 많은 픽셀을 넣으면 작동하지 않습니다. 여기 코드는 다음과 같습니다while JavaScript의 charAt

여기
xhr=new XMLHttpRequest(); 
xhr.open("GET", "graphtest.prg", false); 
xhr.send(); 
document.write(xhr.responseText); 
var file=xhr.responseText; 
var c=0; 
var bad=false; 
var val=0; 
var rgbs="rgb("; 
while(c<file.length && bad==false) { 
    //alert(file.charAt(c)); 
    cc=file.charAt(c); 
    if (cc=="P" || cc=="R" || cc=="G") { 
     // magic 
    } 
    else { 
     if (cc=="{") { 
      if (val!=5) { 
       var newelem=document.createElement("span"); 
       newelem.innerHTML="&nbsp;"; 
       /*newelem.style.width="1px"; 
       newelem.style.height="1px"; 
       newelem.position="absolute";*/ 
       newelem.className="graf"; 
      } 
     } 
     else if (cc=="'") { 
      c++; 
      cc=file.charAt(c); 
      switch (val) { 
       case 0: 
        //alert("RUN"); 
        var num=""+cc.toString(); 
        c++; 
        while(!isNaN(file.charAt(c))) { 
         num+=file.charAt(c).toString(); 
         c++; 
        } 
        newelem.style.left=num+"px"; 
        //alert(num); 
        val++; 
        c++; 
        continue; 
        break; 
       case 1: 
        var num=""+cc.toString(); 
        c++; 
        while(!isNaN(file.charAt(c))) { 
         num+=file.charAt(c).toString(); 
         c++; 
        } 
        newelem.style.top=num+"px"; 
        //alert(num); 
        val++; 
        c++; 
        continue; 
        break; 
       case 2: 
        var num=""+cc.toString(); 
        c++; 
        while(!isNaN(file.charAt(c))) { 
         num+=file.charAt(c).toString(); 
         c++; 
        } 
        rgbs+=num+","; 
        //alert(num); 
        val++; 
        c++; 
        continue; 
        break; 
       case 3: 
        var num=""+cc.toString(); 
        c++; 
        while(!isNaN(file.charAt(c))) { 
         num+=file.charAt(c).toString(); 
         c++; 
        } 
        rgbs+=num+","; 
        //alert(num); 
        val++; 
        c++; 
        continue; 
        break; 
       case 4: 
        var num=""+cc.toString(); 
        c++; 
        while(!isNaN(file.charAt(c))) { 
         num+=file.charAt(c).toString(); 
         c++; 
        } 
        rgbs+=num+")"; 
        //alert(num); 
        val++; 
        c++; 
        continue; 
        break; 
       case 5: 
        newelem.style.backgroundColor=rgbs; 
        document.body.appendChild(newelem); 
        val=0; 
        rgbs=""; 
        newelem=null; 
        cc=""; 
        num=""; 
        break; 
      } 
     } 
     if (val==5) { 
      newelem.style.backgroundColor=rgbs; 
      document.body.appendChild(newelem); 
      val=0; 
      rgbs=""; 
      //newelem=null; 
      //c; 
      alert(file.charAt(c)); 
      cc=""; 
      num=""; 
     } 
    } 
    c++; 
} 

는 "그래픽 텍스트"파일입니다

PRG{'100','200','0','255','0'}{'200','100','0','0','255'} 

첫 번째 픽셀이 성공적으로 렌더링되지만, 제 2 픽셀은 배경색이 없습니다. 파싱 ​​문제인가요?

+0

음, 파서가 다소 혼란 스럽습니다. 0, 1, 2, 3, 4의 경우 똑같은 일을하지 않습니까? – Bergi

+0

그래픽 표시를 위해 절대 위치 스팬 대신 ''을 사용해야합니다. – Bergi

+0

'number'표현에 문자열 대신 숫자를 사용해야하므로 일부 메모리를 절약하십시오. – Ridcully

답변

2

반복 코드가 많아서 파서가 혼란 스럽습니다. 모든 스위치 케이스에서 동일한 코드를 반복하므로 필요하지 않습니다. 보다

다른 내가 발견 한 것은 당신의 rgbs VAR이 스크립트의 시작 부분에 var rgbs="rgb(";로 초기화됩니다,하지만 픽셀 구문 분석의 끝에서 rgbs="";로 다시 초기화된다는 점이다.

+0

감사! 그것이 문제였습니다 : D – gskartwii