W3Schools.com

HTML5 canvas createLinearGradient 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 createLinearGradient method creates a gradient object that changes from one color to another.

You can specify how and where these changes will take place, using the coordinate parameters.

The x0 and y0 parameters represents the start coordinates of the gradient.

The x1 and y1 parameters represents the end coordinates of the gradient.

Use the addColorStop() method to specify a color, and where to position the color in the gradient.

The gradient can be used to draw/fill rectangles, circles, lines etc.

JavaScript syntax: context.createLinearGradient(x0,y0,x1,y1);


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The createLinearGradient method is supported in all major browsers.


Parameter Values

Parameter Description
x0 The x-coordinate of the start of the gradient
y0 The y-coordinate of the start of the gradient
x1 The x-coordinate of the end of the gradient
y1 The y-coordinate of the end of the gradient


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