Code Monkey home page Code Monkey logo

three-joystick's Introduction

three-joystick

An open source joystick that can be used to control a target in a three.js scene.

Installation

npm i three-joystick

RotationJoystickControls Usage

This class will add a joystick that can rotate a target object.
See demo here
See example code here

  1. Import the RotationJoystickControls class
    import { RotationJoystickControls } from 'three-joystick';
  2. Pass through your camera, scene and the target mesh you want to control
    const joystickControls = new RotationJoystickControls(
      camera,
      scene,
      target,
    );
  3. Invoke update in your animate loop
    function animate() {
      requestAnimationFrame(animate);
    
      /**
       * Updates the rotation of the target mesh
       */
      rotationJoystick.update();
    
      renderer.render(scene, camera);
    }
    
    animate();

JoystickControls Usage

This class will add a joystick that invokes a callback function with the delta x and delta y from the movement of the user.
See demo here
See example code here

  1. Import the JoystickControls class
    import { JoystickControls } from 'three-joystick';
  2. Pass through your camera and scene
    const joystickControls = new JoystickControls(
      camera,
      scene,
    );
  3. Invoke update in your animate loop
    function animate() {
      requestAnimationFrame(animate);
    
      /**
       * Updates a callback function with the delta x and delta y of the users
       * movement
       */
      joystickControls.update((movement) => {
        if (movement) {
          /**
           * The values reported back might be too large for your scene.
           * In that case you will need to control the sensitivity.
           */
          const sensitivity = 0.0001;
    
          /**
           * Do something with the values, for example changing the position
           * of the object
           */
          this.target.position.x += movement.moveX * sensitivity;
          this.target.position.y += movement.moveY * sensitivity;
        }
      });
    
      renderer.render(scene, camera);
    }
    
    animate();

three-joystick's People

Stargazers

Alex avatar  avatar Manav Dahra avatar  avatar Anish avatar SleepyShay avatar Ignat Lomkov avatar  avatar Marko Zetovic avatar Coby Chapple avatar lyhkop avatar SK027 avatar 爱护自己的鼠标手 avatar david avatar hocgin avatar Faded Weiss avatar 伊撒尔 avatar Louis Foster avatar Achmat Fauzi avatar Onofre Aparecido Candido (OnOff) avatar Solo avatar Javier avatar Antoine avatar  avatar Zombi avatar Priyanshu Dangi avatar Marty McGee avatar Joji avatar Smarthug avatar  avatar Richard Cook avatar Yuki Oba avatar Serhii avatar Dmitry avatar Luke Carl Thompson avatar Shashank Shekhar avatar John Ottenlips Franke avatar  avatar Simon Mo avatar

Watchers

James Cloos avatar Simon Mo avatar  avatar Onofre Aparecido Candido (OnOff) avatar

three-joystick's Issues

The option to have two joysticks

It would be great to have the option to have multiple joysticks. Maybe one on each half of the screen? This would make it easier to develop experiences where the user needs to look and move simultaneously.

Something like this
image

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.