Here is a Three.js script that creates a line with two endpoints that the user can drag to move. The user can also pan and zoom the view.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Draggable Line Endpoints with Three.js</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
<!-- Include Three.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r136/three.min.js"></script>
<!-- Include OrbitControls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.136.0/examples/js/controls/OrbitControls.js"></script>
<!-- Include DragControls -->
<script src="https://cdn.jsdelivr.net/npm/three@0.136.0/examples/js/controls/DragControls.js"></script>
</head>
<body>
<script>
// Set up the scene, camera, and renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
const camera = new THREE.PerspectiveCamera(
75, window.innerWidth / window.innerHeight, 0.1, 1000
);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add OrbitControls for panning and zooming
const orbitControls = new THREE.OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;
orbitControls.dampingFactor = 0.05;
orbitControls.screenSpacePanning = true;
// Create two draggable spheres representing the endpoints
const sphereGeometry = new THREE.SphereGeometry(0.1, 32, 32);
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const point1 = new THREE.Mesh(sphereGeometry, sphereMaterial);
point1.position.set(-1, 0, 0);
scene.add(point1);
const point2 = new THREE.Mesh(sphereGeometry, sphereMaterial);
point2.position.set(1, 0, 0);
scene.add(point2);
// Create a line connecting the two points
const lineMaterial = new THREE.LineBasicMaterial({ color: 0x0000ff });
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
point1.position, point2.position
]);
const line = new THREE.Line(lineGeometry, lineMaterial);
scene.add(line);
// Set up DragControls for the endpoints
const dragControls = new THREE.DragControls(
[point1, point2], camera, renderer.domElement
);
// Disable orbit controls while dragging
dragControls.addEventListener('dragstart', function (event) {
orbitControls.enabled = false;
});
dragControls.addEventListener('dragend', function (event) {
orbitControls.enabled = true;
});
// Update the line as points are dragged
dragControls.addEventListener('drag', function (event) {
line.geometry.setFromPoints([point1.position, point2.position]);
});
// Handle window resize
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
orbitControls.update();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
Explanation:
-
Scene Setup:
- A basic Three.js scene is created with a
PerspectiveCamera and a WebGLRenderer.
- The background color of the scene is set to a light gray (
0xf0f0f0).
-
OrbitControls:
OrbitControls are added to allow the user to pan and zoom the camera view.
- Damping is enabled for smoother interactions.
-
Endpoints Creation:
- Two red spheres (
point1 and point2) represent the endpoints of the line.
- They are positioned initially at
(-1, 0, 0) and (1, 0, 0) respectively.
- These spheres are added to the scene and will be draggable by the user.
-
Line Creation:
- A
LineBasicMaterial with a blue color (0x0000ff) is used for the line.
- A
BufferGeometry line is created using the positions of point1 and point2.
- The line is added to the scene.
-
DragControls:
DragControls are set up for the two endpoints, allowing them to be dragged with the mouse.
- When dragging starts,
OrbitControls are disabled to prevent conflicts.
- When dragging ends,
OrbitControls are re-enabled.
- On every drag event, the line's geometry is updated to connect the new positions of the endpoints.
-
Responsive Design:
- The
onWindowResize function ensures the scene adjusts correctly if the window size changes.
-
Animation Loop:
- The
animate function uses requestAnimationFrame to render the scene continuously.
orbitControls.update() is called each frame to handle damping.
Usage:
-
Viewing:
- Click and drag outside the spheres to rotate the camera view around the scene.
- Use the mouse wheel to zoom in and out.
- Right-click and drag to pan the camera.
-
Interacting with the Line:
- Click and drag either of the red spheres to move an endpoint.
- The blue line will update in real-time to connect the two endpoints.
Note:
- Ensure you have an internet connection when running this script because it loads Three.js and control scripts from CDNs.
- This example uses Three.js version
r136. Adjust the version numbers in the script URLs if you use a different version.