위의 답변은 이전 답변에 대한 일종의 내용이지만 약간 (희망 사항)을 추가합니다.
주로 내가 분명히 밝히고 싶은 것은 대개 draw a rectangle at 10, 3
과 같은 그림을 그리는 것입니다.
이렇게 생각하면 다음과 같습니다. move origin to 10, 3
, draw rectangle at 0, 0
. 그 다음에 회전을 추가하면됩니다.
또 다른 중요한 점은 텍스트 정렬입니다. 0, 0에 텍스트를 그리는 것이 가장 쉽습니다. 올바른 맞춤을 사용하면 텍스트 너비를 측정하지 않고도 텍스트를 그릴 수 있습니다.
텍스트를 세로로 가운데로 가져 가려면 텍스트를 여전히 움직여야합니다. 불행히도 캔버스는 큰 선 높이를 지원하지 않으므로 짐작할 수 있습니다.
저는 포인트와 텍스트에 3 가지 맞춤을 제공하는 3 가지 예제를 작성하여 화면상의 실제 지점이 글꼴의 위치를 보여줍니다. 우리가 지점 근처를 중심으로 할 텍스트를 약간 이동을 제외하고
var font, lineHeight, x, y;
x = 100;
y = 100;
font = 20;
lineHeight = 15; // this is guess and check as far as I know
this.context.font = font + 'px Arial';
// Right Aligned
this.context.save();
this.context.translate(x, y);
this.context.rotate(-Math.PI/4);
this.context.textAlign = 'right';
this.context.fillText('right', 0, lineHeight/2);
this.context.restore();
this.context.fillStyle = 'red';
this.context.fillRect(x, y, 2, 2);
// Center
this.context.fillStyle = 'black';
x = 150;
y = 100;
this.context.save();
this.context.translate(x, y);
this.context.rotate(-Math.PI/4);
this.context.textAlign = 'center';
this.context.fillText('center', 0, lineHeight/2);
this.context.restore();
this.context.fillStyle = 'red';
this.context.fillRect(x, y, 2, 2);
// Left
this.context.fillStyle = 'black';
x = 200;
y = 100;
this.context.save();
this.context.translate(x, y);
this.context.rotate(-Math.PI/4);
this.context.textAlign = 'left';
this.context.fillText('left', 0, lineHeight/2);
this.context.restore();
this.context.fillStyle = 'red';
this.context.fillRect(x, y, 2, 2);
라인 this.context.fillText('right', 0, lineHeight/2);
은
당신이 오히려 자신을 구축하는 것보다 기존의 그래프 솔루션에보고 깊이 생각 가지고, 기본적으로
0, 0
입니다 ? flot (http://code.google.com/p/flot/)은 캔버스를 사용하는 한 가지 예입니다. – Bartek