如何在HTML画布中绘制渐变线?

时间:2011-09-24 19:16:28

标签: html5 canvas

我正在尝试绘制一条渐变线。画布怎么可能?

1 个答案:

答案 0 :(得分:36)

是。例如:

// 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();

请在此处查看:

http://jsfiddle.net/9bMPD/