2012-01-08 4 views
3

html5 캔버스에 대한 답변을 찾을 수 없습니다. 원점 좌표를 변경하여 내 그림이 오른쪽으로 15px 이동하도록 할 수 있습니까 (예를 들어). 다음 HTML과 CSS를 감안할 때?html5 canvas의 원점을 변경하는 방법은 무엇입니까?

<canvas id="g" width="auto" height="auto">Your browser doesnt support canvas tag</canvas> 

CSS : 당신이 translate transform이 필요한 것처럼

canvas, section { 
    display: block; 
}  

#g { 
    position: absolute; 
    width:100%; 
    height:100%; 
    overflow:hidden; 
} 
+0

은 전체 컨텍스트에 옵션을 다시 그립니다. 아니면 CSS로 캔버스 요소를 옮기고 싶습니까? – arahaya

+0

@arahaya 다시 그려주는 것이 굉장 할 것입니다! – Jurudocs

답변

11

는 소리. 문맥을 가지고 무엇을하고 있는지에 따라 save() and restore()으로 전화해야 할 수도 있습니다.

var ctx = document.getElementById('myCanvas').getContext('2d'); 
ctx.save(); 
ctx.translate(15, 0); 
// ... do stuff with the transformed origin 
ctx.restore(); 
// ... do stuff with the canvas restored to its original state (if necessary) 
+0

필요한 경우 모든 변경 사항을 '변환'하는 대신'resetTransform()'을 호출하여 원점을 재설정 할 수 있습니다. 자세한 내용은 [MDN] (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/resetTransform)을 참조하십시오. – brichins

관련 문제