Latest web development tutorials

HTML canvas strokeStyle property

HTML canvas Reference Manual HTML canvas Reference Manual

Examples

Draw a rectangle. Red color with a stroke:

YourbrowserdoesnotsupporttheHTML5canvastag.

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 ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 9, Firefox, Opera, Chrome and Safari support strokeStyle property.

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


Definition and Usage

strokeStyle property sets or returns the color, gradient, or pattern for stroke.

Defaults: # 000000
JavaScript syntax: context .strokeStyle = color | gradient | pattern;

Property Value

描述
color 指示绘图笔触颜色的 CSS 颜色值 。默认值是 #000000。
gradient 用于填充绘图的渐变对象( 线性放射性 )。
pattern 用于创建 pattern 笔触的 pattern 对象。


Examples

More examples

Examples

Draw a rectangle. Use a gradient stroke:

YourbrowserdoesnotsupporttheHTML5canvastag.

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"

Examples

With a gradient stroke to write the text "Big smile!":

YourbrowserdoesnotsupporttheHTML5canvastag.

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"


HTML canvas Reference Manual HTML canvas Reference Manual