interp.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. export var Interp;
  2. (function (Interp) {
  3. Interp.number = (a, b) => {
  4. const d = b - a;
  5. return (t) => {
  6. return a + d * t;
  7. };
  8. };
  9. Interp.object = (a, b) => {
  10. const keys = Object.keys(a);
  11. return (t) => {
  12. const ret = {};
  13. for (let i = keys.length - 1; i !== -1; i -= 1) {
  14. const key = keys[i];
  15. ret[key] = a[key] + (b[key] - a[key]) * t;
  16. }
  17. return ret;
  18. };
  19. };
  20. Interp.unit = (a, b) => {
  21. const reg = /(-?[0-9]*.[0-9]*)(px|em|cm|mm|in|pt|pc|%)/;
  22. const ma = reg.exec(a);
  23. const mb = reg.exec(b);
  24. const pb = mb ? mb[1] : '';
  25. const aa = ma ? +ma[1] : 0;
  26. const bb = mb ? +mb[1] : 0;
  27. const index = pb.indexOf('.');
  28. const precision = index > 0 ? pb[1].length - index - 1 : 0;
  29. const d = bb - aa;
  30. const u = ma ? ma[2] : '';
  31. return (t) => {
  32. return (aa + d * t).toFixed(precision) + u;
  33. };
  34. };
  35. Interp.color = (a, b) => {
  36. const ca = parseInt(a.slice(1), 16);
  37. const cb = parseInt(b.slice(1), 16);
  38. const ra = ca & 0x0000ff;
  39. const rd = (cb & 0x0000ff) - ra;
  40. const ga = ca & 0x00ff00;
  41. const gd = (cb & 0x00ff00) - ga;
  42. const ba = ca & 0xff0000;
  43. const bd = (cb & 0xff0000) - ba;
  44. return (t) => {
  45. const r = (ra + rd * t) & 0x000000ff;
  46. const g = (ga + gd * t) & 0x0000ff00;
  47. const b = (ba + bd * t) & 0x00ff0000;
  48. return `#${((1 << 24) | r | g | b).toString(16).slice(1)}`;
  49. };
  50. };
  51. })(Interp || (Interp = {}));
  52. //# sourceMappingURL=interp.js.map