Saturday, April 1, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Maintain Aspect Ratio While Resizing – JavaScript – SitePoint Forums

learningcode_x1mckf by learningcode_x1mckf
January 25, 2023
in JavaScript
0
Time limit for notify – JavaScript – SitePoint Forums
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


So… lets discuss principle for a minute. I’m gonna get mathsy, so I perceive if you wish to skip this publish :wink:

A field could be outlined by 2 factors. Particularly, in an internet browser when defining a field, you outline two factors in abstraction: the highest left nook (x,y), and the underside proper nook (x+width, y+peak). The browser can then extrapolate the remainder of the field’s sides from that info (x,y,w,h). Right here’s my demonstration field. It’s a sq., however it doesnt matter what rectangle it’s, the next holds true.
image

You outline side ratio as width/peak; I’m really going to outline is as peak/width; these are successfully equal statements: w/h = 1/(h/w) … you get a quantity both means round. So why have I outlined it peak/width?
In case you discovered maths in an american system, the subsequent bit ought to ring some bells: I’m going to name my peak the rise, and my width the run. I’m defining a slope of a line. And that is sensible, in our image. If i outline my line to undergo the highest left nook of the field, the slope of the road h/w corresponds to drawing a line from the highest left nook of the field, by way of the underside proper nook of the field:
image

Now right here’s the place resizing is available in. If you wish to keep your side ratio, what you might be saying is the slope of that line can’t change. You’re going to maneuver one nook of the field (for demonstration functions, shall we say you’re shifting the underside proper nook). We now have two methods of resizing the field, as a result of there’s really 3 well-defined factors on our line.

(“Three? the place’s the third?”)

If the road bisects the field, from high left to backside proper, the road should additionally cross by way of the precise heart of the field; the middle level could be discovered from the highest left nook by going half your peak and half your width. (That is the place utilizing Paint considerably fails me, as i’m… going to need to approximate the middle of the field. However you get the purpose.)

image

For the needs of concrete demonstration, I’m going to provide my field some numbers:
The highest left nook of my field is at 40,40. The underside proper nook of my field is at 100,100; my side ratio is 1/1 (a sq.), and so my midpoint have to be at 70,70.

Anchoring

If we’re moving the bottom-right corner of the box, we are redefining our box; the bottom-right corner must be part of the box. There are now two (commonly used) ways of redefining the box: Anchoring from the Middle, and Anchoring from the Top Left (or more generally speaking: Anchoring from the opposite corner from which you’re moving). Which you choose is up to you.

Anchoring from the Middle

You’ve probably seen this one in drawing applications; when you anchor in the middle, the middle of the box stays still. We’re moving the bottom right corner, and we want to keep the aspect ratio the same.
If I move my bottom right corner outwards along the line, in order for the middle point to stay fixed, the top left corner must move the same distance in the opposite direction; if my bottom-right corner goes from 100,100 to 120,120, then my top-left corner which started at 40,40 must move to 20,20, in order for the midpoint to remain at 70,70.
borrows GIF from the internet for demonstration. Demo does not retain aspect ratio, but it shows the anchoring from the middle.
Sketch tutorial: What's new with group resizing in Sketch 44

You might also like

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

Understanding the Power of Proxy in JavaScript – hackernoon.com

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

Anchoring from Top Left (Opposite Corner)

You’ve definitely seen this one – resize your browser window, and you’ll see an example. If i move one corner of the box, the opposite corner is going to be my anchor; that point will not move. So if I move my bottom right corner to 120,120, the top left corner stays at 40,40, and the middle has to move; it’ll end up at 80,80. My aspect ratio is maintained, and the world’s happy.
(this doesnt show maintaining an aspect ratio either, but it demonstrates the anchoring type)
canvas - Correct algorithm to resize a rectangle on mouse drag - Stack Overflow

So step 1 will be figuring out which type of anchoring you want to do, as it changes the code and the maths you need to do. Then, we talk about something we’ve taken for granted…

Maintaining the Aspect Ratio

So we’ve so far just generally breezed over the statement “we’ll keep the aspect ratio”. If you go back to our paint drawing:
image
If we are moving the bottom right corner, as long as we put it down on the green line anywhere, we’re good.
But the mouse isnt limited to the green line. The mouse can go anywhere. How do we handle it when the user puts the mouse somewhere that ISNT on the green line? What if it’s somewhere over here?
image

Well, we cant put the corner there; it won’t have the right aspect ratio. So we’re going to have to change one or both of the dimensions to get it back on the green line.

image

There’s a couple of options here. Paul’s suggestion above would say “ignore the height. take the new width, put the corner on the green line so that the width is where the mouse pointer is; that’s your new box.” (That’s the black dot)
You can of course do the opposite; ignore the width and use the height; effectively the same code, but using the other two variables. (That’s the cyan dot.)
You can try and be clever; if the mouse is above the line, use the width; if it’s below the line, use the height; this is “using the longest dimension”. (In my example above, you would use the width, and put the point at the black dot)
You could also try and do some maths and figure out the closest point on the line to the cursor, but that feels a bit excessive. Or maybe it doesn’t. That’s up to you, again. (That’s the orange dot.)

So… the questions now return to you. Have a think about what you want the code to do; where you want to anchor, and where you want to end up when the mouse isnt on the aspect ratio line. We can help from there.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

4 Packages for Working With Date and Time in JavaScript  MUO - MakeUseOf Source link

Read more

Understanding the Power of Proxy in JavaScript – hackernoon.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Understanding the Power of Proxy in JavaScript  hackernoon.com Source link

Read more

JavaScript vs. TypeScript: What's the difference? – TheServerSide.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JavaScript vs. TypeScript: What's the difference?  TheServerSide.com Source link

Read more

JetBrains updates IDEs for Java, JavaScript, Ruby – InfoWorld

by learningcode_x1mckf
March 31, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JetBrains updates IDEs for Java, JavaScript, Ruby  InfoWorld Source link

Read more

Virtru Announces First Ever FIPS 140-2 Validated JavaScript … – GlobeNewswire

by learningcode_x1mckf
March 30, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Virtru Announces First Ever FIPS 140-2 Validated JavaScript ...  GlobeNewswire Source link

Read more
Next Post
Try Out Code and Ideas Quickly – Real Python

Try Out Code and Ideas Quickly – Real Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Developer – Back End (Java) at Parvana Recruitment – IT-Online

March 15, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

New C++ Sender Library Enables Portable Asynchrony – HPCwire

February 13, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

All About E: The Language that Infiltrated JavaScript – The New Stack

February 26, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?