Guide on how to halt camera animation in three.js when it reaches a specific position upon clicking

I'm currently working on a small piece of code that animates the camera to zoom in on an earth model when clicked. However, I'm facing an issue where I need to stop the camera animation once it reaches a certain position.

Initially, the camera position starts at 20 and gradually reduces as part of the animation loop triggered by clicking on the sphere model. I'm looking for a way to halt this animation when the camera position hits 5.

Check out the current implementation here

var domEvents = new THREEx.DomEvents(camera, renderer.domElement);

domEvents.addEventListener(earth, 'click', function(event){
    console.log('you clicked on the earth');

    alert("test");


var render = function (actions) {

        renderer.render(scene, camera);

        console.log(camera.position.z); // starts at 20 and reduces with every call

        camera.position.z -= 0.1;

        requestAnimationFrame( render );

    };

    render();

    // cameraloop();


}, false)

At present, the camera continues its animation endlessly without stopping. My goal is to make it pause when it reaches position 5.

Answer №1

To establish a minimum distance of 4.11 units for the camera in relation to the sphere, set controls.minDistance to this value (calculated as the sum of sphere radius, camera near plane distance, and a slight buffer).

For more information, visit: https://jsfiddle.net/2nbgfkpy/

Remember to call controls.update() after adjusting the camera position to ensure that OrbitControls properly applies constraints on the camera movement.

Stack Overflow is becoming increasingly stringent with their posting guidelines.

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

Acquiring a website's dynamic value using jquery

This question is pretty self-explanatory... I am trying to extract a value from a website's source code, but the value I need is dynamically generated using jQuery. Let's use example.com as an example: <div id="currentTime"></div> ...

What is the best way to display the JSON data?

<!DOCTYPE HTML> <html> <head> <title></title> <link href="/bundles/hoaxpartner/css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <div id="header">Backbone</div> &l ...

A guide on implementing a Burger Menu to toggle the Sidebar Component within the Header using React Hooks

Seeking assistance with React as I am new to it. Can someone help me with toggling my Sidebar within my Header Component using Hooks? Below is my code: Sidebar.js ...imported modules ...styles export const SideBar = () => { const history = useHisto ...

Utilizing jQuery to Toggle Visibility of Table Rows on Button Click

I have a unique layout on my page where there are two tables positioned side by side. The table on the left consists of buttons with company names, and the table on the right should display employees associated with each specific company. Upon initially l ...

I seem to be facing some difficulty in dynamically calling my buttons in AngularJS

Can you assist me in solving this problem? I am new to Angular and just starting out. Initially, there is only one button on load called "Add list". When the user clicks on this button, they are able to add multiple lists. Each list contains a button labe ...

What could be causing node.appendChild() to malfunction?

I need some help with a piece of code that I'm working on for a calendar application. The code is meant to dynamically generate and display dates for the current month using ReactJS. However, I keep encountering an error that says "appendChild is not ...

Discovering an object that lacks a specific property using JSONPath

Here is a sample json: { "data": [ { "name": "Peter", "excluder": 1 }, { "name": "Sansa" } ] } I am looking to extract elements that do not have the ...

Revamping Three Js: Transforming vertices, normals, and indices

This code generates the following render: render of the code I am facing an issue with updating the vertices. In my code, I am attempting to update three points just to test if it works. The geometry includes a normal vector and indices, so perhaps tho ...

VUE- the GetElementByClassName function is malfunctioning

I attempted to utilize 'getelementbyclassname' within VUE methods. My reason for doing so is that instead of applying information using :style, I would like to adjust the width of the div where I applied my class named 'classon'. I ...

Utilizing a class function within the static method `getDerivatedPropsFromState`

I've been updating the deprecated life-cycle method componentWillRecieveProps() to its new replacement static getDerivatedPropsFromState(). The issue I'm encountering with this update is that componentWillRecieveProps() uses a class function int ...

Tips for Increasing Version Number with Gulp Task

Looking to update the version number in a JavaScript file (myConstantsFile.js) with a new string. Currently, the version number is "01.11.15" and appears like this in myConstantsFile.js along with other constants: .constant('productVersion', &ap ...

The jQuery fadeToggle function toggles the visibility of an element starting from hidden instead

I'm having an issue where text in my div only appears on the second click, instead of the first. What could be causing this problem? $('#fPaperCirclePic').on('click', function () { $('#fPaperCircleText, #isargebla, #moq10 ...

Comparing the cost of memory and performance between using classes and interfaces

As I delve into writing TypeScript for my Angular project, one burning question arises — should I use an Interface or a Class to create my domain objects? My quest is to uncover solid data regarding the actual implications of opting for the Class route. ...

Display the div based on the choices made in both select elements

Image of code is currently not accessible on this device Apologies for the lack of visual aid, I have coded on a different device. I am looking for a way to make the div appear based on certain options selected from both dropdown menus. Any assistance wit ...

"Using Node.js for proxying requests and implementing OAuth authentication

Having a small node.js application that relies heavily on oAuth, I encountered an issue: the server where I intend to install it is concealed behind a proxy. This necessitates rewriting a portion of the code to incorporate proxy usage. Here's my query ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

Generating and verifying checksums for strings in Node JS: A step-by-step guide

I am in the process of rewriting a function in Node.js for generating and verifying checksums for payment transactions. I am new to coding in Node.js and need some guidance. The code I have received from the Service Provider needs to be converted into Nod ...

create a JavaScript array variable for posting items

My goal is to use this javascript to make four posts for the 'var msg' array. However, it currently posts 'encodeURIComponent(msg[i])' four times instead. How can I resolve this issue? var msg = ['one', 'two& ...

Is it true that by utilizing Vue's v-for, every line of HTML code becomes a

I'm struggling to utilize v-for in order to create multiple div elements, but no matter how I attempt it (even trying to iterate over a list), the v-for doesn't seem to function properly and always turns the line into a comment when executed. No ...

Is it feasible to choose the component generated by this element?

My current dilemma involves a component that renders a form, however, it also has its own form "catcher". var FormUpload = React.createClass({ submit : function(){ var formdata =new FormData(); ...