1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| ;(function (window) {
const transformProperties = [ "translateX", "translateY", "translateZ", "scale", "scaleX", "scaleY", "scaleZ", "skewX", "skewY", "rotateX", "rotateY", "rotateZ" ] const Normalizations = { registered: {} } for(let i = 0, len = transformProperties.length; i < len; i++) { const transformName = transformProperties[i] Normalizations.registered[transformName] = function (propertyValue) { return transformName + '(' + propertyValue + ')' } } function getPropertyValue (element, property) { return window.getComputedStyle(element, null).getPropertyValue(property) } function setPropertyValue (element, property, value) { let propertyName = property let propertyValue = value if (Normalizations.registered[property]) { propertyName = 'transform' propertyValue = Normalizations.registered[property](value) } element.style[propertyName] = propertyValue } function separateValue (property, value) { let unitType, numericValue numericValue = value.toString().replace(/[%A-z]+$/, function(match) { unitType = match return "" }) function getUnitType (property) { if (/^(rotate|skew)/i.test(property)) { return "deg" } else if (/(^(scale|scaleX|scaleY|scaleZ|opacity|alpha|fillOpacity|flexGrow|flexHeight|zIndex|fontWeight)$)|color/i.test(property)) { return "" } else { return "px" } } if (!unitType) { unitType = getUnitType(property) } return [ numericValue, unitType ] }
function Animation (element) { this.element = element } Animation.easing = { swing: function (a) { return .5 - Math.cos(a * Math.PI) / 2 } } Animation.prototype.animation = function (propertiesMap) { const element = this.element const opts = { duration: 400 } let propertiesContainer = {} for(let property in propertiesMap) { const startSeparatedValue = separateValue(property, getPropertyValue(element, property)) const startValue = parseFloat(startSeparatedValue[0]) || 0 const startValueUnitType = startSeparatedValue[1] const endSeparatedValue = separateValue(property, propertiesMap[property]) const endValue = parseFloat(endSeparatedValue[0]) || 0 const endValueUnitType = endSeparatedValue[1]
propertiesContainer[property] = { startValue, endValue, unitType: endValueUnitType } } let timeStart let isTicking = true function tick () { let timeCurrent = (new Date).getTime() if (!timeStart) { timeStart = timeCurrent - 16 } const percentComplete = Math.min((timeCurrent - timeStart) / opts.duration, 1) for(let property in propertiesContainer) { const tween = propertiesContainer[property] if (percentComplete === 1) { currentValue = tween.endValue } else { currentValue = parseFloat(tween.startValue) + ((tween.endValue - tween.startValue) * Animation.easing['swing'](percentComplete)) tween.currentValue = currentValue } setPropertyValue(element, property, currentValue + tween.unitType) } if (percentComplete === 1) { isTicking = false } if (isTicking) { requestAnimationFrame(tick) } } tick() } window.Animation = Animation })(window)
|