• ⚠️ INFORMATION: SAFETY & SUPPORT Resources here are generally safe, but false positives may occur on Virustotal due to certain coding techniques. Exercise caution and test before use.

javascript How to extract a selected area from a 3D globe in Three.js and transfer it to a new canvas while maintaining zoom and pan effects?

Joined
Dec 31, 2024
Messages
373
Reaction score
7
Points
18
User icon
<svg xmlns="http://www.w3.org/2000/svg" height="14" width="15.75" viewBox="0 0 576 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path fill="#63E6BE" d="M309 106c11.4-7 19-19.7 19-34c0-22.1-17.9-40-40-40s-40 17.9-40 40c0 14.4 7.6 27 19 34L209.7 220.6c-9.1 18.2-32.7 23.4-48.6 10.7L72 160c5-6.7 8-15 8-24c0-22.1-17.9-40-40-40S0 113.9 0 136s17.9 40 40 40c.2 0 .5 0 .7 0L86.4 427.4c5.5 30.4 32 52.6 63 52.6l277.2 0c30.9 0 57.4-22.1 63-52.6L535.3 176c.2 0 .5 0 .7 0c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40c0 9 3 17.3 8 24l-89.1 71.3c-15.9 12.7-39.5 7.5-48.6-10.7L309 106z"/></svg>
To extract a selected area from a 3D globe in Three.js and transfer it to a new canvas while maintaining zoom and pan effects, you need to:

1. Identify the Selected Area:

Allow the user to select a region on the globe. This can be done using raycasting or defining latitude/longitude bounds.



2. Create a Camera Focused on the Selected Area:

Calculate a new camera position and orientation to focus on the selected area. This ensures that the zoom and pan effects are preserved.



3. Render to a Separate Canvas:

Use a secondary WebGLRenderer and render the scene focused on the selected area to a separate canvas.




Here's a detailed implementation:

1. Select an Area

Use Raycaster to determine the user's selection:

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

JavaScript:
function onMouseClick(event) {

  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;

  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;



  raycaster.setFromCamera(mouse, camera);

  const intersects = raycaster.intersectObject(globe); // globe is your sphere mesh

  if (intersects.length > 0) {

    const point = intersects[0].point; // The point on the globe

    // Calculate lat/lon or identify the selected region

    console.log("Selected Point:", point);

  }

}

2. Zoom into the Area

Adjust the camera to focus on the selected region:

JavaScript:
function focusOnRegion(lat, lon) {

  const radius = globe.geometry.parameters.radius;

  const phi = (90 - lat) * (Math.PI / 180); // Latitude

  const theta = (180 - lon) * (Math.PI / 180); // Longitude



  const x = radius * Math.sin(phi) * Math.cos(theta);

  const y = radius * Math.cos(phi);

  const z = radius * Math.sin(phi) * Math.sin(theta);



  // Position the camera slightly offset to ensure visibility

  camera.position.set(x * 1.5, y * 1.5, z * 1.5);

  camera.lookAt(new THREE.Vector3(0, 0, 0)); // Look at the globe center

}

3. Render to a Separate Canvas

Create a second renderer and render the scene to a new canvas:

JavaScript:
// Create a new renderer

const secondaryRenderer = new THREE.WebGLRenderer({ antialias: true });

secondaryRenderer.setSize(512, 512); // Adjust as needed

document.body.appendChild(secondaryRenderer.domElement);



function renderSelectedArea() {

  secondaryRenderer.render(scene, camera); // Use the adjusted camera

}

4. Update and Synchronize Zoom/Pan

If the globe has interactive controls (like OrbitControls), attach the same controls to the secondary renderer:

JavaScript:
const controls = new THREE.OrbitControls(camera, secondaryRenderer.domElement);

controls.addEventListener("change", renderSelectedArea); // Re-render on interaction

5. Final Integration

Capture the selected area (latitude/longitude bounds) from the main globe.

Adjust the camera to focus on this area in the secondary renderer.

Synchronize any user interaction (zoom, pan) with the secondary renderer.


This approach allows you to maintain the zoom and pan effects for the selected area on a new canvas, providing a seamless user experience.
 
Top