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