diff options
author | Sam Potts <sam@potts.es> | 2021-04-18 16:58:44 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-18 16:58:44 +1000 |
commit | 438e42583859eed0d2e9fd1a00b9cdc32a71f9b4 (patch) | |
tree | 95660a86c755d79b04bc3e18b7375b970df63281 /src/js/utils/style.js | |
parent | ddb5ad071e4f6387cce72156ba1d85e75d41fd2c (diff) | |
download | plyr-438e42583859eed0d2e9fd1a00b9cdc32a71f9b4.tar.lz plyr-438e42583859eed0d2e9fd1a00b9cdc32a71f9b4.tar.xz plyr-438e42583859eed0d2e9fd1a00b9cdc32a71f9b4.zip |
fix: aspect ratio improvements (#2171)
- Use CSS aspect-ratio (retain fallback for legacy browsers)
- Round aspect ratios (fixes YouTube black border issue)
Diffstat (limited to 'src/js/utils/style.js')
-rw-r--r-- | src/js/utils/style.js | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/js/utils/style.js b/src/js/utils/style.js index f02b0ba5..a9659cf4 100644 --- a/src/js/utils/style.js +++ b/src/js/utils/style.js @@ -2,9 +2,30 @@ // Style utils // ========================================================================== +import { closest } from './arrays'; import is from './is'; -export function validateRatio(input) { +// Standard/common aspect ratios +const standardRatios = [ + [1, 1], + [4, 3], + [3, 4], + [5, 4], + [4, 5], + [3, 2], + [2, 3], + [16, 10], + [10, 16], + [16, 9], + [9, 16], + [21, 9], + [9, 21], + [32, 9], + [9, 32], +].reduce((out, [x, y]) => ({ ...out, [x / y]: [x, y] }), {}); + +// Validate an aspect ratio +export function validateAspectRatio(input) { if (!is.array(input) && (!is.string(input) || !input.includes(':'))) { return false; } @@ -14,6 +35,7 @@ export function validateRatio(input) { return ratio.map(Number).every(is.number); } +// Reduce an aspect ratio to it's lowest form export function reduceAspectRatio(ratio) { if (!is.array(ratio) || !ratio.every(is.number)) { return null; @@ -26,8 +48,9 @@ export function reduceAspectRatio(ratio) { return [width / divider, height / divider]; } +// Calculate an aspect ratio export function getAspectRatio(input) { - const parse = (ratio) => (validateRatio(ratio) ? ratio.split(':').map(Number) : null); + const parse = (ratio) => (validateAspectRatio(ratio) ? ratio.split(':').map(Number) : null); // Try provided ratio let ratio = parse(input); @@ -58,10 +81,20 @@ export function setAspectRatio(input) { const { wrapper } = this.elements; const ratio = getAspectRatio.call(this, input); - const [w, h] = is.array(ratio) ? ratio : [0, 0]; - const padding = (100 / w) * h; - wrapper.style.paddingBottom = `${padding}%`; + if (!is.array(ratio)) { + return {}; + } + + const [x, y] = ratio; + const useNative = window.CSS?.supports(`aspect-ratio: ${x} / ${y}`) ?? false; + const padding = (100 / x) * y; + + if (useNative) { + wrapper.style.aspectRatio = `${x}/${y}`; + } else { + wrapper.style.paddingBottom = `${padding}%`; + } // For Vimeo we have an extra <div> to hide the standard controls and UI if (this.isVimeo && !this.config.vimeo.premium && this.supported.ui) { @@ -80,4 +113,18 @@ export function setAspectRatio(input) { return { padding, ratio }; } +// Round an aspect ratio to closest standard ratio +export function roundAspectRatio(x, y, tolerance = 0.05) { + const ratio = x / y; + const closestRatio = closest(Object.keys(standardRatios), ratio); + + // Check match is within tolerance + if (Math.abs(closestRatio - ratio) <= tolerance) { + return standardRatios[closestRatio]; + } + + // No match + return [x, y]; +} + export default { setAspectRatio }; |