-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix canvas rendering issue and update error messages
- Loading branch information
etienne
committed
Feb 17, 2024
1 parent
245d4b0
commit 3a3022e
Showing
3 changed files
with
38 additions
and
4 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
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,34 @@ | ||
import { createContext, useContext, useEffect, useRef, useState } from "react"; | ||
import { Application } from "@splinetool/runtime"; | ||
|
||
const CanvasContext = createContext<Application | null>(null); | ||
|
||
export const useCanvas = () => useContext(CanvasContext); | ||
|
||
export const CanvasProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const canvasRef = useRef<HTMLCanvasElement | null>(null); | ||
const [app, setApp] = useState<Application | null>(null); | ||
|
||
useEffect(() => { | ||
if (canvasRef.current && !app) { | ||
const newApp = new Application(canvasRef.current); | ||
newApp.load( | ||
"https://prod.spline.design/hnkOtrqss6sQ-wLy/scene.splinecode" | ||
); | ||
setApp(newApp); | ||
} | ||
}, [canvasRef, app]); | ||
|
||
return ( | ||
<CanvasContext.Provider value={app}> | ||
<canvas | ||
ref={canvasRef} | ||
className="absolute inset-0" | ||
style={{ pointerEvents: "none" }} | ||
/> | ||
{children} | ||
</CanvasContext.Provider> | ||
); | ||
}; |