Canvas strokeStyle attribute

Definition and usage

strokeStyle Property that sets or returns the color, gradient, or pattern used for the stroke.

Example

Example 1

Draw a rectangle. Please use blue as the stroke color:

Your browser does not support the canvas tag.

JavaScript:

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

Try it yourself

Syntax

context.strokeStyle=color|gradient|pattern;

Attribute value

Value Description
color Indicates the drawing stroke color CSS color value). The default value is #000000.
gradient The gradient object used to fill the drawing (LinearOrRadial).
pattern The pattern object used to create pattern strokes.

Technical details

Default value: #000000

More examples

Example 2

Draw a rectangle. Use gradient stroke:

Your browser does not support the canvas tag.

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 yourself

Example 3

Write the text "codew3c.com" with a gradient stroke:

Your browser does not support the canvas tag.

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 yourself

Browser support

The numbers in the table indicate the browser version that fully supports this attribute for the first time.

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.