Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add useTresReady #712

Merged
merged 3 commits into from
Jun 15, 2024
Merged

feat: add useTresReady #712

merged 3 commits into from
Jun 15, 2024

Conversation

andretchen0
Copy link
Contributor

@andretchen0 andretchen0 commented Jun 1, 2024

Features

This PR adds the following features via a private composable useTresReady:

  • onTresReady hook
  • @ready emit from TresCanvas
  • start loop onTresReady

Note

Tres is "ready" when either:

  • context, renderer, and canvas exist and canvas has a height and width greater than 0.
  • 100 ms has passed since TresContext was created.

Important

The 100 ms timeout above exists to keep Tres working as expected even in edge cases – like the user setting the canvas size to 0 x 0 explicitly.

Because of the timeout, it is not guaranteed that the canvas will be renderable when @ready.

Playground

pnpm run playground

... then open http://localhost:5173/basic/ready


Problem

useLoop calls user callbacks before THREE can actually render to the screen, e.g., in the default setup, <canvas> will have a height/width of 0 when useLoop starts.

For debugging, this means that the developer needs to wait for a few frames to pass before they get "real" values in their logs/debugger.

Solution

Start the loop and trigger ready callbacks when Tres is "ready".

Use case

DX, particularly debugging.

Problem

#572

Users had been asking for an easier way to get the context from a TresCanvas in a <script setup>. It currently requires a few steps that users find non-obvious, as outlined in the issue above.

Solution

  • Add a @ready emit that takes a callback and calls it with TresContext when Tres is "ready"
<script setup>
const myFn = (ctx: TresContext) => {
  // NOTE: do something with ctx
}
</script>

<template>
  <TresCanvas @ready="myFn" />
</template>

Use case

DX. Advanced users who want to write a bit less code in order to access TresContext.

Problem

Child components can't take advantage of "ready" without registering a loop callback.

Solution

  • Add onTresReady hook
<script setup>
import { onTresReady } from '@tres/core';

// NOTE: requires `TresContext`, so it can only be used in child components.
onTresReady((ctx: TresContext) => {
  // NOTE: do something with ctx
})
</script>

Use case

DX. Advanced users who want to be notified when Tres is "ready" in child components.


Meta

closes #595 – the present PR modifies useLoop not useRenderLoop as in the issue. But useRenderLoop is no longer Tres' preferred looping method
closes #572

* add onTresReady hook
* add @ready emit to TresCanvas
* start loop onTresReady
Copy link

netlify bot commented Jun 1, 2024

Deploy Preview for tresjs-docs ready!

Name Link
🔨 Latest commit 6789a3b
🔍 Latest deploy log https://app.netlify.com/sites/tresjs-docs/deploys/666d60c2f6cc8b0008ab595d
😎 Deploy Preview https://deploy-preview-712--tresjs-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@andretchen0 andretchen0 marked this pull request as ready for review June 2, 2024 00:30
@alvarosabu alvarosabu added feature p3-significant High-priority enhancement (priority) labels Jun 2, 2024
Copy link
Member

@alvarosabu alvarosabu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great @andretchen0 thank you so much. sorry took me so long to review it , I just tested everything 💚

@alvarosabu alvarosabu merged commit 15e3f07 into main Jun 15, 2024
7 checks passed
@alvarosabu alvarosabu deleted the feat/595-use-tres-ready branch June 15, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature p3-significant High-priority enhancement (priority)
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

useRenderLoop().onLoop ticks before the renderer can meaningfully render Use default slot props to get Canvas context
2 participants