Heim  >  Artikel  >  Web-Frontend  >  Ausführliche Erläuterung des Tween-Animationsalgorithmus von tween.js

Ausführliche Erläuterung des Tween-Animationsalgorithmus von tween.js

小云云
小云云Original
2018-02-22 09:43:501700Durchsuche

Dieser Artikel stellt Ihnen hauptsächlich das Tween-Animationsbeispiel von tween.js vor. Der Herausgeber findet es ziemlich gut, daher werde ich es jetzt mit Ihnen teilen und als Referenz geben. Folgen wir dem Herausgeber, um einen Blick darauf zu werfen. Ich hoffe, es kann allen helfen.

1. Tween.js verstehen

Wenn Sie das oben Genannte verstehen, können Sie den folgenden Teil überspringen Um dieses Tween zu verwenden, müssen zunächst die drei Parameter b, c und d (d. h. Anfangswert, Änderungsbetrag, Dauer) bestimmt werden, bevor die Lockerung beginnt. Führen Sie zunächst ein Konzept für Tweening-Animationen ein. Flash verwendet die Tween-Klasse, wenn Sie Animationen erstellen. Sie kann zum Erstellen vieler Animationseffekte wie Beschleunigung, Federung usw. verwendet werden. tween.js kann als Tweening-Animation in Flash interpretiert werden. Die Frage ist also: Was ist Tweening-Animation?

Ich glaube, dass diejenigen, die Flash studiert haben, wissen, dass Tweening-Animation die wichtigste und sehr wichtige Ausdrucksmethode von Flash ist 1. Es gibt zwei Arten von Tweening-Animationen: Aktions-Tweening-Animationen und Form-Tweening-Animationen, aber Sie müssen nicht so viel darüber in js wissen. Schauen wir uns ohne weiteres die Informationen der Baidu-Enzyklopädie zum Thema Tweening an Definition: Tween-Animation: Bei der Durchführung einer Flash-Animation muss eine „Tweening-Animation“ zwischen zwei Schlüsselbildern durchgeführt werden, um die Bewegung des Bildes zu realisieren. Nach dem Einfügen der Tweening-Animation wird der Interpolationsrahmen zwischen den beiden Schlüsselbildern automatisch generiert Der Computer.

Durch Berechnung ermittelt

Also, was sind Schlüsselbilder? Lassen Sie uns zuerst etwas Populäres tun. Die Filme und Animationen, die wir normalerweise sehen, sind alle 24 Bilder Das menschliche Auge kann innerhalb des Bereichs 22 Punkte erfassen, die eine gerade Linie oder Kurve bilden. Jeder Punkt stellt die kleinste Einheit eines einzelnen Bildes dar Animation und ein einzelnes Bild Es kann als Objekt betrachtet werden (alles ist ein Objekt, außer Werttypen) Und diese Linie stellt die Bewegungsbahn des Objekts dar.

    Zwei und vier Parameter
  1. t: aktuelle Zeit--> stellt den ersten Punkt dar, der das erste Bild ist, an dem eine Animation beginnt.
  2. b: Anfangswert--> stellt den Anfangswert dar, der den Anfangswert darstellt. Wenn wir uns Filme oder Animationen ansehen, schauen wir uns im Allgemeinen nicht den Prolog an Beginn und wählen Sie den ersten Teil aus. Sie möchten mit der Betrachtung der Position zwischen dem ersten und dem letzten Frame beginnen. Diese Position ist der Anfangswert.
  3. c: Wertänderung--> stellt den letzten Frame minus dem Anfangswert dar, der den Betrag der Änderung darstellt,
  4. d : Dauer -->Stellt das letzte Bild, das Ende von 1 Sekunden und das Ende der Animation dar.

Verwendung von tween.js 1. Herunterladen 2. Importieren 3. Verwendung der tween.js-Syntax

Beschleunigungseffektname (t, b, c,d);

Hinweis: Wenn die Anzahl der Startschritte auf die Anzahl der Endschritte ansteigt, endet die gesamte Bewegung. Hinweis: Die Bewegung endet erst, wenn t gleich d ist , die Bewegung wird nicht aufhören.

3. Tween-Dateicode
/*
 * Tween.js
 * t: current time(当前时间);
 * b: beginning value(初始值);
 * c: change in value(变化量);
 * d: duration(持续时间)。
*/
var Tween = {
  Linear: function(t, b, c, d) { //匀速
    return c * t / d + b; 
  },
  Quad: { //二次方缓动效果
    easeIn: function(t, b, c, d) {
      return c * (t /= d) * t + b;
    },
    easeOut: function(t, b, c, d) {
      return -c *(t /= d)*(t-2) + b;
    },
    easeInOut: function(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t + b;
      return -c / 2 * ((--t) * (t-2) - 1) + b;
    }
  },
  Cubic: { //三次方缓动效果
    easeIn: function(t, b, c, d) {
      return c * (t /= d) * t * t + b;
    },
    easeOut: function(t, b, c, d) {
      return c * ((t = t/d - 1) * t * t + 1) + b;
    },
    easeInOut: function(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t*t + b;
      return c / 2*((t -= 2) * t * t + 2) + b;
    }
  },
  Quart: { //四次方缓动效果
    easeIn: function(t, b, c, d) {
      return c * (t /= d) * t * t*t + b;
    },
    easeOut: function(t, b, c, d) {
      return -c * ((t = t/d - 1) * t * t*t - 1) + b;
    },
    easeInOut: function(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
      return -c / 2 * ((t -= 2) * t * t*t - 2) + b;
    }
  },
  Quint: { //五次方缓动效果
    easeIn: function(t, b, c, d) {
      return c * (t /= d) * t * t * t * t + b;
    },
    easeOut: function(t, b, c, d) {
      return c * ((t = t/d - 1) * t * t * t * t + 1) + b;
    },
    easeInOut: function(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
      return c / 2*((t -= 2) * t * t * t * t + 2) + b;
    }
  },
  Sine: { //正弦缓动效果
    easeIn: function(t, b, c, d) {
      return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
    },
    easeOut: function(t, b, c, d) {
      return c * Math.sin(t/d * (Math.PI/2)) + b;
    },
    easeInOut: function(t, b, c, d) {
      return -c / 2 * (Math.cos(Math.PI * t/d) - 1) + b;
    }
  },
  Expo: { //指数缓动效果
    easeIn: function(t, b, c, d) {
      return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    },
    easeOut: function(t, b, c, d) {
      return (t==d) ? b + c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    easeInOut: function(t, b, c, d) {
      if (t==0) return b;
      if (t==d) return b+c;
      if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
      return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
  },
  Circ: { //圆形缓动函数
    easeIn: function(t, b, c, d) {
      return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
    },
    easeOut: function(t, b, c, d) {
      return c * Math.sqrt(1 - (t = t/d - 1) * t) + b;
    },
    easeInOut: function(t, b, c, d) {
      if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
      return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
    }
  },
  Elastic: { //指数衰减正弦曲线缓动函数
    easeIn: function(t, b, c, d, a, p) { //加速
      var s;
      if (t==0) return b;
      if ((t /= d) == 1) return b + c;
      if (typeof p == "undefined") p = d * .3;
      if (!a || a < Math.abs(c)) {
        s = p / 4;
        a = c;
      } else {
        s = p / (2 * Math.PI) * Math.asin(c / a);
      }
      return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
    },
    easeOut: function(t, b, c, d, a, p) { //减速
      var s;
      if (t==0) return b;
      if ((t /= d) == 1) return b + c;
      if (typeof p == "undefined") p = d * .3;
      if (!a || a < Math.abs(c)) {
        a = c; 
        s = p / 4;
      } else {
        s = p/(2*Math.PI) * Math.asin(c/a);
      }
      return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
    },
    easeInOut: function(t, b, c, d, a, p) { //先加速后减速
      var s;
      if (t==0) return b;
      if ((t /= d / 2) == 2) return b+c;
      if (typeof p == "undefined") p = d * (.3 * 1.5);
      if (!a || a < Math.abs(c)) {
        a = c; 
        s = p / 4;
      } else {
        s = p / (2 *Math.PI) * Math.asin(c / a);
      }
      if (t < 1) return -.5 * (a * Math.pow(2, 10* (t -=1 )) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
      return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p ) * .5 + c + b;
    }
  },
  Back: { //超过范围的三次方的缓动函数
    easeIn: function(t, b, c, d, s) {
      if (typeof s == "undefined") s = 1.70158;
      return c * (t /= d) * t * ((s + 1) * t - s) + b;
    },
    easeOut: function(t, b, c, d, s) {
      if (typeof s == "undefined") s = 1.70158;
      return c * ((t = t/d - 1) * t * ((s + 1) * t + s) + 1) + b;
    },
    easeInOut: function(t, b, c, d, s) {
      if (typeof s == "undefined") s = 1.70158; 
      if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
      return c / 2*((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
    }
  },
  Bounce: { //指数衰减的反弹曲线缓动函数
    easeIn: function(t, b, c, d) {
      return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
    },
    easeOut: function(t, b, c, d) {
      if ((t /= d) < (1 / 2.75)) {
        return c * (7.5625 * t * t) + b;
      } else if (t < (2 / 2.75)) {
        return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
      } else if (t < (2.5 / 2.75)) {
        return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
      } else {
        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
      }
    },
    easeInOut: function(t, b, c, d) {
      if (t < d / 2) {
        return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
      } else {
        return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
      }
    }
  }
}
Math.tween = Tween;


4. Persönliche Erfahrung
<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script src="Tween/tween.js"></script>
    <style>
      *{margin: 0;padding: 0;}
      .out{width: 800px;height: 500px;background: #e5e5e5;position: relative;padding: 20px;text-align: center;}
      .inner{width: 50px;height: 50px;border-radius: 50%;background: #FF0000; position: absolute;left: 50px;top: 50px;}
    </style>
  </head>

  <body>
    <p id="app" class="out">
      <p class="inner" id="ball"></p>
      <button id="start" @click="start()">start</button>
    </p>
  </body>
  <script type="text/javascript">
    var app = new Vue({
      el: '#app',
      data: {
        t: 0,
        b: 50,
        c: 500,
        d: 1500,
      },
      methods:{
        start(){
          var t = this.t;
          const b = this.b;
          const c = this.c;
          const d = this.d;
          const setInt = setInterval(()=>{
            t++;
            console.log(t)
            if(t==300){clearInterval(setInt)}
            console.log(t);
            const ballLeft = Tween.Linear(t,b,c,d)+"px";
            ball.style.left = ballLeft;
          },20)
        }
      }
    })
  </script>
</html>

Der Vorteil von Tween besteht darin, dass die Wirkung von Tween auf einem Algorithmus und nicht auf der Grammatik einer bestimmten Sprache basiert, sodass es an einer Vielzahl von Orten verwendet werden kann und Sie davon profitieren werden, wenn Sie es einmal tun lerne es.

Verwandte Empfehlungen:

Detaillierte Erläuterung der Anwendungsbeispiele von tween.js

Einfaches Beispiel-Tutorial zur Kapselung der Animationsbibliothek tween.js

Detaillierte Einführung in die Tween.js-Animation

Das obige ist der detaillierte Inhalt vonAusführliche Erläuterung des Tween-Animationsalgorithmus von tween.js. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn