PageSourceSearch

https://www.bertrandt.com/typo3conf/ext/tag_cloud/Resources/Public/Js/Plugins/cloud.js?1575388982

js bertrandt.com collected 2026-10-02 06:29:16 UTC 14,304 bytes, 424 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.