Giter Club home page Giter Club logo

orbitcontrolszoomtocursor.js's Introduction

OrbitControlsZoomToCursor.js


What is OrbitControlsZoomToCursor.js?

OrbitControlsZoomToCursor.js is modified OrbitControls.js with added feature of zooming to cursor position.

All code added to OrbitControls.js is wrapped in this comment pattern:
// ZOOM-TO-CURSOR
some_code();
//

Compatibility

OrbitControlsZoomToCursor.js is compatible with three.js r.146.

How to install

To use OrbitControlsZoomToCursor.js in your THREE.js project, copy OrbitControlsZoomToCursor.js file to your project's directory and import OrbitControls or MapControls from it.
import {OrbitControls, MapControls} from './OrbitControlsZoomToCursor.js';

How to use?

Usage of OrbitControlsZoomToCursor.js is identical as usage of OrbitControls.js from three.js examples. OrbitControls.js documentation: link.

Adding OrbitControlsZoomToCursor.js to your project:


OrbitControls exaple:
// ADDING CONTROLS
const controls = new OrbitControls(camera, renderer.domElement);
//IMPORTANT, enabling zooming to cursor
controls.enableZoomToCursor = true;

MapControls example:
// ADDING CONTROLS
const controls = new MapControls(camera, renderer.domElement);
//IMPORTANT, enabling zooming to cursor
controls.enableZoomToCursor = true;
// setting maximum target distance from origin 
controls.maxTargetDistanceFromOrigin = 2;

Notice that, to enable zooming to cursor, you need to set enableZoomToCursor property of controls to true, by default enableZoomToCursor is set to false, and controls are working identical as original ones from three.js examples.

For MapControls, it is recomended to set maxTargetDistanceFromOrigin value, because in MapControls camera pans in the plane orthogonal to the camera's up direction, and zooming to cursor, while keeping camera in the plane orthogonal to the camera's up direction can result in moving controls target vector to really big coordinates. By default maxTargetDistanceFromOrigin is set to Infinity.

Example scene with OrbitControlsZoomToCursor.js

import * as THREE from 'three';
import {OrbitControls, MapControls} from '../OrbitControlsZoomToCursor.js';
import {GUI} from './lib/three.js/lil-gui.module.min.js';
import Stats from './lib/three.js/stats.module.js';

THREE.Object3D.DefaultUp = new THREE.Vector3(0, 0, 1);

function resize(){
    renderer.setPixelRatio(window.devicePixelRatio);
    const width = container.clientWidth;
    const height = container.clientHeight;
    renderer.setSize(width, height);
    camera.aspect = width / height;

    if(camera.isOrthographicCamera){
        const cameraHeight = 2;

        camera.left = -cameraHeight / 2 * width / height;
        camera.right = cameraHeight / 2 * width / height;
        camera.top = cameraHeight / 2;
        camera.bottom = -cameraHeight / 2;
    }
    camera.updateProjectionMatrix();
}

//SETTING UP THREE.JS SCENE.
const scene = new THREE.Scene();
// const camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.01, 1000);
const camera = new THREE.OrthographicCamera();

const renderer = new THREE.WebGLRenderer({alpha: true, antialias: true});
const container = document.getElementById("container")
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);

window.addEventListener('resize', e => resize(e));
resize();

camera.position.set(1, 1, 1);

// ADDING CONTROLS
const controls = new MapControls(camera, renderer.domElement);
// const controls = new OrbitControls(camera, renderer.domElement);
controls.minPolarAngle = 0;
controls.maxPolarAngle = Math.PI / 2;
//IMPORTANT, enabling zooming to cursor
controls.enableZoomToCursor = true;
controls.maxTargetDistanceFromOrigin = 2;

// ADDING STATS BOX
const stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);

const gui = new GUI();
gui.add(controls, 'enableZoomToCursor')

const size = 1;
const divisions = 10;
const grid = new THREE.GridHelper(size, divisions);
grid.rotation.x = Math.PI / 2;
scene.add(grid);
const axesHelper = new THREE.AxesHelper(1);
scene.add(axesHelper);

const cube = new THREE.Mesh(new THREE.BoxGeometry(0.5, 0.5, 0.5), [
    new THREE.MeshBasicMaterial({color: 0xff0000}),
    new THREE.MeshBasicMaterial({color: 0x00ff00}),
    new THREE.MeshBasicMaterial({color: 0x0000ff}),
    new THREE.MeshBasicMaterial({color: 0xffff00}),
    new THREE.MeshBasicMaterial({color: 0xff00ff}),
    new THREE.MeshBasicMaterial({color: 0x00ffff}),
]);
scene.add(cube);
cube.position.z = 0.25;


// CREATING ANIMATION LOOP.
function animate() {
    stats.begin();

    requestAnimationFrame(animate);

    renderer.render(scene, camera);

    controls.update();

    stats.end();
};

animate();

You can find this code in examples/example.js. To run this code, run server.exe, and in browser go to localhost/examples/example.html

Creator: Artur Brytkowski

orbitcontrolszoomtocursor.js's People

Contributors

allala0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.