W3Schools.com

HTML5 canvas addColorStop Method

HTML5 track Tag Reference HTML5 canvas reference

Example

Draw a rectangle with a red and green gradient filling:

Yor browser does not support the canvas tag.

JavaScript:

var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var grd=ctx.createLinearGradient(0,0,150,0);
grd.addColorStop(0.3,"red");
grd.addColorStop(0.7,"green");
ctx.fillStyle=grd;
ctx.fillRect(0,0,150,100);

Try it yourself »

Definition and Usage

The addColorStop method specifies a color, and where to position the color in a gradient object.

The addColorStop method is used togheter with the createLinearGradient method or the createRadialrGradient.

JavaScript syntax: gradient.addColorStop(0.5,"#FF0000");


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The addColorStop method is supported in all major browsers.


Parameter Values

Parameter Values Description Play it
position number A value between 0.0 and 1.0 where 0.0 is at the beginning of the gradient and 1.0 is at the end Play it »
color color A CSS colorvalue representing a colorstop at the given position Play it »


HTML5 track Tag Reference HTML5 canvas reference


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE