/
githubmirror
/
anime
Обзор
Документация
Войти
/
githubmirror
/
anime
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/core/units.js
63 строки
2 KB
Julian Garnier
4.2.0-beta.0
26 авг 2025, 18:50
26 авг 2025, 18:50
36c932c
Код
Авторство
О чём код?
import { doc, valueTypes, } from './consts.js'; import { isUnd, PI } from './helpers.js'; const angleUnitsMap = { 'deg': 1, 'rad': 180 / PI, 'turn': 360 }; const convertedValuesCache = {}; /** * @import { * DOMTarget, * TweenDecomposedValue, * } from '../types/index.js' */ /** * @param {DOMTarget} el * @param {TweenDecomposedValue} decomposedValue * @param {String} unit * @param {Boolean} [force] * @return {TweenDecomposedValue} */ export const convertValueUnit = (el, decomposedValue, unit, force = false) => { const currentUnit = decomposedValue.u; const currentNumber = decomposedValue.n; if (decomposedValue.t === valueTypes.UNIT && currentUnit === unit) { // TODO: Check if checking against the same unit string is necessary return decomposedValue; } const cachedKey = currentNumber + currentUnit + unit; const cached = convertedValuesCache[cachedKey]; if (!isUnd(cached) && !force) { decomposedValue.n = cached; } else { let convertedValue; if (currentUnit in angleUnitsMap) { convertedValue = currentNumber * angleUnitsMap[currentUnit] / angleUnitsMap[unit]; } else { const baseline = 100; const tempEl = /** @type {DOMTarget} */(el.cloneNode()); const parentNode = el.parentNode; const parentEl = (parentNode && (parentNode !== doc)) ? parentNode : doc.body; parentEl.appendChild(tempEl); const elStyle = tempEl.style; elStyle.width = baseline + currentUnit; const currentUnitWidth = /** @type {HTMLElement} */(tempEl).offsetWidth || baseline; elStyle.width = baseline + unit; const newUnitWidth = /** @type {HTMLElement} */(tempEl).offsetWidth || baseline; const factor = currentUnitWidth / newUnitWidth; parentEl.removeChild(tempEl); convertedValue = factor * currentNumber; } decomposedValue.n = convertedValue; convertedValuesCache[cachedKey] = convertedValue; } decomposedValue.t === valueTypes.UNIT; decomposedValue.u = unit; return decomposedValue; }