Canvas gradients are a way of filling shapes or strokes with color transitions.
A linear gradient is created using the 2D Context function createLinearGradient(x1, y1, x2, y2). The gradient extends from the start point x1, y1 to the end point x2, y2.
Once we've created our gradient, we can insert colors using the addColorStop(position, color).
After the gradient is created and assigned to a variable, this variable can be assigned to fillStyle and strokeStyle properties.
Functions:
Properties:
Try the following options:
var gradient = ctx.createLinearGradient(100,100,200,200); gradient.addColorStop(0,"#DD33CC"); gradient.addColorStop(1,"white"); ctx.fillStyle = gradient; ctx.fillRect(0,0,400,400);
var gradient = ctx.createLinearGradient(150, 0, 350,0); gradient.addColorStop(0, 'rgb(0, 0, 255)'); gradient.addColorStop(1, 'rgb(0, 255, 0)'); ctx.fillStyle = gradient; ctx.fillRect(10,10,130, 100); ctx.fillRect(150,10, 150, 100); ctx.fillRect(320,10, 80, 100); ctx.fillRect(100,120, 150, 100); ctx.fillRect(280,120, 150, 100);
var gradient = ctx.createLinearGradient(0, 0, 400, 0); gradient.addColorStop(0, 'red'); gradient.addColorStop(1 / 6, 'orange'); gradient.addColorStop(2 / 6, 'yellow'); gradient.addColorStop(3 / 6, 'green'); gradient.addColorStop(4 / 6, 'blue'); gradient.addColorStop(5 / 6, 'indigo'); gradient.addColorStop(1, 'violet'); ctx.fillStyle = gradient; ctx.fillRect(0, 0, 400, 75);