index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1676793464762, function(require, module, exports) {
  8. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g=(g.d3||(g.d3 = {}));g=(g.layout||(g.layout = {}));g.cloud = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  9. // Word cloud layout by Jason Davies, https://www.jasondavies.com/wordcloud/
  10. // Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf
  11. var dispatch = require("d3-dispatch").dispatch;
  12. var cloudRadians = Math.PI / 180,
  13. cw = 1 << 11 >> 5,
  14. ch = 1 << 11;
  15. module.exports = function() {
  16. var size = [256, 256],
  17. text = cloudText,
  18. font = cloudFont,
  19. fontSize = cloudFontSize,
  20. fontStyle = cloudFontNormal,
  21. fontWeight = cloudFontNormal,
  22. rotate = cloudRotate,
  23. padding = cloudPadding,
  24. spiral = archimedeanSpiral,
  25. words = [],
  26. timeInterval = Infinity,
  27. event = dispatch("word", "end"),
  28. timer = null,
  29. random = Math.random,
  30. cloud = {},
  31. canvas = cloudCanvas;
  32. cloud.canvas = function(_) {
  33. return arguments.length ? (canvas = functor(_), cloud) : canvas;
  34. };
  35. cloud.start = function() {
  36. var contextAndRatio = getContext(canvas()),
  37. board = zeroArray((size[0] >> 5) * size[1]),
  38. bounds = null,
  39. n = words.length,
  40. i = -1,
  41. tags = [],
  42. data = words.map(function(d, i) {
  43. d.text = text.call(this, d, i);
  44. d.font = font.call(this, d, i);
  45. d.style = fontStyle.call(this, d, i);
  46. d.weight = fontWeight.call(this, d, i);
  47. d.rotate = rotate.call(this, d, i);
  48. d.size = ~~fontSize.call(this, d, i);
  49. d.padding = padding.call(this, d, i);
  50. return d;
  51. }).sort(function(a, b) { return b.size - a.size; });
  52. if (timer) clearInterval(timer);
  53. timer = setInterval(step, 0);
  54. step();
  55. return cloud;
  56. function step() {
  57. var start = Date.now();
  58. while (Date.now() - start < timeInterval && ++i < n && timer) {
  59. var d = data[i];
  60. d.x = (size[0] * (random() + .5)) >> 1;
  61. d.y = (size[1] * (random() + .5)) >> 1;
  62. cloudSprite(contextAndRatio, d, data, i);
  63. if (d.hasText && place(board, d, bounds)) {
  64. tags.push(d);
  65. event.call("word", cloud, d);
  66. if (bounds) cloudBounds(bounds, d);
  67. else bounds = [{x: d.x + d.x0, y: d.y + d.y0}, {x: d.x + d.x1, y: d.y + d.y1}];
  68. // Temporary hack
  69. d.x -= size[0] >> 1;
  70. d.y -= size[1] >> 1;
  71. }
  72. }
  73. if (i >= n) {
  74. cloud.stop();
  75. event.call("end", cloud, tags, bounds);
  76. }
  77. }
  78. }
  79. cloud.stop = function() {
  80. if (timer) {
  81. clearInterval(timer);
  82. timer = null;
  83. }
  84. return cloud;
  85. };
  86. function getContext(canvas) {
  87. canvas.width = canvas.height = 1;
  88. var ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2);
  89. canvas.width = (cw << 5) / ratio;
  90. canvas.height = ch / ratio;
  91. var context = canvas.getContext("2d");
  92. context.fillStyle = context.strokeStyle = "red";
  93. context.textAlign = "center";
  94. return {context: context, ratio: ratio};
  95. }
  96. function place(board, tag, bounds) {
  97. var perimeter = [{x: 0, y: 0}, {x: size[0], y: size[1]}],
  98. startX = tag.x,
  99. startY = tag.y,
  100. maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
  101. s = spiral(size),
  102. dt = random() < .5 ? 1 : -1,
  103. t = -dt,
  104. dxdy,
  105. dx,
  106. dy;
  107. while (dxdy = s(t += dt)) {
  108. dx = ~~dxdy[0];
  109. dy = ~~dxdy[1];
  110. if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break;
  111. tag.x = startX + dx;
  112. tag.y = startY + dy;
  113. if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
  114. tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
  115. // TODO only check for collisions within current bounds.
  116. if (!bounds || !cloudCollide(tag, board, size[0])) {
  117. if (!bounds || collideRects(tag, bounds)) {
  118. var sprite = tag.sprite,
  119. w = tag.width >> 5,
  120. sw = size[0] >> 5,
  121. lx = tag.x - (w << 4),
  122. sx = lx & 0x7f,
  123. msx = 32 - sx,
  124. h = tag.y1 - tag.y0,
  125. x = (tag.y + tag.y0) * sw + (lx >> 5),
  126. last;
  127. for (var j = 0; j < h; j++) {
  128. last = 0;
  129. for (var i = 0; i <= w; i++) {
  130. board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
  131. }
  132. x += sw;
  133. }
  134. delete tag.sprite;
  135. return true;
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. cloud.timeInterval = function(_) {
  142. return arguments.length ? (timeInterval = _ == null ? Infinity : _, cloud) : timeInterval;
  143. };
  144. cloud.words = function(_) {
  145. return arguments.length ? (words = _, cloud) : words;
  146. };
  147. cloud.size = function(_) {
  148. return arguments.length ? (size = [+_[0], +_[1]], cloud) : size;
  149. };
  150. cloud.font = function(_) {
  151. return arguments.length ? (font = functor(_), cloud) : font;
  152. };
  153. cloud.fontStyle = function(_) {
  154. return arguments.length ? (fontStyle = functor(_), cloud) : fontStyle;
  155. };
  156. cloud.fontWeight = function(_) {
  157. return arguments.length ? (fontWeight = functor(_), cloud) : fontWeight;
  158. };
  159. cloud.rotate = function(_) {
  160. return arguments.length ? (rotate = functor(_), cloud) : rotate;
  161. };
  162. cloud.text = function(_) {
  163. return arguments.length ? (text = functor(_), cloud) : text;
  164. };
  165. cloud.spiral = function(_) {
  166. return arguments.length ? (spiral = spirals[_] || _, cloud) : spiral;
  167. };
  168. cloud.fontSize = function(_) {
  169. return arguments.length ? (fontSize = functor(_), cloud) : fontSize;
  170. };
  171. cloud.padding = function(_) {
  172. return arguments.length ? (padding = functor(_), cloud) : padding;
  173. };
  174. cloud.random = function(_) {
  175. return arguments.length ? (random = _, cloud) : random;
  176. };
  177. cloud.on = function() {
  178. var value = event.on.apply(event, arguments);
  179. return value === event ? cloud : value;
  180. };
  181. return cloud;
  182. };
  183. function cloudText(d) {
  184. return d.text;
  185. }
  186. function cloudFont() {
  187. return "serif";
  188. }
  189. function cloudFontNormal() {
  190. return "normal";
  191. }
  192. function cloudFontSize(d) {
  193. return Math.sqrt(d.value);
  194. }
  195. function cloudRotate() {
  196. return (~~(Math.random() * 6) - 3) * 30;
  197. }
  198. function cloudPadding() {
  199. return 1;
  200. }
  201. // Fetches a monochrome sprite bitmap for the specified text.
  202. // Load in batches for speed.
  203. function cloudSprite(contextAndRatio, d, data, di) {
  204. if (d.sprite) return;
  205. var c = contextAndRatio.context,
  206. ratio = contextAndRatio.ratio;
  207. c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
  208. var x = 0,
  209. y = 0,
  210. maxh = 0,
  211. n = data.length;
  212. --di;
  213. while (++di < n) {
  214. d = data[di];
  215. c.save();
  216. c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font;
  217. var w = c.measureText(d.text + "m").width * ratio,
  218. h = d.size << 1;
  219. if (d.rotate) {
  220. var sr = Math.sin(d.rotate * cloudRadians),
  221. cr = Math.cos(d.rotate * cloudRadians),
  222. wcr = w * cr,
  223. wsr = w * sr,
  224. hcr = h * cr,
  225. hsr = h * sr;
  226. w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5;
  227. h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
  228. } else {
  229. w = (w + 0x1f) >> 5 << 5;
  230. }
  231. if (h > maxh) maxh = h;
  232. if (x + w >= (cw << 5)) {
  233. x = 0;
  234. y += maxh;
  235. maxh = 0;
  236. }
  237. if (y + h >= ch) break;
  238. c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
  239. if (d.rotate) c.rotate(d.rotate * cloudRadians);
  240. c.fillText(d.text, 0, 0);
  241. if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, 0, 0);
  242. c.restore();
  243. d.width = w;
  244. d.height = h;
  245. d.xoff = x;
  246. d.yoff = y;
  247. d.x1 = w >> 1;
  248. d.y1 = h >> 1;
  249. d.x0 = -d.x1;
  250. d.y0 = -d.y1;
  251. d.hasText = true;
  252. x += w;
  253. }
  254. var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data,
  255. sprite = [];
  256. while (--di >= 0) {
  257. d = data[di];
  258. if (!d.hasText) continue;
  259. var w = d.width,
  260. w32 = w >> 5,
  261. h = d.y1 - d.y0;
  262. // Zero the buffer
  263. for (var i = 0; i < h * w32; i++) sprite[i] = 0;
  264. x = d.xoff;
  265. if (x == null) return;
  266. y = d.yoff;
  267. var seen = 0,
  268. seenRow = -1;
  269. for (var j = 0; j < h; j++) {
  270. for (var i = 0; i < w; i++) {
  271. var k = w32 * j + (i >> 5),
  272. m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0;
  273. sprite[k] |= m;
  274. seen |= m;
  275. }
  276. if (seen) seenRow = j;
  277. else {
  278. d.y0++;
  279. h--;
  280. j--;
  281. y++;
  282. }
  283. }
  284. d.y1 = d.y0 + seenRow;
  285. d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
  286. }
  287. }
  288. // Use mask-based collision detection.
  289. function cloudCollide(tag, board, sw) {
  290. sw >>= 5;
  291. var sprite = tag.sprite,
  292. w = tag.width >> 5,
  293. lx = tag.x - (w << 4),
  294. sx = lx & 0x7f,
  295. msx = 32 - sx,
  296. h = tag.y1 - tag.y0,
  297. x = (tag.y + tag.y0) * sw + (lx >> 5),
  298. last;
  299. for (var j = 0; j < h; j++) {
  300. last = 0;
  301. for (var i = 0; i <= w; i++) {
  302. if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0))
  303. & board[x + i]) return true;
  304. }
  305. x += sw;
  306. }
  307. return false;
  308. }
  309. function cloudBounds(bounds, d) {
  310. var b0 = bounds[0],
  311. b1 = bounds[1];
  312. if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0;
  313. if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0;
  314. if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1;
  315. if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1;
  316. }
  317. function collideRects(a, b) {
  318. return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y;
  319. }
  320. function archimedeanSpiral(size) {
  321. var e = size[0] / size[1];
  322. return function(t) {
  323. return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)];
  324. };
  325. }
  326. function rectangularSpiral(size) {
  327. var dy = 4,
  328. dx = dy * size[0] / size[1],
  329. x = 0,
  330. y = 0;
  331. return function(t) {
  332. var sign = t < 0 ? -1 : 1;
  333. // See triangular numbers: T_n = n * (n + 1) / 2.
  334. switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) {
  335. case 0: x += dx; break;
  336. case 1: y += dy; break;
  337. case 2: x -= dx; break;
  338. default: y -= dy; break;
  339. }
  340. return [x, y];
  341. };
  342. }
  343. // TODO reuse arrays?
  344. function zeroArray(n) {
  345. var a = [],
  346. i = -1;
  347. while (++i < n) a[i] = 0;
  348. return a;
  349. }
  350. function cloudCanvas() {
  351. return document.createElement("canvas");
  352. }
  353. function functor(d) {
  354. return typeof d === "function" ? d : function() { return d; };
  355. }
  356. var spirals = {
  357. archimedean: archimedeanSpiral,
  358. rectangular: rectangularSpiral
  359. };
  360. },{"d3-dispatch":2}],2:[function(require,module,exports){
  361. // https://d3js.org/d3-dispatch/ Version 1.0.3. Copyright 2017 Mike Bostock.
  362. (function (global, factory) {
  363. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  364. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  365. (factory((global.d3 = global.d3 || {})));
  366. }(this, (function (exports) {
  367. var noop = {value: function() {}};
  368. function dispatch() {
  369. for (var i = 0, n = arguments.length, _ = {}, t; i < n; ++i) {
  370. if (!(t = arguments[i] + "") || (t in _)) throw new Error("illegal type: " + t);
  371. _[t] = [];
  372. }
  373. return new Dispatch(_);
  374. }
  375. function Dispatch(_) {
  376. this._ = _;
  377. }
  378. function parseTypenames(typenames, types) {
  379. return typenames.trim().split(/^|\s+/).map(function(t) {
  380. var name = "", i = t.indexOf(".");
  381. if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i);
  382. if (t && !types.hasOwnProperty(t)) throw new Error("unknown type: " + t);
  383. return {type: t, name: name};
  384. });
  385. }
  386. Dispatch.prototype = dispatch.prototype = {
  387. constructor: Dispatch,
  388. on: function(typename, callback) {
  389. var _ = this._,
  390. T = parseTypenames(typename + "", _),
  391. t,
  392. i = -1,
  393. n = T.length;
  394. // If no callback was specified, return the callback of the given type and name.
  395. if (arguments.length < 2) {
  396. while (++i < n) if ((t = (typename = T[i]).type) && (t = get(_[t], typename.name))) return t;
  397. return;
  398. }
  399. // If a type was specified, set the callback for the given type and name.
  400. // Otherwise, if a null callback was specified, remove callbacks of the given name.
  401. if (callback != null && typeof callback !== "function") throw new Error("invalid callback: " + callback);
  402. while (++i < n) {
  403. if (t = (typename = T[i]).type) _[t] = set(_[t], typename.name, callback);
  404. else if (callback == null) for (t in _) _[t] = set(_[t], typename.name, null);
  405. }
  406. return this;
  407. },
  408. copy: function() {
  409. var copy = {}, _ = this._;
  410. for (var t in _) copy[t] = _[t].slice();
  411. return new Dispatch(copy);
  412. },
  413. call: function(type, that) {
  414. if ((n = arguments.length - 2) > 0) for (var args = new Array(n), i = 0, n, t; i < n; ++i) args[i] = arguments[i + 2];
  415. if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
  416. for (t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
  417. },
  418. apply: function(type, that, args) {
  419. if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type);
  420. for (var t = this._[type], i = 0, n = t.length; i < n; ++i) t[i].value.apply(that, args);
  421. }
  422. };
  423. function get(type, name) {
  424. for (var i = 0, n = type.length, c; i < n; ++i) {
  425. if ((c = type[i]).name === name) {
  426. return c.value;
  427. }
  428. }
  429. }
  430. function set(type, name, callback) {
  431. for (var i = 0, n = type.length; i < n; ++i) {
  432. if (type[i].name === name) {
  433. type[i] = noop, type = type.slice(0, i).concat(type.slice(i + 1));
  434. break;
  435. }
  436. }
  437. if (callback != null) type.push({name: name, value: callback});
  438. return type;
  439. }
  440. exports.dispatch = dispatch;
  441. Object.defineProperty(exports, '__esModule', { value: true });
  442. })));
  443. },{}]},{},[1])(1)
  444. });
  445. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  446. return __REQUIRE__(1676793464762);
  447. })()
  448. //miniprogram-npm-outsideDeps=["d3-dispatch"]
  449. //# sourceMappingURL=index.js.map