Can you focus on the central area of the visible picture?

My current project involves developing an e-comic book reader in electron, and I am facing a math-heavy challenge. I want to implement a zoom functionality that doesn't just enlarge from the top-left corner but instead zooms in from the center.

@100%        @200% Currently   @200% Ideally      
----         --------          --------
|  |         |  |   |          | ---- |
----         ----   |          | |  | |
visible      |      |          | ---- |
size         --------          --------

The zoom feature is connected to an input slider with a range of 100 to 300, which is then converted to a percentage for the visible div.

function pageZoom() { // invoked onchange
 var outer = document.getElementById('viewer');
 var inner = document.getElementById('innerWindow');
 var zoomSlide = document.getElementById('zoomSlider');

 inner.style.width = zoomSlide.value + "%";
 var scrollShift = (zoomSlide.value - 1)/2; // This is the function that needs to change

 outer.scrollTop = scrollShift; // to update the scrollbars to the correct position
 outer.scrollLeft = scrollShift;
};

This is how the HTML code looks like...

<div class="mainWindow dragscroll" id="viewer"> <!-- this element has the scroll bars -->
    <div id="innerWindow"> <!-- this element grows by input -->
        <img id="viewImgOne" /> <!-- set at 50% width -->
        <img id="viewImgTwo" /> <!-- set at 50% width -->
    </div>
</div>

It has been quite some time since I last dealt with any mathematical concepts, so my algebra skills are a bit rusty.

 var body = document.body; // clientHeight & clientWidth represent the user-visible dimensions
 var outer = document.getElementById('viewer');
 var inner = document.getElementById('innerWindow');
 var zoomSlide = document.getElementById('zoomSlider'); // Ranging from 100 to 300

Do you have any recommendations or suggestions?


edit: The concept is similar to CSS but requires different techniques for handling zoom operations, as well as a more permanent execution aside from just hovering over elements. Various inputs are needed for window dimensions, different zoomed locations, and implementing JavaScript to address these challenges.

Answer №1

function adjustZoom() {
 var container = document.getElementById('viewer');
 var windowContent = document.getElementById('innerWindow');
 var zoomSlider = document.getElementById('zoomSlider');
 var imageOne = document.getElementById('viewImgOne');
 var imageTwo = document.getElementById('viewImgTwo');
 console.log(windowContent.clientHeight)

 // Find Center Points
 var centerX = container.scrollTop + container.clientHeight/2;
 var centerY = container.scrollLeft + container.clientWidth/2;

 // Calculate Position Ratios
 var ratioX = centerX/windowContent.clientHeight;
 var ratioY = centerY/windowContent.clientWidth;

 windowContent.style.width = zoomSlider.value + "%";
 if(imageOne.clientHeight >= imageTwo.clientHeight) {
   windowContent.style.height = imageOne.clientHeight + "px";
 } else {
   windowContent.style.height = imageTwo.clientHeight + "px";
 };
 container.scrollTop = windowContent.clientHeight*ratioX - container.clientHeight/2;
 container.scrollLeft = windowContent.clientWidth*ratioY - container.clientWidth/2;
};

Retrieve the percentages, store them, zoom in, then reapply the percentages to the whole, subtracting half of `outer.clientWidth` to align with the `scrollTop` and `scrollLeft` positions again. It may not be perfectly smooth, but it does the job.

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

Attempting to iterate over a JSON object and display its contents using HTML

I have been attempting to iterate through this JSON data and display it in an accordion format similar to the one shown here: but unfortunately, my implementation is not functioning correctly. Here is what I currently have: HTML: <div class="a ...

Preventing users from using alt+tab on an IE8 aspx web page

I need help with disabling the alt+tab function in my IE8 web browser for a page that displays a modal dialogue. Can anyone assist me with this issue? ...

The background image is failing to display, although other styles for the class or element are visible

Why won't the background image display, even though I have verified that the image path is correct and changing other properties like color or background color works just fine? Here is my CSS code snippet: .container { background-imag ...

Is there a way to maintain the vertical alignment of spans within an HTML div after numerous drag and drop operations have taken place?

Recently, I created a JavaScript program (code provided below) to enable the dragging and dropping of <span> elements containing Greek characters within a basic <div> container. Upon initial page load, everything appears perfectly aligned vert ...

The jQuery plugin is not functioning properly on the subdomain

Encountered an issue where a jQuery plugin is not loading in Chromium for a peculiar reason. Interestingly, the plugin loads fine when accessing the page through the root domain but fails to load when accessed via a subdomain in Chromium. However, everyth ...

Implementing the ability to add and remove classes in JavaScript

I am trying to implement an add or remove class feature in JavaScript, but I'm facing some issues with it. Below is the code snippet I have: if (window.location.hash == "#/profile/"){ document.getElementById("tabMenu").removeClass("bottomTa ...

Empower Your Development with Remote Logging using React

My latest project involves setting up an Express Server with Mongo to capture console logs during debug testing of an Electron app using React. Instead of manually logging events, I use ajax to send data that would typically be printed with console.log. W ...

A useful tip for emphasizing tags that have attributes with endings

Here is the HTML list I'm working with: <li class=​"info" data-info=​"" data-title=​"info 1" data-info-id=​"222643">…</li> <li class=​"info" data-info=​"" data-title=​"info 2" data-info-id=​"217145">…</li> ...

Having trouble with the installation of Parcel bundler via npm

Issue encountered while trying to install Parcel bundler for my React project using npm package manager. The terminal displayed a warning/error message during the command npm i parcel-bundler: npm WARN deprecated [email protected]: core-js@<3 is ...

Amazon S3 is not sending the 'Access-Control-Allow-Origin' header for the requested resource

Currently utilizing reactjs and axios Encountering an issue while attempting to fetch a PNG file from S3 to the local environment. Access to fetch at 'https://xxx-xxx-xxx.s3.ap-northeast-1.amazonaws.com/0.png' from origin 'http://localhost: ...

Is there a way to make an element disappear after a certain amount of time without it appearing to shrink?

What I am trying to achieve: I am looking for a way to make an element or image disappear after a specific time period, such as 3 seconds. However, using the .hide(3000) method causes a shrinking effect, while the .fadeOut(3000) method fades the element o ...

"The changes made to the list are not being accurately displayed by Angular's ng

I am working on a directive that injects dynamic templates during ng-repeat. While I can successfully add items to the list, they do not appear in the view for some reason. It seems like I am missing a crucial piece to make it work correctly. You can find ...

Numerous Ajax calls made to Flask-Python endpoint - Yet, only one was successful

i'm attempting to iterate through several forms with input file type and employ Ajax to send them to Flask This is how the javascript looks like :- I have a list of forms stored in the variable var upFC = document.getElementsByClassName('upload ...

What is the best way to verify the status codes of multiple URLs using JavaScript?

In my JavaScript project, I am currently checking the status codes of various URLs. While I can successfully check the status for one URL at a time by writing the code manually, I am facing an issue as I have over 100 URLs to check. This is making my cod ...

It seems that you're facing an issue with the react-native-git-upgrade tool. The error message

I encountered issues while attempting to upgrade using react-native-git-upgrade. One of the first problems I faced was similar to what was described in https://github.com/facebook/react-native/issues/11578. Unfortunately, none of the solutions mentioned i ...

The background color is not extending to the edges of the row or column

After making adjustments to the layout of a specific section on my webpage by adding rows and columns, I noticed a blank strip where the background color fails to fill in. The rows within this section are contained within a div class called container-fluid ...

Troubleshooting an issue with an Asp.net page freezing during the conversion of a Word

Currently, I have a function that I utilize to convert a Word document to an HTML file, which is utilized in a web-based document viewer. The function returns the path to the newly generated HTML file, which is then displayed within an iframe. For your ref ...

Sending query parameters from one URL to another using jQuery

Greetings! I have implemented a load more feature using a script that loads additional content on the same page when the 'Load More' button is clicked. Below is the script: <script type="text/javascript"> var track_page = 1; load_c ...

Unleashing the Power of Aurelia's HTML Attribute Binding

I need to bind the "required" html attribute in my template and model. Along with passing other information like the label value using label.bind="fieldLabel", I also want to pass whether the element is required or not. However, simply using required="tr ...

Adjusting the positioning of elements in a constantly changing scenario

Utilizing Bootstrap, I aim to keep the top-right corner of the red background fixed to the upper right corner of the blue section within the visible area regardless of window resizing due to its responsive design. The red element should be position fixed ...