Advertisement
443eb9

Untitled

Apr 26th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default function IslandsCanvas() {
  2.     const [initialized, setInitialized] = useState(false);
  3.  
  4.     const canvasRef = useCallback((node: HTMLDivElement | null) => {
  5.         if (node != null && !initialized) {
  6.             initScene(node);
  7.             setInitialized(true);
  8.         }
  9.     }, [initialized]);
  10.  
  11.     return (
  12.         <div ref={canvasRef}></div>
  13.     );
  14. }
  15.  
  16. function initScene(node: HTMLDivElement) {
  17.     const scene = new Scene();
  18.     const camera = new PerspectiveCamera();
  19.     const renderer = new WebGLRenderer();
  20.     renderer.setClearColor(0xffffff);
  21.     renderer.setSize(window.innerWidth, window.innerHeight);
  22.     node.appendChild(renderer.domElement);
  23.  
  24.     scene.add(camera);
  25.     scene.add(new Mesh(
  26.         new BoxGeometry(100, 100),
  27.         new MeshStandardMaterial({ color: new Color(1, 1, 1) })
  28.     ));
  29.     camera.position.setZ(5);
  30.  
  31.     renderer.render(scene, camera);
  32.  
  33.     const animate = () => {
  34.         requestAnimationFrame(animate);
  35.         renderer.render(scene, camera);
  36.     }
  37.     animate();
  38.  
  39.     window.addEventListener('resize', () => {
  40.         renderer.setSize(window.innerWidth, window.innerHeight);
  41.     });
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement