three-joystick
An open source joystick that can be used to control a target in a three.js scene.
Installation
npm i three-joystick
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
- Import the JoystickControls class
import { JoystickControls } from 'three-joystick';
- Pass through your camera and scene
const joystickControls = new JoystickControls(
camera,
scene,
);
- Invoke update in your animate loop
function animate() {
requestAnimationFrame(animate);
joystickControls.update((movement) => {
if (movement) {
const sensitivity = 0.0001;
this.target.position.x += movement.moveX * sensitivity;
this.target.position.y += movement.moveY * sensitivity;
}
});
renderer.render(scene, camera);
}
animate();
RotationJoystickControls Usage
This class will add a joystick that can rotate a target object.
See demo here
See example code here
- Import the RotationJoystickControls class
import { RotationJoystickControls } from 'three-joystick';
- Pass through your camera, scene and the target mesh you want to control
const joystickControls = new RotationJoystickControls(
camera,
scene,
target,
);
- Invoke update in your animate loop
function animate() {
requestAnimationFrame(animate);
rotationJoystick.update();
renderer.render(scene, camera);
}
animate();