123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- var Animator = require("../animation/Animator");
- var logError = require("../core/log");
- var _util = require("../core/util");
- var isString = _util.isString;
- var isFunction = _util.isFunction;
- var isObject = _util.isObject;
- var isArrayLike = _util.isArrayLike;
- var indexOf = _util.indexOf;
- var Animatable = function () {
-
- this.animators = [];
- };
- Animatable.prototype = {
- constructor: Animatable,
-
- animate: function (path, loop) {
- var target;
- var animatingShape = false;
- var el = this;
- var zr = this.__zr;
- if (path) {
- var pathSplitted = path.split('.');
- var prop = el;
- animatingShape = pathSplitted[0] === 'shape';
- for (var i = 0, l = pathSplitted.length; i < l; i++) {
- if (!prop) {
- continue;
- }
- prop = prop[pathSplitted[i]];
- }
- if (prop) {
- target = prop;
- }
- } else {
- target = el;
- }
- if (!target) {
- logError('Property "' + path + '" is not existed in element ' + el.id);
- return;
- }
- var animators = el.animators;
- var animator = new Animator(target, loop);
- animator.during(function (target) {
- el.dirty(animatingShape);
- }).done(function () {
-
- animators.splice(indexOf(animators, animator), 1);
- });
- animators.push(animator);
- if (zr) {
- zr.animation.addAnimator(animator);
- }
- return animator;
- },
-
- stopAnimation: function (forwardToLast) {
- var animators = this.animators;
- var len = animators.length;
- for (var i = 0; i < len; i++) {
- animators[i].stop(forwardToLast);
- }
- animators.length = 0;
- return this;
- },
-
-
- animateTo: function (target, time, delay, easing, callback, forceAnimate) {
- animateTo(this, target, time, delay, easing, callback, forceAnimate);
- },
-
- animateFrom: function (target, time, delay, easing, callback, forceAnimate) {
- animateTo(this, target, time, delay, easing, callback, forceAnimate, true);
- }
- };
- function animateTo(animatable, target, time, delay, easing, callback, forceAnimate, reverse) {
-
- if (isString(delay)) {
- callback = easing;
- easing = delay;
- delay = 0;
- }
- else if (isFunction(easing)) {
- callback = easing;
- easing = 'linear';
- delay = 0;
- }
- else if (isFunction(delay)) {
- callback = delay;
- delay = 0;
- }
- else if (isFunction(time)) {
- callback = time;
- time = 500;
- }
- else if (!time) {
- time = 500;
- }
- animatable.stopAnimation();
- animateToShallow(animatable, '', animatable, target, time, delay, reverse);
-
- var animators = animatable.animators.slice();
- var count = animators.length;
- function done() {
- count--;
- if (!count) {
- callback && callback();
- }
- }
-
- if (!count) {
- callback && callback();
- }
-
- for (var i = 0; i < animators.length; i++) {
- animators[i].done(done).start(easing, forceAnimate);
- }
- }
- function animateToShallow(animatable, path, source, target, time, delay, reverse) {
- var objShallow = {};
- var propertyCount = 0;
- for (var name in target) {
- if (!target.hasOwnProperty(name)) {
- continue;
- }
- if (source[name] != null) {
- if (isObject(target[name]) && !isArrayLike(target[name])) {
- animateToShallow(animatable, path ? path + '.' + name : name, source[name], target[name], time, delay, reverse);
- } else {
- if (reverse) {
- objShallow[name] = source[name];
- setAttrByPath(animatable, path, name, target[name]);
- } else {
- objShallow[name] = target[name];
- }
- propertyCount++;
- }
- } else if (target[name] != null && !reverse) {
- setAttrByPath(animatable, path, name, target[name]);
- }
- }
- if (propertyCount > 0) {
- animatable.animate(path, false).when(time == null ? 500 : time, objShallow).delay(delay || 0);
- }
- }
- function setAttrByPath(el, path, name, value) {
-
-
- if (!path) {
- el.attr(name, value);
- } else {
-
- var props = {};
- props[path] = {};
- props[path][name] = value;
- el.attr(props);
- }
- }
- var _default = Animatable;
- module.exports = _default;
|