Skip to content

Commit

Permalink
Merge pull request #698 from J00nz/main
Browse files Browse the repository at this point in the history
#697: Added "setNow"
  • Loading branch information
trusktr authored Jan 11, 2025
2 parents eb07dd2 + 76085cd commit 87e6a43
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 10 deletions.
10 changes: 9 additions & 1 deletion dist/tween.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ define(['exports'], (function (exports) { 'use strict';
},
});

var now = function () { return performance.now(); };
var _nowFunc = function () { return performance.now(); };
var now = function () {
return _nowFunc();
};
function setNow(nowFunction) {
_nowFunc = nowFunction;
}

/**
* Controlling groups of tweens
Expand Down Expand Up @@ -1139,6 +1145,7 @@ define(['exports'], (function (exports) { 'use strict';
Group: Group,
Interpolation: Interpolation,
now: now,
setNow: setNow,
Sequence: Sequence,
nextId: nextId,
Tween: Tween,
Expand Down Expand Up @@ -1398,6 +1405,7 @@ define(['exports'], (function (exports) { 'use strict';
exports.now = now;
exports.remove = remove;
exports.removeAll = removeAll;
exports.setNow = setNow;
exports.update = update;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
10 changes: 9 additions & 1 deletion dist/tween.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ var Easing = Object.freeze({
},
});

var now = function () { return performance.now(); };
var _nowFunc = function () { return performance.now(); };
var now = function () {
return _nowFunc();
};
function setNow(nowFunction) {
_nowFunc = nowFunction;
}

/**
* Controlling groups of tweens
Expand Down Expand Up @@ -1141,6 +1147,7 @@ var exports$1 = {
Group: Group,
Interpolation: Interpolation,
now: now,
setNow: setNow,
Sequence: Sequence,
nextId: nextId,
Tween: Tween,
Expand Down Expand Up @@ -1400,4 +1407,5 @@ exports.nextId = nextId;
exports.now = now;
exports.remove = remove;
exports.removeAll = removeAll;
exports.setNow = setNow;
exports.update = update;
4 changes: 3 additions & 1 deletion dist/tween.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ declare class Group {
}

declare const now: () => number;
declare function setNow(nowFunction: Function): void;

/**
* Utils
Expand Down Expand Up @@ -470,6 +471,7 @@ declare const exports: {
};
};
now: () => number;
setNow: typeof setNow;
Sequence: typeof Sequence;
nextId: typeof Sequence.nextId;
Tween: typeof Tween;
Expand Down Expand Up @@ -719,4 +721,4 @@ declare const exports: {
};
};

export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, update };
export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, setNow, update };
11 changes: 9 additions & 2 deletions dist/tween.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@ var Easing = Object.freeze({
},
});

var now = function () { return performance.now(); };
var _nowFunc = function () { return performance.now(); };
var now = function () {
return _nowFunc();
};
function setNow(nowFunction) {
_nowFunc = nowFunction;
}

/**
* Controlling groups of tweens
Expand Down Expand Up @@ -1137,6 +1143,7 @@ var exports = {
Group: Group,
Interpolation: Interpolation,
now: now,
setNow: setNow,
Sequence: Sequence,
nextId: nextId,
Tween: Tween,
Expand Down Expand Up @@ -1383,4 +1390,4 @@ var exports = {
update: update,
};

export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, update };
export { Easing, Group, Interpolation, Sequence, Tween, VERSION, add, exports as default, getAll, nextId, now, remove, removeAll, setNow, update };
10 changes: 9 additions & 1 deletion dist/tween.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,13 @@
},
});

var now = function () { return performance.now(); };
var _nowFunc = function () { return performance.now(); };
var now = function () {
return _nowFunc();
};
function setNow(nowFunction) {
_nowFunc = nowFunction;
}

/**
* Controlling groups of tweens
Expand Down Expand Up @@ -1143,6 +1149,7 @@
Group: Group,
Interpolation: Interpolation,
now: now,
setNow: setNow,
Sequence: Sequence,
nextId: nextId,
Tween: Tween,
Expand Down Expand Up @@ -1402,6 +1409,7 @@
exports.now = now;
exports.remove = remove;
exports.removeAll = removeAll;
exports.setNow = setNow;
exports.update = update;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
6 changes: 6 additions & 0 deletions docs/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,12 @@ Note that the interpolation function is global to all properties that are tweene

Check [06_array_interpolation](../examples/06_array_interpolation.html) for an example.

## Changing the Definition of "Now"

When working with tweening, you inevitably rely on a definition of what "now" is. By default, Tween.js uses performance.now, which is a reliable and precise approach. However, if you need to adjust the flow of time—for instance, to slow it down or manipulate it for a custom purpose—you may encounter discrepancies between your internal definition of "now" and what Tween.js considers "now."

To address this, a new function, setNow, has been introduced. This function allows you to redefine the internal "now" used by Tween.js. You can pass a custom function to setNow, which will replace the default definition. This provides greater flexibility and enables synchronization with your specific requirements for time control.

## Getting the best performance

While Tween.js tries to be performant on its own, nothing prevents you from using it in a way that is counterperformant. Here are some of the ways you can avoid slowing down your projects when using Tween.js (or when animating in the web, in general).
Expand Down
2 changes: 1 addition & 1 deletion scripts/write-version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import pkg from '../package.json' assert {type: 'json'}
import pkg from '../package.json' with {type: 'json'}

const {version} = pkg

Expand Down
5 changes: 3 additions & 2 deletions src/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Easing from './Easing'
import Group from './Group'
import Interpolation from './Interpolation'
import now from './Now'
import now, { setNow } from './Now'
import Sequence from './Sequence'
import Tween from './Tween'
import VERSION from './Version'
Expand Down Expand Up @@ -273,13 +273,14 @@ const update = TWEEN.update.bind(TWEEN)

// NOTE! Make sure both lists of exports below are kept in sync:

export {Easing, Group, Interpolation, now, Sequence, nextId, Tween, VERSION, getAll, removeAll, add, remove, update}
export {Easing, Group, Interpolation, now, setNow, Sequence, nextId, Tween, VERSION, getAll, removeAll, add, remove, update}

const exports = {
Easing,
Group,
Interpolation,
now,
setNow,
Sequence,
nextId,
Tween,
Expand Down
10 changes: 9 additions & 1 deletion src/Now.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const now = (): number => performance.now()
let _nowFunc: Function = () => performance.now()

const now = (): number => {
return _nowFunc()
}

export function setNow(nowFunction: Function) {
_nowFunc = nowFunction
}

export default now

0 comments on commit 87e6a43

Please sign in to comment.