fix: Contain
now preserves aspect ratio of tall images (height > width)
#6537
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #3708, closes #4407
Problem
Setting both
width
andheight
to"Contain"
correctly scales wide and square images, and are not affected by this PR's change. However, for tall images,Contain
distorts the aspect ratio due to the currenttarget_width
formula inversely scaling the width.Solution
Replace the current
target_width
formula with one that properly scales the width proportionally to the height, keeping the aspect ratio.current
updated
where:
target_width
: scaled width of imagetex_width
,tex_height
: original image dimensionspixel_height
: window height (also be used as the scaled height of the image)This was derived from basic direct proportionality, since we want to scale while preserving aspect ratio:
Examples
The art is by @sue_kun. This image is chosen because its height is greater than its width. The config used is:
Most notable change in scaling behavior is when both width and height are set to
"Contain"
, where it maintains the aspect ratio now, like css'sobject-fit: contain
. Other combinations only change behavior whenwidth
is set to"Contain"
. In these scenarios, the width is scaled as though the height were also set to "Contain", even if it is not. As you will see in the examples below, all widths are the same and only the height differs.h: Contain
h: Cover
h: 250px
h: 250pt
h: 50%
h: 30cell