English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The fillStyle() property of HTML canvas is used to set the drawing color, gradient, or pattern. The default value is #000000. <canvas> elements allow you to draw graphics on web pages using JavaScript. Each canvas has two elements that describe the canvas height and width, respectively, height and width.
Define red fill color for the rectangle:
JavaScript:
<!DOCTYPE html> <html> <head> <title>Usage of HTML canvas fillStyle Property (Basic Tutorial Website oldtoolbag.com)</title>/<title> </<head> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> Your browser does not support HTML5 canvas tag.</canvas> <script> var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.fillStyle="#FF0000"; ctx.fillStyle = my_gradient;20,20,150,10ctx.fillRect( </script> </body> </html>Test to see ‹/›
IEFirefoxOperaChromeSafari
Internet Explorer 9Firefox, Opera, Chrome, and Safari support the fillStyle property.
Note:Internet Explorer 8 and earlier versions do not support the canvas element.
<canvas> elements allow you to draw graphics on web pages using JavaScript. Each canvas has two elements that describe the canvas height and width, respectively, height and width.
Default value: | #000000 |
---|---|
JavaScript syntax: | context.fillStyle=color|gradient|pattern; |
value | description |
---|---|
color | indicating the drawing fill color CSS color value). The default value is #000000. |
gradient | gradient object used for filling the drawing (linear or radial). |
pattern | used for filling the drawing pattern object. |
Define a gradient (from top to bottom) as the fill style for a rectangle:
JavaScript:
<!DOCTYPE html> <html> <head> <title>Usage of HTML canvas fillStyle Property (Basic Tutorial Website oldtoolbag.com)</title>/<title> </<head> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> Your browser does not support HTML5 Your browser does not support the canvas tag./canvas> <script> canvas tag.< var c = document.getElementById("myCanvas");2d"); var my_gradient = ctx.createLinearGradient(0, 0, 0, 17ctx.fillRect( 0, 0); , "red");1my_gradient.addColorStop( , "white"); ctx.fillStyle = my_gradient;20, 20, 150, 10ctx.fillRect( </script> 0); 8<p>Attention: Internet Explorer/p> </body> </html>Test to see ‹/›
定义从左到右的渐变,作为矩形的填充样式:
JavaScript:
<!DOCTYPE html> <html> <head> <title>Usage of HTML canvas fillStyle Property (Basic Tutorial Website oldtoolbag.com)</title>/<title> </<head> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> Your browser does not support HTML5 Your browser does not support the canvas tag./canvas> <script> canvas tag.< var c = document.getElementById("myCanvas");2d"); Define a gradient from left to right as the fill style for the rectangle: 17var my_gradient = ctx.createLinearGradient(0, 0, var grd = ctx.createLinearGradient(0, 0, grd.addColorStop(0, "black");5my_gradient.addColorStop(0. grd.addColorStop(0.1my_gradient.addColorStop( grd.addColorStop( ctx.fillStyle = my_gradient;20, 20, 150, 10ctx.fillRect( </script> 0); 8<p>Attention: Internet Explorer/p> </body> </html>Test to see ‹/›
ctx.fillStyle = grd;
JavaScript:
<!DOCTYPE html> <html> <head> <title>Usage of HTML canvas fillStyle Property (Basic Tutorial Website oldtoolbag.com)</title>/<title> </<head> <body> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> Your browser does not support HTML5 Your browser does not support the canvas tag./canvas> <script> canvas tag.< var c = document.getElementById("myCanvas");2d"); var ctx = c.getContext(" 17var my_gradient = ctx.createLinearGradient(0, 0, 0, 0); my_gradient.addColorStop(0, "black");5my_gradient.addColorStop(0. , "red");1my_gradient.addColorStop( , "white"); ctx.fillStyle = my_gradient;20, 20, 150, 10ctx.fillRect( </script> 0); 8<p>Attention: Internet Explorer/p> </body> </html>Test to see ‹/›
Use images to fill the drawing:
JavaScript:
<!DOCTYPE html> <html> <head> <title>Usage of HTML canvas fillStyle Property (Basic Tutorial Website oldtoolbag.com)</title>/<title> </<head> <body> <p>Image Application:</p>/p> <img src="haha.gif" id="lamp"> <p>Canvas:</p>/p> <button onclick="draw(&39;repeat#39;)">Repeat</button> <button onclick="draw(&39;repeat-x#39;)">Repeat<-x</button> <button onclick="draw(&39;repeat-y#39;)">Repeat<-y</button> <button onclick="draw(&39;no-repeat#39;)">No Repeat</button> <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3> Your browser does not support HTML5 canvas tag. </canvas> <script> function draw(direction) { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clearRect(0,0,c.width,c.height); var img=document.getElementById("lamp") var pat=ctx.createPattern(img,direction); ctx.rect(0,0,220,128); ctx.fillStyle=pat; ctx.fill(); } </script> </body> </html>Test to see ‹/›