design mode
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
import { Clock } from 'three';
|
||||
|
||||
class MotionEngine {
|
||||
constructor() {
|
||||
const aq = [];
|
||||
const clock = new Clock();
|
||||
|
||||
this.animationQueue = aq;
|
||||
|
||||
function calcValues(target, values, initial, k = 1, mode = 'offset') {
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
if (value && typeof value === 'object') {
|
||||
if (!target[key]) {
|
||||
target[key] = {};
|
||||
}
|
||||
calcValues(target[key], value, initial[key], k, mode);
|
||||
return;
|
||||
}
|
||||
if (mode == 'offset') {
|
||||
target[key] = (initial[key] || 0) + value * k;
|
||||
} else if (typeof (value) == 'function') {
|
||||
target[key] = value(k);
|
||||
} else {
|
||||
target[key] = (initial[key] || 0) + (value - (initial[key] || 0)) * k;
|
||||
}
|
||||
});
|
||||
return target;
|
||||
}
|
||||
|
||||
this.add = function (a) {
|
||||
a = Array.isArray(a) ? a : [a];
|
||||
a.forEach(e => {
|
||||
aq.push(e);
|
||||
});
|
||||
};
|
||||
|
||||
this.clear = function (object) {
|
||||
for (var i = aq.length - 1; i >= 0; i--) {
|
||||
if (object && aq[i].o == object || !object && aq[i].ct == aq[i].t) {
|
||||
aq.splice(i, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.remove = function (a) {
|
||||
let idx = aq.indexOf(a);
|
||||
if (idx > -1) {
|
||||
a.ct = 0;
|
||||
this.animate(a, 0);
|
||||
aq.splice(idx, 1);
|
||||
}
|
||||
};
|
||||
|
||||
this.update = () => {
|
||||
let t = clock.getDelta();
|
||||
if (aq.length) {
|
||||
aq.forEach(e => this.animate(e, t));
|
||||
this.clear();
|
||||
}
|
||||
};
|
||||
|
||||
this.animate = function (e, t) {
|
||||
if (e.d && (e.dt || 0) < e.d) {
|
||||
e.dt = (e.dt || 0) + t;
|
||||
return;
|
||||
}
|
||||
if (e.ct === undefined) {
|
||||
e.ct = 0;
|
||||
e.iv = calcValues({}, e.a, e.o, 0);
|
||||
}
|
||||
e.ct = e.ct + t;
|
||||
if (e.ct > e.t) {
|
||||
e.ct = e.t;
|
||||
e.f && e.f();
|
||||
}
|
||||
calcValues(e.o, e.a, e.iv, e.ct / e.t, e.m || 'value');
|
||||
if (e.ct == e.t && e.r) {
|
||||
e.ct = e.m == 'offset' ? undefined : 0;
|
||||
if (e.rd) {
|
||||
e.dt = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.isActive = function (object) {
|
||||
return aq.find(e => e.o == object);
|
||||
};
|
||||
|
||||
this.isIdle = function () {
|
||||
return aq.length == 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export {MotionEngine}
|
||||
Reference in New Issue
Block a user