CanvasRenderingContext2D:getLineDash() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Canvas 2D API 的 CanvasRenderingContext2D 接口的 getLineDash() 方法用于获取当前的虚线样式。

语法

js
getLineDash()

参数

无。

返回值

一个由数字组成的 Array,用于指定绘制虚线和空隙的距离(以坐标空间单位计算)。如果在设置数组元素时数组长度为奇数,则数组元素会被复制并连接起来。例如,将虚线设置为 [5, 15, 25] 将得到 [5, 15, 25, 5, 15, 25]

示例

获取当前的虚线设置

此示例演示了 getLineDash 方法。

HTML

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

JavaScript

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.setLineDash([10, 20]);
console.log(ctx.getLineDash()); // [10, 20]

// 画一条虚线
ctx.beginPath();
ctx.moveTo(0, 50);
ctx.lineTo(300, 50);
ctx.stroke();

结果

规范

Specification
HTML
# dom-context-2d-getlinedash-dev

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getLineDash

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见