intertween
Advanced tools
Comparing version 0.0.2 to 0.0.3
354
index.js
@@ -1,16 +0,24 @@ | ||
(function(factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define([], factory); | ||
} else if (typeof module !== "undefined" && module.exports) { | ||
module.exports = factory(); | ||
} else if (typeof window !== "undefined") { | ||
window.InterTween = factory(); | ||
} else { | ||
this.InterTween = factory(); | ||
} | ||
}(function() { | ||
/** | ||
* @name InterTween | ||
* @description The lightweight, fastest, smartest, effecient value interpolator with no-dependecy, zero-configuration and relative interpolation | ||
* @author @dalisoft (https://github.com/dalisoft) | ||
* @license MIT-License | ||
* First Release at 20 August 2017, by @dalisoft | ||
*/ | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define([], factory); | ||
} else if (typeof module !== 'undefined' && module.exports) { | ||
module.exports = factory(); | ||
} else { | ||
root.InterTween = factory(); | ||
} | ||
} | ||
(typeof(window) !== 'undefined' ? window : this, function (undefined) { | ||
// RegExp variables | ||
var colorMatch = /rgb|hsl|hsv/g; | ||
var isIncrementReqForColor = /ahsv|ahsl|argb/g; | ||
// This RegExp (numRegExp) is original from @jkroso string tweening and optimized by @dalisoft | ||
var numRegExp = | ||
@@ -20,161 +28,231 @@ /\s+|([A-Za-z?().,{}:""[\]#]+)|([-+/*%]+=)?([-+*/%]+)?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g; | ||
var trimRegExp = /\n|\r|\t/g; | ||
var rgbMax = 255; | ||
// Helpers | ||
function s2f(val) { | ||
var floatedVal = parseFloat(val); | ||
return typeof floatedVal === "number" && !isNaN(floatedVal) ? floatedVal : val; | ||
var floatedVal = parseFloat(val); | ||
return typeof floatedVal === "number" && !isNaN(floatedVal) ? floatedVal : val; | ||
} | ||
var isArray = Array.isArray; | ||
function h2r_f(all, hex) { | ||
var r; | ||
var g; | ||
var b; | ||
if (hex.length === 3) { | ||
r = parseInt(hex[0] + hex[0], 16); | ||
g = parseInt(hex[1] + hex[1], 16); | ||
b = parseInt(hex[2] + hex[2], 16); | ||
} else if (hex.length === 6) { | ||
r = parseInt(hex.substr(0, 2), 16); | ||
g = parseInt(hex.substr(2, 2), 16); | ||
b = parseInt(hex.substr(4, 6), 16); | ||
} | ||
return "rgb(" + r + "," + g + "," + b + ")"; | ||
var r; | ||
var g; | ||
var b; | ||
if (hex.length === 3) { | ||
r = hex[0]; | ||
g = hex[1]; | ||
b = hex[2]; | ||
hex = r + r + g + g + b + b; | ||
} | ||
var color = parseInt(hex, 16); | ||
r = color >> 16 & rgbMax; | ||
g = color >> 8 & rgbMax; | ||
b = color & rgbMax; | ||
return "rgb(" + r + "," + g + "," + b + ")"; | ||
}; | ||
function trim(str) { | ||
return typeof str === "string" ? str.replace(trimRegExp, "") : str; | ||
return typeof str === "string" ? str.replace(trimRegExp, "") : str; | ||
}; | ||
var relativeModes = { '+=': 1, '-=': 1, '*=': 2, '/=': 3, '%=': 4 }; | ||
var relativeModes = { | ||
'+=': 1, | ||
'-=': 1, | ||
'*=': 2, | ||
'/=': 3, | ||
'%=': 4 | ||
}; | ||
function r2n(s, e) { | ||
if (typeof e === 'number') { | ||
return e - s; | ||
} else { | ||
var rv = relativeModes[e.substr(0, 2)], | ||
v = e.substr(2); | ||
if (rv === 1) { | ||
var e2 = e[0] + v; | ||
return (s + parseFloat(e2)) - s; | ||
} else if (rv === 2) { | ||
return (s * +v) - s; | ||
} else if (rv === 3) { | ||
return (s / +v) - s; | ||
} else if (rv === 4) { | ||
return (s * (+v / 100)) - s; | ||
} | ||
if (typeof e === 'number') { | ||
return e - s; | ||
} else { | ||
var rv = relativeModes[e.substr(0, 2)], | ||
v = e.substr(2); | ||
if (rv === 1) { | ||
var e2 = e[0] + v; | ||
return (s + parseFloat(e2)) - s; | ||
} else if (rv === 2) { | ||
return (s * +v) - s; | ||
} else if (rv === 3) { | ||
return (s / +v) - s; | ||
} else if (rv === 4) { | ||
return (s * (+v / 100)) - s; | ||
} | ||
return e; | ||
} | ||
return e; | ||
} | ||
var h2r = function(hex) { | ||
return typeof hex !== 'string' ? hex : trim(hex) | ||
.replace(hexColor, h2r_f); | ||
var h2r = function (hex) { | ||
return typeof hex !== 'string' ? hex : trim(hex) | ||
.replace(hexColor, h2r_f); | ||
} | ||
function s2n(str) { | ||
return h2r(str) | ||
.match(numRegExp) | ||
.map(s2f); | ||
return h2r(str).match(numRegExp).map(s2f); | ||
} | ||
// Splitted functions | ||
var stringTween = function(s, e, d) { | ||
d = d !== undefined ? d : 10000; | ||
var sv = s2n(s); | ||
var ev = s2n(e); | ||
var cm = null; | ||
var cmls = null; | ||
var rv = ev.map(function(e, i) { return typeof e === 'string' && e.indexOf('=') === | ||
1 ? e : null; }); | ||
ev = ev.map(function(e, i) { if (isIncrementReqForColor.test(e)) { cm = i + 2; | ||
cmls = i + 11; } else if (colorMatch.test(e)) { cm | ||
= i; | ||
cmls = i + 9; } return sv[i] === e ? null : rv[ | ||
i] !== null ? r2n(sv[i], e) : typeof e === | ||
'number' ? e - sv[i] : e; }); | ||
return function(t) { | ||
var str = ''; | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var a = sv[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
str += typeof b === 'number' ? cm !== null && i > cm && | ||
i < cmls ? (a + b * t) | 0 : (((a + b * t) * d) | | ||
0) / d : a; | ||
if (t === 1 && r !== null) { | ||
sv[i] += b; | ||
ev[i] = r2n(sv[i], r); | ||
} | ||
} | ||
return str; | ||
function stringTween(s, e, d) { | ||
d = d !== undefined ? d : 10000; | ||
if (!numRegExp.test(e)) | ||
return e; | ||
var sv = s2n(s); | ||
var ev = s2n(e); | ||
var uv = unitTween(sv, ev); | ||
if (uv) { | ||
return uv; | ||
} | ||
uv = null; | ||
var cm = null; | ||
var cmls = null; | ||
var rv = []; | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var ve = ev[i], | ||
vs = sv[i]; | ||
rv[i] = typeof ve === 'string' && ve.indexOf('=') === 1 ? e : null; | ||
if (isIncrementReqForColor.test(ve)) { | ||
cm = i + 2; | ||
cmls = i + 11; | ||
} else if (colorMatch.test(ve)) { | ||
cm = i; | ||
cmls = i + 9; | ||
} | ||
ev[i] = vs === ve ? null : rv[i] !== null ? r2n(vs, ve) : typeof ve === 'number' ? ve - vs : ve; | ||
} | ||
return function (t) { | ||
var str = ''; | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var a = sv[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
str += typeof b === 'number' ? cm !== null && i > cm && | ||
i < cmls ? (a + b * t) | 0 : (((a + b * t) * d) | | ||
0) / d : a; | ||
if (t === 1 && r !== null) { | ||
sv[i] += b; | ||
ev[i] = r2n(sv[i], r); | ||
} | ||
} | ||
return str; | ||
} | ||
} | ||
var arrayTween = function(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
var rv = ev.map(function(e, i) { return typeof e === 'string' && e.indexOf('=') === | ||
1 ? e : null; }); | ||
ev = ev.map(function(e, i) { return sv[i] === e ? null : Array.isArray(e) ? | ||
arrayTween(sv[i], e, d) : typeof e === 'object' ? | ||
objectTween(sv[i], e, d) : rv[i] !== null ? r2n( | ||
sv[i], e) : typeof e === 'string' ? | ||
stringTween(sv[i], e, d) : typeof e === | ||
'number' ? e - sv[i] : e; }); | ||
var s = sv.slice(); | ||
return function(t) { | ||
s.length = 0; | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var a = sv[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
s[i] = typeof b === 'number' ? (((a + b * t) * d) | 0) / | ||
d : typeof b === 'function' ? b(t) : a; | ||
if (t === 1 && r !== null) { | ||
sv[i] += b; | ||
ev[i] = r2n(sv[i], r); | ||
} | ||
} | ||
return s; | ||
function tweenThemTo(sv, ev) { | ||
var vs = []; | ||
for (var i = 0, len = sv.length; i < len; i++) { | ||
var s = sv[i]; | ||
vs[i] = isArray(s) ? arrayTween(s, ev) : typeof s === 'object' ? objectTween(s, ev) : typeof s === 'string' ? stringTween(s, ev) : s; | ||
} | ||
return function (t) { | ||
for (var i = 0, len = vs.length; i < len; i++) { | ||
sv[i] = vs[i](t); | ||
} | ||
return sv; | ||
} | ||
}; | ||
function parseInterpolatables(sv, ev) { | ||
var vs = []; | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var t = ev[i]; | ||
vs[i] = mainTween(i === 0 ? sv : ev[i - 1], t); | ||
} | ||
return vs; | ||
} | ||
var objectTween = function(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
var rv = {}; | ||
for (var i in ev) { | ||
var e = ev[i], | ||
s = sv[i]; | ||
rv[i] = typeof e === 'string' && e.indexOf('=') === 1 ? e : null; | ||
ev[i] = s === e ? null : Array.isArray(e) ? arrayTween(s, e, d) : | ||
typeof e === 'object' ? objectTween(s, e, d) : rv[i] !== null ? | ||
r2n(s, e) : typeof e === 'string' ? stringTween(s, e, d) : | ||
typeof e === 'number' ? e - s : e; | ||
function arrayTween(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
var s = sv.slice(); | ||
var rv = []; | ||
for (var i = 0, len = s.length; i < len; i++) { | ||
var vs = s[i], | ||
ve = ev[i]; | ||
rv[i] = typeof ve === 'string' && ve.indexOf('=') === 1 ? ve : null; | ||
s[i] = vs === ve ? null : isArray(ve) ? | ||
isArray(vs) ? arrayTween(vs, ve, d) : parseInterpolatables(vs, ve) : isArray(vs) ? tweenThemTo(vs, ve) : typeof ve === 'object' ? | ||
objectTween(vs, ve, d) : rv[i] !== null ? r2n( | ||
vs, ve) : typeof ve === 'string' ? | ||
stringTween(vs, ve, d) : vs; | ||
} | ||
return function (t) { | ||
for (var i = 0, len = ev.length; i < len; i++) { | ||
var a = s[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
if (a === null) | ||
continue; | ||
sv[i] = typeof a === 'number' ? (((a + (b - a) * t) * d) | 0) / | ||
d : typeof a === 'function' ? a(t) : b; | ||
if (t === 1 && r !== null) { | ||
s[i] += b; | ||
ev[i] = r2n(s[i], r); | ||
} | ||
} | ||
var s = {}; | ||
for (var i in sv) { | ||
s[i] = sv[i]; | ||
return sv; | ||
} | ||
} | ||
var units = ["px", "pt", "pc", "deg", "rad", "turn", "em", "ex", "cm", "mm", "dm", "inch", "in", "rem", "vw", "vh", "vmin", "vmax", "%"]; | ||
function unitTween(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
if (ev.length === 2 && sv.length === 2) { | ||
var unidx = units.indexOf(ev[1]); | ||
if (unidx !== -1) { | ||
var s = +sv[0], | ||
e = +ev[0] - s, | ||
u = ev[1]; | ||
return function (t) { | ||
return ((((s + e * t) * d) | 0) / d) + u; | ||
} | ||
} | ||
return function(t) { | ||
for (var i in s) { | ||
var a = sv[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
s[i] = typeof b === 'number' ? (((a + b * t) * d) | 0) / | ||
d : typeof b === 'function' ? b(t) : a; | ||
if (t === 1 && r !== null) { | ||
sv[i] += b; | ||
ev[i] = r2n(sv[i], r); | ||
} | ||
} | ||
return s; | ||
} | ||
return false; | ||
} | ||
function objectTween(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
var rv = {}; | ||
var s = {}; | ||
for (var i in sv) { | ||
s[i] = sv[i]; | ||
var vs = s[i], | ||
ve = ev[i]; | ||
rv[i] = typeof ve === 'string' && ve.indexOf('=') === 1 ? ve : null; | ||
s[i] = vs === ve ? null : isArray(ve) ? | ||
isArray(vs) ? arrayTween(vs, ve, d) : parseInterpolatables(vs, ve) : isArray(vs) ? tweenThemTo(vs, ve) : typeof ve === 'object' ? | ||
objectTween(vs, ve, d) : rv[i] !== null ? r2n( | ||
vs, ve) : typeof ve === 'string' ? | ||
stringTween(vs, ve, d) : vs; | ||
} | ||
return function (t) { | ||
for (var i in s) { | ||
var a = s[i], | ||
b = ev[i], | ||
r = rv[i]; | ||
if (a === null) | ||
continue; | ||
sv[i] = typeof a === 'number' ? (((a + (b - a) * t) * d) | 0) / | ||
d : typeof a === 'function' ? a(t) : b; | ||
if (t === 1 && r !== null) { | ||
s[i] += b; | ||
ev[i] = r2n(s[i], r); | ||
} | ||
} | ||
return sv; | ||
} | ||
} | ||
return function(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
return Array.isArray(ev) ? arrayTween(sv, ev, d) : typeof ev === 'object' ? | ||
objectTween(sv, ev, d) : typeof ev === 'string' ? stringTween(sv, ev, d) : | ||
function(t) { return typeof ev === 'number' ? (((sv + (ev - sv) * t) * | ||
d) | 0) / d : sv; } | ||
function mainTween(sv, ev, d) { | ||
d = d !== undefined ? d : 10000; | ||
return isArray(ev) ? isArray(sv) ? arrayTween(sv, ev, d) : parseInterpolatables(sv, ev) : isArray(sv) ? tweenThemTo(sv, ev) : typeof ev === 'object' ? | ||
objectTween(sv, ev, d) : typeof ev === 'string' ? stringTween(sv, ev, d) : | ||
function (t) { | ||
return typeof ev === 'number' ? (((sv + (ev - sv) * t) * | ||
d) | 0) / d : sv; | ||
} | ||
} | ||
})); | ||
return mainTween; | ||
})); |
{ | ||
"name": "intertween", | ||
"version": "0.0.2", | ||
"description": "A value interpolator between to any value with relative support", | ||
"version": "0.0.3", | ||
"description": "The lightweight, fastest, smartest, effecient value interpolator with no-dependecy, zero-configuration and relative interpolation", | ||
"main": "index.js", | ||
@@ -14,2 +14,11 @@ "cdn": "index.min.js", | ||
"interpolation", | ||
"value", | ||
"relative", | ||
"any", | ||
"fast", | ||
"memory", | ||
"effecient", | ||
"no catch", | ||
"no-garbage", | ||
"anywhere", | ||
"javascript", | ||
@@ -16,0 +25,0 @@ "performance", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12005
240
1