I am trying to draw a line which is a gradient. How is that possible in canvas?
Asked
Active
Viewed 2.0k times
1 Answers
47
Yes. Example:
// linear gradient from start to end of line
var grad= ctx.createLinearGradient(50, 50, 150, 150);
grad.addColorStop(0, "red");
grad.addColorStop(1, "green");
ctx.strokeStyle = grad;
ctx.beginPath();
ctx.moveTo(50,50);
ctx.lineTo(150,150);
ctx.stroke();
See it in action here:
Simon Sarris
- 60,553
- 13
- 135
- 167
-
4Just a note that the gradient is not follow the circle but is linear. It will not be uniformly distributed. You make it more uniform by splitting the circle into several parts and creating separate gradient for each part. – Viesturs May 30 '13 at 12:08