Last active
July 9, 2020 08:35
-
-
Save 0xf0f0f0/594b92fe7fb8b039ea7001a33ffaf4e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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