javascript - 如何制作旋转渐变(在圆上)?

标签 javascript html canvas gradient

我用 arc 函数创建了一个简单的圆:

/* ctx is the 2d context */ 
ctx.beginPath();
  var gradient = ctx.createLinearGradient(0, 0, this.radius, this.radius);
  gradient.addColorStop(0, '#FF0000');
  gradient.addColorStop(1, '#FFFFFF');
  ctx.lineWidth = 10;

  ctx.arc(this.radius, this.radius, this.radius, 0, 2*Math.PI, true);
  ctx.strokeStyle = gradient;
ctx.stroke();

所以我想旋转渐变,可以吗? 我尝试过使用 ctx.rotate(x),但这会旋转整个圆!

最佳答案

是的。您的渐变将从 x1,y1x2,y2,这是 createLinearGradient

的最后四个参数

例如,要反转渐变,请执行以下操作:

var 渐变 = ctx.createLinearGradient(this.radius, this.radius, 0, 0);

或者随意更改它:

var 渐变 = ctx.createLinearGradient(this.radius, 0, 0, 0);

等等。

关于javascript - 如何制作旋转渐变(在圆上)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807887/

相关文章:

javascript - Google Charts - 尝试在弹出窗口中添加链接或任何形式的自定义 html

javascript - 使图像出现在特定 div 中以响应按钮单击事件的最佳方法是什么?

javascript - 渲染分页符中的多个谷歌图表

javascript - 更改 Javascript 的 CanvasJS 中的字体

javascript - 保存带有背景图像的 Canvas

javascript - 为什么 getImageData 给出一个大数组?

javascript - D3 Canvas 地球仪在 LongLat 上带有文字

java - 如何上传图片并将其保存到数据库中?

javascript - 分配对象属性时的 DRY

javascript - 为什么我的代码循环两次?