When the image is zoomed in, it is not moving smoothly when being dragged

Recently, I have come across an issue with the function provided below. It seems that when an image is zoomed in, it can be dragged beyond its boundaries. This results in a gap forming between the image and its container, which widens as the image scale increases.

It appears that the dimensions of the image are being accurately captured by the getBoundingClientRect method as it scales. However, the root cause of the problem remains unidentified.

If you have any suggestions or solutions to address this issue, they would be greatly appreciated.

dragMove(event) {
    if (this.isDragging && this.imageScale > this.minZoom) {
        const containerRect = this.$refs.croquisContainer.getBoundingClientRect();
        const imageRect = this.$refs.croquisImage.getBoundingClientRect();
    
        const maxOffsetX = (imageRect.width - containerRect.width) / 2;
        const maxOffsetY = (imageRect.height - containerRect.height) / 2;
    
        const deltaX = event.clientX - this.lastX;
        const deltaY = event.clientY - this.lastY;
    
        const newOffsetX = this.offsetX + deltaX;
        const newOffsetY = this.offsetY + deltaY;
    
        this.offsetX = Math.max(Math.min(newOffsetX, maxOffsetX), -maxOffsetX);
        this.offsetY = Math.max(Math.min(newOffsetY, maxOffsetY), -maxOffsetY);
    
        this.lastX = event.clientX;
        this.lastY = event.clientY;
    }
}

JSFiddle: https://jsfiddle.net/n4d7txwy/

UPDATE

This fixes the initial issue

const maxOffsetX = (imageRect.width - containerRect.width) / (2 * this.imageScale);
const maxOffsetY = (imageRect.height - containerRect.height) / (2 * this.imageScale);

Answer №1

It appears that considering the current scale factor is crucial in calculating the maximum offset for your situation - as demonstrated by

const maxOffsetX = (imageRect.width - containerRect.width) / 2 / this.imageScale;
const maxOffsetY = (imageRect.height - containerRect.height) / 2 / this.imageScale;

This approach seems to yield the expected behavior, view it here: https://jsfiddle.net/bzdwL914/

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

applying various conditions to JavaScript arrays for filtering

After spending countless hours trying to solve my filtering issue, I'm still struggling. I'm in the middle of creating a react marketplace where users need to be able to apply multiple filters on one page. Here's an example of my product lis ...

Associate a fresh entity with an Angular form rather than individual attributes

My interface is quite simple: (models.ts) export interface User{ Id : number; FirstName : string; LastName : string; Email: string; PhoneNumber: string; } As for my form, it's also pretty straightforward: (app.component.html) <fieldset class ...

Having trouble getting my Node.js application to deploy properly on Heroku

Check out my repository here: https://github.com/jacklemasters/tech-blog Although the app runs smoothly on my local machine, I can't figure out why it's not working on Heroku. I've tried moving the code completely and creating a new reposit ...

Elements encapsulated by jQuery are not functioning properly within a div that has been dynamically loaded by jQuery-AJ

jQuery(function($) { $('#content').on('click', '.pgr a', function(e){ e.preventDefault(); var link = $(this).attr('href'); $('#view').fadeOut(500, function(){ $(this) ...

Execute the php code once more following a database row update

Is it possible to rerun a PHP line after updating a table in the database? I have an HTML button that triggers a jQuery post request: btn.onclick = function(e) { $.post('inc/pstPts.php', { pts: pts }); } I ...

Using AJAX to Transfer Numerical Data

I am new to javascript and I am trying to implement a LIKE button on my website. <a href="" onClick="likePost(<?php echo $post->ID; ?>); return false;">LIKE</a> Additionally, here is the function that I am using: function likePost( ...

Issues with NextJS detecting environmental variables

I recently encountered an issue with my NextJS app running on Next.js v12.2.5 where it appears to be ignoring the environment variables I've configured. To address this, I created a .env.local file with the following content: NEXT_PUBLIC_SERVER_URL=h ...

The error message "SharedArrayBuffer is not defined" occurred when attempting to utilize ffmpeg.wasm

<!DOCTYPE html> <html> <head> <title>TikTok Live Downloader</title> </head> <body> <h1>TikTok Live Downloader</h1> <label for="username">Username:</label> ...

Transmit specific elements from an array to another array exclusively with Javascript

I have some data stored in a JSON array like this: source=[{"OperationName":"All","PrivilegeName":"Roles CRUD"}, {"OperationName":"Read","PrivilegeName":"Roles Read Delete"}, {"OperationName":"Delete","PrivilegeName":"Roles Read Delete"}, ...

Using Jquery to Redirect Users According to URL Position

My current dilemma involves... I want to create a condition where if the URL specifically contains /foldername/index.htm or /foldername/ on mydomain.com, then it should redirect to http://www.example.com If the URL includes any URL parameter with /folder ...

What is the proper way to utilize useRef with HTMLInputElements?

Dealing with React and hooks: In my code, there is a MainComponent that has its own operations and content which refreshes whenever the value of props.someData changes. Additionally, I have designed a customized InputFieldComponent. This component generat ...

Tips for eliminating the apostrophes in the request header of a Node.js application

Just a simple header object, that's all I have. let headers = { "pragma": "no-cache", "upgrade-insecure-requests": 1 } Using this header object with request-promise in node.js causes a timeout when sending a request to the web url. However, if ...

Guide on embedding a form component within an iframe using ReactJS

I am trying to figure out how to render my form component inside an iframe, but so far it's only rendering the iframe itself. Any tips on how to accomplish this task? import './App.css'; import ReactDOM from "react-dom/client" impo ...

The specified path does not contain any of these files

Encountered an error while attempting to run a react native app on an android emulator: None of these files exist: * src\components\spacer\spacer.component(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.na ...

Can someone explain what {...props} means within the context of React Navigation's Stack.Screen?

Can you explain the importance of using {...props} on this specific page? <Stack.Screen name="Home"> {props => <HomeScreen {...props} extraData={someData} />} </Stack.Screen> Interestingly, my application functions correctly even w ...

What is the most effective method for determining if an object contains a particular property?

Can anyone advise on the best approach to check if an ajax response object has a specific property? I've done some research and it seems there are various methods to do this. One way is: if(ajaxResponse.hasOwnProperty('someProperty')){ ...

How does BodyParser from Express handle a request that results in a Bad Request with a Status

I am currently working with Node and Express. Here is the function I am using to fetch JSON data from the server: $.ajax({ url: url, contentType: "application/json", dataType: "json", ...

Making changes to an object using a different object

Is it possible to merge one object with another object? For instance master = { name: "John", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e389a389cd808c8e">[email protected]</a>" } child = { phone: ...

Running an AJAX request in a Flask application's select box onchange event: A guide

I'm working on a Flask app that has multiple select boxes. Each select box should populate with data based on the selection made in the previous select box. I plan to use AJAX for this functionality, where each select box will be submitted upon the on ...

What is the best way to delete an input field once it has been cleared?

After scouring through resources, I found a way to dynamically add input fields on keystroke by referencing an answer from this question. To see my implementation, check out this example. However, one challenge remains - removing a field if the user delete ...