--- title: CanvasRenderingContext2D.direction slug: Web/API/CanvasRenderingContext2D/direction tags: - Canvas - CanvasRenderingContext2D translation_of: Web/API/CanvasRenderingContext2D/direction ---
CanvasRenderingContext2D
.direction
是Canvas 2D API 用来在绘制文本时,描述当前文本方向的属性。
ctx.direction = "ltr" || "rtl" || "inherit";
有效值:
默认值是inherit。
direction
属性这是一段简单的代码片段,对文本设置不同的direction数值。
<canvas id="canvas"></canvas>
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);
<canvas id="canvas" width="550" height="500"></canvas>
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);
{{ EmbedLiveSample('Playable_code', 700, 180) }}
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "scripting.html#dom-context-2d-direction", "CanvasRenderingContext2D.direction")}} | {{Spec2('HTML WHATWG')}} |
此页面上的兼容性表是根据结构化数据生成的。 如果您想提供数据,请查看https://github.com/mdn/browser-compat-data并向我们发送请求请求。
{{Compat("api.CanvasRenderingContext2D.direction")}}