Commit b4f552ef authored by rockyl's avatar rockyl

修改tween

parent b807e097
/**
* Created by rockyl on 2018/11/8.
*
* 补间动画
*/
import {Component} from "../core";
import {lerp, lerpObj} from "../tools/math";
import {injectProp} from "../tools/utils";
import HashObject from "../core/HashObject";
......@@ -18,14 +18,13 @@ enum STATUS {
}
export interface ITweenPlugin {
//resolveLerp(fromValue, toValue, ratio, allowOutOfBounds):any;
resolveLerp(fromValue, toValue, ratio, allowOutOfBounds): any;
}
export interface TweenOptions {
loop?: number;
autoPlay?: boolean;
initFields?: string[];
clazz?: any;
fields?: string[];
}
......@@ -85,7 +84,6 @@ export class Tween extends HashObject {
startTime;
plugins: ITweenPlugin[];
clazz;
fields;
autoPlay: boolean;
......@@ -102,7 +100,6 @@ export class Tween extends HashObject {
this.target = target;
this.loop = options ? options.loop : 0;
this.autoPlay = options ? (options.hasOwnProperty('autoPlay') ? options.autoPlay : true) : true;
this.clazz = options ? options.clazz : null;
this.fields = options ? options.fields : null;
this.plugins = plugins;
......@@ -112,11 +109,29 @@ export class Tween extends HashObject {
}
}
private resolveLerp(fromValue, toValue, ratio) {
let currentValue;
if (this.plugins.length > 0) {
for (let plugin of this.plugins) {
currentValue = plugin.resolveLerp(fromValue, toValue, ratio, true);
}
} else {
if (typeof toValue == 'object') {
let {fields} = this;
currentValue = lerpObj(fromValue, toValue, ratio, fields || Object.keys(toValue), true);
} else {
currentValue = lerp(fromValue, toValue, ratio, true);
}
}
return currentValue;
}
onUpdate = (t) => {
this.t = t;
switch (this.status) {
case STATUS.DO_TO:
var {target, startTime, fromProps, toProps, duration, ease, clazz, fields} = this;
var {target, startTime, fromProps, toProps, duration, ease,} = this;
var passTime = t - startTime;
let timeRatio = Math.min(1, passTime / duration);
......@@ -131,16 +146,16 @@ export class Tween extends HashObject {
let currentValue;
if (timeRatio < 1) {
if (typeof toValue == 'object') {
currentValue = lerpObj(fromValue, toValue, ratio, clazz, fields || Object.keys(toValue), true);
} else {
currentValue = lerp(fromValue, toValue, ratio, true);
}
currentValue = this.resolveLerp(fromValue, toValue, ratio);
} else {
currentValue = toValue;
}
target[key] = currentValue;
if(typeof currentValue === 'string'){
injectProp(target[key], currentValue);
}else{
target[key] = currentValue;
}
}
if (timeRatio >= 1) {
......@@ -209,14 +224,14 @@ export class Tween extends HashObject {
killTweens(this.target);
}
if(delay > 0){
if (delay > 0) {
setTimeout(this._doPlay, delay, resetLoopCounting)
}else{
} else {
this._doPlay(resetLoopCounting);
}
}
private _doPlay=(resetLoopCounting)=>{
private _doPlay = (resetLoopCounting) => {
addTween(this.target, this);
this._start(resetLoopCounting);
};
......@@ -266,7 +281,7 @@ export class Tween extends HashObject {
_start(resetLoopCounting: boolean = true) {
this.status = STATUS.PENDING;
if(resetLoopCounting){
if (resetLoopCounting) {
this.loopCounting = 0;
}
this.host.callOnNextTick(this._readyStart);
......
......@@ -34,18 +34,17 @@ export function lerp(begin, end, t, allowOutOfBounds = false) {
* @param begin
* @param end
* @param t
* @param clazz
* @param fields
* @param allowOutOfBounds
* @return
*/
export function lerpObj(begin, end, t, clazz, fields, allowOutOfBounds = false) {
export function lerpObj(begin, end, t, fields, allowOutOfBounds = false) {
const type = typeof begin;
if (type !== typeof end) {
console.error('begin and end need same type')
}
const temp = new clazz();
const temp = {};
for (let field of fields) {
temp[field] = lerp(begin[field], end[field], t, allowOutOfBounds);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment