forked from mdn/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
144 lines (105 loc) · 4.85 KB
/
index.html
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
---
title: CanvasRenderingContext2D.strokeText()
slug: Web/API/CanvasRenderingContext2D/strokeText
tags:
- API
- Canvas
- CanvasRenderingContext2D
- Draw String
- Draw Text
- Drawing Strings
- Drawing Text
- Method
- Reference
- Stroke String
- Stroking Text
- strokeText
browser-compat: api.CanvasRenderingContext2D.strokeText
---
<div>{{APIRef}}</div>
<p>The {{domxref("CanvasRenderingContext2D")}} method
<code><strong>strokeText()</strong></code>, part of the Canvas 2D API, strokes — that
is, draws the outlines of — the characters of a text string at the specified
coordinates. An optional parameter allows specifying a maximum width for the rendered
text, which the {{Glossary("user agent")}} will achieve by condensing the text or by
using a lower font size.</p>
<p>This method draws directly to the canvas without modifying the current path, so any
subsequent {{domxref("CanvasRenderingContext2D.fill()", "fill()")}} or
{{domxref("CanvasRenderingContext2D.stroke()", "stroke()")}} calls will have no effect
on it.</p>
<div class="notecard note">
<p><strong>Note:</strong> Use the {{domxref('CanvasRenderingContext2D.fillText()', 'fillText()')}} method to
fill the text characters rather than having just their outlines drawn.</p>
</div>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: js"><em>CanvasRenderingContext2D</em>.strokeText(<em>text</em>, <em>x</em>, <em>y</em> [, <em>maxWidth</em>]);
</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><code>text</code></dt>
<dd>A {{domxref("DOMString")}} specifying the text string to render into the context.
The text is rendered using the settings specified by
{{domxref("CanvasRenderingContext2D.font","font")}},
{{domxref("CanvasRenderingContext2D.textAlign","textAlign")}},
{{domxref("CanvasRenderingContext2D.textBaseline","textBaseline")}}, and
{{domxref("CanvasRenderingContext2D.direction","direction")}}.</dd>
<dt><code>x</code></dt>
<dd>The x-axis coordinate of the point at which to begin drawing the text.</dd>
<dt><code>y</code></dt>
<dd>The y-axis coordinate of the point at which to begin drawing the text.</dd>
<dt><code>maxWidth</code> {{optional_inline}}</dt>
<dd>The maximum width the text may be once rendered. If not specified, there is no limit
to the width of the text. However, if this value is provided, the user agent will
adjust the kerning, select a more horizontally condensed font (if one is available or
can be generated without loss of quality), or scale down to a smaller font size in
order to fit the text in the specified width.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>{{jsxref("undefined")}}.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Drawing_text_outlines">Drawing text outlines</h3>
<p>This example writes the words "Hello world" using the <code>strokeText()</code> method.
</p>
<h4 id="HTML">HTML</h4>
<p>First, we need a canvas to draw into. This code creates a context 400 pixels wide and
150 pixels high.</p>
<pre class="brush: html"><canvas id="canvas" width="400" height="150"></canvas>
</pre>
<h4 id="JavaScript">JavaScript</h4>
<p>The JavaScript code for this example follows.</p>
<pre class="brush: js; highlight:[5]">const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '50px serif';
ctx.strokeText('Hello world', 50, 90);
</pre>
<p>This code obtains a reference to the {{HTMLElement("canvas")}}, then gets a reference
to its 2D graphics context.</p>
<p>With that in hand, we set the {{domxref("CanvasRenderingContext2D.font", "font")}} to
50-pixel-tall "serif" (the user's default {{interwiki("wikipedia", "serif")}} font),
then call <code>strokeText()</code> to draw the text "Hello world," starting at the
coordinates (50, 90).</p>
<h4 id="Result">Result</h4>
<p>{{ EmbedLiveSample('Drawing_text_outlines', 700, 180) }}</p>
<h3 id="Restricting_the_text_size">Restricting the text size</h3>
<p>This example writes the words "Hello world," restricting its width to 140 pixels.</p>
<h4 id="HTML_2">HTML</h4>
<pre class="brush: html"><canvas id="canvas" width="400" height="150"></canvas>
</pre>
<h4 id="JavaScript_2">JavaScript</h4>
<pre class="brush: js; highlight:[5]">const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '50px serif';
ctx.strokeText('Hello world', 50, 90, 140);
</pre>
<h4 id="Result_2">Result</h4>
<p>{{ EmbedLiveSample('Restricting_the_text_size', 700, 180) }}</p>
<h2 id="Specifications">Specifications</h2>
{{Specifications}}
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_text">Drawing text</a></li>
<li>The interface defining this method: {{domxref("CanvasRenderingContext2D")}}</li>
<li>{{domxref("CanvasRenderingContext2D.fillText()")}}</li>
</ul>