d3-interpolate.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // https://d3js.org/d3-interpolate/ v1.4.0 Copyright 2019 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-color')) :
  4. typeof define === 'function' && define.amd ? define(['exports', 'd3-color'], factory) :
  5. (global = global || self, factory(global.d3 = global.d3 || {}, global.d3));
  6. }(this, function (exports, d3Color) { 'use strict';
  7. function basis(t1, v0, v1, v2, v3) {
  8. var t2 = t1 * t1, t3 = t2 * t1;
  9. return ((1 - 3 * t1 + 3 * t2 - t3) * v0
  10. + (4 - 6 * t2 + 3 * t3) * v1
  11. + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
  12. + t3 * v3) / 6;
  13. }
  14. function basis$1(values) {
  15. var n = values.length - 1;
  16. return function(t) {
  17. var i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n),
  18. v1 = values[i],
  19. v2 = values[i + 1],
  20. v0 = i > 0 ? values[i - 1] : 2 * v1 - v2,
  21. v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
  22. return basis((t - i / n) * n, v0, v1, v2, v3);
  23. };
  24. }
  25. function basisClosed(values) {
  26. var n = values.length;
  27. return function(t) {
  28. var i = Math.floor(((t %= 1) < 0 ? ++t : t) * n),
  29. v0 = values[(i + n - 1) % n],
  30. v1 = values[i % n],
  31. v2 = values[(i + 1) % n],
  32. v3 = values[(i + 2) % n];
  33. return basis((t - i / n) * n, v0, v1, v2, v3);
  34. };
  35. }
  36. function constant(x) {
  37. return function() {
  38. return x;
  39. };
  40. }
  41. function linear(a, d) {
  42. return function(t) {
  43. return a + t * d;
  44. };
  45. }
  46. function exponential(a, b, y) {
  47. return a = Math.pow(a, y), b = Math.pow(b, y) - a, y = 1 / y, function(t) {
  48. return Math.pow(a + t * b, y);
  49. };
  50. }
  51. function hue(a, b) {
  52. var d = b - a;
  53. return d ? linear(a, d > 180 || d < -180 ? d - 360 * Math.round(d / 360) : d) : constant(isNaN(a) ? b : a);
  54. }
  55. function gamma(y) {
  56. return (y = +y) === 1 ? nogamma : function(a, b) {
  57. return b - a ? exponential(a, b, y) : constant(isNaN(a) ? b : a);
  58. };
  59. }
  60. function nogamma(a, b) {
  61. var d = b - a;
  62. return d ? linear(a, d) : constant(isNaN(a) ? b : a);
  63. }
  64. var rgb = (function rgbGamma(y) {
  65. var color = gamma(y);
  66. function rgb(start, end) {
  67. var r = color((start = d3Color.rgb(start)).r, (end = d3Color.rgb(end)).r),
  68. g = color(start.g, end.g),
  69. b = color(start.b, end.b),
  70. opacity = nogamma(start.opacity, end.opacity);
  71. return function(t) {
  72. start.r = r(t);
  73. start.g = g(t);
  74. start.b = b(t);
  75. start.opacity = opacity(t);
  76. return start + "";
  77. };
  78. }
  79. rgb.gamma = rgbGamma;
  80. return rgb;
  81. })(1);
  82. function rgbSpline(spline) {
  83. return function(colors) {
  84. var n = colors.length,
  85. r = new Array(n),
  86. g = new Array(n),
  87. b = new Array(n),
  88. i, color;
  89. for (i = 0; i < n; ++i) {
  90. color = d3Color.rgb(colors[i]);
  91. r[i] = color.r || 0;
  92. g[i] = color.g || 0;
  93. b[i] = color.b || 0;
  94. }
  95. r = spline(r);
  96. g = spline(g);
  97. b = spline(b);
  98. color.opacity = 1;
  99. return function(t) {
  100. color.r = r(t);
  101. color.g = g(t);
  102. color.b = b(t);
  103. return color + "";
  104. };
  105. };
  106. }
  107. var rgbBasis = rgbSpline(basis$1);
  108. var rgbBasisClosed = rgbSpline(basisClosed);
  109. function numberArray(a, b) {
  110. if (!b) b = [];
  111. var n = a ? Math.min(b.length, a.length) : 0,
  112. c = b.slice(),
  113. i;
  114. return function(t) {
  115. for (i = 0; i < n; ++i) c[i] = a[i] * (1 - t) + b[i] * t;
  116. return c;
  117. };
  118. }
  119. function isNumberArray(x) {
  120. return ArrayBuffer.isView(x) && !(x instanceof DataView);
  121. }
  122. function array(a, b) {
  123. return (isNumberArray(b) ? numberArray : genericArray)(a, b);
  124. }
  125. function genericArray(a, b) {
  126. var nb = b ? b.length : 0,
  127. na = a ? Math.min(nb, a.length) : 0,
  128. x = new Array(na),
  129. c = new Array(nb),
  130. i;
  131. for (i = 0; i < na; ++i) x[i] = value(a[i], b[i]);
  132. for (; i < nb; ++i) c[i] = b[i];
  133. return function(t) {
  134. for (i = 0; i < na; ++i) c[i] = x[i](t);
  135. return c;
  136. };
  137. }
  138. function date(a, b) {
  139. var d = new Date;
  140. return a = +a, b = +b, function(t) {
  141. return d.setTime(a * (1 - t) + b * t), d;
  142. };
  143. }
  144. function number(a, b) {
  145. return a = +a, b = +b, function(t) {
  146. return a * (1 - t) + b * t;
  147. };
  148. }
  149. function object(a, b) {
  150. var i = {},
  151. c = {},
  152. k;
  153. if (a === null || typeof a !== "object") a = {};
  154. if (b === null || typeof b !== "object") b = {};
  155. for (k in b) {
  156. if (k in a) {
  157. i[k] = value(a[k], b[k]);
  158. } else {
  159. c[k] = b[k];
  160. }
  161. }
  162. return function(t) {
  163. for (k in i) c[k] = i[k](t);
  164. return c;
  165. };
  166. }
  167. var reA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g,
  168. reB = new RegExp(reA.source, "g");
  169. function zero(b) {
  170. return function() {
  171. return b;
  172. };
  173. }
  174. function one(b) {
  175. return function(t) {
  176. return b(t) + "";
  177. };
  178. }
  179. function string(a, b) {
  180. var bi = reA.lastIndex = reB.lastIndex = 0, // scan index for next number in b
  181. am, // current match in a
  182. bm, // current match in b
  183. bs, // string preceding current number in b, if any
  184. i = -1, // index in s
  185. s = [], // string constants and placeholders
  186. q = []; // number interpolators
  187. // Coerce inputs to strings.
  188. a = a + "", b = b + "";
  189. // Interpolate pairs of numbers in a & b.
  190. while ((am = reA.exec(a))
  191. && (bm = reB.exec(b))) {
  192. if ((bs = bm.index) > bi) { // a string precedes the next number in b
  193. bs = b.slice(bi, bs);
  194. if (s[i]) s[i] += bs; // coalesce with previous string
  195. else s[++i] = bs;
  196. }
  197. if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
  198. if (s[i]) s[i] += bm; // coalesce with previous string
  199. else s[++i] = bm;
  200. } else { // interpolate non-matching numbers
  201. s[++i] = null;
  202. q.push({i: i, x: number(am, bm)});
  203. }
  204. bi = reB.lastIndex;
  205. }
  206. // Add remains of b.
  207. if (bi < b.length) {
  208. bs = b.slice(bi);
  209. if (s[i]) s[i] += bs; // coalesce with previous string
  210. else s[++i] = bs;
  211. }
  212. // Special optimization for only a single match.
  213. // Otherwise, interpolate each of the numbers and rejoin the string.
  214. return s.length < 2 ? (q[0]
  215. ? one(q[0].x)
  216. : zero(b))
  217. : (b = q.length, function(t) {
  218. for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
  219. return s.join("");
  220. });
  221. }
  222. function value(a, b) {
  223. var t = typeof b, c;
  224. return b == null || t === "boolean" ? constant(b)
  225. : (t === "number" ? number
  226. : t === "string" ? ((c = d3Color.color(b)) ? (b = c, rgb) : string)
  227. : b instanceof d3Color.color ? rgb
  228. : b instanceof Date ? date
  229. : isNumberArray(b) ? numberArray
  230. : Array.isArray(b) ? genericArray
  231. : typeof b.valueOf !== "function" && typeof b.toString !== "function" || isNaN(b) ? object
  232. : number)(a, b);
  233. }
  234. function discrete(range) {
  235. var n = range.length;
  236. return function(t) {
  237. return range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
  238. };
  239. }
  240. function hue$1(a, b) {
  241. var i = hue(+a, +b);
  242. return function(t) {
  243. var x = i(t);
  244. return x - 360 * Math.floor(x / 360);
  245. };
  246. }
  247. function round(a, b) {
  248. return a = +a, b = +b, function(t) {
  249. return Math.round(a * (1 - t) + b * t);
  250. };
  251. }
  252. var degrees = 180 / Math.PI;
  253. var identity = {
  254. translateX: 0,
  255. translateY: 0,
  256. rotate: 0,
  257. skewX: 0,
  258. scaleX: 1,
  259. scaleY: 1
  260. };
  261. function decompose(a, b, c, d, e, f) {
  262. var scaleX, scaleY, skewX;
  263. if (scaleX = Math.sqrt(a * a + b * b)) a /= scaleX, b /= scaleX;
  264. if (skewX = a * c + b * d) c -= a * skewX, d -= b * skewX;
  265. if (scaleY = Math.sqrt(c * c + d * d)) c /= scaleY, d /= scaleY, skewX /= scaleY;
  266. if (a * d < b * c) a = -a, b = -b, skewX = -skewX, scaleX = -scaleX;
  267. return {
  268. translateX: e,
  269. translateY: f,
  270. rotate: Math.atan2(b, a) * degrees,
  271. skewX: Math.atan(skewX) * degrees,
  272. scaleX: scaleX,
  273. scaleY: scaleY
  274. };
  275. }
  276. var cssNode,
  277. cssRoot,
  278. cssView,
  279. svgNode;
  280. function parseCss(value) {
  281. if (value === "none") return identity;
  282. if (!cssNode) cssNode = document.createElement("DIV"), cssRoot = document.documentElement, cssView = document.defaultView;
  283. cssNode.style.transform = value;
  284. value = cssView.getComputedStyle(cssRoot.appendChild(cssNode), null).getPropertyValue("transform");
  285. cssRoot.removeChild(cssNode);
  286. value = value.slice(7, -1).split(",");
  287. return decompose(+value[0], +value[1], +value[2], +value[3], +value[4], +value[5]);
  288. }
  289. function parseSvg(value) {
  290. if (value == null) return identity;
  291. if (!svgNode) svgNode = document.createElementNS("http://www.w3.org/2000/svg", "g");
  292. svgNode.setAttribute("transform", value);
  293. if (!(value = svgNode.transform.baseVal.consolidate())) return identity;
  294. value = value.matrix;
  295. return decompose(value.a, value.b, value.c, value.d, value.e, value.f);
  296. }
  297. function interpolateTransform(parse, pxComma, pxParen, degParen) {
  298. function pop(s) {
  299. return s.length ? s.pop() + " " : "";
  300. }
  301. function translate(xa, ya, xb, yb, s, q) {
  302. if (xa !== xb || ya !== yb) {
  303. var i = s.push("translate(", null, pxComma, null, pxParen);
  304. q.push({i: i - 4, x: number(xa, xb)}, {i: i - 2, x: number(ya, yb)});
  305. } else if (xb || yb) {
  306. s.push("translate(" + xb + pxComma + yb + pxParen);
  307. }
  308. }
  309. function rotate(a, b, s, q) {
  310. if (a !== b) {
  311. if (a - b > 180) b += 360; else if (b - a > 180) a += 360; // shortest path
  312. q.push({i: s.push(pop(s) + "rotate(", null, degParen) - 2, x: number(a, b)});
  313. } else if (b) {
  314. s.push(pop(s) + "rotate(" + b + degParen);
  315. }
  316. }
  317. function skewX(a, b, s, q) {
  318. if (a !== b) {
  319. q.push({i: s.push(pop(s) + "skewX(", null, degParen) - 2, x: number(a, b)});
  320. } else if (b) {
  321. s.push(pop(s) + "skewX(" + b + degParen);
  322. }
  323. }
  324. function scale(xa, ya, xb, yb, s, q) {
  325. if (xa !== xb || ya !== yb) {
  326. var i = s.push(pop(s) + "scale(", null, ",", null, ")");
  327. q.push({i: i - 4, x: number(xa, xb)}, {i: i - 2, x: number(ya, yb)});
  328. } else if (xb !== 1 || yb !== 1) {
  329. s.push(pop(s) + "scale(" + xb + "," + yb + ")");
  330. }
  331. }
  332. return function(a, b) {
  333. var s = [], // string constants and placeholders
  334. q = []; // number interpolators
  335. a = parse(a), b = parse(b);
  336. translate(a.translateX, a.translateY, b.translateX, b.translateY, s, q);
  337. rotate(a.rotate, b.rotate, s, q);
  338. skewX(a.skewX, b.skewX, s, q);
  339. scale(a.scaleX, a.scaleY, b.scaleX, b.scaleY, s, q);
  340. a = b = null; // gc
  341. return function(t) {
  342. var i = -1, n = q.length, o;
  343. while (++i < n) s[(o = q[i]).i] = o.x(t);
  344. return s.join("");
  345. };
  346. };
  347. }
  348. var interpolateTransformCss = interpolateTransform(parseCss, "px, ", "px)", "deg)");
  349. var interpolateTransformSvg = interpolateTransform(parseSvg, ", ", ")", ")");
  350. var rho = Math.SQRT2,
  351. rho2 = 2,
  352. rho4 = 4,
  353. epsilon2 = 1e-12;
  354. function cosh(x) {
  355. return ((x = Math.exp(x)) + 1 / x) / 2;
  356. }
  357. function sinh(x) {
  358. return ((x = Math.exp(x)) - 1 / x) / 2;
  359. }
  360. function tanh(x) {
  361. return ((x = Math.exp(2 * x)) - 1) / (x + 1);
  362. }
  363. // p0 = [ux0, uy0, w0]
  364. // p1 = [ux1, uy1, w1]
  365. function zoom(p0, p1) {
  366. var ux0 = p0[0], uy0 = p0[1], w0 = p0[2],
  367. ux1 = p1[0], uy1 = p1[1], w1 = p1[2],
  368. dx = ux1 - ux0,
  369. dy = uy1 - uy0,
  370. d2 = dx * dx + dy * dy,
  371. i,
  372. S;
  373. // Special case for u0 ≅ u1.
  374. if (d2 < epsilon2) {
  375. S = Math.log(w1 / w0) / rho;
  376. i = function(t) {
  377. return [
  378. ux0 + t * dx,
  379. uy0 + t * dy,
  380. w0 * Math.exp(rho * t * S)
  381. ];
  382. };
  383. }
  384. // General case.
  385. else {
  386. var d1 = Math.sqrt(d2),
  387. b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2 * w0 * rho2 * d1),
  388. b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2 * w1 * rho2 * d1),
  389. r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0),
  390. r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
  391. S = (r1 - r0) / rho;
  392. i = function(t) {
  393. var s = t * S,
  394. coshr0 = cosh(r0),
  395. u = w0 / (rho2 * d1) * (coshr0 * tanh(rho * s + r0) - sinh(r0));
  396. return [
  397. ux0 + u * dx,
  398. uy0 + u * dy,
  399. w0 * coshr0 / cosh(rho * s + r0)
  400. ];
  401. };
  402. }
  403. i.duration = S * 1000;
  404. return i;
  405. }
  406. function hsl(hue) {
  407. return function(start, end) {
  408. var h = hue((start = d3Color.hsl(start)).h, (end = d3Color.hsl(end)).h),
  409. s = nogamma(start.s, end.s),
  410. l = nogamma(start.l, end.l),
  411. opacity = nogamma(start.opacity, end.opacity);
  412. return function(t) {
  413. start.h = h(t);
  414. start.s = s(t);
  415. start.l = l(t);
  416. start.opacity = opacity(t);
  417. return start + "";
  418. };
  419. }
  420. }
  421. var hsl$1 = hsl(hue);
  422. var hslLong = hsl(nogamma);
  423. function lab(start, end) {
  424. var l = nogamma((start = d3Color.lab(start)).l, (end = d3Color.lab(end)).l),
  425. a = nogamma(start.a, end.a),
  426. b = nogamma(start.b, end.b),
  427. opacity = nogamma(start.opacity, end.opacity);
  428. return function(t) {
  429. start.l = l(t);
  430. start.a = a(t);
  431. start.b = b(t);
  432. start.opacity = opacity(t);
  433. return start + "";
  434. };
  435. }
  436. function hcl(hue) {
  437. return function(start, end) {
  438. var h = hue((start = d3Color.hcl(start)).h, (end = d3Color.hcl(end)).h),
  439. c = nogamma(start.c, end.c),
  440. l = nogamma(start.l, end.l),
  441. opacity = nogamma(start.opacity, end.opacity);
  442. return function(t) {
  443. start.h = h(t);
  444. start.c = c(t);
  445. start.l = l(t);
  446. start.opacity = opacity(t);
  447. return start + "";
  448. };
  449. }
  450. }
  451. var hcl$1 = hcl(hue);
  452. var hclLong = hcl(nogamma);
  453. function cubehelix(hue) {
  454. return (function cubehelixGamma(y) {
  455. y = +y;
  456. function cubehelix(start, end) {
  457. var h = hue((start = d3Color.cubehelix(start)).h, (end = d3Color.cubehelix(end)).h),
  458. s = nogamma(start.s, end.s),
  459. l = nogamma(start.l, end.l),
  460. opacity = nogamma(start.opacity, end.opacity);
  461. return function(t) {
  462. start.h = h(t);
  463. start.s = s(t);
  464. start.l = l(Math.pow(t, y));
  465. start.opacity = opacity(t);
  466. return start + "";
  467. };
  468. }
  469. cubehelix.gamma = cubehelixGamma;
  470. return cubehelix;
  471. })(1);
  472. }
  473. var cubehelix$1 = cubehelix(hue);
  474. var cubehelixLong = cubehelix(nogamma);
  475. function piecewise(interpolate, values) {
  476. var i = 0, n = values.length - 1, v = values[0], I = new Array(n < 0 ? 0 : n);
  477. while (i < n) I[i] = interpolate(v, v = values[++i]);
  478. return function(t) {
  479. var i = Math.max(0, Math.min(n - 1, Math.floor(t *= n)));
  480. return I[i](t - i);
  481. };
  482. }
  483. function quantize(interpolator, n) {
  484. var samples = new Array(n);
  485. for (var i = 0; i < n; ++i) samples[i] = interpolator(i / (n - 1));
  486. return samples;
  487. }
  488. exports.interpolate = value;
  489. exports.interpolateArray = array;
  490. exports.interpolateBasis = basis$1;
  491. exports.interpolateBasisClosed = basisClosed;
  492. exports.interpolateCubehelix = cubehelix$1;
  493. exports.interpolateCubehelixLong = cubehelixLong;
  494. exports.interpolateDate = date;
  495. exports.interpolateDiscrete = discrete;
  496. exports.interpolateHcl = hcl$1;
  497. exports.interpolateHclLong = hclLong;
  498. exports.interpolateHsl = hsl$1;
  499. exports.interpolateHslLong = hslLong;
  500. exports.interpolateHue = hue$1;
  501. exports.interpolateLab = lab;
  502. exports.interpolateNumber = number;
  503. exports.interpolateNumberArray = numberArray;
  504. exports.interpolateObject = object;
  505. exports.interpolateRgb = rgb;
  506. exports.interpolateRgbBasis = rgbBasis;
  507. exports.interpolateRgbBasisClosed = rgbBasisClosed;
  508. exports.interpolateRound = round;
  509. exports.interpolateString = string;
  510. exports.interpolateTransformCss = interpolateTransformCss;
  511. exports.interpolateTransformSvg = interpolateTransformSvg;
  512. exports.interpolateZoom = zoom;
  513. exports.piecewise = piecewise;
  514. exports.quantize = quantize;
  515. Object.defineProperty(exports, '__esModule', { value: true });
  516. }));