What are the steps to modify the camera type in Three.js?

I have added a dropdown list on the page for selecting between two cameras - Perspective and Orthographic. Currently, my Three Scene is using a perspective Camera. I would like to dynamically change the camera to "Orthographic" when selected in the dropdown list, and switch back to Perspective if that option is chosen. Can anyone please assist me with achieving this functionality?

For those interested in the detailed code, here is the link to the Jsfiddle: http://jsfiddle.net/sagh0900/7Anag/9/

camera = new THREE.PerspectiveCamera( 45, width / height, 0.1, 1000 );
camera.position.y = 0;
camera.position.z = 40;
camera.lookAt(new THREE.Vector3(0, 0, 0));  
scene = new THREE.Scene();
scene.add(camera);
mesh = new THREE.Mesh(new THREE.Geometry());
scene.add(mesh);

Thank you in advance for your help.

Answer №1

Previously, I recommended using CombinedCamera, but it is no longer available. Instead, you can follow the suggestion provided by @prisoner849 in this post Three JS, Combined camera Documentation and support

@prisoner849 suggests using a variable that will allow you to assign one camera or another.

Answer №2

It appears that altering the Camera type of a camera instance on the spot is not feasible, given that they are distinct classes requiring separate instantiation.

Nevertheless, you can create two different camera instances - one Ortographic and one Perspective camera. Then, during your render() call, you can specify which camera to utilize. You will need to update both cameras' positions using UI event handlers, or alternatively, clone the position within the render function.

Upon examining the three.js source code, it seems possible to construct a custom camera class supporting orto/perspective mode switching. However, this approach may be considered somewhat of a workaround.

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

What measures can I take to restrict Node REPL instances from accessing the global scope?

When setting up a new repl instance in code, it automatically gains access to the global scope. While custom variables can be exposed in the local scope for the repl to utilize, restricting access to the global scope isn't straightforward. It would be ...

What causes React Router to render the Navigation to the <Home/> component when the userData is still available, but redirects to "/login" otherwise?

I'm currently developing a Note-taking application using React with React Router for navigation. For creating routes, I am utilizing createBrowserRouter(). Below is the code snippet: import './App.css'; import NoteList from "./componen ...

Discover a specific segment of the pie chart

Currently, I have a pie chart that has been created using HTML5 canvas. I am able to retrieve the coordinates (X,Y) when the mouse hovers over it. Now, my goal is to determine which slice of the pie chart the Point (X,Y) falls into. Please note: I have ...

Understanding the Submission of a Form upon the submission event

On my form, I have multiple buttons - two of them use ajax to submit the form and clear it for adding multiple records, while the last button is for moving onto the next page. I'm wondering if jQuery's .submit() method can determine how the form ...

Extracting live content from a website within a native Webview

Running an eCommerce website along with a simple mobile app for both iOS and Android that features a basic tab bar menu, including icons like a shopping cart, profile, refresh button, etc., as well as a Webview to display the website content. The App' ...

Utilizing slid.bs.carousel to retrieve values upon slide change

I am working on incorporating Bootstrap 4's Carousel with jQuery and PHP to create an odometer that dynamically changes its value on each slide. My plan is to utilize .addClass based on the length of the value. One challenge I am facing is that when ...

Exploring the process of logging large objects in Node.js 11 using the console command

When working with Node.js 11, I've noticed that when I use console.log to output a large object, it sometimes throws a stack trace error or logs the full object including all of its inheritance. In my Jest tests, I encountered an error when trying to ...

Logging out of Laravel after sending a POST request

I'm developing a laravel application that heavily relies on POST requests. One common type of request in my app looks like this: var classElements = document.querySelectorAll("tr.ui-selected td.filename"); var csrf = $('input[name=_token]') ...

Unable to sign out user from the server side using Next.js and Supabase

Is there a way to log out a user on the server side using Supabase as the authentication provider? I initially thought that simply calling this function would work: export const getServerSideProps: GetServerSideProps = withPageAuth({ redirectTo: &apos ...

Using AJAX to set a session variable in PHP

Upon clicking a div, this JavaScript function is executed: $('.test').click(function(e) { e.preventDefault(); $.ajax({ url: 'ajax.php', type: 'POST', data: {"id": "<? ...

Experienced an unexpected setback with the absence of the right-click capability on a Javascript-powered hyperlink, specialized for

I am facing an issue with a hyperlink on my website. This particular hyperlink submits a hidden form using the POST method to redirect users to another site. However, when someone right-clicks on this hyperlink and tries to open it in a new tab, they are o ...

What is the best way to insert distinct values into an array in mongoDB?

I have a table of likes, and within that table, there is an array called videoLikes. I want to store the userData of users who have liked the video. However, when I attempt to add an object with a userId field to that array, I end up with duplicate data. S ...

Facing difficulties in resetting the time for a countdown in React

I've implemented the react-countdown library to create a timer, but I'm facing an issue with resetting the timer once it reaches zero. The timer should restart again and continue running. Take a look at my code: export default function App() { ...

Exploring a utility function for saving object information in a dynamic state

Imagine my state was structured like this: state = { customer: { name: { elementType: "input", elementConfig: { type: "text", placeholder: "Your Name" }, value: "" }, street: { e ...

The time.js library is providing inaccurate second values for the given date and time

Utilizing the moment.js library along with the Timezone and BusinessDays extensions in Vue.js, I am developing a datetime format for storing in a MySQL database. Here is the code snippet: import moment from 'moment-timezone' ...

Error in the syntax containing ?callback=jQuery1113

I'm attempting to initiate a simple JSONP query: <html> <head> <script type="text/javascript" src="js/jquery1.js"></script> <script> $(document).ready(function(){ $.ajax({ ...

Testing the mongoose.connect callback method in Jest: A step-by-step guide

Currently working on a new Express application and utilizing Jest as the testing framework. All sections of code have been successfully covered, except for the callback of the mongoose.connect method: https://i.sstatic.net/SNrep.png I attempted to spy o ...

Is there a way to extract information from an uploaded file in JavaScript without having to actually submit the file?

Looking for a way to extract data from a user uploaded file using Javascript without page submission? The goal is to process this data to provide additional options in a form on the same page. Any assistance with this would be highly appreciated. ...

Implementing an onclick function to initiate a MySQL update query

<?php include_once("db.php"); $result=mysql_query("SELECT * FROM stu WHERE receiver='DM4'"); while($row=mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ptype'] . "</td>"; echo "<td>" . $row[&apo ...

Versatile route able to handle any request thrown its way

My Node.js app is up and running smoothly using the Express.js framework. All routes in my application are set to post routes, such as: app.post('/login', function(req, res){ //perform login validation }); app.post('/AppHomepage', fun ...