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.
void gradient.addColorStop(offset, color);
0
and 1
. An INDEX_SIZE_ERR
is raised, if the number is not in that range.<color>
. A SYNTAX_ERR
is raised, if the value can not be parsed as a CSS <color>
value.addColorStop
methodThis is just a simple code snippet which uses the addColorStop
method with a CanvasGradient
object.
<canvas id="canvas"></canvas>
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:
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasGradient.addColorStop' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
CanvasGradient
CanvasRenderingContext2D.createLinearGradient()
CanvasRenderingContext2D.createRadialGradient()
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient/addColorStop