-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from Tresjs/feature/PamCameraMouse
feat(cientos): adding mouse pam effect component
- Loading branch information
Showing
11 changed files
with
134 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# PamCameraMouse | ||
|
||
![](/cientos/PamCameraMouse.gif) | ||
|
||
`<PamCameraMouse />` is a component that allow you to create easily the pam mouse camera effect. The camera will update automatically according to the mouse position, creating a beautiful nice effect | ||
|
||
## Usage | ||
|
||
You only need import it and use it `<PamCameraMouse />` additionally you can pass two props, disabled and factor. Factor is a number to increase the movement range of the camera | ||
|
||
```vue | ||
<template> | ||
<TresCanvas> | ||
<PamCameraMouse /> | ||
<TresScene> | ||
<Text3D text="TresJS" font="/fonts/FiraCodeRegular.json"> | ||
<TresMeshNormalMaterial /> | ||
</Text3D> | ||
</TresScene> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
|
||
::: warning | ||
By the nature of the pam mouse camera effect it creates conflicts if it's used in combination with OrbitControls | ||
::: | ||
|
||
## Props [[1]](#1) | ||
|
||
| Prop | Description | Default | | ||
| :----------- | :------------------------------------------------------ | ------- | | ||
| **disabled** | enable or disabled the effect, boolean | false | | ||
| **factor** | Number use to increase the range of the camera movement | 5 | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,32 @@ | ||
/* eslint-disable no-console */ | ||
export const isProd = import.meta.env.MODE === 'production' | ||
|
||
const logPrefix = '[TresJS - Cientos ▲ ■ ♥] ' | ||
|
||
interface LoggerComposition { | ||
logError: (message: string, error?: Error | ErrorEvent) => void | ||
logWarning: (message: string) => void | ||
logMessage: (name: string, value: any) => void | ||
} | ||
|
||
export function useLogger(): LoggerComposition { | ||
function logError(message: string, error?: Error | ErrorEvent) { | ||
console.error(`${logPrefix} ${message}`, error || '') | ||
} | ||
|
||
function logWarning(message: string) { | ||
console.warn(`${logPrefix} ${message}`) | ||
} | ||
|
||
function logMessage(name: string, value: any) { | ||
if (!isProd) { | ||
console.log(`${logPrefix} - ${name}:`, value) | ||
} | ||
} | ||
|
||
return { | ||
logError, | ||
logWarning, | ||
logMessage, | ||
} | ||
} |
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,29 @@ | ||
import { defineComponent } from 'vue' | ||
import { usePamCameraMouse } from '.' | ||
import { useCientos } from '../useCientos' | ||
|
||
export const PamCameraMouse = defineComponent({ | ||
name: 'PamCameraMouse', | ||
props: { | ||
disabled: { | ||
type: Boolean, | ||
required: false, | ||
default: false, | ||
}, | ||
factor: { | ||
type: Number, | ||
required: false, | ||
default: 5, | ||
}, | ||
}, | ||
setup(props: any) { | ||
const { state } = useCientos() | ||
const camera = state?.camera | ||
|
||
const PamCameraMouse = usePamCameraMouse(props.disabled as boolean, props.factor as number, camera) | ||
|
||
return () => { | ||
PamCameraMouse | ||
} | ||
}, | ||
}) |
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,24 @@ | ||
import { watchEffect, computed } from 'vue' | ||
import { Camera } from 'three' | ||
import { useWindowSize, useMouse } from '@vueuse/core' | ||
import { useLogger } from '/@/composables/useLogger' | ||
|
||
export function usePamCameraMouse(disabled = false, factor = 5, camera: Camera | undefined) { | ||
const { x, y } = useMouse() | ||
const { logWarning } = useLogger() | ||
const { width, height } = useWindowSize() | ||
const cameraX = computed(() => (x.value / width.value - 0.5) * factor) | ||
const cameraY = computed(() => -(y.value / height.value - 0.5) * factor) | ||
if (camera) { | ||
const { x: initX, y: initY } = camera.position | ||
watchEffect(() => { | ||
if (disabled) return | ||
if (camera) { | ||
camera.position.x = initX + cameraX.value | ||
camera.position.y = initY + cameraY.value | ||
} | ||
}) | ||
} else { | ||
logWarning('Scene must contain a Camera component to use this composable') | ||
} | ||
} |
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