Skip to content

Commit

Permalink
do not change animation times & start when using #addOffset; fix #add…
Browse files Browse the repository at this point in the history
…Offset working with shortRotation
  • Loading branch information
David Schäfer committed Jun 4, 2018
1 parent 776e712 commit 04275dc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
35 changes: 24 additions & 11 deletions src/angle-attriboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export default class AngleAttriboot extends NumberAttriboot {

constructor({

shortRotation: shortRotation = false,
wrap: wrap = false
shortRotation: shortRotation = false,
wrap: wrap = false

} = {}

) {
} = {}) {

super(...arguments);

this._shortRotation = shortRotation;
Expand Down Expand Up @@ -48,18 +47,23 @@ export default class AngleAttriboot extends NumberAttriboot {
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)
if (this._wrap && this._shortRotation && !this._isAddingOffset)
this._applyShortRotation();

this._triggerChange('target', this._target);
Expand Down Expand Up @@ -130,6 +134,7 @@ export default class AngleAttriboot extends NumberAttriboot {

/**
* Adds `offset` to `target` and `current`.
* Will not reset animation times.
* @param {number} offset
* @override
*/
Expand All @@ -139,7 +144,11 @@ export default class AngleAttriboot extends NumberAttriboot {

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;
Expand Down Expand Up @@ -170,12 +179,16 @@ export default class AngleAttriboot extends NumberAttriboot {
* @private
*/
_applyShortRotation() {
this._start = this._wrapTo360Degrees(this._start);

// fix for short rotation
while (this._start - this._target > 180)
while (this._start - this._target > 180) {
this._start -= 360;
}

while (this._start - this._target < -180)
while (this._start - this._target < -180) {
this._start += 360;
}
}

/**
Expand Down
29 changes: 17 additions & 12 deletions src/number-attriboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ export default class NumberAttriboot extends BaseAttriboot {

constructor({

value: value = 0,
min: min = Number.NEGATIVE_INFINITY,
max: max = Number.POSITIVE_INFINITY,
exclusiveMin: exclusiveMin = false,
exclusiveMax: exclusiveMax = false,
exclusivePrecision: exclusivePrecision = Math.pow(10, -16)
value: value = 0,
min: min = Number.NEGATIVE_INFINITY,
max: max = Number.POSITIVE_INFINITY,
exclusiveMin: exclusiveMin = false,
exclusiveMax: exclusiveMax = false,
exclusivePrecision: exclusivePrecision = Math.pow(10, -16)

} = {}

) {
} = {}) {

super(...arguments);

Expand Down Expand Up @@ -66,11 +64,13 @@ export default class NumberAttriboot extends BaseAttriboot {

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);

Expand Down Expand Up @@ -353,6 +353,7 @@ export default class NumberAttriboot extends BaseAttriboot {

/**
* Adds `offset` to `target` and `current`.
* Will not reset animation times.
* @param {number} offset
*/
addOffset(offset) {
Expand All @@ -361,7 +362,11 @@ export default class NumberAttriboot extends BaseAttriboot {

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;
Expand Down
6 changes: 5 additions & 1 deletion test/test.angle-attriboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ describe('attriboots', () => {
it('should work', () => {

attriboot.target = 5;
expect(attriboot._start).to.equal(0);
expect(attriboot.current).to.equal(0);
expect(attriboot.target).to.equal(5);

attriboot.addOffset(2);
expect(attriboot._start).to.equal(2);
expect(attriboot.current).to.equal(2);
expect(attriboot.target).to.equal(7);
});
Expand All @@ -187,14 +189,16 @@ describe('attriboots', () => {

attriboot.target = 350;
attriboot.addOffset(-5);
expect(attriboot._start).to.equal(355);
expect(attriboot.current).to.equal(355);
expect(attriboot.target).to.equal(345);

attriboot.target = 0;
attriboot.updateImmediate();

attriboot.target = 350;
attriboot.addOffset(-730);
expect(attriboot._start).to.equal(350);
expect(attriboot.current).to.equal(350);
expect(attriboot.target).to.equal(340);
});
Expand Down

0 comments on commit 04275dc

Please sign in to comment.