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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:31:41+00:00 2026-06-02T20:31:41+00:00

Looking for an expression that allows me to accomplish this: I have an image

  • 0

Looking for an expression that allows me to accomplish this:

I have an image of arbitrary width/height, whose dimensions I can grab before I draw it.
Because the image may be very large, I want to scale it down.

My canvas is going to have width w and height h.
For illustration purposes let’s just say it’s 320×240.

If the dimensions of the image are equal or smaller than the dimensions of the canvas, then the scale ratio is just 1.
If they are larger, I will scale it proportional to how much larger it is compared to the canvas size.

So for example if my image is 640×480, my scale ratio will be 0.5
If my image is 640×240, my scale ratio would still be 0.5
Similarly if it were 320×480

Can this be written in a single math expression? For ex:

def scale_ratio(canvas_width, canvas_height, image_width, image_height)
   #math formula for calculating scale
   return scale
  • 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-02T20:31:43+00:00Added an answer on June 2, 2026 at 8:31 pm
    function scale(canvas_width, canvas_height, image_width, image_height) {
      return Math.min(Math.max(canvas_width / image_width, canvas_height / image_height), 1);
    }
    

    EDIT: You might want to do something like this to reduce rounding errors:

    var scale_width = image_width;
    var scale_height = image_height;
    if (image_width > canvas_width || image_height > canvas_height) {
        var image_ratio = image_height / image_width;
        if (image_ratio * canvas_width > canvas_height) {
            scale_width = canvas_height / image_ratio;
            scale_height = canvas_height;
        } else {
            scale_width = canvas_width;
            scale_height = image_ratio * canvas_width;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to have a regular expression that allows only specific numbers to
I'm looking for a regular expression that can find the alpha representation of a
Let's say for example that I have a regular expression like this: The quick
Hi I'm looking a regular expression that will change this: [check out this URL!](http://www.reallycoolURL.com)
I'm looking specifically for a regular expression that will grab the last term of
I'm looking for something that allows me to sort a list of regular expression,
I'm looking for a regular expression to use in Refex.Replace that allows me to
I'm looking for a regular expression that allows for either single-quoted or double-quoted strings,
I am looking for a single regular expression that will replace all replace me
We are looking to sum the output of an expression meaning that it does

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.