aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/canvasrenderingcontext2d/textalign/index.html
blob: bfeb7c5a7319ff98efc5a2a1d83a45fc573bf5a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
title: CanvasRenderingContext2D.textAlign
slug: Web/API/CanvasRenderingContext2D/textAlign
translation_of: Web/API/CanvasRenderingContext2D/textAlign
---
<div>{{APIRef}}</div>

<p><code><strong>CanvasRenderingContext2D</strong></code><strong><code>.textAlign</code></strong> 是 Canvas 2D API 描述绘制文本时,文本的对齐方式的属性。注意,该对齐是基于CanvasRenderingContext2D.fillText方法的x的值。所以如果textAlign="center",那么该文本将画在 x-50%*width。</p>

<blockquote>
<p>译者注:这里的textAlign="center"比较特殊。textAlign的值为center时候文本的居中是基于你在fillText的时候所给的x的值,也就是说文本一半在x的左边,一半在x的右边(可以理解为计算x的位置时从默认文字的左端,改为文字的中心,因此你只需要考虑x的位置即可)。所以,如果你想让文本在整个canvas居中,就需要将fillText的x值设置成canvas的宽度的一半。</p>
</blockquote>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var><em>ctx</em>.textAlign = "left" || "right" || "center" || "start" || "end";</var>
</pre>

<h3 id="选项">选项</h3>

<p>有效值:</p>

<dl>
 <dt>left</dt>
 <dd>文本左对齐。</dd>
 <dt>right</dt>
 <dd>文本右对齐。</dd>
 <dt>center</dt>
 <dd>文本居中对齐。</dd>
 <dt>start</dt>
 <dd>文本对齐界线开始的地方 (左对齐指本地从左向右,右对齐指本地从右向左)。</dd>
 <dt>end</dt>
 <dd>文本对齐界线结束的地方 (左对齐指本地从左向右,右对齐指本地从右向左)。</dd>
</dl>

<p>默认值是 <code>start。</code></p>

<blockquote>
<p><code>译者注:direction属性会对此属性产生影响。如果direction属性设置为ltr,则left和start的效果相同,right和end的效果相同;如果direction属性设置为rtl,则left和end的效果相同,right和start的效果相同。</code></p>
</blockquote>

<h2 id="示例">示例</h2>

<h3 id="Using_the_textAlign_property" name="Using_the_textAlign_property">使用 <code>textAlign</code> 属性</h3>

<p>这是一段简单的代码片段,使用 <code>textAlign</code> 属性设置文本的不同对齐方式。</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

ctx.font = "48px serif";
ctx.textAlign = "left";
ctx.strokeText("Hello world", 0, 100);
</pre>

<p>修改下面的代码并在线查看 canvas 的变化:</p>

<div class="hidden">
<h6 id="Playable_code" name="Playable_code">Playable code</h6>

<pre class="brush: html">&lt;canvas id="canvas" width="400" height="200" class="playable-canvas"&gt;&lt;/canvas&gt;
&lt;div class="playable-buttons"&gt;
  &lt;input id="edit" type="button" value="Edit" /&gt;
  &lt;input id="reset" type="button" value="Reset" /&gt;
&lt;/div&gt;
&lt;textarea id="code" class="playable-code"&gt;
ctx.font = "48px serif";
ctx.textAlign = "left";
ctx.strokeText("Hello world", 0, 100);&lt;/textarea&gt;
</pre>

<pre class="brush: js">var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var textarea = document.getElementById("code");
var reset = document.getElementById("reset");
var edit = document.getElementById("edit");
var code = textarea.value;

function drawCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  eval(textarea.value);
}

reset.addEventListener("click", function() {
  textarea.value = code;
  drawCanvas();
});

edit.addEventListener("click", function() {
  textarea.focus();
})

textarea.addEventListener("input", drawCanvas);
window.addEventListener("load", drawCanvas);
</pre>
</div>

<p>{{ EmbedLiveSample('Playable_code', 700, 360) }}</p>

<h2 id="规范描述">规范描述</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-textalign", "CanvasRenderingContext2D.textAlign")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatGeckoDesktop("1.9.1") }}</td>
   <td>{{ CompatIE(9) }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatGeckoMobile("1.9.1") }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
   <td>{{ CompatVersionUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="参见">参见</h2>

<ul>
 <li>接口定义, {{domxref("CanvasRenderingContext2D")}}.</li>
</ul>