HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Canvas Object : Define a gradient (left to right) that goes from black to white, as the fill style for the rectangle
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
createLinearGradient() | 4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
The createLinearGradient() method creates a linear gradient object.
The gradient can be used to fill rectangles, circles, lines, text, etc.
JavaScript syntax: | 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 |
Define a gradient (top to bottom) as the fill style for the rectangle:
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);
Define a gradient that goes from black, to red, to white, as the fill style for the rectangle:
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);
❮ Canvas Object