MCPcopy
hub / github.com/jasondavies/d3-cloud / cloudSprite

Function cloudSprite

index.js:241–328  ·  view source on GitHub ↗
(contextAndRatio, d, data, di)

Source from the content-addressed store, hash-verified

239// Fetches a monochrome sprite bitmap for the specified text.
240// Load in batches for speed.
241function cloudSprite(contextAndRatio, d, data, di) {
242 if (d.sprite) return;
243 var c = contextAndRatio.context,
244 ratio = contextAndRatio.ratio;
245
246 c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
247 var x = 0,
248 y = 0,
249 maxh = 0,
250 n = data.length;
251 --di;
252 while (++di < n) {
253 d = data[di];
254 c.save();
255 c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font;
256 const metrics = c.measureText(d.text);
257 const anchor = -Math.floor(metrics.width / 2);
258 let w = (metrics.width + 1) * ratio;
259 let h = d.size << 1;
260 if (d.rotate) {
261 var sr = Math.sin(d.rotate * RADIANS),
262 cr = Math.cos(d.rotate * RADIANS),
263 wcr = w * cr,
264 wsr = w * sr,
265 hcr = h * cr,
266 hsr = h * sr;
267 w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5;
268 h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
269 } else {
270 w = (w + 0x1f) >> 5 << 5;
271 }
272 if (h > maxh) maxh = h;
273 if (x + w >= (cw << 5)) {
274 x = 0;
275 y += maxh;
276 maxh = 0;
277 }
278 if (y + h >= ch) break;
279 c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
280 if (d.rotate) c.rotate(d.rotate * RADIANS);
281 c.fillText(d.text, anchor, 0);
282 if (d.padding) c.lineWidth = 2 * d.padding, c.strokeText(d.text, anchor, 0);
283 c.restore();
284 d.width = w;
285 d.height = h;
286 d.xoff = x;
287 d.yoff = y;
288 d.x1 = w >> 1;
289 d.y1 = h >> 1;
290 d.x0 = -d.x1;
291 d.y0 = -d.y1;
292 d.hasText = true;
293 x += w;
294 }
295 var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data,
296 sprite = [];
297 while (--di >= 0) {
298 d = data[di];

Callers 1

stepFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…