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