2011-12-11 3 views
2

나는 html5에 간단한 줄을 그리고 다음과 같은 문제점을 발견했다.HTML5 Canvas drawing basic line. 그게 버그 야?

아래 코드는 훌륭하게 작동합니다. 제대로

topcanvas = document.getElementById("topborder"); 
topcontext = topcanvas.getContext("2d"); 

topcontext.clearRect(0, 0, topcanvas.width, topcanvas.height); 
    topcontext.moveTo(1, 0); 
    topcontext.lineTo(4,0); 
    topcontext.lineWidth = 3; 
topcontext.strokeStyle = "#ff0000"; 
topcontext.stroke(); 

은 아래 코드는 어떻게해야 선을 그립니다 정확히 동일하지만, 그것은 첫 선을 그립니다 그리고 내가 어떤 변화를 볼 해달라고 이유는 확실하지 않다. 출력은 (4,0)

topcanvas = document.getElementById("topborder"); 
topcontext = topcanvas.getContext("2d"); 

    topcontext.clearRect(0, 0, topcanvas.width, topcanvas.height); 
    topcontext.moveTo(1000, 0); 
    topcontext.lineTo(4,0); 
    topcontext.lineWidth = 3; 
topcontext.strokeStyle = "#ff0000"; 
topcontext.stroke(); 

topcontext.clearRect(0, 0, topcanvas.width, topcanvas.height); 
    topcontext.moveTo(1, 0); 
    topcontext.lineTo(4,0); 
    topcontext.lineWidth = 3; 
topcontext.strokeStyle = "#ff0000"; 
topcontext.stroke(); 

캔버스 폭 1000 및 I에 대한 최종 결과로 예상대로 (4,0) (1,0) 내지 (1000,0)로부터 라인 캔버스를 지우려면 높이 3

+1

나뿐만 아니라 (이 * 어딘가 * =^_^= http://jsfiddle.net에 최소한의 실패 사례를 게시 고려 버그 확신) 다른 사용자가 다른 브라우저에서 문제를보고 수정 된 솔루션을 쉽게 게시 할 수 있도록합니다. –

답변

1

는 다음을 수행하십시오

topcanvas.width=topcanvas.width; 
+0

Brilliant! 그것은 잘 작동합니다. – Shoogle

+0

topcanvas.width가 topcanvas.width와 다른 이유는 무엇입니까? – Shoogle

+2

너비가 지정되어 있다는 사실은 기본 캔버스 구현이 이전에 가지고 있던 모든 픽셀을 잊어 버리게 만듭니다. 캔버스 사양을 참조하십시오 : "캔버스 요소가 만들어지고 width 및 height 특성이 설정 될 때마다 (새 값 또는 이전 값으로 설정 될 때마다) 비트 맵 및 모든 관련 컨텍스트는 초기 상태로 다시 지워야합니다 새로 지정된 좌표 공간 차원으로 다시 초기화하십시오. " –