UNPKG

awv3

Version:
453 lines (403 loc) 15.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = undefined; var _assign = require('babel-runtime/core-js/object/assign'); var _assign2 = _interopRequireDefault(_assign); var _typeof2 = require('babel-runtime/helpers/typeof'); var _typeof3 = _interopRequireDefault(_typeof2); var _promise = require('babel-runtime/core-js/promise'); var _promise2 = _interopRequireDefault(_promise); var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _keys = require('babel-runtime/core-js/object/keys'); var _keys2 = _interopRequireDefault(_keys); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _three = require('three'); var THREE = _interopRequireWildcard(_three); var _easing2 = require('./easing'); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var tweens = []; var Tween = function () { function Tween(parent, properties) { (0, _classCallCheck3.default)(this, Tween); this.parent = parent; this.internalUpdate = null; this.isPlaying = false; this.paused = false; this.duration = 0; this.repeatTimes = 0; this.repeatMode = Tween.Repeat.Normal; this.reversed = false; this.delayTime = 0; this.onStartCallbackFired = false; this.easingFunction = _easing2.exponential.out; this.startTime = null; this.onStartCallback = null; this.onUpdateCallback = null; this.onCompleteCallback = null; this.thenCallback = null; this.onStopCallback = null; this.valuesEnd = flatten(parent, typeof properties === 'function' ? properties() : properties); this.valuesStart = {}; this.valuesStartRepeat = {}; this.object = {}; this.map = {}; for (var key in this.valuesEnd) { var prop = this.map[key] = returnValue(parent, key), value = prop[0][prop[1]]; this.object[key] = value; this.valuesStart[key] = value; this.valuesStartRepeat[key] = value; } } (0, _createClass3.default)(Tween, [{ key: 'hasProperty', value: function hasProperty(key) { return this.object.hasOwnProperty(key); } }, { key: 'getProperties', value: function getProperties() { return (0, _extends3.default)({}, this.valuesStart); } }, { key: 'removeProperty', value: function removeProperty(key) { delete this.valuesStart[key]; delete this.valuesEnd[key]; delete this.object[key]; delete this.map[key]; if ((0, _keys2.default)(this.object).length === 0) this.stop(); return this; } }, { key: 'from', value: function from(properties) { if (!properties) return this; properties = typeof properties === 'function' ? properties() : properties; var flattened = flatten(this.parent, properties); var previous = undefined; for (var key in flattened) { var prop = returnValue(properties, key); if (prop[0]) previous = prop[0]; prop[0] = prop[0] || previous; var value = prop[0][prop[1]]; var actualObject = this.map[key]; actualObject[0][actualObject[1]] = value; this.object[key] = value; this.valuesStart[key] = value; } return this; } }, { key: 'start', value: function start(length) { this.duration = length || 0; Tween.add(this); this.isPlaying = true; this.paused = false; this.onStartCallbackFired = false; this.startTime = null; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = (0, _getIterator3.default)(Tween.getAll()), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; if (item !== this && item.isPlaying && item.parent == this.parent) { for (var key in this.object) { if (item.hasProperty(key)) item.removeProperty(key); } } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } if (this.duration == 0) { for (var property in this.valuesEnd) { var end = this.valuesEnd[property], actualObject = this.map[property]; this.object[property] = end; actualObject[0][actualObject[1]] = end; } } this.parent.view && this.parent.view.invalidate(); return this; } }, { key: 'now', value: function now() { return this.start(0); } }, { key: 'stop', value: function stop() { this.isPlaying = false; this.paused = false; if (this.onStopCallback !== null) this.onStopCallback.call(this.object); if (this.thenCallback !== null) { this.thenCallback.call(this.object); this.thenCallback = null; } return this; } }, { key: 'pause', value: function pause() { this.paused = true; return this; } }, { key: 'continue', value: function _continue() { this.paused = false; return this; } }, { key: 'toggle', value: function toggle() { this.paused = !this.paused; return this; } }, { key: 'delay', value: function delay(amount) { this.delayTime = amount; return this; } }, { key: 'repeat', value: function repeat() { var times = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var yoyo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Tween.Repeat.Normal; this.repeatTimes = times; this.repeatMode = yoyo; return this; } }, { key: 'easing', value: function easing(_easing) { this.easingFunction = _easing; return this; } }, { key: 'onStart', value: function onStart(callback) { this.onStartCallback = callback; return this; } }, { key: 'onUpdate', value: function onUpdate(callback) { this.onUpdateCallback = callback; return this; } }, { key: 'onComplete', value: function onComplete(callback) { this.onCompleteCallback = callback; return this; } }, { key: 'wait', value: function wait() { var _this = this; if (!this.isPlaying) return _promise2.default.resolve();else return new _promise2.default(function (resolve) { return _this.thenCallback = resolve; }); } }, { key: 'onStop', value: function onStop(callback) { this.onStopCallback = callback; return this; } }, { key: 'update', value: function update(time) { if (this.paused) return true; if (!this.isPlaying) return false; if (this.startTime == null) { this.startTime = window.performance.now(); this.startTime += this.delayTime; } var property = void 0; if (time < this.startTime) return true; if (this.onStartCallbackFired === false) { if (this.onStartCallback !== null) this.onStartCallback.call(this.object); this.onStartCallbackFired = true; } var elapsed = (time - this.startTime) / this.duration; elapsed = elapsed > 1 ? 1 : elapsed; var value = this.easingFunction(elapsed); for (property in this.valuesEnd) { var start = this.valuesStart[property], end = this.valuesEnd[property], fraction = start + (end - start) * value, actualObject = this.map[property]; this.object[property] = fraction; actualObject[0][actualObject[1]] = fraction; } if (this.onUpdateCallback !== null) this.onUpdateCallback.call(this.object, value, time); if (elapsed == 1) { if (this.repeatTimes > 0) { if (isFinite(this.repeatTimes)) this.repeatTimes--; for (property in this.valuesStartRepeat) { if (typeof this.valuesEnd[property] === 'string') this.valuesStartRepeat[property] = this.valuesStartRepeat[property] + parseFloat(this.valuesEnd[property], 10); if (this.repeatMode === Tween.Repeat.Yoyo) { var tmp = this.valuesStartRepeat[property]; this.valuesStartRepeat[property] = this.valuesEnd[property]; this.valuesEnd[property] = tmp; } this.valuesStart[property] = this.valuesStartRepeat[property]; } if (this.repeatMode === Tween.Repeat.Yoyo) this.reversed = !this.reversed; this.startTime = time + this.delayTime; return true; } else { if (this.onCompleteCallback !== null) this.onCompleteCallback.call(this.object); if (this.thenCallback !== null) { this.thenCallback.call(this.object); this.thenCallback = null; } return false; } } return true; } }], [{ key: 'getAll', value: function getAll() { return tweens; } }, { key: 'removeAll', value: function removeAll() { tweens = []; } }, { key: 'add', value: function add(tween) { tweens.push(tween); } }, { key: 'remove', value: function remove(tween) { var i = tweens.indexOf(tween); if (i !== -1) tweens.splice(i, 1); } }, { key: 'removeObjectTweens', value: function removeObjectTweens(object) { Tween.getAll().forEach(function (item) { if (item.parent == object) { item.stop(); } }); } }, { key: 'update', value: function update(time, renderer) { time = time !== undefined ? time : window.performance.now(); if (tweens.length === 0) return false; var index = 0, length = tweens.length; for (index; index < length; index++) { var tween = tweens[index]; if (!tween.update(time)) { tweens.splice(index--, 1); length--; } else if (!tween.paused && !!tween.parent.view) { tween.parent.view.invalidate(); } else if (!tween.paused && renderer) { renderer.invalidateViews(); } } return true; } }]); return Tween; }(); exports.default = Tween; Tween.Repeat = { Normal: 1, Yoyo: 2 }; function flatten(parent, source, pathArray, result) { // Roll out merged arrays if (Array.isArray(source) && source.length == 1 && (0, _typeof3.default)(source[0]) === 'object') { source = mergeArray(parent, pathArray.join('.'), source[0], true); } pathArray = typeof pathArray === 'undefined' ? [] : pathArray; result = typeof result === 'undefined' ? {} : result; var key = void 0, value = void 0, newKey = void 0; for (var i in source) { if (source.hasOwnProperty(i)) { key = i; value = source[i]; pathArray.push(key); if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object' && value !== null) { if (value instanceof THREE.Euler) { result[key + '.x'] = value.x; result[key + '.y'] = value.y; result[key + '.z'] = value.z; } else result = flatten(parent, value, pathArray, result); } else if (typeof value === 'number') { newKey = pathArray.join('.'); result[newKey] = value; } pathArray.pop(); } } return result; } function ref(obj, str) { return str.split('.').reduce(function (o, x) { return o[x]; }, obj); } function returnValue(obj, key) { var parts = key.split(/\.(?=[^.]+$)/); if (parts.length == 1) return [obj, parts[0], parts.length];else return [ref(obj, parts[0]), parts[1], parts.length]; } function mergeArray(root, path, properties, justContent) { var object = {}, result = [], map = returnValue(root, path); map[0][map[1]].forEach(function (item) { if (Array.isArray(properties)) { var array = {}; properties.forEach(function (prop) { var obj = {}; obj[prop] = item[prop]; (0, _assign2.default)(array, obj); }); result.push(array); } else if ((typeof properties === 'undefined' ? 'undefined' : (0, _typeof3.default)(properties)) === 'object') { result.push(properties); } }); if (map[2] == 1 || !!justContent) object = result;else object[map[1]] = result; return object; }