CanvasRenderingContext2D.fillText() - Web APIs 编辑

The CanvasRenderingContext2D method fillText(), part of the Canvas 2D API, draws a text string at the specified coordinates, filling the string's characters with the current fillStyle. An optional parameter allows specifying a maximum width for the rendered text, which the 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 fill() or stroke() calls will have no effect on it.

The text is rendered using the font and text layout configuration as defined by the font, textAlign, textBaseline, and direction properties.

To draw the outlines of the characters in a string, call the context's strokeText() method.

Syntax

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

Parameters

text
A DOMString specifying the text string to render into the context. The text is rendered using the settings specified by font, textAlign, textBaseline, and direction.
x
The x-axis coordinate of the point at which to begin drawing the text, in pixels.
y
The y-axis coordinate of the point at which to begin drawing the text, in pixels.
maxWidth Optional
The maximum number of pixels wide 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.

Return value

undefined.

Examples

Drawing filled text

This example writes the words "Hello world" using the fillText() method.

HTML

First, we need a canvas to draw into. This code creates a context 400 pixels wide and 150 pixels across.

<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.fillText('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 fillText() to draw the text "Hello world," starting at the coordinates (50, 90).

Result

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.fillText('Hello world', 50, 90, 140);

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.fillText' in that specification.
Living Standard 

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:108 次

字数:7578

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文