Implementing camera orientation changes in Three.js upon button click

Is there a way to adjust the camera's lookat position by simply clicking a button or link? Below is the code I currently have:

HTML:

 <a href="#" id="testButton">TEST</a>

JS:

render();
function render() {
    trackballControls.update(60);
    requestAnimationFrame(render);
    webGLRenderer.render(scene, camera);
}

// test button function
// this attempt seems to be unsuccessful
var testButton = document.getElementById('testButton');
testButton.onclick = function ()
{
     camera.lookAt(new THREE.Vector3(50,60,70));
}; 

// another test button function
// this method sort of works but the camera quickly reverts back to its original position
var testButton2 = document.getElementById('testButton');
testButton2.onclick = function ()
{
     camera.lookAt(new THREE.Vector3(50,60,70));
     webGLRenderer.render(scene, camera);
}; 

What am I missing? Check out the test page (make sure to wait for the Eiffel Tower to load).

Answer №1

When utilizing OrbitControls and TrackballControls, the camera is focused on the position specified by controls.target, which should be a THREE.Vector3().

To adjust the target, follow this format:

controls.target.set( newX, newY, newZ );

This information pertains to version r.67 of three.js.

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

Tips for dynamically assigning unique IDs to HTML form elements created within a JavaScript loop

let count = 0; while (count < 4) { $('#container').append("<div><input type='textbox' class ='left' id='left-${count}'/><input type='textbox' class ='right' id=' ...

What methods can I use to evaluate my Angular scope functions in karma and jasmine?

I recently started learning Angular and am new to Jasmine testing. I have a function in my controller that adds an object from JSON data into an empty array. My controller with the cart-related functions: $scope.cart = []; $scope.addItemToCart = funct ...

Tips for navigating a dynamic viewport using scroll movement

Attempting to create a responsive page with two distinct sections at this example link including: Map View Table View Both of these views (table and map divs) need to be responsive without a hard-coded height, so the size of the map div adjusts automatic ...

Discovering a locator based on the initial portion of its value

Here's a piece of code that is used to click on a specific locator: await page.locator('#react-select-4-option-0').click(); Is there a way to click on a locator based on only the initial part of the code, like this: await page.locator(&apos ...

"Using JavaScript to toggle a radio button and display specific form fields according to the selected

Currently, I am attempting to show specific fields based on the selected radio button, and it seems like I am close to the solution. However, despite my efforts, the functionality is not working as expected and no errors are being displayed. I have define ...

Retrieve from MongoDB the items where the age is greater than 10 using the find function in the learngyoumongo

Currently working my way through the learnyoumongo tutorial and facing a challenge in part 3. The task involves a test database filled with parrots, and the objective is to retrieve the parrots whose age exceeds a specified input value. Despite using Mongo ...

Exploring the main directive flow, attaining access to `ctrl.$modelView` in AngularJS is

Four Methods Explained: What Works and What Doesn't I recently created an angular js directive where I encountered difficulty accessing the ctrl.$modelValue in the main flow. In my quest to find a solution, I came up with four potential methods, eac ...

Displaying each character of text individually with jQuery

I am trying to display the text within a ul tag one by one when hovering over some text. However, I am encountering an error. How can I resolve this issue? You can view the code for mouseover functionality by hovering over the "hover here hover again" lin ...

Retrieve the Object from the array if the input value is found within a nested Array of objects

Below is the nested array of objects I am currently working with: let arrayOfElements = [ { "username": "a", "attributes": { roles:["Tenant-Hyd"], groups:["InspectorIP", "InspectorFT"] } }, { ...

jquery target descendants with specific names

In the provided HTML code snippet, there is a main div with the class name of cxfeeditem feeditem, and it contains multiple child elements with similar class names and structure. My query pertains to extracting values from specific children within all the ...

Adjust the size of an image within a canvas while maintaining its resolution

My current project involves using a canvas to resize images client-side before uploading to the server. maxWidth = 500; maxHeight = 500; //handle resizing if (image.width >= image.height) { var ratio = 1 / (image.width / maxWidth); } else { var ...

Guide: Building Angular/Bootstrap button checkboxes within a loop

I am in the process of designing a grid (table with ng-repeat) in which each row contains 4 columns of buttons. My goal is to use checkboxes as the buttons, like the Angular/Bootstrap btn-checkbox, so that they can be toggled on and off. I plan to set thei ...

What is the best way to iterate through a JSON associative array using JavaScript?

When I receive a JSON response from the server, my goal is to loop through the array in JavaScript and extract the values. However, I am facing difficulties in doing so. The structure of the JSON response array is as follows: { "1": "Schools", "20" ...

Implement a Bootstrap button that can efficiently collapse all elements in one click

Within my HTML file, I have included the following code: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div class="list-group list-group-flush"> <a href="javascript: void(0)" da ...

How to efficiently switch between classes in Ember Octane using Handlebars?

What is the best way to toggle between displaying a class on and off using Ember.js Octane? Should I use an @action or @tracked in this case? <img src="flower.jpg" alt="flower" class="display-on"> or <img src="flower.jpg" alt="flower" class=" ...

Generate a distinct variable name within the *ngFor loop

I am trying to create a dynamic table where clicking a button displays the row directly beneath it. I checked out a helpful post on this topic, but didn't find the exact solution I needed. The current setup works but reveals all hidden rows because ...

What is the most effective way to prevent actions while waiting for ajax in each specific method?

Within my JS component, I have various methods that handle events like click events and trigger ajax requests. To prevent the scenario where multiple clicks on the same button result in several ajax requests being fired off simultaneously, I typically use ...

Vue.js Conditional Templates

I am attempting to implement VueJs conditional rendering using handlebars in vueJs 2.0 as outlined in their official documentation, but eslint is throwing an error: - avoid using JavaScript keyword as property name: "if" in expression {{#if ok}} - avoid us ...

An improved solution for avoiding repetitive typeof checks when accessing nested properties in the DOM

One common issue I encounter when working with nested DOM objects is the risk of undefined errors. To address this, I often use a conditional check like the one shown below: if("undefined" != typeof parent && "undefined" != typeof parent.main ...

Is it possible to string together requests in a Vue method?

Whenever a user clicks on a specific button, a request is sent to the server and an answer is received. If the user clicks on this button 100 times, I want to send 100 consecutive requests to the server. Each request must be sent after the previous one, as ...