The CanvasGradient
.addColorStop()
method adds a new stop, defined by an offset
and a color
, to the gradient. If the offset is not between 0
and 1
, an INDEX_SIZE_ERR
is raised, if the color can't be parsed as a CSS <color>
, a SYNTAX_ERR
is raised.
Syntax
void gradient.addColorStop(offset, color);
Parameters
- offset
- A number between
0
and1
. AnINDEX_SIZE_ERR
is raised, if the number is not in that range. - color
- A CSS
<color>
. ASYNTAX_ERR
is raised, if the value can not be parsed as a CSS<color>
value.
Examples
Using the addColorStop
method
This is just a simple code snippet which uses the addColorStop
method with a CanvasGradient
object.
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var gradient = ctx.createLinearGradient(0,0,200,0); gradient.addColorStop(0,"green"); gradient.addColorStop(1,"white"); ctx.fillStyle = gradient; ctx.fillRect(10,10,200,100);
Edit the code below and see your changes update live in the canvas:
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasGradient.addColorStop' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
See also
- The interface defining it,
CanvasGradient
CanvasRenderingContext2D.createLinearGradient()
CanvasRenderingContext2D.createRadialGradient()
Document Tags and Contributors
Tags:
Contributors to this page:
Sebastianz,
fscholz
Last updated by:
Sebastianz,