HTML canvas strokeRect() 方法

定義和用法

strokeRect() 方法繪制矩形(不填色)。筆觸的默認顏色是黑色。

提示:請使用 strokeStyle 屬性來設置筆觸的顏色、漸變或模式。

實例

繪制 150*100 像素的矩形:

Your browser does not support the HTML5 canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.strokeRect(20,20,150,100);

親自試一試

語法

context.strokeRect(x,y,width,height);

參數值

參數 描述
x 矩形左上角的 x 坐標。
y 矩形左上角的 y 坐標。
width 矩形的寬度,以像素計。
height 矩形的高度,以像素計。

瀏覽器支持

表中的數字注明了首個完全支持該屬性的瀏覽器版本。

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

注釋:Internet Explorer 8 以及更早的版本不支持 <canvas> 元素。