UI
Hiber3D works well with React and Tailwind CSS, making it easy to build UI on top of the game. The engine renders everything in a <canvas>
element, and your React components sit above it in the DOM. This works seamlessly with the engine’s event-based communication
Examples
Health Bar Component
Below is a simple example of a health bar UI that listens to an in-game event (PlayerHealthChange) and updates accordingly.
import { , } from "react";
import { , } from "@hiber3d/web";
const = () => {
const [, ] = (100);
const { } = ();
(() => {
const = .(() => {
(.);
})
return () => {
.();
}
}, [])
return (
< ="absolute top-0 left-0 p-4">
< ="bg-gray-200 rounded-full w-32 h-4">
<
="bg-green-500 h-full rounded-full"
={{ : `${}%` }}
/>
</>
</>
);
}
export const = () => {
return (
<>
< />
</>
);
}
Common Pitfalls & Tips
- Layering: Use
absolute
,fixed
, orz-index
positioning for UI elements to ensure they overlay correctly on the game canvas. - Access: UI components that rely on
useHiber3D()
must be rendered inside the<Hiber3D />
provider. - Pointer handling: If you use
pointer-events: none
on overlay elements, ensure you don’t block engine input unintentionally. - Event Cleanup: Always clean up event listeners to avoid memory leaks. Use the
api.removeEventCallback
method in the cleanup function ofuseEffect
. - Immersive features: Don’t forget to utilize Web APIs like
Fullscreen API
andPointer Lock API
for a more immersive experience.
Last updated on