HTML canvas strokeStyle attribute
Example
Draw a rectangle. Use a red stroke color:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.strokeStyle="#FF0000";
ctx.strokeRect(20,20,150,100);
Try it »
Browser support





Internet Explorer 9, Firefox, Opera, Chrome and Safari support the strokeStyle attribute.
**Note:** Internet Explorer 8 and earlier versions do not support
Definition and Usage
The strokeStyle attribute sets or returns the color, gradient, or pattern used for the stroke.
| Default value: | #000000 |
|---|---|
| JavaScript syntax: | context.strokeStyle=color|gradient|pattern; |
Attribute values
| Value | Description |
|---|---|
| color | indicates the drawing stroke color A CSS color value. The default value is #000000. |
| gradient | Gradient object used to fill the drawing (linear or radial). |
| pattern | used to create a pattern stroke pattern object. |
More examples
Example
Draw a rectangle. Use a gradient stroke:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var gradient=ctx.createLinearGradient(0,0,170,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Fill with gradient
ctx.strokeStyle=gradient;
ctx.lineWidth=5;
ctx.strokeRect(20,20,150,100);
Try it »
Example
Write the text "Big smile!" using a gradient stroke:
JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Verdana";
// Create gradient
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Fill with gradient
ctx.strokeStyle=gradient;
ctx.strokeText("Big smile!",10,50);
Try it »
Canvas object