|
| 1 | +/** |
| 2 | + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @fileoverview Provides a function to calculate the dimensions of the |
| 19 | + * rendered image inside of an `<img>` Element. For example, if you have an |
| 20 | + * `<img>` with `object-fit: contain` and an image that is portrait inside of |
| 21 | + * an `<img>` with landscape dimensions, you will have something looks like: |
| 22 | + * _____________ |
| 23 | + * | | | | |
| 24 | + * | i | r | i | |
| 25 | + * |___|_____|___| |
| 26 | + * |
| 27 | + * Where the area denoted by `r` is the rendered image and the areas denoted |
| 28 | + * by `i` are extra spacing on either side of the rendered image to center it |
| 29 | + * within the containing `<img>` Element. |
| 30 | + * @see https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit |
| 31 | + */ |
| 32 | + |
| 33 | + export interface Size { |
| 34 | + width: number, |
| 35 | + height: number, |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Constrains the size of the image to the given width and height. This either |
| 40 | + * caps the width or the height depending on the aspect ratio of original img |
| 41 | + * and if we want to have the smaller or larger dimension fit the container. |
| 42 | + * @param naturalSize The natural dimensions of the image. |
| 43 | + * @param containerSize The size of the container we want to render the image |
| 44 | + * in. |
| 45 | + * @param toMin If we should cap the smaller dimension of the image to fit the |
| 46 | + * container (`object-fit: cover`) or the larger dimension |
| 47 | + * (`object-fit: contain`). |
| 48 | + * @return The Size that the image should be rendered as. |
| 49 | + */ |
| 50 | +function constrain( |
| 51 | + naturalSize: Size, containerSize: Size, toMin: boolean): Size { |
| 52 | + const elAspectRatio = containerSize.width / containerSize.height; |
| 53 | + const naturalAspectRatio = naturalSize.width / naturalSize.height; |
| 54 | + |
| 55 | + if (naturalAspectRatio > elAspectRatio !== toMin) { |
| 56 | + return { |
| 57 | + width: containerSize.height * naturalAspectRatio, |
| 58 | + height: containerSize.height, |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + width: containerSize.width, |
| 64 | + height: containerSize.width / naturalAspectRatio, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function getDimensionsForObjectFitCover( |
| 69 | + naturalSize: Size, containerSize: Size): Size { |
| 70 | + return constrain(naturalSize, containerSize, false); |
| 71 | +} |
| 72 | + |
| 73 | +function getDimensionsForObjectFitContain( |
| 74 | + naturalSize: Size, containerSize: Size): Size { |
| 75 | + return constrain(naturalSize, containerSize, true); |
| 76 | +} |
| 77 | + |
| 78 | +function getDimensionsForObjectFitFill(containerSize: Size): Size { |
| 79 | + return containerSize; |
| 80 | +} |
| 81 | + |
| 82 | +function getDimensionsForObjectFitNone(naturalSize: Size): Size { |
| 83 | + return naturalSize; |
| 84 | +} |
| 85 | + |
| 86 | +function getDimensionsForObjectFitScaleDown( |
| 87 | + naturalSize: Size, containerSize: Size): Size { |
| 88 | + const noneSize = getDimensionsForObjectFitNone(naturalSize); |
| 89 | + const containSize = getDimensionsForObjectFitContain( |
| 90 | + naturalSize, containerSize); |
| 91 | + |
| 92 | + // Since both have the same aspect ratio, we can simply take the smaller |
| 93 | + // dimension for both. |
| 94 | + return { |
| 95 | + width: Math.min(noneSize.width, containSize.width), |
| 96 | + height: Math.min(noneSize.height, containSize.height), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Gets the dimensions for the rendered "image" rather than the container |
| 102 | + * that constrains the size with the CSS `object-fit` property. |
| 103 | + * @param img The HTMLImageElement |
| 104 | + * @param containerSize The size of the container element. |
| 105 | + * @return The width/height of the "actual" image. |
| 106 | + */ |
| 107 | +export function getRenderedDimensions( |
| 108 | + img: HTMLImageElement, containerSize: Size): Size { |
| 109 | + const objectFit = getComputedStyle(img).getPropertyValue('object-fit'); |
| 110 | + const naturalSize = { |
| 111 | + width: img.naturalWidth, |
| 112 | + height: img.naturalHeight, |
| 113 | + }; |
| 114 | + |
| 115 | + switch(objectFit) { |
| 116 | + case 'cover': |
| 117 | + return getDimensionsForObjectFitCover(naturalSize, containerSize); |
| 118 | + case 'contain': |
| 119 | + return getDimensionsForObjectFitContain(naturalSize, containerSize); |
| 120 | + case 'fill': |
| 121 | + return getDimensionsForObjectFitFill(containerSize); |
| 122 | + case 'none': |
| 123 | + return getDimensionsForObjectFitNone(naturalSize); |
| 124 | + case 'scale-down': |
| 125 | + return getDimensionsForObjectFitScaleDown(naturalSize, containerSize); |
| 126 | + default: |
| 127 | + throw new Error(`object-fit: ${objectFit} not supported`); |
| 128 | + } |
| 129 | +} |
0 commit comments