Guide to animating camera movement to the top using three.js

Is it possible to tween the camera to a top position with a focus on an object in three.js using tween.js? The camera tweens to the right spot perfectly.

Please refer to this codepen: http://codepen.io/anon/pen/ObQxjV?editors=1111

Click here for image reference

var camera, renderer, controls;

//scene
scene = new THREE.Scene();

//camera
camera = new THREE.OrthographicCamera(640 / -2, 640 / 2, 480 / 2, 480 / -2, -5000, 10000000);
camera.position.set(500, 500, 500);      
scene.add(camera);

//renderer
renderer = new THREE.WebGLRenderer({ precision: "highp", antialias: true, preserveDrawingBuffer: false });
renderer.setSize(640, 480);
renderer.setClearColor(0xc2c2c2);
$("#canvas").append(renderer.domElement);

//controls
controls = new THREE.OrbitControls(camera, renderer.domElement, renderer, scene);
controls.target = new THREE.Vector3(250, 0, 0)

//axis 
var axis =  new THREE.AxisHelper( 30000 );
scene.add(axis);

//geometry
var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
var material = new THREE.MeshNormalMaterial();
var cube = new THREE.Mesh( geometry, material );
cube.position.set(250, 0, 0);
scene.add( cube );

animate();

function animate() {
  camera.updateProjectionMatrix();
  controls.update();
  TWEEN.update();
  requestAnimationFrame(animate);
  render();
}

function render(){
  renderer.render(scene, camera);
}

//working tween to the right
$("#workingTweenToTheRight").click(function () {
    var destPos = new THREE.Vector3(500, 0, 0);
    var destLookAt = new THREE.Vector3(250, 0, 0);

    var currentCamPos = {
      x: camera.position.x,
      y: camera.position.y,
      z: camera.position.z
    };

    var destCamPos = {
      x: destPos.x,
      y: destPos.y,
      z: destPos.z
    };
    var tween = new TWEEN.Tween(currentCamPos)
    .to(destCamPos, 600)
    .onUpdate(function () {
      camera.position.set(this.x, this.y, this.z);
      camera.lookAt(destLookAt);
      controls.target = destLookAt;
    })
    .start();
});

//not working tween to top
$("#notWorkingTweenToTheTop").click(function () {
    var destPos = new THREE.Vector3(0, 500, 0);
    var destLookAt = new THREE.Vector3(250, 0, 0);

    var currentCamPos = {
      x: camera.position.x,
      y: camera.position.y,
      z: camera.position.z
    };

    var destCamPos = {
      x: destPos.x,
      y: destPos.y,
      z: destPos.z
    };
    var tween = new TWEEN.Tween(currentCamPos)
    .to(destCamPos, 600)
    .onUpdate(function () {
      camera.position.set(this.x, this.y, this.z);
      camera.lookAt(destLookAt);
      controls.target = destLookAt;
    })
    .start();      
});
body { margin: 0; }
#canvas {float: left; width: 640px; height: 380px; border: 1px solid #d3d3d3 }
.button { border: 1px solid black; cursor: pointer; width: 230px; height: 20px;}
.button:hover{background: blue; color: white;}
<script src="http://sole.github.io/tween.js/build/tween.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="http://threejs.org/build/three.min.js"></script>
<div id="canvas">
<div id="workingTweenToTheRight" class="button" >working tween to the right</div>
<div id="notWorkingTweenToTheTop" class="button" >not working tween to the top</div>
</div>

Answer №1

To achieve the "tween to the top" effect, you must adjust the destPos setting in a unique manner:

var destPos = new THREE.Vector3().addVectors(cylinder.position, new THREE.Vector3(0, 500, 0));

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 could be causing the JavaScript array to not successfully transfer to PHP?

Despite searching for solutions, I am unable to get the desired outcome. When I check my JavaScript array in the console, it appears like this: [] 0:Object stock:27 createdtime:"2016-04-08T04:00:00+0000" id:"693852404037393999" units:438 ...

Even though the model value is set to true, the material check box remains unchecked

My mat-table contains data and checkboxes. The checkboxes should only be checked when the element.select property is true. However, when I use [(ngModel)]="element.select", all the checkboxes end up getting checked. Below you can find the code snippet: &l ...

js Display Type Error

Here is a snippet of JavaScript code that seems to be causing an issue: needSubIdCheck = $("#needSubIdCheck").text(); liveSupplierCount = $("#liveSupplierCount").text(); subIdCount = $("#subIdCount").text(); if(needSubIdC ...

Can you tell me how to add a variable to an array of objects in JavaScript?

I am currently engaged in a small project aimed at: Reading data from a CSV file (such as employee names and shifts) Displaying this data on FullCalendar. How can I incorporate the CSV result into this line of code: { id: 'a', title: 'Audi ...

Failure to Trigger Error Callback in Ajax Requests

I am encountering an issue with the error callback not being called when I pass the error function as an object parameter in a function. Strangely, when I define it directly within the ajax code, it works without any problems. var options = new Object(); ...

"Utilize Node to import either all dependencies or selectively choose specific

Should we only require the specific properties we need or the entire object? Example: Below is a snippet from my helper file 'use strict'; /** * getCallback * return a function used to make callback * @param {callback} callback - the callb ...

Create a JavaScript script within a CSS file

I'm attempting to create this using only CSS: Codepen and I would like to achieve the same effect but solely with CSS. This is what my CSS looks like: .text{ --a: calc(var(--a);*0.82+(1.5-var(--b);)/10); --b: calc(var(--b);+var(--a);); transfor ...

Exploring Manipulation of M:N Associations in Sequelize

I have set up a sequelize schema using postgre DB export const Commune = sq.define("commune",{ codeCommune: { type: DataTypes.STRING(5), allowNull: false, primaryKey: true }, libelleCommune: { type: ...

Using React Native - Issue occurs when tapping a row within a ListView: Property `_pressRow` cannot be read as it is null

Currently, I am working on a small ReactNative + Redux application that utilizes a ListView. While referencing the code example from the documentation available at here, I have implemented a setup similar to the provided sample. However, I am encountering ...

Determine whether an array is undefined in a React component

I am facing an issue in my React component where I have a code that renders the match name. However, sometimes the current match has an empty object and I don't know how to fix this problem. Here is the component code: import React from 'react&ap ...

Learning how to implement server side rendering in React JS through tutorials

After diving into the world of React js and mastering the basics, I successfully created web pages using this technology. I also honed my skills with node js and express. However, now I am faced with a new challenge: server side rendering. The tutorials av ...

Identifying shifts in offsetTop

In my design, I have a page where scroll bars are not allowed, meaning all content must be visible at once. The image below shows the basic layout of the screen to give you an idea. The layout consists of two blocks - Block 1 and Block 2. Block 1 is expan ...

Infura makes ten calls to eth_getBlockByNumber for every eth_call request

Currently, I am in the process of creating a straightforward nextjs API route (https://nextjs.org/docs/api-routes/introduction) that is linked to the Ethereum blockchain for executing a view function (which doesn't require any gas) from a smart contra ...

What steps can I take to prevent Geolocation from sending the information twice?

I am currently developing a crime alert platform and my goal is to automatically extract the user's longitude and latitude when they submit a new alert. However, I am encountering an issue where the submission is being duplicated, with one instance co ...

What is the best way to create an index for a user-provided value in a textbox

Looking for guidance on how to set an index to user-provided values in a textbox, append them to a table in a new row, and display that index in the first column of every row. I am unfamiliar with this type of functionality. $(document).ready(function() ...

Searching for a specific value in an array with jQuery: A step-by-step guide

I am looking to search for a specific value within an array and retrieve its index. Sample HTML: <div class="container"> <div class="col-md-6"> <div id="task0">Task0</div> </div> <div class="col-md-6"> &l ...

What strategies can be used to steer clear of overhyped products after adjusting state?

I have a function that retrieves data from Firebase. After getting the data, I want to set it into a state. So, I create an array and push all the data into it. Then, I update my state with this array. However, when I log or render this state, I encounter ...

Simulate a new Date object in Deno for testing purposes

Has anyone successfully implemented something similar to jest.spyOn(global, 'Date').mockImplementation(() => now); in Deno? I've searched through the Deno documentation for mock functionality available at this link, as well as explored t ...

The console is displaying 'undefined' when attempting to render the JSON response in Vue

In my vue.js application, I'm trying to display text using TypeScript. Here is an example of the JSON response: { "data": [ { "firstName": "Foo", "lastName": "Smith" }, { "firstName": "Mi ...

Tips for utilizing jQuery buttons to submit forms in PHP

I'm struggling to submit the form using PHP from a button in jQuery. I've tried adding an ID for the button, but I can't seem to submit the values. Since I'm new to jQuery, this task is proving to be a bit challenging. Can anyone provid ...