修改代码

This commit is contained in:
ival 2021-04-04 01:10:37 +08:00
parent eb429506f5
commit f16c7e0b11
3 changed files with 36 additions and 29 deletions

View File

@ -1,33 +1,48 @@
import Group from 'zrender/src/container/Group'; import Group from 'zrender/src/container/Group';
function traverse(group, map) {
group.eachChild(el => {
if (el instanceof Group) {
traverse(el, map);
} else {
map[el.name] = el;
}
})
return map;
}
class Animate { class Animate {
constructor(state) { constructor(state) {
this.state = {...state} this.state = {...state}
this.shapeFactory = this.shape? this.shape.shapeFactory: null; this.shapeFactory = this.shape? this.shape.shapeFactory: null;
this.timer = null; this.sum = 0;
this.total = 0;
this.count = 0; this.count = 0;
this.first = true;
} }
run(delay) { run(interval) {
this.total = this.total + delay; this.sum = this.sum + interval;
if (this.total > this.state.time) { const total = this.state.time + (this.first? this.state.delay: 0)
clearTimeout(this.timer); if (this.sum > total) {
this.timer = setTimeout(this.__animate.bind(this), this.state.delay); this.animate(this)
this.count = this.count + 1; this.count = this.count + 1;
this.total = 0; this.sum = 0;
this.first = false;
} }
} }
isLoop() { isLoop() {
return this.state.loop; return this.state.loop;
} }
__animate() { isNeedDefault() {
return this.state.isNeedDefault;
}
animate() {
const shape = this.state.shape; const shape = this.state.shape;
const frameList = this.state.frameList; const frameList = this.state.frameList;
const mapView = this.__traverse(shape, {}); const mapView = traverse(shape, {});
if(shape && frameList) { if(shape && frameList) {
const size = frameList.length; const size = frameList.length;
const frame = frameList[this.count%size]; const frame = frameList[this.count%size];
@ -36,21 +51,11 @@ class Animate {
const model = frame[name]; const model = frame[name];
if (view && model) { if (view && model) {
view.attr({shape: model.shape, style: model.style}); view.attr({shape: model.shape, style: model.style});
view.dirty();
} }
}) })
} }
} }
__traverse(group, map) {
group.eachChild(el => {
if (el instanceof Group) {
this.__traverse(el, map);
} else {
map[el.name] = el;
}
})
return map;
}
} }
class AnimateHandle { class AnimateHandle {

View File

@ -27,9 +27,9 @@ class Painter extends Group {
onframe(zr) { onframe(zr) {
const onframe = zr.animation.onframe; const onframe = zr.animation.onframe;
const animateHandle = this.$animateHandle; const animateHandle = this.$animateHandle;
zr.animation.onframe = (...args) => { zr.animation.onframe = interval => {
onframe.apply(zr.animation, args); onframe.call(zr.animation, interval);
animateHandle.onframe(...args); animateHandle.onframe(interval);
} }
} }
@ -72,9 +72,11 @@ class Painter extends Group {
} }
update(stateList=[]) { update(stateList=[]) {
stateList.forEach(state => { stateList
this.$animateHandle.animate(state); .sort((a,b) => a.weight - b.weight)
}) .forEach(state => {
this.$animateHandle.animate(state);
})
} }
updateTransform(opt) { updateTransform(opt) {

View File

@ -33,7 +33,7 @@ export default class StateHandle {
// this.updateState(this.parse(shapeFactory, state)), // 处理自身 // this.updateState(this.parse(shapeFactory, state)), // 处理自身
// this.updateState(this.parse(shapeFactory, state)) // 处理依赖 // this.updateState(this.parse(shapeFactory, state)) // 处理依赖
]; ];
}, []).sort((a,b) => a.weight - b.weight); }, []);
} }
updateState(state={}) { updateState(state={}) {