create a react component from the following code that will work between page navigations…
- User
create a react component from the following code that will work between page navigations on a next js react app: import ThreeGlobe from "three-globe"; import { WebGLRenderer, Scene } from "three"; import { PerspectiveCamera, AmbientLight, DirectionalLight, Color, Fog, // AxesHelper, // DirectionalLightHelper, // CameraHelper, PointLight, SphereGeometry, } from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; import { createGlowMesh } from "three-glow-mesh"; import countries from "./files/globe-data-min.json"; import travelHistory from "./files/my-flights.json"; import airportHistory from "./files/my-airports.json"; var renderer, camera, scene, controls; let mouseX = 0; let mouseY = 0; let windowHalfX = window.innerWidth / 2; let windowHalfY = window.innerHeight / 2; var Globe;
init(); initGlobe(); onWindowResize(); animate();
// SECTION Initializing core ThreeJS elements function init() { // Initialize renderer renderer = new WebGLRenderer({ antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); // renderer.outputEncoding = THREE.sRGBEncoding; document.body.appendChild(renderer.domElement);
// Initialize scene, light scene = new Scene(); scene.add(new AmbientLight(0xbbbbbb, 0.3)); scene.background = new Color(0x040d21);
// Initialize camera, light camera = new PerspectiveCamera(); camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix();
var dLight = new DirectionalLight(0xffffff, 0.8); dLight.position.set(-800, 2000, 400); camera.add(dLight);
var dLight1 = new DirectionalLight(0x7982f6, 1); dLight1.position.set(-200, 500, 200); camera.add(dLight1);
var dLight2 = new PointLight(0x8566cc, 0.5); dLight2.position.set(-200, 500, 200); camera.add(dLight2);
camera.position.z = 400; camera.position.x = 0; camera.position.y = 0;
scene.add(camera);
// Additional effects scene.fog = new Fog(0x535ef3, 400, 2000);
// Helpers // const axesHelper = new AxesHelper(800); // scene.add(axesHelper); // var helper = new DirectionalLightHelper(dLight); // scene.add(helper); // var helperCamera = new CameraHelper(dLight.shadow.camera); // scene.add(helperCamera);
// Initialize controls controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true; controls.dynamicDampingFactor = 0.01; controls.enablePan = false; controls.minDistance = 200; controls.maxDistance = 500; controls.rotateSpeed = 0.8; controls.zoomSpeed = 1; controls.autoRotate = false;
controls.minPolarAngle = Math.PI / 3.5; controls.maxPolarAngle = Math.PI - Math.PI / 3;
window.addEventListener("resize", onWindowResize, false); document.addEventListener("mousemove", onMouseMove); }
// SECTION Globe function initGlobe() { // Initialize the Globe Globe = new ThreeGlobe({ waitForGlobeReady: true, animateIn: true, }) .hexPolygonsData(countries.features) .hexPolygonResolution(3) .hexPolygonMargin(0.7) .showAtmosphere(true) .atmosphereColor("#3a228a") .atmosphereAltitude(0.25) .hexPolygonColor((e) => { if ( ["KGZ", "KOR", "THA", "RUS", "UZB", "IDN", "KAZ", "MYS"].includes( e.properties.ISO_A3 ) ) { return "rgba(255,255,255, 1)"; } else return "rgba(255,255,255, 0.7)"; });
// NOTE Arc animations are followed after the globe enters the scene setTimeout(() => { Globe.arcsData(travelHistory.flights) .arcColor((e) => { return e.status ? "#9cff00" : "#FF4000"; }) .arcAltitude((e) => { return e.arcAlt; }) .arcStroke((e) => { return e.status ? 0.5 : 0.3; }) .arcDashLength(0.9) .arcDashGap(4) .arcDashAnimateTime(1000) .arcsTransitionDuration(1000) .arcDashInitialGap((e) => e.order * 1) .labelsData(airportHistory.airports) .labelColor(() => "#ffcb21") .labelDotOrientation((e) => { return e.text === "ALA" ? "top" : "right"; }) .labelDotRadius(0.3) .labelSize((e) => e.size) .labelText("city") .labelResolution(6) .labelAltitude(0.01) .pointsData(airportHistory.airports) .pointColor(() => "#ffffff") .pointsMerge(true) .pointAltitude(0.07) .pointRadius(0.05); }, 1000);
Globe.rotateY(-Math.PI * (5 / 9)); Globe.rotateZ(-Math.PI / 6); const globeMaterial = Globe.globeMaterial(); globeMaterial.color = new Color(0x3a228a); globeMaterial.emissive = new Color(0x220038); globeMaterial.emissiveIntensity = 0.1; globeMaterial.shininess = 0.7;
// NOTE Cool stuff // globeMaterial.wireframe = true;
scene.add(Globe); }
function onMouseMove(event) { mouseX = event.clientX - windowHalfX; mouseY = event.clientY - windowHalfY; // console.log("x: " + mouseX + " y: " + mouseY); }
function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); windowHalfX = window.innerWidth / 1.5; windowHalfY = window.innerHeight / 1.5; renderer.setSize(window.innerWidth, window.innerHeight); }
function animate() { camera.position.x += Math.abs(mouseX) <= windowHalfX / 2 ? (mouseX / 2 - camera.position.x) * 0.005 : 0; camera.position.y += (-mouseY / 2 - camera.position.y) * 0.005; camera.lookAt(scene.position); controls.update(); renderer.render(scene, camera); requestAnimationFrame(animate); }
- Assistant
To create a React component from the provided code for use in a Next.js application, you can follow these steps:
- Wrap the entire initialization and animation logic into a React component lifecycle methods or useEffect hooks.
- Handle component mount and unmount to initialize and clean up Three.js objects respectively.
- Ensure the renderer is appended to a specific DOM element controlled by React, for instance, using
useRef.
Here's an example of how you could convert this code into a functional React component:
import React, { useRef, useEffect } from 'react'; import ThreeGlobe from "three-globe"; import { WebGLRenderer, PerspectiveCamera, Scene, AmbientLight, DirectionalLight, Color, Fog, PointLight } from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; import countries from "./files/globe-data-min.json"; import travelHistory from "./files/my-flights.json"; import airportHistory from "./files/my-airports.json"; const GlobeComponent = () => { const globeContainerRef = useRef(); useEffect(() => { let renderer, camera, scene, controls; let windowHalfX = window.innerWidth / 2; let windowHalfY = window.innerHeight / 2; let Globe; // ... (Initialization code goes here, using `renderer`, `scene`, `camera`) const onWindowResize = () => { // ... (Your window resize handler) }; const onMouseMove = (event) => { // ... (Your mouse move handler) }; const animate = () => { // ... (Your animation loop) requestAnimationFrame(animate); }; // Initialization function (could be split into multiple functions) function init() { // ... (Same as your current init() function) // Append renderer to the ref element instead of document.body if (globeContainerRef.current && !globeContainerRef.current.contains(renderer.domElement)) { globeContainerRef.current.appendChild(renderer.domElement); } } // Call init and animate functions init(); animate(); // Event listeners window.addEventListener('resize', onWindowResize, false); document.addEventListener('mousemove', onMouseMove, false); // Cleanup return () => { window.removeEventListener('resize', onWindowResize); document.removeEventListener('mousemove', onMouseMove); // Dispose Three.js objects and event listeners to prevent memory leaks scene.dispose(); renderer.dispose(); }; }, []); return ( <div ref={globeContainerRef} style={{ width: '100%', height: '100%' }} /> ); }; export default GlobeComponent;To use this component in your Next.js application, simply import and place it within your page component:
import GlobeComponent from './path-to-your-GlobeComponent'; const MyPage = () => { return ( <div style={{ width: '100vw', height: '100vh' }}> <GlobeComponent /> </div> ); }; export default MyPage;Remember to tweak the styles and logic according to your needs, ensuring that the component cleans up Three.js resources when unmounted to avoid memory leaks.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.