Exploring the functionality of view offset and zoom properties on the camera in three.js simultaneously

I'm currently working on programming a three.js scene using a PerspectiveCamera. The goal is to display a specific sub-region of the entire camera view within the canvas, with the ability to zoom in. I have meticulously reviewed my offset values and confirmed that the zoom factor aligns logically.

When fully vertically fitted, the parameters are as follows:

current zoom 1 
x 714.4099378881989 y 0 w 3755.1801242236024 h 3888

Initially, everything functions correctly when the zoom is set to 1. However, once the zoom factor is modified, the alignment goes awry. Surprisingly, increasing the zoom beyond 1 works seamlessly without any offsets, focusing on the frustum's center. Despite utilizing TrackballControls, they should not affect this scenario since all values are being configured through code.

The relevant function responsible for updating the camera in the PerspectiveCamera class is as follows:

    updateProjectionMatrix: function () {

        var near = this.near,
            top = near * Math.tan( _Math.DEG2RAD * 0.5 * this.fov ) / this.zoom,
            height = 2 * top,
            width = this.aspect * height,
            left = - 0.5 * width,
            view = this.view;

        if ( this.view !== null && this.view.enabled ) {

            var fullWidth = view.fullWidth,
                fullHeight = view.fullHeight;

            left += view.offsetX * width / fullWidth;
            top -= view.offsetY * height / fullHeight;
            width *= view.width / fullWidth;
            height *= view.height / fullHeight;

        }

        var skew = this.filmOffset;
        if ( skew !== 0 ) left += near * skew / this.getFilmWidth();

        this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far );

        this.projectionMatrixInverse.getInverse( this.projectionMatrix );

    },

Upon initial analysis, it seems like the field of view (fov) pertains to the complete view rather than adjusting for sub-views. Alternatively, it might be necessary to divide the zoom by a factor based on the sub-view dimensions, but I am unsure where to commence in resolving this issue.

Answer №1

Finally cracked the code!

While working on my update function, I made a crucial discovery:

    this.zoom = zoom;

    this.setViewOffset(
      full.w,
      full.h,
      offset.x,
      offset.y,
      offset.w,
      offset.h
    );

    this.controls.update();

However, what truly unlocked the solution was adjusting the zoom relative to the offset like so:

this.zoom = zoom * (offset.h / full.h)

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

Guide to deploying a node.js web application with a local MongoDB database

Seeking guidance for a crucial query. I have developed a node.js API project that is currently linked with MongoDB on localhost. However, I intend to deploy this API for my react native project and require a URL address for it. The primary issue lies in t ...

Is there a maximum limit to the number of uniform or texture bindings in WebGL?

I'm attempting to display a car's license plate in WebGL using a purple texture named diffTex. When I render the car with a simple black material and no textures, the license plate is displayed correctly in purple as expected. The drawcall that ...

a guide to structuring REST API requests with axios in Vue.js

In my Vue.js app, I am utilizing Axios to consume a REST API. Each time I make an API call, I create a new instance of Axios with the necessary authentication headers by calling axios.get in various places. // UserdataComponent.vue const anInstance = axio ...

issue with brightcove player's complete event not triggering upon video replay

I have a videoplayer with an event listener added in an onTemplateReady function. Upon completion of the video, I want to replay it: videoPlayer.addEventListener(brightcove.api.events.MediaEvent.COMPLETE, completedCallback); function completedCallback(){ ...

Steps to incorporate this jQuery script

After receiving a solution to my problem, I'm struggling with how to actually put it into practice. $(function(){ $.get('file1.php', function(data){ $('#dropdown1').html( data ); }); // when dropdown1 is chang ...

What causes the discrepancy between the values returned by the jQuery getter css() method and JavaScript when accessing the style variable

After recently beginning to use jquery, I decided to switch due to initialization issues when loading external styles with javascript. Here is a rule defined in an external style sheet: #siteLogoDiv { position:absolute; left:0px; top:0px; width:100%; heig ...

Use Javascript to toggle the display to none for a specific table row (tr)

Is there a way to implement JavaScript code that will hide a specific table row using display:none;, and then reveal it again when a button is clicked? ...

CSS not reflecting changes after the href of the <link> tag has been updated

Hey there, I'm a beginner in the world of programming and currently facing an issue that I need help with. My goal is to dynamically update the CSS of my webpage by using JQuery to change the 'href' value in the link tag. In order to test t ...

The function `React.on('languageChanged')` is not effectively detecting changes in the language

I'm new to working with React and I'm looking for the best way to detect when a user changes their language preference. I am currently using next-translate to handle translations, but for some resources that come from an API, I need to update the ...

Enhancing presentation with a multitude of pictures

I am currently in the process of developing a slideshow feature that includes an extensive collection of images for users to navigate through using 'next' and 'previous' buttons. At the moment, each time a user clicks on the navigation ...

Order of execution and loading of JavaScript files in Webkit threading

I'm currently working on creating an XSS widget and I'm encountering some issues specifically with Webkit browsers when loading external JavaScript files that I'm appending to the DOM. Here's how it's set up: Widget.js appends 3 ...

The issue with GatsbyJS and Contentful: encountering undefined data

Within the layout folder of my project, I have a Header component that includes a query to fetch some data. However, when I attempt to log this.props.data in the console, all I get is 'undefined'. Could someone please point out where I might be m ...

How can I implement a view counter feature in a Vue application?

I am currently working on a Vue-based post website. One feature I want to add is the ability for users to see the number of views the post has received. Each time a user clicks on a post, the view count should increase by 1. I am utilizing Firebase as m ...

What is causing my sorting algorithm to produce inaccurate results?

After finding this sorting function as the best answer to my question, I tested it with example data and it worked perfectly. However, when I tried it with my actual data, it didn't work as expected and I'm not sure why. You can view my data her ...

Is it possible to manipulate videos embedded in an iframe using javascript?

It's common knowledge that JavaScript commands from Google can be used to control YouTube videos, while Vimeo provides its own JavaScript commands for their videos. Both videos are typically embedded within 'iframes' on websites. I'm ...

Utilizing jQuery to add a class to an element when a different one is hovered above

I'm currently working on implementing effects on the edges of a webpage that will be triggered when a central div is hovered over. The main tool I'm using for this task is jQuery. Despite my efforts to diagnose the issue by using alerts in my Ja ...

Discovering multiple attribute values in Semantic-UI-React: A comprehensive guide

This question is quite straightforward. One thing to note is that I have the multiple attribute, so when a value is selected, it gets pushed into an array. This means that having multiple values will result in an array with multiple values, but not the spe ...

Create a React component using the value stored within an object

I am interested in creating an object: import React from "react"; import { Registration } from "../../"; const RouteObj = { Registration: { route: "/registration", comp: <Registration /> } }; export default RouteObj; Next, in a separat ...

Using jQuery to manipulate HTML elements that are dynamically loaded via ajax

Currently, I am enhancing my basic HTML tables with the help of the jQuery plugin called DataTables. The following code allows me to load the DataTables plugin for all pages: <script> $(document).ready(function() { $('table. ...

liberating RAM in three.js

My application is loading a large number of meshes. When I try to dispose of old meshes to free up memory, it seems the memory is not actually being released. Am I missing something? Here is a simple example to reproduce the issue: Load 100 large binary ...