首頁  >  文章  >  web前端  >  html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle

html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle

黄舟
黄舟原創
2017-11-04 09:24:113008瀏覽

實例

定義以藍色填滿的矩形:

html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle

#JavaScript:

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

瀏覽器支援

Internet Explorer 9、Firefox、Opera、Chrome 以及Safari 支援fillStyle 屬性

註解:Internet Explorer 8 以及更早的版本不支援 元素。

定義和用法

fillStyle 屬性設定或傳回用於填滿繪畫的顏色、漸層或模式。

預設值: #000000
JavaScript 語法: context.fillStyle =color|gradient|pattern;

屬性值

#描述
color 指示繪圖填滿顏色的 CSS 色彩值。預設值是#000000
gradient #用於填滿繪圖的漸層物件(線性或放射性)
#pattern 用於填入繪圖的pattern 物件

#更多實例

##實例1

定義從上到下的漸變,作為矩形的填滿樣式:

html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle

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);

實例2

定義從左到右右邊的漸變,作為矩形的填滿樣式:

html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle

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);


以上是html設定或傳回用於填滿繪畫的顏色漸層或模式的屬性fillStyle的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn