diff --git a/bower.json b/bower.json index 1a6e5a4..0777eeb 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "name": "attriboots", "description": "", "homepage": "https://github.com/okitu/attriboots", - "version": "0.0.12", + "version": "0.0.13", "main": "dist/attriboots.js", "author": { "name": "David Schäfer", diff --git a/dist/attriboots.js b/dist/attriboots.js index 3b422db..d65c3a9 100644 --- a/dist/attriboots.js +++ b/dist/attriboots.js @@ -1,5 +1,5 @@ /** - * attriboots@0.0.12 + * attriboots@0.0.13 * https://github.com/okitu/attriboots * * @license @@ -31,7 +31,7 @@ (function(global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : - (factory((global.attriboots = global.attriboots || {}))); + (factory((global.attriboots = {}))); }(this, (function(exports) { 'use strict'; @@ -818,6 +818,7 @@ /** * Adds `offset` to `target` and `current`. + * Will not reset animation times. * @param {number} offset */ @@ -828,7 +829,11 @@ if (!this.locked && offset !== 0) { + // Prevent animation start & times from changing + this._isAddingOffset = true; + this.target += offset; + this._isAddingOffset = false; // Target may have been clamped var actualOffset = this._target - this._lastTarget; @@ -905,11 +910,13 @@ this._lastTarget = this._target; - this._start = this.current; - this._startTime = this._currentTime = Date.now(); + if (!this._isAddingOffset) { + this._start = this.current; + this._startTime = this._currentTime = Date.now(); + this._targetTime = this._startTime + this._animationTime; + } this._target = target; - this._targetTime = Date.now() + this._animationTime; this._triggerChange('target', this._target); @@ -1142,6 +1149,7 @@ /** * Adds `offset` to `target` and `current`. + * Will not reset animation times. * @param {number} offset * @override */ @@ -1153,7 +1161,11 @@ if (!this.locked && offset !== 0) { + // Prevent animation start & times from changing + this._isAddingOffset = true; + this.target += offset; + this._isAddingOffset = false; // Target may have been clamped var actualOffset = this.target - this._lastTarget; @@ -1185,10 +1197,13 @@ }, { key: '_applyShortRotation', value: function _applyShortRotation() { + this._start = this._wrapTo360Degrees(this._start); + // fix for short rotation while (this._start - this._target > 180) { this._start -= 360; } + while (this._start - this._target < -180) { this._start += 360; } @@ -1241,17 +1256,21 @@ if (!this._ignoreBounds) target = this._clamp(target); + if (this._wrap) target = this._wrapTo360Degrees(target); + if (target == this._target) return; this._lastTarget = this.target; - this._start = this.current; - this._startTime = this._currentTime = Date.now(); + if (!this._isAddingOffset) { + this._start = this.current; + this._startTime = this._currentTime = Date.now(); + this._targetTime = this._startTime + this._animationTime; + } this._target = target; - this._targetTime = Date.now() + this._animationTime; - if (this._wrap && this._shortRotation) this._applyShortRotation(); + if (this._wrap && this._shortRotation && !this._isAddingOffset) this._applyShortRotation(); this._triggerChange('target', this._target); diff --git a/dist/attriboots.min.js b/dist/attriboots.min.js index 3625a5f..966c496 100644 --- a/dist/attriboots.min.js +++ b/dist/attriboots.min.js @@ -1,5 +1,5 @@ /** - * attriboots@0.0.12 + * attriboots@0.0.13 * https://github.com/okitu/attriboots * * @license @@ -27,4 +27,4 @@ * SOFTWARE. * @license */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.attriboots=t.attriboots||{})}(this,function(t){"use strict";var e=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(t,e){for(var i=0;i0&&void 0!==arguments[0]?arguments[0]:{},n=i.id,r=void 0===n?null:n,a=i.enabled,o=void 0===a||a,u=i.ignoreBounds,h=void 0!==u&&u,c=i.animationTime,_=void 0===c?300:c,l=i.easing,f=void 0===l?s.outQuad:l,g=i.locked,d=void 0!==g&&g;if(e(this,t),this.constructor===t)throw new TypeError("BaseAttriboot is an abstract class and cannot be instantiated directly.");this._id=r,this.enabled=o,this.locked=d,this.ignoreBounds=h,this.animationTime=_,this.easing=f,this._updated=!1}return i(t,[{key:"update",value:function(){return!1}},{key:"updateImmediate",value:function(){return!1}},{key:"stop",value:function(){return!1}},{key:"addEventListener",value:function(t,e){this._listeners||(this._listeners={}),t in this._listeners||(this._listeners[t]=[]),this._listeners[t].push(e)}},{key:"removeEventListener",value:function(t,e){if(t in this._listeners)for(var i=this._listeners[t],n=0,r=i.length;n0&&void 0!==arguments[0]?arguments[0]:{},i=t.value,n=void 0===i?0:i,a=t.min,o=void 0===a?Number.NEGATIVE_INFINITY:a,u=t.max,h=void 0===u?Number.POSITIVE_INFINITY:u,c=t.exclusiveMin,_=void 0!==c&&c,l=t.exclusiveMax,f=void 0!==l&&l,g=t.exclusivePrecision,d=void 0===g?Math.pow(10,-16):g;e(this,s);var p=r(this,(s.__proto__||Object.getPrototypeOf(s)).apply(this,arguments));return p._target=n,p._lastTarget=n,p._current=n,p._previous=n,p._raw=n,p._stored=0,p.min=o,p.max=h,p.exclusiveMin=_,p.exclusiveMax=f,p.exclusivePrecision=d,p}return n(s,t),i(s,[{key:"_clamp",value:function(t){return this.min===this.max?this.min:t<=this.min?this.exclusiveMin?this.min+this.exclusivePrecision:this.min:t>=this.max?this.exclusiveMax?this.max-this.exclusivePrecision:this.max:t}},{key:"update",value:function(t){if(void 0===t)t=Date.now()-this._startTime;else{if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"delta" must be a positive number');t=Math.round(Math.abs(t))}if(this.dirty){this._previous=this._current,this._currentTime+=t;var e=Math.min((this._currentTime-this._startTime)/(this._targetTime-this._startTime),1);this._current=this.easing(e,this._start,this._target-this._start,1),this._updated=!0}else this._updated=!1;return this._updated&&this._triggerUpdate(),this._updated}},{key:"updateImmediate",value:function(){return this.dirty?(this._previous=this._current,this._current=this._target,this._updated=!0):this._updated=!1,this._updated&&this._triggerUpdate(),this._updated}},{key:"stop",value:function(){var t;return this.dirty?(this._lastTarget=this._target,this._target=this._current,t=!0):t=!1,t&&this._triggerChange(),t}},{key:"apply",value:function(t){this.target=t,this.updateImmediate()}},{key:"addOffset",value:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"offset" must be a number');if(!this.locked&&0!==t){this.target+=t;var e=this._target-this._lastTarget;0!==e&&(this._start+=e,this._current+=e,this._updated=!0,this._triggerUpdate())}}},{key:"store",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:NaN;if("number"!=typeof t)throw new TypeError('"value" must be a number');this._stored=isNaN(t)?this.target:t}},{key:"restore",value:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];if("boolean"!=typeof t)throw new TypeError('"ignoreLock" must be a boolean');var e=this._locked;t&&(this._locked=!1),this.target=this.stored,this._locked=e}},{key:"raw",get:function(){return this._raw}},{key:"target",get:function(){return this._target},set:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"target" must be a number');this._locked||(this._raw=t,this._ignoreBounds||(t=this._clamp(t)),t!=this._target&&(this._lastTarget=this._target,this._start=this.current,this._startTime=this._currentTime=Date.now(),this._target=t,this._targetTime=Date.now()+this._animationTime,this._triggerChange("target",this._target),0===this._animationTime&&this.updateImmediate()))}},{key:"lastTarget",get:function(){return this._lastTarget}},{key:"current",get:function(){return this._current}},{key:"previous",get:function(){return this._previous}},{key:"min",get:function(){return this._min},set:function(t){if(null!==t&&void 0!==t||(t=Number.NEGATIVE_INFINITY),"number"!=typeof t||isNaN(t))throw new TypeError('"min" must be a number');t>=this._max&&(t=this.exclusiveMax?this.max-this.exclusivePrecision:this.max),t!=this._min&&(this._min=t,this._triggerChange("min",this._min),this.target=this._raw)}},{key:"max",get:function(){return this._max},set:function(t){if(null!==t&&void 0!==t||(t=Number.POSITIVE_INFINITY),"number"!=typeof t||isNaN(t))throw new TypeError('"max" must be a number');t<=this._min&&(t=this.exclusiveMin?this.min+this.exclusivePrecision:this.min),t!=this._max&&(this._max=t,this._triggerChange("max",this._max),this.target=this._raw)}},{key:"exclusiveMin",get:function(){return this._exclusiveMin},set:function(t){if("boolean"!=typeof t)throw new TypeError('"exclusiveMin" must be a boolean');t!=this._exclusiveMin&&(this._exclusiveMin=t,this._triggerChange("exclusiveMin",this._exclusiveMin),this.target=this._raw)}},{key:"exclusiveMax",get:function(){return this._exclusiveMax},set:function(t){if("boolean"!=typeof t)throw new TypeError('"exclusiveMax" must be a boolean');t!=this._exclusiveMax&&(this._exclusiveMax=t,this._triggerChange("exclusiveMax",this._exclusiveMax),this.target=this._raw)}},{key:"exclusivePrecision",get:function(){return this._exclusivePrecision},set:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"exclusivePrecision" must be a number');t!=this._exclusivePrecision&&(this._exclusivePrecision=t,this._triggerChange("exclusivePrecision",this._exclusivePrecision))}},{key:"stored",get:function(){return this._stored}}]),s}(a),u=function(t){function s(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=t.shortRotation,n=void 0!==i&&i,a=t.wrap,o=void 0!==a&&a;e(this,s);var u=r(this,(s.__proto__||Object.getPrototypeOf(s)).apply(this,arguments));return u._shortRotation=n,u._wrap=o,u}return n(s,t),i(s,[{key:"getTargetRadians",value:function(){return this.target*Math.PI/180}},{key:"getCurrentRadians",value:function(){return this.current*Math.PI/180}},{key:"addOffset",value:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"offset" must be a number');if(!this.locked&&0!==t){this.target+=t;var e=this.target-this._lastTarget;0!==e&&(this._start+=e,this._wrap&&this._shortRotation&&this._applyShortRotation(),this._current+=e,this._wrap&&(this._current=this._wrapTo360Degrees(this._current)),this._updated=!0,this._triggerUpdate())}}},{key:"_applyShortRotation",value:function(){for(;this._start-this._target>180;)this._start-=360;for(;this._start-this._target<-180;)this._start+=360}},{key:"_wrapTo360Degrees",value:function(t){if(t>360)return t%360;for(;t<0;)t+=360;return t}},{key:"current",get:function(){return this._wrap?this._wrapTo360Degrees(this._current):this._current}},{key:"target",get:function(){return this._wrap?this._wrapTo360Degrees(this._target):this._target},set:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"target" must be a number');this._locked||(this._raw=t,this._ignoreBounds||(t=this._clamp(t)),t!=this._target&&(this._lastTarget=this.target,this._start=this.current,this._startTime=this._currentTime=Date.now(),this._target=t,this._targetTime=Date.now()+this._animationTime,this._wrap&&this._shortRotation&&this._applyShortRotation(),this._triggerChange("target",this._target),0===this._animationTime&&this.updateImmediate()))}},{key:"wrap",get:function(){return this._wrap},set:function(t){if("boolean"!=typeof t)throw new TypeError('"wrap" must be a boolean');t!=this._wrap&&(this._wrap=t,this._triggerChange("wrap",this._wrap))}},{key:"shortRotation",get:function(){return this._shortRotation},set:function(t){if("boolean"!=typeof t)throw new TypeError('"shortRotation" must be a boolean');t!=this._shortRotation&&(this._shortRotation=t,this._triggerChange("shortRotation",this._shortRotation))}}]),s}(o);t.Easing=s,t.NumberAttriboot=o,t.AngleAttriboot=u,Object.defineProperty(t,"__esModule",{value:!0})}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.attriboots={})}(this,function(t){"use strict";var d=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},e=function(){function n(t,e){for(var i=0;i=this.max?this.exclusiveMax?this.max-this.exclusivePrecision:this.max:t}},{key:"update",value:function(t){if(void 0===t)t=Date.now()-this._startTime;else{if("number"!=typeof t||t<0||isNaN(t))throw new TypeError('"delta" must be a positive number');t=Math.round(Math.abs(t))}if(this.dirty){this._previous=this._current,this._currentTime+=t;var e=Math.min((this._currentTime-this._startTime)/(this._targetTime-this._startTime),1);this._current=this.easing(e,this._start,this._target-this._start,1),this._updated=!0}else this._updated=!1;return this._updated&&this._triggerUpdate(),this._updated}},{key:"updateImmediate",value:function(){return this.dirty?(this._previous=this._current,this._current=this._target,this._updated=!0):this._updated=!1,this._updated&&this._triggerUpdate(),this._updated}},{key:"stop",value:function(){var t;return this.dirty?(this._lastTarget=this._target,this._target=this._current,t=!0):t=!1,t&&this._triggerChange(),t}},{key:"apply",value:function(t){this.target=t,this.updateImmediate()}},{key:"addOffset",value:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"offset" must be a number');if(!this.locked&&0!==t){this._isAddingOffset=!0,this.target+=t,this._isAddingOffset=!1;var e=this._target-this._lastTarget;0!==e&&(this._start+=e,this._current+=e,this._updated=!0,this._triggerUpdate())}}},{key:"store",value:function(){var t=0=this._max&&(t=this.exclusiveMax?this.max-this.exclusivePrecision:this.max),t!=this._min&&(this._min=t,this._triggerChange("min",this._min),this.target=this._raw)}},{key:"max",get:function(){return this._max},set:function(t){if(null==t&&(t=Number.POSITIVE_INFINITY),"number"!=typeof t||isNaN(t))throw new TypeError('"max" must be a number');t<=this._min&&(t=this.exclusiveMin?this.min+this.exclusivePrecision:this.min),t!=this._max&&(this._max=t,this._triggerChange("max",this._max),this.target=this._raw)}},{key:"exclusiveMin",get:function(){return this._exclusiveMin},set:function(t){if("boolean"!=typeof t)throw new TypeError('"exclusiveMin" must be a boolean');t!=this._exclusiveMin&&(this._exclusiveMin=t,this._triggerChange("exclusiveMin",this._exclusiveMin),this.target=this._raw)}},{key:"exclusiveMax",get:function(){return this._exclusiveMax},set:function(t){if("boolean"!=typeof t)throw new TypeError('"exclusiveMax" must be a boolean');t!=this._exclusiveMax&&(this._exclusiveMax=t,this._triggerChange("exclusiveMax",this._exclusiveMax),this.target=this._raw)}},{key:"exclusivePrecision",get:function(){return this._exclusivePrecision},set:function(t){if("number"!=typeof t||isNaN(t))throw new TypeError('"exclusivePrecision" must be a number');t!=this._exclusivePrecision&&(this._exclusivePrecision=t,this._triggerChange("exclusivePrecision",this._exclusivePrecision))}},{key:"stored",get:function(){return this._stored}}]),g}(),s=function(t){function a(){var t=0