Skip to content

Commit

Permalink
format code, ensure CI uses clean-install for reproducible builds, re…
Browse files Browse the repository at this point in the history
…name 'lint' script to 'format', add a Three.js example to README
  • Loading branch information
trusktr committed May 5, 2024
1 parent 78120c5 commit 6415312
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm clean-install
- run: npm test
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ JavaScript (TypeScript) tweening engine for easy animations, incorporating optim

More languages: [English](./README.md), [简体中文](./README_zh-CN.md)

---
# Example

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/20.0.0/tween.umd.js"></script>
Expand Down Expand Up @@ -48,7 +48,10 @@ More languages: [English](./README.md), [简体中文](./README_zh-CN.md)
</script>
```

[Try this example on CodePen](https://codepen.io/trusktr/pen/KKGaBVz?editors=1000)
[Try the above example on CodePen](https://codepen.io/trusktr/pen/KKGaBVz?editors=1000)

Animate numbers in any JavaScript object. For example, [rotate a 3D box made
with Three.js](https://codepen.io/trusktr/pen/ExJqvgZ):

# Installation

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"tsc": "tsc",
"tsc-watch": "tsc --watch",
"examples": "npx serve .",
"test": "npm run build && npm run test-lint && npm run test-unit",
"test": "npm run build && npm run format-check && npm run test-unit",
"test-unit": "nodeunit test/unit/nodeunitheadless.cjs",
"test-lint": "npm run prettier -- --check",
"lint": "npm run prettier -- --write",
"format-check": "npm run prettier -- --check",
"format": "npm run prettier -- --write",
"prettier": "prettier .",
"prepare": "npm run build",
"version": "npm test && git add .",
Expand Down
4 changes: 2 additions & 2 deletions src/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class Tween<T extends UnknownProps> {
const elapsed = this._calculateElapsedPortion(elapsedTime, durationAndDelay, totalTime)
const value = this._easingFunction(elapsed)

const status = this._calculateCompletionStatus(elapsedTime, durationAndDelay);
const status = this._calculateCompletionStatus(elapsedTime, durationAndDelay)

if (status === 'repeat') {
// the current update is happening after the instant the tween repeated
Expand All @@ -463,7 +463,7 @@ export class Tween<T extends UnknownProps> {

this._onEveryStartCallbackFired = false
} else if (status === 'completed') {
this._isPlaying = false;
this._isPlaying = false

if (this._onCompleteCallback) {
this._onCompleteCallback(this._object)
Expand Down
109 changes: 50 additions & 59 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,22 +1286,18 @@ export const tests = {
'Test repeat behaves the same with quick and slow updates'(test: Test): void {
TWEEN.removeAll()

const makeTween = (obj: { x: number }) =>
new TWEEN.Tween(obj)
.to({x: 100}, 100)
.repeat(20)
.start(0);
const makeTween = (obj: {x: number}) => new TWEEN.Tween(obj).to({x: 100}, 100).repeat(20).start(0)

const obj1 = {x: 0};
const tween1 = makeTween(obj1);
const obj1 = {x: 0}
const tween1 = makeTween(obj1)

for (let t = 0; t <= 300; t += 25) {
tween1.update(t);
tween1.update(t)

const obj2 = {x: 0};
const tween2 = makeTween(obj2);
tween2.update(t);
test.equal(obj1.x, obj2.x, `t=${t}: ${obj1.x} === ${obj2.x}`);
const obj2 = {x: 0}
const tween2 = makeTween(obj2)
tween2.update(t)
test.equal(obj1.x, obj2.x, `t=${t}: ${obj1.x} === ${obj2.x}`)
}

test.done()
Expand All @@ -1310,23 +1306,18 @@ export const tests = {
'Test repeat+delay behaves the same with quick and slow updates'(test: Test): void {
TWEEN.removeAll()

const makeTween = (obj: { x: number }) =>
new TWEEN.Tween(obj)
.to({x: 100}, 100)
.delay(50)
.repeat(20)
.start(0);
const makeTween = (obj: {x: number}) => new TWEEN.Tween(obj).to({x: 100}, 100).delay(50).repeat(20).start(0)

const obj1 = {x: 0};
const tween1 = makeTween(obj1);
const obj1 = {x: 0}
const tween1 = makeTween(obj1)

for (let t = 0; t <= 300; t += 25) {
tween1.update(t);
tween1.update(t)

const obj2 = {x: 0};
const tween2 = makeTween(obj2);
tween2.update(t);
test.equal(obj1.x, obj2.x, `t=${t}: ${obj1.x} === ${obj2.x}`);
const obj2 = {x: 0}
const tween2 = makeTween(obj2)
tween2.update(t)
test.equal(obj1.x, obj2.x, `t=${t}: ${obj1.x} === ${obj2.x}`)
}

test.done()
Expand Down Expand Up @@ -1459,38 +1450,38 @@ export const tests = {
},

'Test yoyo reverses at right instant'(test: Test): void {
TWEEN.removeAll();
TWEEN.removeAll()

const obj = { x: 0 };
new TWEEN.Tween(obj).to({ x: 100 }, 100).repeat(1).yoyo(true).start(0);
const obj = {x: 0}
new TWEEN.Tween(obj).to({x: 100}, 100).repeat(1).yoyo(true).start(0)

TWEEN.update(98);
test.equal(obj.x, 98);
TWEEN.update(98)
test.equal(obj.x, 98)

TWEEN.update(99);
test.equal(obj.x, 99);
TWEEN.update(99)
test.equal(obj.x, 99)

// Previously this would fail, the first update after 100 would happen as if yoyo=false
TWEEN.update(101);
test.equal(obj.x, 99);
TWEEN.update(101)
test.equal(obj.x, 99)

TWEEN.update(101);
test.equal(obj.x, 99);
TWEEN.update(101)
test.equal(obj.x, 99)

TWEEN.update(102);
test.equal(obj.x, 98);
TWEEN.update(102)
test.equal(obj.x, 98)

test.done();
test.done()
},

'Test yoyo callbacks happen on right order'(test: Test): void {
TWEEN.removeAll();
TWEEN.removeAll()

let events: string[] = [];
const obj = { x: 0 }
let events: string[] = []
const obj = {x: 0}

new TWEEN.Tween(obj)
.to({ x: 100 }, 100)
.to({x: 100}, 100)
.repeat(1)
.yoyo(true)
.easing(TWEEN.Easing.Linear.None)
Expand All @@ -1499,26 +1490,26 @@ export const tests = {
.onEveryStart(() => events.push('everystart'))
.onRepeat(() => events.push('repeat'))
.onComplete(() => events.push('complete'))
.start(0);
.start(0)

function testAndReset(expected: string[]) {
test.deepEqual(events, expected);
events = [];
test.deepEqual(events, expected)
events = []
}

testAndReset([]);
TWEEN.update(99);
testAndReset(['start', 'everystart', 'update']);
TWEEN.update(101);
testAndReset(['update', 'repeat']);
TWEEN.update(150);
testAndReset(['everystart', 'update']);
TWEEN.update(199);
testAndReset(['update']);
TWEEN.update(201);
testAndReset(['update', 'complete']);

test.done();
testAndReset([])
TWEEN.update(99)
testAndReset(['start', 'everystart', 'update'])
TWEEN.update(101)
testAndReset(['update', 'repeat'])
TWEEN.update(150)
testAndReset(['everystart', 'update'])
TWEEN.update(199)
testAndReset(['update'])
TWEEN.update(201)
testAndReset(['update', 'complete'])

test.done()
},

'Test TWEEN.Tween.stopChainedTweens()'(test: Test): void {
Expand Down

0 comments on commit 6415312

Please sign in to comment.