CanvasRenderingContext2D 方法 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 用户代理 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 fill() or stroke() calls will have no effect on it.

使用 fillText() method to fill the text characters rather than having just their outlines drawn.

句法

CanvasRenderingContext2D.strokeText(text, x, y [, maxWidth]);
					

参数

text
DOMString specifying the text string to render into the context. The text is rendered using the settings specified by font , textAlign , textBaseline ,和 direction .
x

The x-axis coordinate of the point at which to begin drawing the text.

y

The y-axis coordinate of the point at which to begin drawing the text.

maxWidth 可选

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.

返回值

undefined .

范例

Drawing text outlines

This example writes the words "Hello world" using the strokeText() 方法。

HTML

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>
						

JavaScript

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 <canvas> , then gets a reference to its 2D graphics context.

With that in hand, we set the 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).

结果

Restricting the text size

This example writes the words "Hello world," restricting its width to 140 pixels.

HTML

<canvas id="canvas" width="400" height="150"></canvas>
						

JavaScript

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '50px serif';
ctx.strokeText('Hello world', 50, 90, 140);
						

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.strokeText' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
strokeText Chrome Yes Edge 12 Firefox 3.5 IE 9 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: