-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: MouseParallax, clean code (#134)
- Loading branch information
1 parent
62f677c
commit 257238a
Showing
6 changed files
with
72 additions
and
90 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { defineComponent, computed } from 'vue' | ||
import { useRenderLoop } from '@tresjs/core' | ||
import { useWindowSize, useMouse, watchOnce } from '@vueuse/core' | ||
import { useCientos } from '../../useCientos' | ||
import { Group } from 'three' | ||
|
||
export interface MouseParallaxProps { | ||
/** | ||
* Whether to disable the mouse controls. | ||
* @type {boolean} | ||
* @default false | ||
* @memberof MouseParallaxProps | ||
* | ||
*/ | ||
disabled?: boolean | ||
/** | ||
* The factor to multiply the mouse movement by. | ||
* @type {number} | ||
* @default 2.5 | ||
* @memberof MouseParallaxProps | ||
* | ||
**/ | ||
factor?: number | ||
/** | ||
* The factor to multiply the mouse movement by. | ||
* @type {number} | ||
* @default 2.5 | ||
* @memberof MouseParallaxProps | ||
* | ||
**/ | ||
ease?: number | ||
} | ||
|
||
export const MouseParallax = defineComponent<MouseParallaxProps>({ | ||
name: 'MouseParallax', | ||
props: ['disabled', 'factor', 'ease'] as unknown as undefined, | ||
setup(props) { | ||
const { state } = useCientos() | ||
|
||
const { x, y } = useMouse() | ||
const { width, height } = useWindowSize() | ||
|
||
const { onLoop } = useRenderLoop() | ||
const cameraGroup = new Group() | ||
state.scene?.add(cameraGroup) | ||
const easeFactor = props.ease || 2.5 | ||
const factor = props.factor || 2.5 | ||
|
||
const cursorX = computed(() => (x.value / width.value - 0.5) * factor) | ||
const cursorY = computed(() => -(y.value / height.value - 0.5) * factor) | ||
|
||
watchOnce(() => state.camera, () => { | ||
if(state.camera){ | ||
cameraGroup.add(state.camera) | ||
} | ||
}) | ||
|
||
onLoop(({ delta }) => { | ||
if (props.disabled) return | ||
cameraGroup.position.x += (cursorX.value - cameraGroup.position.x) * easeFactor * delta | ||
cameraGroup.position.y += (cursorY.value - cameraGroup.position.y) * easeFactor * delta | ||
}) | ||
return () => { | ||
null | ||
} | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.