CanvasGradient.addColorStop()
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
JavaScript
Copy Code
void <var>gradient.addColorStop(offset, color);</var>
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
HTML
Copy Code
<canvas id="canvas"></canvas>
JavaScript
JavaScript
Copy Code
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:
Playable code
HTML
Copy Code
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> 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);</textarea>
JavaScript
Copy Code
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var textarea = document.getElementById("code"); var reset = document.getElementById("reset"); var edit = document.getElementById("edit"); var code = textarea.value; function drawCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); eval(textarea.value); } reset.addEventListener("click", function() { textarea.value = code; drawCanvas(); }); edit.addEventListener("click", function() { textarea.focus(); }) textarea.addEventListener("input", drawCanvas); window.addEventListener("load", drawCanvas);
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()
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/canvasgradient/addcolorstop