Coder Social home page Coder Social logo

ehtick / threejs-object-controls Goto Github PK

View Code? Open in Web Editor NEW

This project forked from albertopiras/threejs-object-controls

0.0 0.0 0.0 191 KB

ThreeJS module that allows you to rotate an Object (mesh) independently from the rest of the scene, and to zoom in/out moving the camera.

License: MIT License

JavaScript 62.46% HTML 37.54%

threejs-object-controls's Introduction

ThreeJS Object Controls

npm version

ThreeJS module that allows you to rotate one or more Objects(mesh) independently from the rest of the scene, and to zoom in/out moving the camera.

view live example

Install

npm install --save threejs-object-controls

Usage

import * as THREE from 'three';
import {ObjectControls} from 'threeJS-object-controls';

Create a new instance of Controls, passig 3 arguments:

  • camera
  • renderer element
  • the mesh(s) to move
var controls = new ObjectControls(camera, renderer.domElement, myMesh);

or

var controls = new ObjectControls(camera, renderer.domElement, [myMesh, myOtherMesh]);

(look at the index.html file to see an usage example, or check the github example repo to see an npm usage example with Angular)

You can also change the mesh you want rotate! ๐ŸŽ‰

Auto rotation example is also included in the demo code.

view live example and click the 'useMesh2' button on the right panel to interact with the second cube.

Options

You can set different options like

  • rotation speed
  • zoom speed
  • min-max distance of the camera
  • mesh(s) to rotate
  • enable disable axis rotations
  • set max rotation angle

Eg:

- controls.setDistance(8, 200); // sets the min - max distance able to zoom
- controls.setZoomSpeed(1); // sets the zoom speed ( 0.1 == slow, 1 == fast)
- controls.disableZoom(); // disables zoom
- controls.enableZoom(); // enables zoom
- controls.setObjectToMove(newMesh); // changes the object to interact with
- controls.setObjectToMove([mshBox,mshBox2]); // changes the objects to interact with
- controls.setRotationSpeed(0.05); // sets a new rotation speed for desktop ( 0.1 == slow, 1 == fast)
- controls.setRotationSpeedTouchDevices(value); // sets a new rotation speed for mobile
- controls.enableVerticalRotation(); // enables the vertical rotation
- constrols.disableVerticalRotation();  // disables the vertical rotation
- controls.enableHorizontalRotation(); // enables the horizontal rotation
- controls.disableHorizontalRotation();// disables the horizontal rotation
- controls.setMaxVerticalRotationAngle(Math.PI / 4, Math.PI / 4); // sets a max angle value for the vertical rotation of the object
- controls.setMaxHorizontalRotationAngle(R,R); // sets a max angle value for the horizontal rotation of the object
- controls.disableMaxHorizontalAngleRotation(); // disables angle limits for horizontal rotation
- controls.disableMaxVerticalAngleRotation(); // disables angle limits for vertical rotation
- controls.isUserInteractionActive() //returns true if the user is interacting with the UI, false otherwise

Tips

TypeScript Gotchas

In case of problems with ts types required by threejs in a typescript project do the following:

  1. go to the tsconfig.json file
  2. add skipLibCheck: true in "compilerOptions" object.
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "strict": true,
        "target": "es5",
        "declaration": true,
        "declarationDir": "dist-debug/",
        "skipLibCheck": true, /// Needs to be true to fix wrong alias types being used
        ...
    }

Working with multiple meshes

If more than one mesh is passed in as the object to move, all objects will stop move as soon as the first hits its rotation limit.

Version

1.2.8

License

MIT

threejs-object-controls's People

Contributors

albertopiras avatar claytonrothschild 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.