CanvasRenderingContext2D:getLineDash() 方法
CanvasRenderingContext2D 介面的 getLineDash() 方法用於獲取當前的虛線樣式。
語法
js
getLineDash()
引數
無。
返回值
一個數字 Array,用於指定交替繪製線條和間隙(以座標空間單位)的距離。如果設定元素時的數字是奇數,則陣列中的元素會被複制並連線。例如,將虛線設定為 [5, 15, 25] 會得到 [5, 15, 25, 5, 15, 25]。
示例
獲取當前虛線設定
此示例演示了 getLineDash() 方法。
HTML
html
<canvas id="canvas"></canvas>
JavaScript
由 setLineDash() 設定,描邊由 10 個單位寬的線條組成,每條線之間有 20 個單位的間隙。
js
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 # dom-context-2d-getlinedash-dev |
瀏覽器相容性
載入中…