files/en-us/web/api/canvasrenderingcontext2d/stroketext/index.md
{{APIRef("Canvas API")}}
The {{domxref("CanvasRenderingContext2D")}} method
strokeText(), 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.
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.
[!NOTE] Use the {{domxref('CanvasRenderingContext2D.fillText()', 'fillText()')}} method to fill the text characters rather than having just their outlines drawn.
strokeText(text, x, y)
strokeText(text, x, y, maxWidth)
text
x
y
maxWidth {{optional_inline}}
None ({{jsxref("undefined")}}).
This example writes the words "Hello world" using the strokeText() method.
First, we need a canvas to draw into. This code creates a context 400 pixels wide and 150 pixels high.
<canvas id="canvas" width="400" height="150"></canvas>
The JavaScript code for this example follows.
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.font = "50px serif";
ctx.strokeText("Hello world", 50, 90);
This code obtains a reference to the {{HTMLElement("canvas")}}, then gets a reference to its 2D graphics context.
With that in hand, we set the {{domxref("CanvasRenderingContext2D.font", "font")}} to
50-pixel-tall "serif" (the user's default serif font),
then call strokeText() to draw the text "Hello world," starting at the
coordinates (50, 90).
{{ EmbedLiveSample('Drawing_text_outlines', 700, 180) }}
This example writes the words "Hello world," restricting its width to 140 pixels.
<canvas id="canvas" width="400" height="150"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.font = "50px serif";
ctx.strokeText("Hello world", 50, 90, 140);
{{ EmbedLiveSample('Restricting_the_text_size', 700, 180) }}
{{Specifications}}
{{Compat}}