Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3307010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:21:22+00:00 2026-05-17T21:21:22+00:00

I have a draggeable image contained within a box. You can zoom in and

  • 0

I have a draggeable image contained within a box. You can zoom in and zoom out on the image in the box which will make the image larger or smaller but the box size remains the same. The box’s height and width will vary as the browser is resized. The top and left values for the image will change as it is dragged around.

I’m trying to keep whatever the point the box was centered on in the image, in the center. Kind of like how zoom on Google Maps works or the zoom on Mac OS X zooms.

What I’m doing right now is calculating the center of the box (x = w/2, y = h/2) and then using the top and left values for the image to calculate the position of the image in the center of the box. (x -= left, y -= top).

Then I zoom the image by growing or shrinking it and I use the scale change to adjust the coordinates (x = (x * (old_width/new_width), y = (y * (old_height/new_height)).

I then reposition the image so that its center is what it was before zoom by grabbing the coordinates it is currently centered on (that changed with the resize) and adding the difference between the old center values and the new values to the top and left values (new_left = post_zoom_left + (old_center_x – new_center_x), new_top = post_zoom_top + (old_center_y – new_center_y).

This works ok for zoom in, but zoom out seems to be somewhat off.

Any suggestions?

My code is below:

app.Puzzle_Viewer.prototype.set_view_dimensions = function () {

  var width, height, new_width, new_height, coordinates, x_scale,
    y_scale;

  coordinates = this.get_center_position();
  width = +this.container.width();
  height = +this.container.height();
  //code to figure out new width and height
  //snip ...
  x_scale = width/new_width;
  y_scale = height/new_height;
  coordinates.x = Math.round(coordinates.x * x_scale);
  coordinates.y = Math.round(coordinates.y * y_scale);
  //resize image to new_width & new_height
  this.center_on_position(coordinates);
};

app.Puzzle_Viewer.prototype.get_center_position = function () {

  var top, left, bottom, right, x, y, container;

  right = +this.node.width();
  bottom = +this.node.height();
  x = Math.round(right/2);
  y = Math.round(bottom/2);
  container = this.container.get(0);
  left = container.style.left;
  top = container.style.top;
  left = left ? parseInt(left, 10) : 0;
  top  = top ? parseInt(top, 10) : 0;
  x -= left;
  y -= top;
  return {x: x, y: y, left: left, top: top};
};

app.Puzzle_Viewer.prototype.center_on_position = function (coordinates) {

  var current_center, x, y, container;

  current_center = this.get_center_position();
  x = current_center.left + coordinates.x - current_center.x;
  y = current_center.top + coordinates.y - current_center.y;
  container = this.container.get(0);
  container.style.left = x + "px";
  container.style.top = y + "px";
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:21:22+00:00Added an answer on May 17, 2026 at 9:21 pm

    [Working demo]

    Data

    • Resize by: R
    • Canvas size: Cw, Ch
    • Resized image size: Iw, Ih
    • Resized image position: Ix, Iy
    • Click position on canvas: Pcx, Pcy
    • Click position on original image: Pox, Poy
    • Click position on resized image: Prx, Pry

    Method

    1. Click event position on canvas -> position on image: Pox = Pcx - Ix, Poy = Pcy - Iy
    2. Position on image -> Pos on resized image: Prx = Pox * R, Pry = Poy * R
    3. top = (Ch / 2) - Pry, left = (Cw / 2) - Prx
    4. ctx.drawImage(img, left, top, img.width, img.height)

    Implementation

    // resize image
    I.w *= R;
    I.h *= R;
    
    // canvas pos -> image pos
    Po.x = Pc.x - I.left;
    Po.y = Pc.y - I.top;
    
    // old img pos -> resized img pos
    Pr.x = Po.x * R;
    Pr.y = Po.y * R;
    
    // center the point
    I.left = (C.w / 2) - Pr.x;
    I.top  = (C.h / 2) - Pr.y;
    
    // draw image
    ctx.drawImage(img, I.left, I.top, I.w, I.h);
    

    This is a general formula that works for zooming in or out, and can handle any point as the new center. To make it specific to your problem:

    • Pcx = Cw / 2, Pcy = Ch / 2 (alway use the center)
    • R < 1 for zooming out, and R > 1 for zooming in
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a draggable image within a div. I can click on the image
I have an image which when onmousedown is triggered, it runs a function that
I am trying to make overlay over image while dragging but its also moving
I have a div containing an image that can be dragged around inside the
I have a draggable view that a user will touch, but some rectangles of
I have a div with overflow:hidden which contains image ( .draggable()) that is usually
I've been developing a image manipulation script which has a background and user can
I have a form in which user inputs its data along with its image.
I have a draggable image within a div functioning. However I would like to
I have some particular need to clone an element (image) and be draggable inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.