Answer the question
In order to leave comments, you need to log in
What does the CSS properties: contain and cover look like from a mathematical point of view?
There are two interesting background scaling algorithms in CSS, they are contain and cover.
Let's say if I want to replicate this behavior on a canvas with an image inserted using drawImage, what formula should I use to calculate this proportional scaling. I understand everything, but it’s impossible to write it down mathematically so that it’s perfect. I'm sure I'll manage a little later, but I wanted to quickly, suddenly someone has already encountered such a question.
Thank you!
Answer the question
In order to leave comments, you need to log in
cover
an image with x1 and y1 dimensions (width and height)
a container with x2 and y2 dimensions
compare the ratio of width to height
if x1/y1 < x2/y2 then enter in width.
(xN is the new width of the image, yN is the new height of the image)
xN = x2
yN = x2/x1*y1
is a proportional increase
Vertically you need to "cut off": k = yN - y2
If larger then the same for the height.
contain is symmetrical in reverse.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question