HTML canvas addColorStop() Method
Example
Defines a gradient from black to white as the fill style of a rectangle:
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);
Try it »
Browser support





Internet Explorer 9, Firefox, Opera, Chrome, and Safari support the addColorStop() method.
**Note:** Internet Explorer 8 and earlier versions do not support
Definition and Usage
The addColorStop() method specifies the color and position in the gradient object.
The addColorStop() method works with createLinearGradient() or createRadialGradient() .
**Note:** You can call addColorStop() multiple times to change the gradient. If you do not use this method on the gradient object, the gradient will be invisible. To get a visible gradient, you need to create at least one color stop.
| JavaScript syntax: | gradient.addColorStop(stop,color); |
|---|
Parameter value
| Parameters | Description |
|---|---|
| stop | A value between 0.0 and 1.0 representing the position between the start and end of the gradient. |
| color | in stop The color displayed at the position A CSS color value. |
More examples
Example
Defining a gradient with multiple addColorStop() methods:
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("0.3","magenta");
grd.addColorStop("0.5","blue");
grd.addColorStop("0.6","green");
grd.addColorStop("0.8","yellow");
grd.addColorStop(1,"red");
ctx.fillStyle=grd;
ctx.fillRect(20,20,150,100);
Try it »
Canvas object