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
8 fontSize = cloudFontSize,
9 fontStyle = cloudFontNormal,
10 fontWeight = cloudFontNormal,
12 padding = cloudPadding,
13 spiral = archimedeanSpiral,
15 timeInterval = Infinity,
16 event = d3.dispatch("word", "end"),
20 cloud.start = function() {
21 var board = zeroArray((size[0] >> 5) * size[1]),
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);
35 }).sort(function(a, b) { return b.size - a.size; });
45 d3.select("body").append("canvas").attr("id", "maskCanvas")
46 maskCanvas = document.getElementById('maskCanvas'),
47 maskCanvas.width = 85;
48 maskCanvas.height = 85;
49 maskContext = maskCanvas.getContext('2d');
50 maskImage = new Image();
51 maskImage.src = 'mask.png';
52 maskImage.onload = function(){
53 maskContext.drawImage(maskImage,0,0,85,85 * maskImage.height / maskImage.width);
54 maskData = maskContext.getImageData(0,0,85,85).data;
57 while (hh < maskData.length) {
59 if (maskData[hh] == 0) {
69 if (timer) clearInterval(timer);
70 timer = setInterval(step, 0);
76 var start = +new Date,
78 while (+new Date - start < timeInterval && ++i < n && timer) {
80 d.x = (size[0] * (Math.random() + .5)) >> 1;
81 d.y = (size[1] * (Math.random() + .5)) >> 1;
82 cloudSprite(d, data, i);
83 if (d.hasText && place(board, d, bounds)) {
86 if (bounds) cloudBounds(bounds, d);
87 else bounds = [{x: d.x + d.x0, y: d.y + d.y0}, {x: d.x + d.x1, y: d.y + d.y1}];
95 event.end(tags, bounds);
100 cloud.stop = function() {
102 clearInterval(timer);
108 cloud.timeInterval = function(x) {
109 if (!arguments.length) return timeInterval;
110 timeInterval = x == null ? Infinity : x;
114 function place(board, tag, bounds) {
115 var perimeter = [{x: 0, y: 0}, {x: size[0], y: size[1]}],
118 maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]),
120 dt = Math.random() < .5 ? 1 : -1,
126 while (dxdy = s(t += dt)) {
130 if (Math.min(dx, dy) > maxDelta) break;
135 if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
136 tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
137 // TODO only check for collisions within current bounds.
138 if (!bounds || !cloudCollide(tag, board, size[0])) {
139 if (!bounds || collideRects(tag, bounds)) {
140 var sprite = tag.sprite,
143 lx = tag.x - (w << 4),
147 x = (tag.y + tag.y0) * sw + (lx >> 5),
149 for (var j = 0; j < h; j++) {
151 for (var i = 0; i <= w; i++) {
152 board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
164 cloud.words = function(x) {
165 if (!arguments.length) return words;
170 cloud.size = function(x) {
171 if (!arguments.length) return size;
172 size = [+x[0], +x[1]];
176 cloud.font = function(x) {
177 if (!arguments.length) return font;
178 font = d3.functor(x);
182 cloud.fontStyle = function(x) {
183 if (!arguments.length) return fontStyle;
184 fontStyle = d3.functor(x);
188 cloud.fontWeight = function(x) {
189 if (!arguments.length) return fontWeight;
190 fontWeight = d3.functor(x);
194 cloud.rotate = function(x) {
195 if (!arguments.length) return rotate;
196 rotate = d3.functor(x);
200 cloud.text = function(x) {
201 if (!arguments.length) return text;
202 text = d3.functor(x);
206 cloud.spiral = function(x) {
207 if (!arguments.length) return spiral;
208 spiral = spirals[x + ""] || x;
212 cloud.fontSize = function(x) {
213 if (!arguments.length) return fontSize;
214 fontSize = d3.functor(x);
218 cloud.padding = function(x) {
219 if (!arguments.length) return padding;
220 padding = d3.functor(x);
224 return d3.rebind(cloud, event, "on");
227 function cloudText(d) {
231 function cloudFont() {
235 function cloudFontNormal() {
239 function cloudFontSize(d) {
240 return Math.sqrt(d.value);
243 function cloudRotate() {
244 return (~~(Math.random() * 6) - 3) * 30;
247 function cloudPadding() {
251 // Fetches a monochrome sprite bitmap for the specified text.
252 // Load in batches for speed.
253 function cloudSprite(d, data, di) {
254 if (d.sprite) return;
255 c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
264 c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font;
265 var w = c.measureText(d.text + "m").width * ratio,
268 var sr = Math.sin(d.rotate * cloudRadians),
269 cr = Math.cos(d.rotate * cloudRadians),
274 w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5;
275 h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
277 w = (w + 0x1f) >> 5 << 5;
279 if (h > maxh) maxh = h;
280 if (x + w >= (cw << 5)) {
285 if (y + h >= ch) break;
286 c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
287 if (d.rotate) c.rotate(d.rotate * cloudRadians);
288 c.fillText(d.text, 0, 0);
289 if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, 0, 0);
302 var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data,
306 if (!d.hasText) continue;
311 for (var i = 0; i < h * w32; i++) sprite[i] = 0;
313 if (x == null) return;
317 for (var j = 0; j < h; j++) {
318 for (var i = 0; i < w; i++) {
319 var k = w32 * j + (i >> 5),
320 m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0;
324 if (seen) seenRow = j;
332 d.y1 = d.y0 + seenRow;
333 d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
337 // Use mask-based collision detection.
338 function cloudCollide(tag, board, sw) {
340 var sprite = tag.sprite,
342 lx = tag.x - (w << 4),
346 x = (tag.y + tag.y0) * sw + (lx >> 5),
348 for (var j = 0; j < h; j++) {
350 for (var i = 0; i <= w; i++) {
351 if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0))
352 & board[x + i]) return true;
359 function cloudBounds(bounds, d) {
362 if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0;
363 if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0;
364 if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1;
365 if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1;
368 function collideRects(a, b) {
369 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;
372 function archimedeanSpiral(size) {
373 var e = size[0] / size[1];
375 return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)];
379 function rectangularSpiral(size) {
381 dx = dy * size[0] / size[1],
385 var sign = t < 0 ? -1 : 1;
386 // See triangular numbers: T_n = n * (n + 1) / 2.
387 switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) {
388 case 0: x += dx; break;
389 case 1: y += dy; break;
390 case 2: x -= dx; break;
391 default: y -= dy; break;
397 // TODO reuse arrays?
398 function zeroArray(n) {
401 while (++i < n) a[i] = 0;
405 var cloudRadians = Math.PI / 180,
411 if (typeof document !== "undefined") {
412 canvas = document.createElement("canvas");
415 ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2);
416 canvas.width = (cw << 5) / ratio;
417 canvas.height = ch / ratio;
419 // Attempt to use node-canvas.
420 canvas = new Canvas(cw << 5, ch);
423 var c = canvas.getContext("2d"),
425 archimedean: archimedeanSpiral,
426 rectangular: rectangularSpiral
428 c.fillStyle = c.strokeStyle = "red";
429 c.textAlign = "center";
431 if (typeof module === "object" && module.exports) module.exports = cloud;
432 else (d3.layout || (d3.layout = {})).cloud = cloud;