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 8748785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:35:07+00:00 2026-06-13T12:35:07+00:00

I have an interesting problem that I’ve yet to find a proper solution to.

  • 0

I have an interesting problem that I’ve yet to find a proper solution to. You can view the fiddle here: http://jsfiddle.net/DgN8C/3/. Basically, I have an image of a certain size. That image needs to be scaled down so that the user chosen center point (indicated by the red dot visually and data-center-* attributes [these are percentages based on the image width / height]) is in the middle of the containing div (x,y of 50,50 in the example). So, in an ideal world, given the following image:

<img src="image-source" data-center-x="59.125" data-center-y="37.4296" />

The ideal positioning within a 100 x 100 div would have a height of 138px with a left margin of -72px. This would ensure that the red dot in the center of the image (again, see the fiddle) is aligned to the center of the containing div.

Any help to get me going in the right direction would be greatly appreciated.

  • 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-06-13T12:35:08+00:00Added an answer on June 13, 2026 at 12:35 pm

    I think I’ve got it. You may want to check that the images are to scale. http://jsfiddle.net/DgN8C/7/

    Basically, I scale, then set the margins. The limiting factor, or the factor that tells us whether or not we need to scale the image, will be the smaller of the two sides. So if the dot is at 80%, we need the pixel width between the dot and the right side (image_width * 20%) to be able to fit in our enclosing div object (obj_width / 2). Note the if statement.

    So if our image does not fit in the object, we need to scale it bigger. And somehow I did that. It hurts to think about.

    // First, we need to scale image so that it will fit within the dimensions 
    // of the object. The limiting factor is shorter side of the pixel
    limit = Math.min(center_x, 100-center_x) / 100.0;
    
    // Check if image needs to be scaled
    if ((obj_width/2) > (limit*img.width())) {
        // Image must be scaled
        chunk = limit * 2; // Section we need from image
        // image_width * chunk = obj_width
        img.width( Math.ceil( obj_width / chunk ) );
    }
    

    Then I do the same thing for height. It looks like jQuery’s width() and height() functions keep everything to scale. May require further testing.

    Then I change the margins.

    img.css('margin-left', -1 * (((center_x/100.0)*img.width()) - (obj_width/2)));
    img.css('margin-top', -1 * (((center_y/100.0)*img.height()) - (obj_height/2)));
    

    EDIT Ok, the first attempt was not preserving aspect ratio. Should be fixed now. http://jsfiddle.net/DgN8C/9/ Logic is basically the same as below, except that I intelligently chose either width or height to scale. Setting only one will preserve aspect ratio.

    EDIT One more. http://jsfiddle.net/DgN8C/13/ This scales both larger and smaller to fit the containing div object. Previous solution was only scaling larger if necessary.


    This was a helpful post: jQuery resize to aspect ratio

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting SQL problem that I need help with. Here is the
Here's an interesting problem that I have just come across. It is possible to
I have an interesting problem here. I have a MySql database table that has
I have an interesting problem that i can't seem to figure out. I am
I have an interesting problem to solve here that may require some creative direction.
He're an interesting problem that looks for the most Pythonic solution. Suppose I have
Here's an interesting problem that I hope someone can lend a hand with. I
I have an interesting problem. I'd like to create a generic class that can
I have stumbled upon an interesting problem/bug that I eventually solved, but the solution
I have an interesting problem and I was looking for a solution; Hoping that

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.