HTML 캔버스 textBaseline 속성
예시
y=100에 빨간색 선을 그린 다음 다른 textBaseline 값을 사용하여 y=100에 각 단어를 배치합니다.
자바스크립트:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//Draw a red line at y = 100
ctx.strokeStyle = "red";
ctx.moveTo(5, 100);
ctx.lineTo(395, 100);
ctx.stroke();
ctx.font = "20px Arial"
//Place each word at y = 100 with different textBaseline values
ctx.textBaseline = "top";
ctx.fillText("Top", 5, 100);
ctx.textBaseline = "bottom";
ctx.fillText("Bottom", 50, 100);
ctx.textBaseline = "middle";
ctx.fillText("Middle", 120, 100);
ctx.textBaseline = "alphabetic";
ctx.fillText("Alphabetic", 190, 100);
ctx.textBaseline = "hanging";
ctx.fillText("Hanging", 290, 100);
브라우저 지원
표의 숫자는 속성을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.
Property | |||||
---|---|---|---|---|---|
textBaseline | Yes | 9.0 | Yes | Yes | Yes |
참고: textBaseline 속성은 특히 "hanging" 또는 "ideographic"을 사용할 때 다른 브라우저에서 다르게 작동합니다.
정의 및 사용
textBaseline 속성은 텍스트를 그릴 때 사용되는 현재 텍스트 기준선을 설정하거나 반환합니다.
아래 그림은 textBaseline 속성이 지원하는 다양한 기준선을 보여줍니다.
참고: fillText () 및 strokeText() 메서드는 캔버스에 텍스트를 배치할 때 지정된 textBaseline 값을 사용합니다.
기본값: | 알파벳 |
---|---|
자바스크립트 구문: | context .textBaseline="알파벳|상단|매달기|중간|표의문자|하단"; |
속성 값
Values | Description | Play it |
---|---|---|
alphabetic | Default. The text baseline is the normal alphabetic baseline | |
top | The text baseline is the top of the em square | |
hanging | The text baseline is the hanging baseline | |
middle | The text baseline is the middle of the em square | |
ideographic | The text baseline is the ideographic baseline | |
bottom | The text baseline is the bottom of the bounding box |
❮ HTML 캔버스 참조