HTML5 Canvas Gradient

HTML5 Canvas Gradient

 
A gradient is a smooth transition between two or more colors. They can be used to fill rectangles, circles, lines, text, etc. 
 
There are two types of gradients.
 

createLinearGradient(x0, y0, x1, y1)

 
x0: The x-coordinate, in pixels, of the start point of the gradient.
y0: The y-coordinate, in pixels, of the start point of the gradient.
x1: The x-coordinate, in pixels, of the endpoint of the gradient.
y1: The y-coordinate, in pixels, of the endpoint of the gradient.
 

createRadialGradient(x0, y0, r0, x1, y1, r1)

 
x0: The x-coordinate of the starting circle of the gradient.
y0: The y-coordinate of the starting circle of the gradient.
r0: The radius of the starting circle.
x1: The x-coordinate of the ending circle of the gradient.
y1: The y-coordinate of the ending circle of the gradient.
r1: The radius of the ending circle.
 
The first three parameters represent the start circle, with origin (x0, y0) and radius r0. The last three parameters represent the end circle, with origin (x1, y1) and radius r1.
 

Linear Gradient

 

gradient = ctx.createLinearGradient(0, 0, canvas.width, 0)

 
The y values (the 2nd and 4th parameters) are both 0, this gradient will shade evenly from left to right. 
 
Once we have a gradient object, we must add two or more color stops. To add a color stop, you need to specify its position along the gradient. Gradient positions can be anywhere between 0 and 1. 
  1. gradient.addColorStop("0", "magenta");  
  2. gradient.addColorStop(".25", "blue");  
  3. gradient.addColorStop(".50", "green");  
  4. gradient.addColorStop(".75", "yellow");  
  5. gradient.addColorStop("1.0", "red"); 
Defining a gradient doesn't draw anything on the canvas. To draw a gradient, you set your fillStyleto the gradient and draw a shape.
  1. ctx.fillStyle = gradient;  
  2. ctx.fillRect(0, 0, 300, 250);  
  3. ctx.fillRect(250, 300, 500, 450);
Example of Linear Gradient
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title>AddColorStop Example</title>  
  7.     <style type="text/css">  
  8.         #MyCanvas {  
  9.             border: 1px solid black;  
  10.         }  
  11.     </style>  
  12.     <script type="text/javascript">  
  13.         function draw()  
  14.         {  
  15.             var canvas = document.getElementById("MyCanvas");  
  16.             if (canvas.getContext)  
  17.             {  
  18.                 var ctx = canvas.getContext("2d");  
  19.                 gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);  
  20.    
  21.                 gradient.addColorStop("0", "magenta");  
  22.                 gradient.addColorStop(".25", "blue");  
  23.                 gradient.addColorStop(".50", "green");  
  24.                 gradient.addColorStop(".75", "yellow");  
  25.                 gradient.addColorStop("1.0", "red");  
  26.    
  27.                 ctx.fillStyle = gradient;  
  28.                 ctx.fillRect(0, 0, 300, 250);  
  29.                 ctx.fillRect(250, 300, 500, 450);  
  30.             }  
  31.         }  
  32.     </script>  
  33. </head>  
  34. <body onload="draw();">  
  35.     <h3>Linear Gradient using Canvas Element in HTML5</h3>  
  36.     <canvas id="MyCanvas" width="270" height="225">This browser or document mode doesn't support canvas</canvas>  
  37. </body>  
  38. </html>   
Output
 
linear.jpg
 

Radial Gradient

 
To create a radial gradient, we can use the "createRadialGradient()" method. Radial gradients are defined with two imaginary circles, a starting circle and an ending circle, in which the gradient starts with the start circle and moves towards the end circle.
 
Radial gradients are similar. Rather than drawing a gradient in a straight line, they draw a series of circular color bands. The first color is the center of the circle, and the last color defines an outer radius. Building a radial gradient is very similar to building a linear gradient.
 
Once the gradient is defined, the addColorStops() method works exactly as it does for linear gradients.
 
Syntax
  1. <script>  
  2. var grd=context.createRadialGradient(startX, startY, startRadius, endX, endY, endRadius);  
  3. grd.addColorStop(offset, color);  
  4. </script> 
Example of Radial Gradient
  1. <!DOCTYPE html>  
  2.    
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title>Canvas Radial Gradient test </title>  
  7.     <style>  
  8.         canvas  
  9.          {  
  10.             position: absolute;  
  11.             top: 0;  
  12.             left: 0;  
  13.             height: 600;  
  14.             width: 600;  
  15.         }  
  16.     </style>  
  17.     <script>  
  18.         function draw()  
  19.         {  
  20.             var canvas = document.getElementsByTagName('canvas')[0];  
  21.             body = document.getElementsByTagName('body')[0];  
  22.             if (canvas.getContext('2d'))  
  23.             {  
  24.    
  25.                 ctx = canvas.getContext('2d');  
  26.                 ctx.clearRect(0, 0, 600, 600);  
  27.                 gradient = ctx.createRadialGradient(300, 300, 0, 300, 300, 300);  
  28.                 ctx.fillStyle = getColors(gradient);  
  29.                 ctx.fillRect(0, 0, 600, 600);  
  30.                 body.onmousemove = function (event)  
  31.                 {  
  32.                     var width = window.innerWidth,  
  33.                         height = window.innerHeight,  
  34.                         x = event.clientX,  
  35.                         y = event.clientY,  
  36.                         rx = 600 * x / width,  
  37.                         ry = 600 * y / height;  
  38.    
  39.                     gradient = ctx.createRadialGradient(rx, ry, 0, rx, ry, 300);  
  40.                     ctx.fillStyle = getColors(gradient);  
  41.                     ctx.fillStyle = gradient;  
  42.                     ctx.fillRect(0, 0, 600, 600);  
  43.                 }  
  44.             }  
  45.         }  
  46.         function getColors(gradient)  
  47.         {  
  48.             gradient.addColorStop("0", "magenta");  
  49.             gradient.addColorStop(".25", "blue");  
  50.             gradient.addColorStop(".50", "green");  
  51.             gradient.addColorStop(".75", "yellow");  
  52.             gradient.addColorStop("1.0", "red");  
  53.             return (gradient);  
  54.         }  
  55.    
  56.     </script>  
  57.    
  58. </head>  
  59. <body onload="draw();">  
  60.     <h3>Radial Gradient in HTML5</h3>  
  61.     <canvas height="450" width="500">This browser or document mode doesn't support canvas</canvas>  
  62. </body>  
  63. </html> 
Output
 
radial.jpg