Skip to content

Commit

Permalink
Merge pull request #184 from MageStudio/better-tweening
Browse files Browse the repository at this point in the history
better tweening, now exporting tweento function
  • Loading branch information
marco-ponds authored Aug 12, 2022
2 parents ac48507 + 5d88bfe commit ed30f9d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/rotateTo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class Intro extends Level {
cube.setMaterialFromName(constants.MATERIALS.STANDARD, { roughness: .5, metalness: 0 });
setTimeout(() => {
cube.rotateTo({ x: 4, y: 4, z: 4 }, 5000, { loop: easing.LOOPING.BOUNCE, easing: easing.FUNCTIONS.Quadratic.InOut });
cube.scaleTo({ x: 2, y: 2, z: 2 }, 5000, { loop: easing.LOOPING.BOUNCE });
cube.scaleTo({ x: 2, y: 2, z: 2 }, 5000, { loop: easing.LOOPING.BOUNCE }).then(() => console.log('done'));
}, 2000)
}
}
Expand Down
59 changes: 13 additions & 46 deletions src/entities/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
ENTITY_TYPES,
FLAT_ENTITY_TYPES
} from './constants';
import { FUNCTIONS, LOOPING }from '../lib/easing';
import { FUNCTIONS, LOOPING, tweenTo }from '../lib/easing';

export default class Entity extends EventDispatcher {

Expand Down Expand Up @@ -521,61 +521,28 @@ export default class Entity extends EventDispatcher {
}
}

scaleTo(scale = this.getScale(), time = 250, { easing = FUNCTIONS.Linear.None, loop = LOOPING.NONE } = {}) {
scaleTo(scale = this.getScale(), time = 250, options = {}) {
const { x, y, z } = this.getScale();
const targetScale = { x, y, z, ...scale };
const target = { x, y, z, ...scale };
const onUpdate = value => !this.isDisposed() && this.setScale(value)

return new Promise(resolve => {
const between = new Between({ x, y, z}, targetScale)
.time(time)
.easing(easing);

if (loop) {
between.loop(loop);
}

between
.on('update', value => !this.isDisposed() && this.setScale(value))
.on('complete', resolve)
})
return tweenTo({ x, y, z }, target, { ...options, time, onUpdate });
}

rotateTo(rotation = this.getRotation(), time = 250, { easing = FUNCTIONS.Linear.None, loop = LOOPING.NONE } = {}) {
rotateTo(rotation = this.getRotation(), time = 250, options = {}) {
const { x, y, z } = this.getRotation();
const targetRotation = { x, y, z, ...rotation };
const target = { x, y, z, ...rotation };
const onUpdate = value => !this.isDisposed() && this.setRotation(value)

return new Promise((resolve) => {
const between = new Between({ x, y, z}, targetRotation)
.time(time)
.easing(easing);

if (loop) {
between.loop(loop);
}

between
.on('update', value => !this.isDisposed() && this.setRotation(value))
.on('complete', resolve)
});
return tweenTo({ x, y, z }, target, { ...options, time, onUpdate });
}

goTo(position = this.getPosition(), time = 250, { easing = FUNCTIONS.Linear.None, loop = LOOPING.NONE } = {}) {
goTo(position = this.getPosition(), time = 250, options = {}) {
const { x, y, z } = this.getPosition();
const targetPosition = { x, y, z, ...position };
const target = { x, y, z, ...position };
const onUpdate = value => !this.isDisposed() && this.setPosition(value);

return new Promise((resolve) => {
const between = new Between({ x, y, z}, targetPosition)
.time(time)
.easing(easing);

if (loop) {
between.loop(loop);
}

between
.on('update', value => !this.isDisposed() && this.setPosition(value))
.on('complete', resolve)
});
return tweenTo({ x, y, z }, target, { ...options, time, onUpdate });
}

setUuid = (uuid) => {
Expand Down
36 changes: 36 additions & 0 deletions src/lib/easing.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,43 @@ export const LOOPING = {
NONE: false
};

export const EASING_EVENTS = {
UPDATE: 'update',
COMPLETE: 'complete'
};

export const FUNCTIONS = {
...between.Easing
};

export const tweenTo = (origin, target, options = {}) => (
new Promise((resolve) => {
const {
time,
easing = FUNCTIONS.Linear.None,
loop = LOOPING.NONE,
onUpdate = f => f,
repeat = undefined
} = options;

const tween = new between(origin, target)
.time(time)
.easing(easing)
.on(EASING_EVENTS.UPDATE, onUpdate)

const infinite = loop && !repeat;
const onComplete = () => resolve(tween, infinite);

if (loop) {
tween.loop(loop, repeat);
}

if (infinite) {
const timeToCompleteLoop = time * 2;
setTimeout(onComplete, timeToCompleteLoop);
} else {
tween.on(EASING_EVENTS.COMPLETE, onComplete);
}
})
);

0 comments on commit ed30f9d

Please sign in to comment.