UNPKG

@isaac_ua/drei-html-fix

Version:

drei-html-fix is a React utility that resolves alignment and offset issues with the Html component from @react-three/drei

53 lines (52 loc) 1.48 kB
// src/components/CanvasWrapper.tsx import { Canvas, useThree } from "@react-three/fiber"; import React, { useEffect, useRef, useState } from "react"; var CanvasWrapper = ({ children, canvasProps = {} }) => { const containerRef = useRef(null); const [size, setSize] = useState({ width: 0, height: 0 }); useEffect(() => { const observer = new ResizeObserver(([entry]) => { const { width, height } = entry.contentRect; setSize({ width: Math.round(width % 2 === 0 ? width + 1 : width), height: Math.round(height % 2 !== 0 ? height + 1 : height) }); }); if (containerRef.current) { observer.observe(containerRef.current); } return () => observer.disconnect(); }, []); return /* @__PURE__ */ React.createElement( "div", { ref: containerRef, style: { width: "100vw", height: "100dvh", overflow: "hidden" } }, /* @__PURE__ */ React.createElement( Canvas, { style: { width: size.width, height: size.height }, ...canvasProps }, /* @__PURE__ */ React.createElement(InvalidateOnResize, null), children ) ); }; var InvalidateOnResize = () => { const { invalidate } = useThree(); useEffect(() => { const handle = () => invalidate(); window.addEventListener("resize", handle); return () => window.removeEventListener("resize", handle); }, [invalidate]); return null; }; export { CanvasWrapper };