HTMLキャンバスcreateLinearGradient()メソッド

❮HTMLキャンバスリファレンス

長方形の塗りつぶしスタイルとして、黒から白へのグラデーション(左から右)を定義します。

あなたのブラウザはHTML5canvastagをサポートしていません。

JavaScript:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var grd = ctx.createLinearGradient(0, 0, 170, 0);
grd.addColorStop(0, "black");
grd.addColorStop(1, "white");

ctx.fillStyle = grd;
ctx.fillRect(20, 20, 150, 100);

ブラウザのサポート

表の数字は、このメソッドを完全にサポートする最初のブラウザバージョンを示しています。

Method
createLinearGradient() Yes 9.0 Yes Yes Yes

定義と使用法

createLinearGradient()メソッドは、線形グラデーションオブジェクトを作成します。

グラデーションは、長方形、円、線、テキストなどを塗りつぶすために使用できます。

ヒント:このオブジェクトをstrokeStyle またはfillStyleプロパティの値として使用します。

ヒント:addColorStop()メソッドを使用して、さまざまな色を指定し、グラデーションオブジェクトのどこに色を配置するかを指定します。

JavaScript構文: context .createLinearGradient(x0、y0、x1、y1);

パラメータ値

Parameter Description
x0 The x-coordinate of the start point of the gradient
y0 The y-coordinate of the start point of the gradient
x1 The x-coordinate of the end point of the gradient
y1 The y-coordinate of the end point of the gradient

その他の例

長方形の塗りつぶしスタイルとしてグラデーション(上から下)を定義します。

あなたのブラウザはcanvastagをサポートしていません。

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

長方形の塗りつぶしスタイルとして、黒から赤、白へと変化するグラデーションを定義します。

あなたのブラウザはcanvastagをサポートしていません。

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

❮HTMLキャンバスリファレンス