这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

CanvasRenderingContext2D .direction property of the Canvas 2D API specifies the current text direction used to draw text.

句法

ctx.direction = "ltr" || "rtl" || "inherit";
					

选项

可能的值:

"ltr"

The text direction is left-to-right.

"rtl"

The text direction is right-to-left.

"inherit"
The text direction is inherited from the <canvas> element or the Document as appropriate. Default value.

默认值为 "inherit" .

范例

Changing text direction

This example draws two pieces of text. The first one is left-to-right, and the second is right-to-left. Note that "Hi!" in ltr becomes "!Hi" in rtl .

HTML

<canvas id="canvas"></canvas>
					

JavaScript

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.font = '48px serif';
ctx.fillText('Hi!', 150, 50);
ctx.direction = 'rtl';
ctx.fillText('Hi!', 150, 130);
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.direction' 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
direction Chrome Yes Disabled
Yes Disabled
Disabled This feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge ≤79 Disabled
≤79 Disabled
Disabled From version ≤79: this feature is behind the Experimental Web Platform Features preference.
Firefox No IE No Opera No Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: