Creates an outline of the specified rectangle on a canvas by using the current stroke, line width, and join styles.
![]() |
Syntax
var retval = CanvasRenderingContext2D.strokeRect(x, y, w, h);Parameters
- x [in]
-
Type: Floating-point
The x-coordinate, in pixels, of the upper-left corner of the rectangle in relation to the coordinates of the canvas.
- y [in]
-
Type: Floating-point
The y-coordinate, in pixels, of the upper-left corner of the rectangle in relation to the coordinates of the canvas.
- w [in]
-
Type: Floating-point
The width, in pixels, of the rectangle in relation to the coordinates of the canvas.
- h [in]
-
Type: Floating-point
The height, in pixels, of the rectangle in relation to the coordinates of the canvas.
Return value
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Standards information
- HTML Canvas 2D Context, Section 8
Remarks
The strokeRect method creates a path that requires the use of the stroke method to render the rectangle. The outline uses the current strokeStyle, lineWidth, lineJoin, and, when appropriate, miterLimit properties. If the w or h parameter is zero, a line is drawn. If the w and h parameters are zero, the rectangle is not drawn.
Examples
The following code example draws rectangles by using strokeStyle, lineJoin, and lineWidth for different effects.
<html>
<head>
<title>StrokeRect example</title>
<script type="text/javascript">
function draw() {
var canvas = document.getElementById("MyCanvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.lineWidth = "3";
ctx.strokeStyle = "blue";
ctx.strokeRect(5, 5, 300, 250);
ctx.stroke();
ctx.lineWidth = "5";
ctx.strokeStyle = "red";
ctx.strokeRect(150, 200, 300, 150);
ctx.stroke();
ctx.lineJoin = "round";
ctx.lineWidth = "10";
ctx.lineWidth = "7";
ctx.strokeStyle = "green";
ctx.strokeRect(250, 50, 150, 250);
ctx.stroke();
}
}
</script>
</head>
<body onload="draw();">
<canvas id="MyCanvas" width="600" height="600">This browser or document mode doesn't support canvas</canvas>
</body>
</html>
See also
Send comments about this topic to Microsoft
Build date: 11/28/2012
.png)
.png)