Draw a rectangle with a red and green gradient filling:
JavaScript:
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); |
|---|
![]()
The createLinearGradient method is supported in all major browsers.
| 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 |
| 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 |
|---|