开发者

How can I plot a gradient line in HTML canvas?

开发者 https://www.devze.com 2023-04-07 00:31 出处:网络
I am trying to draw a line which is a gradient. How is that possible in c开发者_JS百科anvas?Yes. Example:

I am trying to draw a line which is a gradient. How is that possible in c开发者_JS百科anvas?


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:

http://jsfiddle.net/9bMPD/

0

精彩评论

暂无评论...
验证码 换一张
取 消