-
-
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.
Merge pull request #206 from Tresjs/feature-178-directives
feat(cientos): add first directive vLog
- Loading branch information
Showing
8 changed files
with
129 additions
and
8 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,56 @@ | ||
# v-log | ||
|
||
### Problem | ||
|
||
When you have to log your instance you have to use the template reference and then log them: | ||
|
||
```vue{3} | ||
<script setup lang="ts"> | ||
import { shallowRef, watch } from 'vue' | ||
const sphereRef = shallowRef() | ||
watch(sphereRef, value => { | ||
console.log(value) // Really for a log?!!! 😫 | ||
}) | ||
</script> | ||
<template> | ||
<TresCanvas > | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<Sphere | ||
ref="sphereRef" | ||
:scale="0.5" | ||
/> | ||
<OrbitControls /> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
|
||
And is A LOT of code just for a simple log right? | ||
|
||
## Solution | ||
|
||
With the new directive v-log provided by **TresJS**, you can do this by just adding `v-log` to the instance. | ||
|
||
```vue{3} | ||
<script setup lang="ts"> | ||
import { OrbitControls, Sphere, vLog } from '@tresjs/cientos' | ||
</script> | ||
<template> | ||
<TresCanvas > | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<Sphere | ||
ref="sphereRef" | ||
:scale="0.5" | ||
v-log:material <!-- will print just the material 🎉 --> | ||
/> | ||
<OrbitControls v-log /> | ||
</TresCanvas> | ||
</template> | ||
``` | ||
|
||
Note that you can pass a modifier with the name of a property, for example `v-log:material`, and will log directly the `material` property 😍 | ||
|
||
::: warning | ||
The component `<TresCanvas >` will not log the canvas or the scene. | ||
::: |
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,49 @@ | ||
<script setup lang="ts"> | ||
import { reactive, shallowRef } from 'vue' | ||
import { TresCanvas, useRenderLoop } from '@tresjs/core' | ||
import { OrbitControls, Stars, Sphere, vLog } from '@tresjs/cientos' | ||
import { SRGBColorSpace, NoToneMapping } from 'three' | ||
const gl = { | ||
clearColor: '#333', | ||
outputColorSpace: SRGBColorSpace, | ||
toneMapping: NoToneMapping, | ||
} | ||
const options = reactive({ | ||
size: 0.1, | ||
sizeAttenuation: true, | ||
count: 5000, | ||
depth: 50, | ||
radius: 100, | ||
}) | ||
const sphereRef = shallowRef() | ||
const { onLoop } = useRenderLoop() | ||
onLoop(({ elapsed }) => { | ||
if (sphereRef.value) { | ||
sphereRef.value.value.position.y = Math.sin(elapsed) * 0.5 | ||
} | ||
}) | ||
</script> | ||
|
||
<template> | ||
<TresCanvas v-bind="gl"> | ||
<TresPerspectiveCamera :position="[0, 2, 5]" /> | ||
<Stars | ||
:radius="options.radius" | ||
:depth="options.depth" | ||
:count="options.count" | ||
:size="options.size" | ||
:size-attenuation="options.sizeAttenuation" | ||
/> | ||
<Sphere | ||
ref="sphereRef" | ||
v-log:material | ||
:scale="0.5" | ||
/> | ||
<TresGridHelper :args="[10, 10]" /> | ||
<OrbitControls /> | ||
</TresCanvas> | ||
</template> |
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 |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
<template> | ||
<Suspense> | ||
<ModelsDemo /> | ||
<DirectivesDemo /> | ||
</Suspense> | ||
</template> |
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,3 @@ | ||
import { vLog } from './vLog' | ||
|
||
export { vLog } |
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,11 @@ | ||
export const vLog = { | ||
updated: (el: any, binding: any) => { | ||
if (binding.arg) { | ||
// eslint-disable-next-line no-console | ||
console.log(`v-log:${binding.arg}`, el[binding.arg]) | ||
return | ||
} | ||
// eslint-disable-next-line no-console | ||
console.log('v-log', el) | ||
}, | ||
} |
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