HTML-doek createLinearGradient() Metode
Voorbeeld
Definieer 'n gradiënt (van links na regs) wat van swart na wit gaan, as die vulstyl vir die reghoek:
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);
Blaaierondersteuning
Die nommers in die tabel spesifiseer die eerste blaaierweergawe wat die metode ten volle ondersteun.
Method | |||||
---|---|---|---|---|---|
createLinearGradient() | Yes | 9.0 | Yes | Yes | Yes |
Definisie en gebruik
Die createLinearGradient() metode skep 'n lineêre gradiëntobjek.
Die gradiënt kan gebruik word om reghoeke, sirkels, lyne, teks, ens.
Wenk: Gebruik hierdie voorwerp as die waarde vir die strokeStyle- of fillStyle- eienskappe.
Wenk: Gebruik die addColorStop() metode om verskillende kleure te spesifiseer, en waar om die kleure in die gradiëntvoorwerp te plaas.
JavaScript-sintaksis: | konteks .createLinearGradient( x0,y0,x1,y1 ); |
---|
Parameterwaardes
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 |
Meer voorbeelde
Voorbeeld
Definieer 'n gradiënt (van bo na onder) as die vulstyl vir die reghoek:
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);
Voorbeeld
Definieer 'n gradiënt wat gaan van swart, na rooi, na wit, as die vulstyl vir die reghoek:
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-doekverwysing