index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.tagCloud = void 0;
  4. const cloudRadians = Math.PI / 180, cw = (1 << 11) >> 5, ch = 1 << 11;
  5. function cloudText(d) {
  6. return d.text;
  7. }
  8. function cloudFont() {
  9. return 'serif';
  10. }
  11. function cloudFontNormal() {
  12. return 'normal';
  13. }
  14. function cloudFontSize(d) {
  15. return d.value;
  16. }
  17. function cloudRotate() {
  18. return ~~(Math.random() * 2) * 90;
  19. }
  20. function cloudPadding() {
  21. return 1;
  22. }
  23. function cloudDispatch() {
  24. return;
  25. }
  26. // Fetches a monochrome sprite bitmap for the specified text.
  27. // Load in batches for speed.
  28. function cloudSprite(contextAndRatio, d, data, di) {
  29. if (d.sprite)
  30. return;
  31. const c = contextAndRatio.context, ratio = contextAndRatio.ratio;
  32. c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
  33. let x = 0, y = 0, maxh = 0;
  34. const n = data.length;
  35. --di;
  36. while (++di < n) {
  37. d = data[di];
  38. c.save();
  39. c.font =
  40. d.style +
  41. ' ' +
  42. d.weight +
  43. ' ' +
  44. ~~((d.size + 1) / ratio) +
  45. 'px ' +
  46. d.font;
  47. let w = c.measureText(d.text + 'm').width * ratio, h = d.size << 1;
  48. if (d.rotate) {
  49. const sr = Math.sin(d.rotate * cloudRadians), cr = Math.cos(d.rotate * cloudRadians), wcr = w * cr, wsr = w * sr, hcr = h * cr, hsr = h * sr;
  50. w =
  51. ((Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5) << 5;
  52. h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
  53. }
  54. else {
  55. w = ((w + 0x1f) >> 5) << 5;
  56. }
  57. if (h > maxh)
  58. maxh = h;
  59. if (x + w >= cw << 5) {
  60. x = 0;
  61. y += maxh;
  62. maxh = 0;
  63. }
  64. if (y + h >= ch)
  65. break;
  66. c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
  67. if (d.rotate)
  68. c.rotate(d.rotate * cloudRadians);
  69. c.fillText(d.text, 0, 0);
  70. if (d.padding) {
  71. c.lineWidth = 2 * d.padding;
  72. c.strokeText(d.text, 0, 0);
  73. }
  74. c.restore();
  75. d.width = w;
  76. d.height = h;
  77. d.xoff = x;
  78. d.yoff = y;
  79. d.x1 = w >> 1;
  80. d.y1 = h >> 1;
  81. d.x0 = -d.x1;
  82. d.y0 = -d.y1;
  83. d.hasText = true;
  84. x += w;
  85. }
  86. const pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data, sprite = [];
  87. while (--di >= 0) {
  88. d = data[di];
  89. if (!d.hasText)
  90. continue;
  91. const w = d.width, w32 = w >> 5;
  92. let h = d.y1 - d.y0;
  93. // Zero the buffer
  94. for (let i = 0; i < h * w32; i++)
  95. sprite[i] = 0;
  96. x = d.xoff;
  97. if (x == null)
  98. return;
  99. y = d.yoff;
  100. let seen = 0, seenRow = -1;
  101. for (let j = 0; j < h; j++) {
  102. for (let i = 0; i < w; i++) {
  103. const k = w32 * j + (i >> 5), m = pixels[((y + j) * (cw << 5) + (x + i)) << 2]
  104. ? 1 << (31 - (i % 32))
  105. : 0;
  106. sprite[k] |= m;
  107. seen |= m;
  108. }
  109. if (seen)
  110. seenRow = j;
  111. else {
  112. d.y0++;
  113. h--;
  114. j--;
  115. y++;
  116. }
  117. }
  118. d.y1 = d.y0 + seenRow;
  119. d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
  120. }
  121. }
  122. // Use mask-based collision detection.
  123. function cloudCollide(tag, board, sw) {
  124. sw >>= 5;
  125. const sprite = tag.sprite, w = tag.width >> 5, lx = tag.x - (w << 4), sx = lx & 0x7f, msx = 32 - sx, h = tag.y1 - tag.y0;
  126. let x = (tag.y + tag.y0) * sw + (lx >> 5), last;
  127. for (let j = 0; j < h; j++) {
  128. last = 0;
  129. for (let i = 0; i <= w; i++) {
  130. if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0)) &
  131. board[x + i])
  132. return true;
  133. }
  134. x += sw;
  135. }
  136. return false;
  137. }
  138. function cloudBounds(bounds, d) {
  139. const b0 = bounds[0], b1 = bounds[1];
  140. if (d.x + d.x0 < b0.x)
  141. b0.x = d.x + d.x0;
  142. if (d.y + d.y0 < b0.y)
  143. b0.y = d.y + d.y0;
  144. if (d.x + d.x1 > b1.x)
  145. b1.x = d.x + d.x1;
  146. if (d.y + d.y1 > b1.y)
  147. b1.y = d.y + d.y1;
  148. }
  149. function collideRects(a, b) {
  150. return (a.x + a.x1 > b[0].x &&
  151. a.x + a.x0 < b[1].x &&
  152. a.y + a.y1 > b[0].y &&
  153. a.y + a.y0 < b[1].y);
  154. }
  155. function archimedeanSpiral(size) {
  156. const e = size[0] / size[1];
  157. return function (t) {
  158. return [e * (t *= 0.1) * Math.cos(t), t * Math.sin(t)];
  159. };
  160. }
  161. function rectangularSpiral(size) {
  162. const dy = 4, dx = (dy * size[0]) / size[1];
  163. let x = 0, y = 0;
  164. return function (t) {
  165. const sign = t < 0 ? -1 : 1;
  166. // See triangular numbers: T_n = n * (n + 1) / 2.
  167. switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) {
  168. case 0:
  169. x += dx;
  170. break;
  171. case 1:
  172. y += dy;
  173. break;
  174. case 2:
  175. x -= dx;
  176. break;
  177. default:
  178. y -= dy;
  179. break;
  180. }
  181. return [x, y];
  182. };
  183. }
  184. // TODO reuse arrays?
  185. function zeroArray(n) {
  186. const a = [];
  187. let i = -1;
  188. while (++i < n)
  189. a[i] = 0;
  190. return a;
  191. }
  192. function cloudCanvas() {
  193. return document.createElement('canvas');
  194. }
  195. function functor(d) {
  196. return typeof d === 'function'
  197. ? d
  198. : function () {
  199. return d;
  200. };
  201. }
  202. const spirals = {
  203. archimedean: archimedeanSpiral,
  204. rectangular: rectangularSpiral,
  205. };
  206. function tagCloud() {
  207. let size = [256, 256], text = cloudText, font = cloudFont, fontSize = cloudFontSize, fontWeight = cloudFontNormal, rotate = cloudRotate, padding = cloudPadding, spiral = archimedeanSpiral, random = Math.random, event = cloudDispatch, words = [], timer = null, timeInterval = Infinity;
  208. const fontStyle = cloudFontNormal;
  209. const canvas = cloudCanvas;
  210. const cloud = {};
  211. cloud.start = function () {
  212. const [width, height] = size;
  213. const contextAndRatio = getContext(canvas()), board = cloud.board ? cloud.board : zeroArray((size[0] >> 5) * size[1]), n = words.length, tags = [], data = words
  214. .map(function (d, i, data) {
  215. d.text = text.call(this, d, i, data);
  216. d.font = font.call(this, d, i, data);
  217. d.style = fontStyle.call(this, d, i, data);
  218. d.weight = fontWeight.call(this, d, i, data);
  219. d.rotate = rotate.call(this, d, i, data);
  220. d.size = ~~fontSize.call(this, d, i, data);
  221. d.padding = padding.call(this, d, i, data);
  222. return d;
  223. })
  224. .sort(function (a, b) {
  225. return b.size - a.size;
  226. });
  227. let i = -1, bounds = !cloud.board
  228. ? undefined
  229. : [
  230. {
  231. x: 0,
  232. y: 0,
  233. },
  234. {
  235. x: width,
  236. y: height,
  237. },
  238. ];
  239. if (timer)
  240. clearInterval(timer);
  241. timer = setInterval(step, 0);
  242. step();
  243. function step() {
  244. const start = Date.now();
  245. while (Date.now() - start < timeInterval && ++i < n) {
  246. const d = data[i];
  247. d.x = (width * (random() + 0.5)) >> 1;
  248. d.y = (height * (random() + 0.5)) >> 1;
  249. cloudSprite(contextAndRatio, d, data, i);
  250. if (d.hasText && place(board, d, bounds)) {
  251. event.call(null, 'word', { cloud, word: d });
  252. tags.push(d);
  253. if (bounds) {
  254. if (!cloud.hasImage) {
  255. // update bounds if image mask not set
  256. cloudBounds(bounds, d);
  257. }
  258. }
  259. else {
  260. bounds = [
  261. { x: d.x + d.x0, y: d.y + d.y0 },
  262. { x: d.x + d.x1, y: d.y + d.y1 },
  263. ];
  264. }
  265. // Temporary hack
  266. d.x -= size[0] >> 1;
  267. d.y -= size[1] >> 1;
  268. }
  269. }
  270. cloud._tags = tags;
  271. cloud._bounds = bounds;
  272. if (i >= n) {
  273. cloud.stop();
  274. event.call(null, 'end', { cloud, words: tags, bounds });
  275. }
  276. }
  277. return cloud;
  278. };
  279. cloud.stop = function () {
  280. if (timer) {
  281. clearInterval(timer);
  282. timer = null;
  283. }
  284. return cloud;
  285. };
  286. function getContext(canvas) {
  287. canvas.width = canvas.height = 1;
  288. const ratio = Math.sqrt(canvas.getContext('2d').getImageData(0, 0, 1, 1).data.length >> 2);
  289. canvas.width = (cw << 5) / ratio;
  290. canvas.height = ch / ratio;
  291. const context = canvas.getContext('2d');
  292. context.fillStyle = context.strokeStyle = 'red';
  293. context.textAlign = 'center';
  294. return { context, ratio };
  295. }
  296. function place(board, tag, bounds) {
  297. // const perimeter = [{ x: 0, y: 0 }, { x: size[0], y: size[1] }],
  298. const startX = tag.x, startY = tag.y, maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]), s = spiral(size), dt = random() < 0.5 ? 1 : -1;
  299. let dxdy, t = -dt, dx, dy;
  300. while ((dxdy = s((t += dt)))) {
  301. dx = ~~dxdy[0];
  302. dy = ~~dxdy[1];
  303. if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta)
  304. break;
  305. tag.x = startX + dx;
  306. tag.y = startY + dy;
  307. if (tag.x + tag.x0 < 0 ||
  308. tag.y + tag.y0 < 0 ||
  309. tag.x + tag.x1 > size[0] ||
  310. tag.y + tag.y1 > size[1])
  311. continue;
  312. // TODO only check for collisions within current bounds.
  313. if (!bounds || !cloudCollide(tag, board, size[0])) {
  314. if (!bounds || collideRects(tag, bounds)) {
  315. const sprite = tag.sprite, w = tag.width >> 5, sw = size[0] >> 5, lx = tag.x - (w << 4), sx = lx & 0x7f, msx = 32 - sx, h = tag.y1 - tag.y0;
  316. let last, x = (tag.y + tag.y0) * sw + (lx >> 5);
  317. for (let j = 0; j < h; j++) {
  318. last = 0;
  319. for (let i = 0; i <= w; i++) {
  320. board[x + i] |=
  321. (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
  322. }
  323. x += sw;
  324. }
  325. delete tag.sprite;
  326. return true;
  327. }
  328. }
  329. }
  330. return false;
  331. }
  332. cloud.createMask = (img) => {
  333. const can = document.createElement('canvas');
  334. const [width, height] = size;
  335. // 当 width 或 height 为 0 时,调用 cxt.getImageData 会报错
  336. if (!width || !height) {
  337. return;
  338. }
  339. const w32 = width >> 5;
  340. const board = zeroArray((width >> 5) * height);
  341. can.width = width;
  342. can.height = height;
  343. const cxt = can.getContext('2d');
  344. cxt.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);
  345. const imageData = cxt.getImageData(0, 0, width, height).data;
  346. for (let j = 0; j < height; j++) {
  347. for (let i = 0; i < width; i++) {
  348. const k = w32 * j + (i >> 5);
  349. const tmp = (j * width + i) << 2;
  350. const flag = imageData[tmp] >= 250 &&
  351. imageData[tmp + 1] >= 250 &&
  352. imageData[tmp + 2] >= 250;
  353. const m = flag ? 1 << (31 - (i % 32)) : 0;
  354. board[k] |= m;
  355. }
  356. }
  357. cloud.board = board;
  358. cloud.hasImage = true;
  359. };
  360. cloud.timeInterval = function (_) {
  361. timeInterval = _ == null ? Infinity : _;
  362. };
  363. cloud.words = function (_) {
  364. words = _;
  365. };
  366. cloud.size = function (_ = []) {
  367. size = [+_[0], +_[1]];
  368. };
  369. cloud.text = function (_) {
  370. text = functor(_);
  371. };
  372. cloud.font = function (_) {
  373. font = functor(_);
  374. };
  375. cloud.fontWeight = function (_) {
  376. fontWeight = functor(_);
  377. };
  378. cloud.rotate = function (_) {
  379. rotate = functor(_);
  380. };
  381. cloud.spiral = function (_) {
  382. spiral = spirals[_] || _;
  383. };
  384. cloud.fontSize = function (_) {
  385. fontSize = functor(_);
  386. };
  387. cloud.padding = function (_) {
  388. padding = functor(_);
  389. };
  390. cloud.random = function (_) {
  391. random = functor(_);
  392. };
  393. cloud.on = function (_) {
  394. event = functor(_);
  395. };
  396. return cloud;
  397. }
  398. exports.tagCloud = tagCloud;
  399. //# sourceMappingURL=index.js.map