var cnv = document.getElementById('canvas');
var ctx = cnv.getContext('2d');
ctx.width = ctx.width; // Clear the context
// Define a linear gradient
var linear = ctx.createLinearGradient(20, 20, 100, 100);
linear.addColorStop(0.0, "#FF0000");
linear.addColorStop(0.5, "#0000FF");
linear.addColorStop(1.0, "#FFFF00");
// Draw a square, filled with the linear gradient.
ctx.beginPath();
ctx.fillStyle = linear;
ctx.rect(20, 20, 100, 100);
ctx.fill();
// Define a radial gradient. Note that the gradient
// is positioned under the square that is going to
// be drawn.
var radial = ctx.createRadialGradient(180 + 50, 20 + 50, 5, 180 + 50, 20 + 50, 100);
radial.addColorStop(0.0, "#FF00FF");
radial.addColorStop(1.0, "#0000FF");
// Draw a square, filled with the radial gradient.
ctx.beginPath();
ctx.fillStyle = radial;
ctx.rect(180, 20, 100, 100);
ctx.fill();