Heim  >  Artikel  >  Web-Frontend  >  Verwendung von tween.js zur Implementierung eines vereinfachten Tweening-Animationsalgorithmus

Verwendung von tween.js zur Implementierung eines vereinfachten Tweening-Animationsalgorithmus

亚连
亚连Original
2018-06-05 18:03:161551Durchsuche

In diesem Artikel wird hauptsächlich das Tween-Animationsbeispiel von tween.js vorgestellt. Jetzt teile ich es mit Ihnen und gebe es als Referenz.

1. Tween.js verstehen

Wenn Sie das oben Gesagte bereits verstanden haben, können Sie den folgenden Teil überspringen Erklärung zu Tween .js Im Folgenden wird die Verwendung dieses Tweens vorgestellt. Zunächst müssen 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 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. Ein Frame ist die kleinste Einheit 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 Anfang und wählen Sie den ersten Teil aus. Sie möchten mit der Betrachtung der Position zwischen einem Frame 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 gleich der Anzahl der Endschritte ist, endet die gesamte Bewegung. Hinweis: Die Bewegung endet erst, wenn t gleich d ist. Wenn Nein, wird die Bewegung nicht gestoppt.3. Tween-Dateicode

rrree Kastanie

/*
 * 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;
5. Persönliche Erfahrung

Der Vorteil von Tween ist, dass die Wirkung von Tween auf einem basiert Algorithmus, nicht eine bestimmte Sprache. Grammatik kann an vielen Orten verwendet werden, und Sie werden davon profitieren, wenn Sie sie erst einmal gelernt haben.

Ich habe das Obige für Sie zusammengestellt und hoffe, dass es Ihnen in Zukunft hilfreich sein wird.

Verwandte Artikel:

Globale Variablen oder Datenmethoden gemäß Vue festlegen (ausführliches Tutorial)

Verwenden Sie JQuery, um auf die Eingabetaste zu klicken um den Login-Effekt zu erzielen (ausführliches Tutorial)

So erhalten Sie die Textinformationen des aktuellen Elements von vue.js

Das obige ist der detaillierte Inhalt vonVerwendung von tween.js zur Implementierung eines vereinfachten Tweening-Animationsalgorithmus. 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