Animating Three.js OrbitControls to smoothly rotate and face the front of a target object

I am trying to smoothly move the OrbitControls(camera) in front of an object (face side) when a button is clicked using tween.js.

However, I encountered an issue where after changing the controls.target and panning too far from the target object, I can only zoom in to a certain level and then I am unable to pan around the object.

Is there an alternative way to view an object? Thank you for any suggestions!

var from = {
    x: controls.target.x,
    y: controls.target.y,
    z: controls.target.z
};

var to = {
    x: object.getWorldPosition().x,
    y: object.getWorldPosition().y,
    z: object.getWorldPosition().z
};

var tween = new TWEEN.Tween(from)
    .to(to, 1000)
    .easing(TWEEN.Easing.Quadratic.InOut) // | TWEEN.Easing.Linear.None
    .onUpdate(function () {
        controls.target.set( this.x, this.y, this.z )
    })
    .onComplete(function () {
        controls.target.set( this.x, this.y, this.z )
    })
    .start();

Answer №1

Here's a helpful tip: At the start of your animation, make sure to disable the controls using controls.enabled = false; so that they don't interfere with your animation. Then, add the following code snippet to one of your tweens' onComplete() callback:

.onComplete(function() {

    controls.enabled = true;
    camera.getWorldDirection( lookDirection );
    controls.target.copy( camera.position ).add( lookDirection.multiplyScalar( 10 ) );

})

This code snippet re-enables the controls, calculates the current view direction, and sets a new target for the controls. If needed, you can also specify a custom target spot for each camera position. The value 10 represents the distance in world units for the target along the view direction.

Check out the updated demonstration here: https://jsfiddle.net/ckgo7qu8/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React components are graced with a clear icon that is visible on every element

I'm attempting to show a remove icon on a grid display when the user hovers over it with their mouse. this.state = { action: [], } <div> {this.state.action.map((value, index) => { return ( <div key={index} onMouseEnte ...

Using data-ng-repeat with various elements in Angular

Within the Angular controller, there is a variable that contains the following: $scope.items = [ { title: 'x', type: 'x'}, { title: 'y', type: 'y'} ]; Currently, there are only 2 items in the array, but there w ...

Is there a conventional method for implementing role-based access control for files in Angular?

I have built 4 projects in Angular that grant access to different roles. The dashboard consists of various content pages, with the initial page being the dashboard when a user logs in. The content displayed on the dashboard is determined by the logged-in ...

Output text in Javascript (not to the console)

I'm on the lookout for a way to welcome a user by their name when they enter it into a JavaScript application. I have tried this code snippet: function getname() { var number = document.getElementById("fname").value; alert("Hello, " + fnam ...

Where to Locate a String Excluding <a> Tags?

I'm in the process of writing a JavaScript code that will scan an HTML document and identify all occurrences of a specific keyword that are NOT contained within a link, meaning not under an <a> tag. To illustrate this, let's examine the fol ...

An object will not be returned unless the opening curly bracket is positioned directly next to the return statement

compClasses: function() { /* The functionality is different depending on the placement of curly brackets */ return { major: this.valA, minor: this.valB } /* It works like this, please pay attention to ...

Using the getAttribute method in Edge with JavaScript

My goal is to dynamically load videos on the page after it has fully loaded. I have a script that successfully works in Firefox and Chrome, but I encounter errors when using Edge/IE. The specific error message I receive is SCRIPT5007: Unable to get propert ...

Updating the placeholder text of a textarea using a conditional statement in PHP

I currently have a textarea within a form that is being outputted: <textarea id='textarea' name='msg' rows='2' maxlength='255' cols='80' placeholder=' Share a thought...'></textarea> ...

Show the name of the channel on the FeatherJS chat application

I've been using this repository as a guide to develop a chat application. Currently, I'm working on displaying the channel name (the default room where users are logged in) in the chat client. Is there a way to retrieve channel information from ...

What is the best way to switch a boolean state in React using TypeScript?

Hey there! I'm diving into the world of React and TypeScript. My goal is to toggle a boolean state (true/false) using a handler function. While I've come across solutions in ES6, I'm struggling to grasp how it can be implemented in TypeScri ...

Error message: Unexpected character found at the beginning of JSON data - REST API using node.js and Express

Recently, I have embarked on the journey of learning REST API with node and express. My main goal is to achieve file read and write operations using APIs. However, a frustrating error arises when attempting to hit the API through Postman. Strangely enough, ...

Service update causing $scope in Ionic Angular Cordova to remain stagnant

As a newcomer to Angular, I've been working on a project to create an app that can answer questions, select images, and send data to the server. I'm facing some challenges with updating the scope properly when a user selects an image. It seems l ...

Unable to substitute a value using the useState hook in React

Whenever I click a key, I attempt to update the value of a variable but it appears not to be working as intended. ↓ The current implementation is not effective and needs improvement import React, { useState, useEffect } from 'react'; const Li ...

What is the shebang used for in JavaScript?

In one article I came across, it was mentioned that by including #! /usr/bin/python in a file named testScript, running ./testScript from the command line would be the same as running /usr/bin/python testScript This concept seems plausible to me as most s ...

Implementing dynamic classes for each level of ul li using JavaScript

Can anyone help me achieve the goal of assigning different classes to each ul li element in vanilla Javascript? I have attempted a solution using jQuery, but I need it done without using any libraries. Any assistance would be greatly appreciated! con ...

Is it possible to obtain a reference that can call an operator?

Is it possible to obtain a reference to an operator (like ===) in TypeScript? The reason behind this question is the following function: function dedup<T>(values: T[], equals: (a: T, b: T) => boolean): T[] { return values.reduce<T[]>((pre ...

Sharing data between two unrelated components in Angular 4

I have an inquiry regarding passing data in Angular. Unlike the usual structure of <parent><child [data]=parent.data></child></parent>, my structure is different: <container> <navbar> <summary></summary& ...

The initial display of jqGrid columnChooser may not show the expected effect on the first attempt

Trying to implement jqGrid in my project has been mostly successful, but I am encountering two specific issues: The first time the columnChooser is clicked, it does not display the effect when clicking the done button. However, it works as intended on th ...

Navigating the ins and outs of Bootstrap 4 grids

I am trying to create a layout with two columns, each using the class col-md-6. In both columns, there are six images. However, I want to have only one image displayed in the left column if the right column is empty. How can I achieve this? The current se ...

The PayPal JavaScript SDK button triggers a blank window with a #blocked URL when used in a Django template, but does not have the same issue

I've been grappling with an issue while trying to incorporate PayPal buttons into my Django website. Every time I attempt to do so, the PayPal popup window shows up as about:blank#blocked. In the console, I can see the following error: popup_open_erro ...