Latest web development tutorials

HTML canvas fillStyle property

HTML canvas Reference Manual HTML canvas Reference Manual

Examples

Filled with red rectangle defined:

Yourbrowserdoesnotsupportthecanvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(20,20,150,100);

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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

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


Definition and Usage

fillStyle property sets or returns the color, gradient, or pattern used to fill the painting.

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

Property Value

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


Examples

More examples

Examples

The definition of the gradient from top to bottom, as the Rectangle's fill style:

Yourbrowserdoesnotsupportthecanvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var my_gradient=ctx.createLinearGradient(0,0,0,170);
my_gradient.addColorStop(0,"black");
my_gradient.addColorStop(1,"white");
ctx.fillStyle=my_gradient;
ctx.fillRect(20,20,150,100);

try it"

Examples

The definition of the gradient from left to right, as the Rectangle's fill style:

Yourbrowserdoesnotsupportthecanvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var my_gradient=ctx.createLinearGradient(0,0,170,0);
my_gradient.addColorStop(0,"black");
my_gradient.addColorStop(1,"white");
ctx.fillStyle=my_gradient;
ctx.fillRect(20,20,150,100);

try it"

Examples

Definition from black to red to white gradient, a rectangle fill style:

Yourbrowserdoesnotsupportthecanvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var my_gradient=ctx.createLinearGradient(0,0,170,0);
my_gradient.addColorStop(0,"black");
my_gradient.addColorStop(0.5,"red");
my_gradient.addColorStop(1,"white");
ctx.fillStyle=my_gradient;
ctx.fillRect(20,20,150,100);

try it"

Images used:

Lamp

Examples

Use an image to fill the drawings:

YourbrowserdoesnotsupporttheHTML5canvastag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("lamp");
var pat=ctx.createPattern(img,"repeat");
ctx.rect(0,0,150,100);
ctx.fillStyle=pat;
ctx.fill();

try it"


HTML canvas Reference Manual HTML canvas Reference Manual