简体   繁体   English

HTML5 Canvas,易于使用

[英]HTML5 Canvas, ease-in globalAlpha

I have the following function: 我有以下功能:

this.alphaArray = function() {
  var alpha   = this.alphStr,
    , alphArr = [],
    , alphInc = (this.alphStr - this.alphEnd) / this.frames;

  for (var i=0;i<this.frames;i++) {
    alphArr.push(alpha);
    alpha -= alphInc;
  }

  return alphArr;
};

The variables are as such: 变量是这样的:

  • this.alphStr - The starting alpha value, currently 1 (100% opaque). this.alphStr起始alpha值,当前为1(100%不透明)。
  • alphArr - An array that the function builds that holds the alpha value for each frame of the animation (used in a loop alongside the drawImage() function. alphArr函数建立的数组,其中保存动画每一帧的alpha值(与drawImage()函数一起循环使用drawImage()
  • alphInc - The increment that is subtracted from the previous alpha before push() 'd into the array. alphIncpush()到数组中的前一个alpha减去的增量。
  • this.alphEnd - The ending alpha value that the images will appear as at the end of the frames, currently .01 (1% opaque). this.alphEnd图像将在帧末尾显示的结尾alpha值,当前为.01(不透明度1%)。
  • this.frames - the total amount of frames (and therefor array values) that the animation is shown for. this.frames显示动画的帧的总数(以及数组值)。

So to add context to this function, here it is with all the values I currently have plugged in: 因此,要向此函数添加上下文,此处包含我当前插入的所有值:

this.alphaArray = function() {
  var alpha   = 1,
    , alphArr = [],
    , alphInc = (1 - .01) / this.1000; // 0.00099

  for (var i=0;i<this.frames;i++) {
    alphArr.push(alpha);
    alpha -= 0.00099;
  }

  return alphArr;
};

As stated before, ultimately alphaArr with be iterated through in a while() loop, and will be assigned to context.globalAlpha before drawing an image. 如前所述,最终alphaArr将在while()循环中进行迭代,并在绘制图像之前将其分配给context.globalAlpha Right now it reduces opaqueness in a linear fashion, using a static increment (or rather, decrement) value. 现在,它使用静态增量(或递减)值以线性方式减少不透明度。 I want to apply an ease-in affect on the transparency using the above method of assigning all the interations in an array. 我想使用上面分配数组中所有关系的方法对透明度施加缓和作用。 Given the values for the initial transparency, the ending transparency, and the duration of the animation ( this.frames ), how can I apply an ease-in effect where it starts out slowly decreasing in opaqueness, and by the end in the opaqueness reduces much quicker? 给定初始透明度,结束透明度和动画的持续时间( this.frames )的值,我如何应用缓和效果,即开始时其不透明性逐渐降低,而最终不透明性降低快多了?

When decrementing, multiply by the current frame like so 递减时,乘以当前帧,如下所示

var frameSum = 0;
for (var i=0; i < this.frames; i++)
{
  frameSum = frameSum + i;
}

var origAlpha = alpha;
for (var i=0; i < this.frames; i++) 
{
  alphArr.push(alpha);
  alpha -= (origAlpha / frameSum) * i;
}

This will increase the opaqueness as you go farther on. 随着您走得更远,这将增加不透明度。 You can add some other multiples to the i value if you want to adjust the speed. 如果要调整速度,可以将其他倍数添加到i值。

Edit 编辑

Fixed the loop to correctly end at 0 on the final frame. 修复了循环,使其在最后一帧正确结束于0。

Use one of the commonly known easing functions. 使用常用的缓动功能之一。

See, for instance, the jQuery Easing library: http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js 例如,请参阅jQuery轻松库: http : //gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js

-- and plugging easeOutQuad from there into your code -- -然后从此处将easeOutQuad插入代码中-

function makeAlphaArray(from, to, frames, easingFunc) {
    var arr = [], delta = to - from;
    for(var i = 0; i < frames; i++) arr.push(easingFunc(0, i, from, delta, frames));
    return arr;
}

this.alphaArray = function() {
    return makeAlphaArray(
      this.alphStr, this.alphEnd, this.frames,
      function /* easeOutQuad */ (x, t, b, c, d){return -c *(t/=d)*(t-2) + b;}
    );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM