在圆上制作旋转渐变?

时间:2013-04-09 10:53:59

标签: javascript html5 animation canvas

我正试图让圆圈上的旋转渐变跟随 - 这个模型。 rotating gradient following line http://f.cl.ly/items/3y161Z3M0t331K3E0j1e/Screen%20Shot%202013-04-09%20at%205.45.37%20PM.png

我在a JSFiddle中嘲笑了我想要做的事情。

为方便起见,这里是代码:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var radius = 75;
var startAngle = 0// 1.1 * Math.PI;
var endAngle = 2.0 * Math.PI;//1.9 * Math.PI;
var counterClockwise = false;

context.beginPath();
var gradient = context.createLinearGradient(radius, radius, 0, 0);
gradient.addColorStop(0, '#3b749a');
gradient.addColorStop(1, '#FFFFFF');
context.lineWidth = 15;

context.arc(radius, radius, radius, startAngle, endAngle, counterClockwise);

context.strokeStyle = gradient;
context.stroke(gradient);

现在,这是我的挑战。无论我尝试什么,我似乎都无法获得跟随路径的渐变。它似乎适用于它想要的任何角度,但不遵循道路。我做错了什么?

第二,是否可以像样机一样在一条边上获得圆角?

对不完美解决方案的后续问题:

所以看起来没有办法用渐变来描绘一条路径,只是将一个渐变应用到路径“切出”的画布上。 (并且HTML5中没有“圆圈”渐变AFAIK。)

由于建议的解决方案需要绘制两个元素,旋转/动画的最佳方法是什么?有了一个对象/上下文,看起来我可以抓住它,转换和旋转。有两个,我需要渲染到图像对象并旋转吗?

2 个答案:

答案 0 :(得分:2)

这实际上并非那么微不足道。也许有一种更简单的方法可以做到这一点,我只是采取了艰难的方法。或者这可能是新鲜的。

我做的是我最终在圆圈内画了一个渐变框。在一定间隔内,圆圈会改变其起点和终点,以便间隙旋转。当发生这种情况时,我重新计算渐变框以适应新的间隙,然后在那里绘制。结果是一个很好的效果。

在这个演示中,我已将其设置为随机循环显示颜色和不同尺寸,以显示不同的微调器的外观并获得一些乐趣。

jsFiddle Demo

操作的核心在于此功能:

function showSpinner(
 startAngle,endAngle,direction,radius,line,context,color,shadow)
{
 context.beginPath();
 var a = startAngle / Math.PI;
 a = a % 2;
 a = 2 - a;
 a *= Math.PI;
 var x = radius + Math.cos(a)*radius*1.7;
 var y = radius - Math.sin(a)*radius*1.7;
 var gradient = context.createLinearGradient(radius,radius,x,y);
 gradient.addColorStop(0.05, color);
 gradient.addColorStop(.60, '#FFFFFF');
 context.lineWidth = line;
 context.lineCap = "round";
 context.shadowBlur = 10;
 if( shadow === true )context.shadowColor = '#727272';
 context.arc(radius, radius, radius-line, startAngle, endAngle, direction);
 context.strokeStyle = gradient;
 context.stroke();
}

此功能利用上述绘图功能来提供动画

function spinner(obj){
 var radius,line,color,shadow;
 if( obj && obj.hasOwnProperty("shadow") ){
  shadow = true;
 }else{ radius = 75; }
 if( obj && obj.hasOwnProperty("radius") ){
  radius = obj.radius;
 }else{ radius = 75; }
 if( obj && obj.hasOwnProperty("line") ){
  line = obj.line;
 }else{ line = 7; }
 var speed = {inc:0.04,loop:15};
 if( obj && obj.hasOwnProperty("speed") ){
  if( obj.speed == "slow" ){
   speed = {inc:0.02,loop:25};
  }
 }
 if( obj && obj.hasOwnProperty("color") ){
  color = obj.color;
 }else{ color = '#3b749a'; } 
 var canvas = document.getElementById('myCanvas');
 canvas.style.height = 2*(radius+line) + "px";
 canvas.style.width = 4*(radius+line) + "px";
 var context = canvas.getContext('2d');
 var startAngle,endAngle;
 var counterClockwise = false;
 var sa = 1.2;
 var ea = 0.85;

 var spinner = setInterval(function(){
   canvas.width = canvas.width;
   sa += speed.inc;
   ea += speed.inc;
   startAngle = sa * Math.PI;
   endAngle = ea * Math.PI;
   showSpinner(
     startAngle,
     endAngle,
     counterClockwise,
     radius,
     line,
     context,
     color, 
     shadow
   );
 },speed.loop);
 setTimeout(function(){ clearInterval(spinner);},15000);
 return spinner;
}

但你真正需要做的就是

spinner()

如果你愿意,可以传递参数。您可以选择半径,线条,颜色,阴影和速度。

var obj = {};
obj.line = int - the size in pixels of the line width
obj.radius = int - the radius of the circle drawn (currently maxed at 75 for viewing, but you can always change the size of the canvas if you want)
obj.color = string - the color that the line of the spinner will be
obj.shadow = bool - show the shadow or don't show it
obj.speed = string - Only supports "slow" right now. Otherwise it will be fast

答案 1 :(得分:1)

也许不是很优雅:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;
var radius = 67.5;
context.beginPath();
context.arc(x, y, radius, 0, 2.0 * Math.PI, false);
context.lineWidth = 15;
context.strokeStyle = '#3b749a';
context.stroke();

context.beginPath();
context.arc(x, y, radius, -Math.PI/2, Math.PI/2, false);
var gradient = context.createLinearGradient(0, y-radius, 0, y+radius);
gradient.addColorStop(0, '#3b749a');
gradient.addColorStop(0.5, '#FFFFFF');
gradient.addColorStop(1, '#3b749a');
context.lineWidth = 15;
context.strokeStyle = gradient;
context.stroke(gradient);