Skip to content

Instantly share code, notes, and snippets.

@0xf0f0f0
Last active July 9, 2020 08:35
Show Gist options
  • Select an option

  • Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.

Select an option

Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.
const defaultProps = {
dist: .15,
time: 10
}
export const cameraShake = (camera = null, count = 10, callback = () => {}, props = defaultProps) => {
if (!camera) {
throw new Error('Camera is not define!');
}
const basePosition = new THREE.Vector3().copy(camera.position);
let counter = count;
let dist = props.dist;
const shakeOnce = () => {
const shakeProps = {
x: basePosition.x + Math.random() * dist * 2 - dist,
y: basePosition.y + Math.random() * dist * 2 - dist
};
const tween = new TWEEN.Tween(camera.position);
tween.easing(TWEEN.Easing.Sinusoidal.InOut);
tween.to(shakeProps, props.time);
return tween.onComplete(() => {
counter--;
if (counter <= 0) {
camera.position.copy(basePosition);
callback && callback();
} else {
shakeOnce();
}
});
};
shakeOnce();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment