HTML canvas strokeRect() 方法

定义和用法

strokeRect() 方法绘制矩形(不填色)。笔触的默认颜色是黑色。

提示:请使用 strokeStyle 属性来设置笔触的颜色、渐变或模式。

实例

绘制150*100像素的矩形:

您的浏览器不支持HTML5 canvas标签。

JavaScript:

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

Try It Yourself

Syntax

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

Parameter Value

Parameter Description
x The x coordinate of the top-left corner of the rectangle.
y The y coordinate of the top-left corner of the rectangle.
width The width of the rectangle, in pixels.
height The height of the rectangle, in pixels.

Browser Support

The numbers in the table indicate the first browser version that fully supports this property.

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

Note:Internet Explorer 8 and earlier versions do not support the <canvas> element.