getLineDash() method of the Canvas 2D API's CanvasRenderingContext2D interface gets the current line dash pattern.

句法

ctx.getLineDash();
					

返回值

数组 of numbers that specify distances to alternately draw a line and a gap (in coordinate space units). If the number, when setting the elements, is odd, the elements of the array get copied and concatenated. For example, setting the line dash to [5, 15, 25] will result in getting back [5, 15, 25, 5, 15, 25] .

范例

Getting the current line dash setting

此范例演示 getLineDash() 方法。

HTML

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

JavaScript

As set by setLineDash() , strokes consist of lines that are 10 units wide, with spaces of 20 units in between each line.

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.setLineDash([10, 20]);
console.log(ctx.getLineDash());  // [10, 20]
// Draw a dashed line
ctx.beginPath();
ctx.moveTo(0, 50);
ctx.lineTo(300, 50);
ctx.stroke();
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'CanvasRenderingContext2D.getLineDash' 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
getLineDash Chrome Yes Edge 12 Firefox 27 IE 11 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 27 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: