| 2 | // Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf |
| 3 | (function(exports) { |
| 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 = cloudPadding.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 (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) { |