Skip to content

Commit

Permalink
add Timeline::update (preparing to add resizing)
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed Jan 13, 2024
1 parent 309dbcc commit e572320
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/animation/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export default class Animation {
return this.timing.duration;
}

init() {
if (this._initialized)
init(reset = false) {
if (this._initialized && !reset)
return;
this._initialized = true;

Expand Down
33 changes: 32 additions & 1 deletion src/tests/properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TestTicker from '../timing/testticker.js';
import { animate, timeline } from '../chnobli.js';
import { el } from '../utils/testdomnode.js';

describe('usecases', () => {
describe('properties', () => {
it('resetprops', () => {
const ticker = new TestTicker;

Expand All @@ -25,4 +25,35 @@ describe('usecases', () => {
expect(div.style.transform).toBe('');
expect(div.style.width).toBe(undefined);
});

it('update timeline', () => {
const ticker = new TestTicker;

const div = el();
div.computedStyle.width = '10px';

const tl = timeline()
.add(div, {
width: 100,
duration: 10
});

tl.seek(0);
ticker.run();
expect(div.style.width).toBe('10.000px');

tl.play();
ticker.run(1);
expect(div.style.width).toBe('19.000px');

// on resize the div changes it's size
div.computedStyle.width = '20px';
tl.update();
expect(div.style.width).toBe('28.000px');

ticker.run(1);
expect(div.style.width).toBe('36.000px');
ticker.run();
expect(div.style.width).toBe('100.000px');
});
});
7 changes: 7 additions & 0 deletions src/timeline/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ export default class PublicTimeline {
this._inner.ticker.applyTargets();
}

/**
* Recalculates all values this should be called for example after a resize
*/
update() {
this._inner.update();
}

/**
* Returns wether the timeline is set to reversed
*/
Expand Down
22 changes: 19 additions & 3 deletions src/timeline/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default class Timeline {
// console.log(this.entries.map(e => e.value._props[0]));
}

_initAnimations() {
// we don't won't to have a reversed timing
_initAnimations(reset = false) {
// we don't wan't to have a reversed timing
const reversed = this.timing.reversed;
this.timing.reversed = false;

Expand All @@ -161,7 +161,7 @@ export default class Timeline {
prevAnimation.render();
}

entry.value.init();
entry.value.init(reset);
}

this.timing.seek(-1);
Expand Down Expand Up @@ -221,6 +221,22 @@ export default class Timeline {
}
}
}

update() {
const pos = this.timing.savePosition();
this.timing.seek(-1);
this._updateTimings();
this.render();
this.ticker.applyTargets();
// we now have removed every style we set
// we should be able to recalculate everything now
this._initAnimations(true);

this.timing.restorePosition(pos);
this._updateTimings();
this.render();
this.ticker.applyTargets();
}
}


Expand Down
10 changes: 10 additions & 0 deletions src/timing/timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ export default class Timing {
}
}

savePosition() {
return {
progress: this._progress
};
}

restorePosition(pos) {
this._updateProgress(pos.progress);
}

setAlternate(alternate) {
if (this.alternate == alternate)
return;
Expand Down
3 changes: 3 additions & 0 deletions src/utils/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export class DomTarget extends Target {
const style = this._extFns.getComputedStyle(this.target);
const styleV = style[name];

if (typeof styleV === 'undefined' || styleV === null)
return styleV;

try {
const value = Value.parse(styleV);
this.styleValues.set(name, value);
Expand Down
8 changes: 7 additions & 1 deletion src/utils/testdomnode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default class TestDomNode {
constructor() {
this.computedStyle = {};

this.style = {
setProperty: (k, v) => {
this.style[k] = v;
Expand All @@ -10,8 +12,12 @@ export default class TestDomNode {
};
}

syncStyles() {
this.computedStyle = { ...this.style };
}

static getComputedStyle(el) {
return el.style;
return el.computedStyle;
}

__simulatedDom__() {}
Expand Down

0 comments on commit e572320

Please sign in to comment.