What is the best way to adjust the camera position in three.js while keeping it from rotating?

I've been working on an interface using three.js and the CSS3DObject rendering tool. To control movement, I've disabled rotation by setting the orbit to 0, allowing only panning and zooming.

Just a heads up - I'm also utilizing Orbit Control in my project.

To position the camera at x=-2000, I used the following code:

camera.position.x=-2000;
camera.position.z=4000;

While this code moves the camera, it still ends up facing (0,0,0) which distorts the view. It seems that giving it a vector may solve this issue.

camera.up = new THREE.Vector3(0,1,0);  //keeps the camera horizontal
camera.lookAt(new THREE.Vector3(2000,0,0));  //should point the camera straight forward

I'm still in the process of understanding how the lookAt function works, so any insights would be appreciated.

Answer №1

After further investigation, it appears that the orbit control is taking precedence over the camera.lookAt function, causing it to not work as expected. In order to achieve the desired panning effect, I adjusted the camera's x position to match the target value. Additionally, I removed the line of code related to camera.up.

var myCameraX = -2000;
var myCameraY = 500;

camera.position.x = myCameraX;
camera.position.y = myCameraY;
control.target.set(myCameraX, myCameraY, 0);

I hope this solution proves helpful to someone else.

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

Assign a default value to empty elements in an array

Here is an example of fetching data in an array: [ { "Id": 111, "Name": "Test 1", "Email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de8e0ece4e1bccde9e2e0ece4e3a3e3e8f9">[email protect ...

The operation of 'val' is not supported by HTMLInputElement

While working with a table row, I am iterating through each cell. Inside every cell lies a text box containing some value that I need to store in an array. function dothing() { var tds = $('#'+selected+' td'); var submi ...

Ways to run evaluations on 'private' functions within an angular service using Karma and Jasmine

In my Angular application, I have a BracketService that consists of various functions for comparing weights and creating brackets based on weight groups. The service includes functions like compareByWeight, filterWeightGroup, and createBracketsByWeightGrou ...

Thinking of hosting an event centered around Google Maps?

Are there specific event listeners for panning or zooming the Google Map, in addition to the mouseover, mouseout, and click events? UPDATE: I tried using 'center_changed', but it didn't work the way I expected. Whenever I move the mouse ov ...

The HTML body content of an iframe

Hey there, I'm trying to get just the body html from an iframe using my code. Currently it returns all the html of the iframe. Does anyone have any ideas on how to modify this? Here's the code snippet: alert( $("#uploaderIframe").contents()[0]. ...

Is there a way to implement a local gltf loader in three.js to display on a GitHub server within my repository?

Every time I run code on my VS app, it works perfectly fine. However, when I try running it on GitHub, it shows an error 404 and gets aborted. I came across some documentation regarding this issue, but I'm having trouble understanding it. Check out ...

JQuery: Checkbox keeps unchecking when switching between buttons

My knowledge of jquery is still at a beginner level, and I have a page that contains a total of 12 buttons/toggles (only 3 are shown as an example). When each button is clicked, it triggers the display of a PHP page with a set of checkboxes listed with var ...

Tips for incorporating mapbox.js as a background while keeping html content and Navbar consistently displayed on top

I am currently integrating MapBox.js into a Bootstrap 3 website. The main concept is to utilize a map as the background for a row that occupies the full width of the site, with html-content displayed on top. To ensure MapBox.js remains in the background, I ...

When utilizing :id with Vue, HTML attributes become hidden from view

I am facing an issue where I need to make HTML elements visible, but they appear invisible upon rendering. Below is my HTML code: <div class="created-links-wrapper" v-for="item in createdUrls" :key="item._id"> <d ...

Depth sorting in Three.js CanvasRenderer is vital for accurate rendering of

I am experiencing some clipping issues with the CubeGeometry objects rendered on CanvasRenderer, especially with depth sorting. Despite trying various adjustments like setting overdraw to 1 and increasing segments, the problem persists. The WebGLRenderer d ...

What is the process for obtaining a JSON result after completing an upload?

I recently started working with the razor view engine and I have encountered a situation where I need to upload an image using the Change event of an image browser. The jQuery function for this task is as follows: $("#billUpload").change(function (){ ...

Angular is throwing an error when trying to create a new service: "Workspace must be loaded before it can be used."

Having trouble adding pusher.js to my angular.json file. After trying to create a new service, I encountered the following error: Error: Workspace needs to be loaded before it is used. Any tips on how to resolve this? I attempted to update the angular cl ...

Utilizing the Ajax success callback to trigger a subsequent Ajax request as the loop iterates

I am currently working on a piece of javascript/jquery code that involves making ajax requests. The code snippet I have includes various variables and functions for handling external data sources and templates. var trigger = $('#loadTabl ...

Exploring AngularJS: Effortlessly Retrieving Object Values

HTML: <div ng-app = "" ng-controller = "personController"> <p>Persoon: <select ng-model="persoon"> <option ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option> & ...

I am encountering problems with images that are imported as module imports in a Vue.js project

Currently, I have stored all the default images for my Vue project in a designated folder. The path to this folder is web/images/defaults/<imageNames>.png. Instead of importing each image individually in my components, I wish to create a file that co ...

The base64 code generated by the toDataURL method on the canvas appears to be incorrect

I am facing an issue with my code while using canvas to draw a cropped image with base 64. The problem is that it works perfectly on Chrome but gives me a blank image on Firefox. async function base64SquareCrop(imgbase64, size = 224) { const img = docume ...

Using a for loop to add a nested div inside another div

Hey there, I've got a question about the loop that's currently in my code. Right now, I'm doing it manually and it seems to be working that way. $( '<div id="demo1" > <span class="value"></span> </div> ...

Importing objects using Three.js OBJLoader without automatically adding them to the scene

I have been working on a function in Three.js to load obj files. Instead of directly adding the object to the scene within that function, I am trying to return it to the calling function. Here is the current code: var loadObjFile = function(modelConfigu ...

What could be causing the service method in the controller not to be called by Node JS?

In my current Node JS project, the folder structure of my app is as follows: src │ index.js # Main entry point for application └───config # Contains application environment variables and secrets └───controllers # Hou ...

Inserting items into the document after updating the list with fresh data

I populate an array of objects when a button is clicked. The array only has 10 objects initially, but users can add more after it's loaded into the DOM. Here's how I handle this: $scope.Information = []; $.each(data, function (i, v) { if ...