squarified 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,1430 @@
1
+ var _computedKey;
2
+ _computedKey = Symbol.iterator;
3
+ class Iter {
4
+ keys;
5
+ data;
6
+ constructor(data){
7
+ this.data = data;
8
+ this.keys = Object.keys(data);
9
+ }
10
+ // dprint-ignore
11
+ *[_computedKey]() {
12
+ for(let i = 0; i < this.keys.length; i++){
13
+ yield {
14
+ key: this.keys[i],
15
+ value: this.data[this.keys[i]],
16
+ index: i,
17
+ peek: ()=>this.keys[i + 1]
18
+ };
19
+ }
20
+ }
21
+ }
22
+ // For strings we only check the first character to determine if it's a number (I think it's enough)
23
+ function perferNumeric(s) {
24
+ if (typeof s === 'number') return true;
25
+ return s.charCodeAt(0) >= 48 && s.charCodeAt(0) <= 57;
26
+ }
27
+
28
+ const DEG_TO_RAD = Math.PI / 180;
29
+ class Matrix2D {
30
+ a;
31
+ b;
32
+ c;
33
+ d;
34
+ e;
35
+ f;
36
+ constructor(loc = {}){
37
+ this.a = loc.a || 1;
38
+ this.b = loc.b || 0;
39
+ this.c = loc.c || 0;
40
+ this.d = loc.d || 1;
41
+ this.e = loc.e || 0;
42
+ this.f = loc.f || 0;
43
+ }
44
+ create(loc) {
45
+ for (const { key, value } of new Iter(loc)){
46
+ if (Object.hasOwnProperty.call(this, key)) {
47
+ this[key] = value;
48
+ }
49
+ }
50
+ return this;
51
+ }
52
+ transform(x, y, scaleX, scaleY, rotation, skewX, skewY) {
53
+ this.scale(scaleX, scaleY).translation(x, y);
54
+ if (skewX || skewY) {
55
+ this.skew(skewX, skewY);
56
+ } else {
57
+ this.roate(rotation);
58
+ }
59
+ return this;
60
+ }
61
+ translation(x, y) {
62
+ this.e += x;
63
+ this.f += y;
64
+ return this;
65
+ }
66
+ scale(a, d) {
67
+ this.a *= a;
68
+ this.d *= d;
69
+ return this;
70
+ }
71
+ skew(x, y) {
72
+ const tanX = Math.tan(x * DEG_TO_RAD);
73
+ const tanY = Math.tan(y * DEG_TO_RAD);
74
+ const a = this.a + this.b * tanX;
75
+ const b = this.b + this.a * tanY;
76
+ const c = this.c + this.d * tanX;
77
+ const d = this.d + this.c * tanY;
78
+ this.a = a;
79
+ this.b = b;
80
+ this.c = c;
81
+ this.d = d;
82
+ return this;
83
+ }
84
+ roate(rotation) {
85
+ if (rotation > 0) {
86
+ const rad = rotation * DEG_TO_RAD;
87
+ const cosTheta = Math.cos(rad);
88
+ const sinTheta = Math.sin(rad);
89
+ const a = this.a * cosTheta - this.b * sinTheta;
90
+ const b = this.a * sinTheta + this.b * cosTheta;
91
+ const c = this.c * cosTheta - this.d * sinTheta;
92
+ const d = this.c * sinTheta + this.d * cosTheta;
93
+ this.a = a;
94
+ this.b = b;
95
+ this.c = c;
96
+ this.d = d;
97
+ }
98
+ return this;
99
+ }
100
+ }
101
+
102
+ const SELF_ID = {
103
+ id: 0,
104
+ get () {
105
+ return this.id++;
106
+ }
107
+ };
108
+ class Display {
109
+ parent;
110
+ id;
111
+ matrix;
112
+ constructor(){
113
+ this.parent = null;
114
+ this.id = SELF_ID.get();
115
+ this.matrix = new Matrix2D();
116
+ }
117
+ destory() {
118
+ //
119
+ }
120
+ }
121
+ const ASSIGN_MAPPINGS = {
122
+ fillStyle: !0,
123
+ strokeStyle: !0,
124
+ font: !0,
125
+ lineWidth: !0,
126
+ textAlign: !0,
127
+ textBaseline: !0
128
+ };
129
+ function createInstruction() {
130
+ return {
131
+ mods: [],
132
+ fillStyle (...args) {
133
+ this.mods.push([
134
+ 'fillStyle',
135
+ args
136
+ ]);
137
+ },
138
+ fillRect (...args) {
139
+ this.mods.push([
140
+ 'fillRect',
141
+ args
142
+ ]);
143
+ },
144
+ strokeStyle (...args) {
145
+ this.mods.push([
146
+ 'strokeStyle',
147
+ args
148
+ ]);
149
+ },
150
+ lineWidth (...args) {
151
+ this.mods.push([
152
+ 'lineWidth',
153
+ args
154
+ ]);
155
+ },
156
+ strokeRect (...args) {
157
+ this.mods.push([
158
+ 'strokeRect',
159
+ args
160
+ ]);
161
+ },
162
+ fillText (...args) {
163
+ this.mods.push([
164
+ 'fillText',
165
+ args
166
+ ]);
167
+ },
168
+ font (...args) {
169
+ this.mods.push([
170
+ 'font',
171
+ args
172
+ ]);
173
+ },
174
+ textBaseline (...args) {
175
+ this.mods.push([
176
+ 'textBaseline',
177
+ args
178
+ ]);
179
+ },
180
+ textAlign (...args) {
181
+ this.mods.push([
182
+ 'textAlign',
183
+ args
184
+ ]);
185
+ }
186
+ };
187
+ }
188
+ class S extends Display {
189
+ width;
190
+ height;
191
+ x;
192
+ y;
193
+ scaleX;
194
+ scaleY;
195
+ rotation;
196
+ skewX;
197
+ skewY;
198
+ constructor(options = {}){
199
+ super();
200
+ this.width = options.width || 0;
201
+ this.height = options.height || 0;
202
+ this.x = options.x || 0;
203
+ this.y = options.y || 0;
204
+ this.scaleX = options.scaleX || 1;
205
+ this.scaleY = options.scaleY || 1;
206
+ this.rotation = options.rotation || 0;
207
+ this.skewX = options.skewX || 0;
208
+ this.skewY = options.skewY || 0;
209
+ }
210
+ }
211
+ class Graph extends S {
212
+ instruction;
213
+ constructor(options = {}){
214
+ super(options);
215
+ this.instruction = createInstruction();
216
+ }
217
+ render(ctx) {
218
+ this.create();
219
+ this.instruction.mods.forEach((mod)=>{
220
+ const direct = mod[0];
221
+ if (direct in ASSIGN_MAPPINGS) {
222
+ // @ts-expect-error
223
+ ctx[direct] = mod[1];
224
+ return;
225
+ }
226
+ // @ts-expect-error
227
+ ctx[direct].apply(ctx, ...mod.slice(1));
228
+ });
229
+ }
230
+ }
231
+
232
+ class Box extends Display {
233
+ elements;
234
+ constructor(){
235
+ super();
236
+ this.elements = [];
237
+ }
238
+ add(...elements) {
239
+ const cap = elements.length;
240
+ for(let i = 0; i < cap; i++){
241
+ const element = elements[i];
242
+ if (element.parent) ;
243
+ this.elements.push(element);
244
+ element.parent = this;
245
+ }
246
+ }
247
+ remove(...elements) {
248
+ const cap = elements.length;
249
+ for(let i = 0; i < cap; i++){
250
+ for(let j = this.elements.length - 1; j >= 0; j--){
251
+ const element = this.elements[j];
252
+ if (element.id === elements[i].id) {
253
+ this.elements.splice(j, 1);
254
+ element.parent = null;
255
+ }
256
+ }
257
+ }
258
+ }
259
+ destory() {
260
+ this.elements.forEach((element)=>element.parent = null);
261
+ this.elements.length = 0;
262
+ }
263
+ }
264
+
265
+ // Runtime is designed for graph element
266
+ function decodeHLS(meta) {
267
+ const { h, l, s, a } = meta;
268
+ if ('a' in meta) {
269
+ return `hsla(${h}deg, ${s}%, ${l}%, ${a})`;
270
+ }
271
+ return `hsl(${h}deg, ${s}%, ${l}%)`;
272
+ }
273
+ function decodeRGB(meta) {
274
+ const { r, g, b, a } = meta;
275
+ if ('a' in meta) {
276
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
277
+ }
278
+ return `rgb(${r}, ${g}, ${b})`;
279
+ }
280
+ function decodeColor(meta) {
281
+ return meta.mode === 'rgb' ? decodeRGB(meta.desc) : decodeHLS(meta.desc);
282
+ }
283
+ function evaluateFillStyle(primitive, opacity = 1) {
284
+ const descibe = {
285
+ mode: primitive.mode,
286
+ desc: {
287
+ ...primitive.desc,
288
+ a: opacity
289
+ }
290
+ };
291
+ return decodeColor(descibe);
292
+ }
293
+ const runtime = {
294
+ evaluateFillStyle
295
+ };
296
+
297
+ class Rect extends Graph {
298
+ style;
299
+ constructor(options = {}){
300
+ super(options);
301
+ this.style = options.style || Object.create(null);
302
+ }
303
+ create() {
304
+ if (this.style.fill) {
305
+ this.instruction.fillStyle(runtime.evaluateFillStyle(this.style.fill, this.style.opacity));
306
+ this.instruction.fillRect(0, 0, this.width, this.height);
307
+ }
308
+ if (this.style.stroke) {
309
+ this.instruction.strokeStyle(this.style.stroke);
310
+ if (typeof this.style.lineWidth === 'number') {
311
+ this.instruction.lineWidth(this.style.lineWidth);
312
+ }
313
+ this.instruction.strokeRect(0, 0, this.width, this.height);
314
+ }
315
+ }
316
+ }
317
+
318
+ class Text extends Graph {
319
+ text;
320
+ style;
321
+ constructor(options = {}){
322
+ super(options);
323
+ this.text = options.text || '';
324
+ this.style = options.style || Object.create(null);
325
+ }
326
+ create() {
327
+ if (this.style.fill) {
328
+ this.instruction.font(this.style.font);
329
+ this.instruction.lineWidth(this.style.lineWidth);
330
+ this.instruction.textBaseline(this.style.baseline);
331
+ this.instruction.fillStyle(this.style.fill);
332
+ this.instruction.fillText(this.text, 0, 0);
333
+ }
334
+ }
335
+ }
336
+
337
+ class Event {
338
+ eventCollections;
339
+ constructor(){
340
+ this.eventCollections = Object.create(null);
341
+ }
342
+ on(evt, handler, c) {
343
+ if (!(evt in this.eventCollections)) {
344
+ this.eventCollections[evt] = [];
345
+ }
346
+ const data = {
347
+ name: evt,
348
+ handler,
349
+ ctx: c || this
350
+ };
351
+ this.eventCollections[evt].push(data);
352
+ }
353
+ off(evt, handler) {
354
+ if (evt in this.eventCollections) {
355
+ if (!handler) {
356
+ this.eventCollections[evt] = [];
357
+ return;
358
+ }
359
+ this.eventCollections[evt] = this.eventCollections[evt].filter((d)=>d.handler !== handler);
360
+ }
361
+ }
362
+ emit(evt, ...args) {
363
+ if (!this.eventCollections[evt]) return;
364
+ const handlers = this.eventCollections[evt];
365
+ if (handlers.length) {
366
+ handlers.forEach((d)=>{
367
+ d.handler.call(d.ctx, ...args);
368
+ });
369
+ }
370
+ }
371
+ bindWithContext(c) {
372
+ return (evt, handler)=>this.on(evt, handler, c);
373
+ }
374
+ }
375
+
376
+ const NAME_SPACE = 'etoile';
377
+ const log = {
378
+ error: (message)=>{
379
+ return `[${NAME_SPACE}] ${message}`;
380
+ }
381
+ };
382
+
383
+ class Render {
384
+ canvas;
385
+ ctx;
386
+ options;
387
+ constructor(to, options){
388
+ this.canvas = document.createElement('canvas');
389
+ this.ctx = this.canvas.getContext('2d');
390
+ this.options = options;
391
+ this.initOptions(options);
392
+ to.appendChild(this.canvas);
393
+ }
394
+ clear(width, height) {
395
+ this.ctx.clearRect(0, 0, width, height);
396
+ }
397
+ initOptions(userOptions = {}) {
398
+ Object.assign(this.options, userOptions);
399
+ const { options } = this;
400
+ this.canvas.width = options.width * options.devicePixelRatio;
401
+ this.canvas.height = options.height * options.devicePixelRatio;
402
+ this.canvas.style.cssText = `width: ${options.width}px; height: ${options.height}px`;
403
+ }
404
+ update(schedule) {
405
+ this.clear(this.options.width, this.options.height);
406
+ schedule.execute(this);
407
+ }
408
+ destory() {}
409
+ }
410
+
411
+ let Schedule$1 = class Schedule extends Box {
412
+ render;
413
+ to;
414
+ event;
415
+ constructor(to, renderOptions = {}){
416
+ super();
417
+ this.to = typeof to === 'string' ? document.querySelector(to) : to;
418
+ if (!this.to) {
419
+ throw new Error(log.error('The element to bind is not found.'));
420
+ }
421
+ const { width, height } = this.to.getBoundingClientRect();
422
+ Object.assign(renderOptions, {
423
+ width,
424
+ height
425
+ }, {
426
+ devicePixelRatio: window.devicePixelRatio || 1
427
+ });
428
+ this.event = new Event();
429
+ this.render = new Render(this.to, renderOptions);
430
+ }
431
+ applyTransform(matrix) {
432
+ const pixel = this.render.options.devicePixelRatio;
433
+ this.render.ctx.setTransform(matrix.a * pixel, matrix.b * pixel, matrix.c * pixel, matrix.d * pixel, matrix.e * pixel, matrix.f * pixel);
434
+ }
435
+ update() {
436
+ this.render.update(this);
437
+ const matrix = this.matrix.create({
438
+ a: 1,
439
+ b: 0,
440
+ c: 0,
441
+ d: 1,
442
+ e: 0,
443
+ f: 0
444
+ });
445
+ this.applyTransform(matrix);
446
+ }
447
+ // execute all graph elements
448
+ execute(render, graph = this) {
449
+ render.ctx.save();
450
+ let matrix = graph.matrix;
451
+ this.applyTransform(matrix);
452
+ if (graph instanceof Box) {
453
+ const cap = graph.elements.length;
454
+ for(let i = 0; i < cap; i++){
455
+ const element = graph.elements[i];
456
+ matrix = element.matrix.create({
457
+ a: 1,
458
+ b: 0,
459
+ c: 0,
460
+ d: 1,
461
+ e: 0,
462
+ f: 0
463
+ });
464
+ if (element instanceof Graph) {
465
+ matrix.transform(element.x, element.y, element.scaleX, element.scaleY, element.rotation, element.skewX, element.skewY);
466
+ }
467
+ this.execute(render, element);
468
+ }
469
+ }
470
+ if (graph instanceof Graph) {
471
+ graph.render(render.ctx);
472
+ }
473
+ render.ctx.restore();
474
+ }
475
+ };
476
+
477
+ function traverse(graphs, handler) {
478
+ graphs.forEach((graph)=>{
479
+ if (graph instanceof Box) {
480
+ traverse(graph.elements, handler);
481
+ } else if (graph instanceof Graph) {
482
+ handler(graph);
483
+ }
484
+ });
485
+ }
486
+ const etoile = {
487
+ Schedule: Schedule$1,
488
+ traverse
489
+ };
490
+
491
+ // Currently, etoile is an internal module, so we won't need too much easing functions.
492
+ // And the animation logic is implemented by user code.
493
+ const easing = {
494
+ linear: (k)=>k,
495
+ quadraticIn: (k)=>k * k,
496
+ quadraticOut: (k)=>k * (2 - k),
497
+ quadraticInOut: (k)=>{
498
+ if ((k *= 2) < 1) {
499
+ return 0.5 * k * k;
500
+ }
501
+ return -0.5 * (--k * (k - 2) - 1);
502
+ },
503
+ cubicIn: (k)=>k * k * k,
504
+ cubicOut: (k)=>{
505
+ if ((k *= 2) < 1) {
506
+ return 0.5 * k * k * k;
507
+ }
508
+ return 0.5 * ((k -= 2) * k * k + 2);
509
+ },
510
+ cubicInOut: (k)=>{
511
+ if ((k *= 2) < 1) {
512
+ return 0.5 * k * k * k;
513
+ }
514
+ return 0.5 * ((k -= 2) * k * k + 2);
515
+ }
516
+ };
517
+
518
+ function sortChildrenByKey(data, ...keys) {
519
+ return data.sort((a, b)=>{
520
+ for (const key of keys){
521
+ const v = a[key];
522
+ const v2 = b[key];
523
+ if (perferNumeric(v) && perferNumeric(v2)) {
524
+ if (v2 > v) return 1;
525
+ if (v2 < v) return -1;
526
+ continue;
527
+ }
528
+ // Not numeric, compare as string
529
+ const comparison = ('' + v).localeCompare('' + v2);
530
+ if (comparison !== 0) return comparison;
531
+ }
532
+ return 0;
533
+ });
534
+ }
535
+ function c2m(data, key, modifier) {
536
+ if (Array.isArray(data.groups)) {
537
+ data.groups = sortChildrenByKey(data.groups.map((d)=>c2m(d, key, modifier)), 'weight');
538
+ }
539
+ const obj = {
540
+ ...data,
541
+ weight: data[key]
542
+ };
543
+ if (modifier) return modifier(obj);
544
+ return obj;
545
+ }
546
+ function flatten(data) {
547
+ const result = [];
548
+ for(let i = 0; i < data.length; i++){
549
+ const { groups, ...rest } = data[i];
550
+ result.push(rest);
551
+ if (groups) {
552
+ result.push(...flatten(groups));
553
+ }
554
+ }
555
+ return result;
556
+ }
557
+ function bindParentForModule(modules, parent) {
558
+ return modules.map((module)=>{
559
+ const next = {
560
+ ...module
561
+ };
562
+ next.parent = parent;
563
+ if (next.groups && Array.isArray(next.groups)) {
564
+ next.groups = bindParentForModule(next.groups, next);
565
+ }
566
+ return next;
567
+ });
568
+ }
569
+ function getNodeDepth(node) {
570
+ let depth = 0;
571
+ while(node.parent){
572
+ node = node.parent;
573
+ depth++;
574
+ }
575
+ return depth;
576
+ }
577
+ function visit(data, fn) {
578
+ if (!data) return null;
579
+ for (const d of data){
580
+ if (d.children) {
581
+ const result = visit(d.children, fn);
582
+ if (result) return result;
583
+ }
584
+ const stop = fn(d);
585
+ if (stop) return d;
586
+ }
587
+ return null;
588
+ }
589
+ function findRelativeNode(c, p, layoutNodes) {
590
+ return visit(layoutNodes, (node)=>{
591
+ const [x, y, w, h] = node.layout;
592
+ if (p.x >= x && p.y >= y && p.x < x + w && p.y < y + h) {
593
+ return true;
594
+ }
595
+ });
596
+ }
597
+ function findRelativeNodeById(id, layoutNodes) {
598
+ return visit(layoutNodes, (node)=>{
599
+ if (node.node.id === id) {
600
+ return true;
601
+ }
602
+ });
603
+ }
604
+
605
+ function squarify(data, rect, layoutDecorator) {
606
+ const result = [];
607
+ if (!data.length) return result;
608
+ const worst = (start, end, shortestSide, totalWeight, aspectRatio)=>{
609
+ const max = data[start].weight * aspectRatio;
610
+ const min = data[end].weight * aspectRatio;
611
+ return Math.max(shortestSide * shortestSide * max / (totalWeight * totalWeight), totalWeight * totalWeight / (shortestSide * shortestSide * min));
612
+ };
613
+ const recursion = (start, rect)=>{
614
+ while(start < data.length){
615
+ let totalWeight = 0;
616
+ for(let i = start; i < data.length; i++){
617
+ totalWeight += data[i].weight;
618
+ }
619
+ const shortestSide = Math.min(rect.w, rect.h);
620
+ const aspectRatio = rect.w * rect.h / totalWeight;
621
+ let end = start;
622
+ let areaInRun = 0;
623
+ let oldWorst = 0;
624
+ // find the best split
625
+ while(end < data.length){
626
+ const area = data[end].weight * aspectRatio;
627
+ const newWorst = worst(start, end, shortestSide, areaInRun + area, aspectRatio);
628
+ if (end > start && oldWorst < newWorst) break;
629
+ areaInRun += area;
630
+ oldWorst = newWorst;
631
+ end++;
632
+ }
633
+ const splited = Math.round(areaInRun / shortestSide);
634
+ let areaInLayout = 0;
635
+ for(let i = start; i < end; i++){
636
+ const children = data[i];
637
+ const area = children.weight * aspectRatio;
638
+ const lower = Math.round(shortestSide * areaInLayout / areaInRun);
639
+ const upper = Math.round(shortestSide * (areaInLayout + area) / areaInRun);
640
+ const [x, y, w, h] = rect.w >= rect.h ? [
641
+ rect.x,
642
+ rect.y + lower,
643
+ splited,
644
+ upper - lower
645
+ ] : [
646
+ rect.x + lower,
647
+ rect.y,
648
+ upper - lower,
649
+ splited
650
+ ];
651
+ const depth = getNodeDepth(children) || 1;
652
+ const { titleAreaHeight, rectGap } = layoutDecorator;
653
+ const diff = titleAreaHeight.max / depth;
654
+ const hh = diff < titleAreaHeight.min ? titleAreaHeight.min : diff;
655
+ result.push({
656
+ layout: [
657
+ x,
658
+ y,
659
+ w,
660
+ h
661
+ ],
662
+ node: children,
663
+ decorator: {
664
+ ...layoutDecorator,
665
+ titleHeight: hh
666
+ },
667
+ children: w > rectGap * 2 && h > hh + rectGap ? squarify(children.groups || [], {
668
+ x: x + rectGap,
669
+ y: y + hh,
670
+ w: w - rectGap * 2,
671
+ h: h - hh - rectGap
672
+ }, layoutDecorator) : []
673
+ });
674
+ areaInLayout += area;
675
+ }
676
+ start = end;
677
+ if (rect.w >= rect.h) {
678
+ rect.x += splited;
679
+ rect.w -= splited;
680
+ } else {
681
+ rect.y += splited;
682
+ rect.h -= splited;
683
+ }
684
+ }
685
+ };
686
+ recursion(0, rect);
687
+ return result;
688
+ }
689
+
690
+ function applyForOpacity(graph, lastState, nextState, easedProgress) {
691
+ const alpha = lastState + (nextState - lastState) * easedProgress;
692
+ if (graph instanceof Rect) {
693
+ graph.style.opacity = alpha;
694
+ }
695
+ }
696
+
697
+ class RegisterModule {
698
+ static mixin(app, methods) {
699
+ methods.forEach(({ name, fn })=>{
700
+ Object.defineProperty(app, name, {
701
+ value: fn(app),
702
+ writable: false
703
+ });
704
+ });
705
+ }
706
+ }
707
+ function registerModuleForSchedule(mod) {
708
+ if (mod instanceof RegisterModule) {
709
+ return (app, treemap, render)=>mod.init(app, treemap, render);
710
+ }
711
+ throw new Error(log.error('The module is not a valid RegisterScheduleModule.'));
712
+ }
713
+
714
+ // etoile is a simple 2D render engine for web and it don't take complex rendering into account.
715
+ // So it's no need to implement a complex event algorithm or hit mode.
716
+ // If one day etoile need to build as a useful library. Pls rewrite it!
717
+ // All of implementation don't want to consider the compatibility of the browser.
718
+ const primitiveEvents = [
719
+ 'click',
720
+ 'mousedown',
721
+ 'mousemove',
722
+ 'mouseup',
723
+ 'mouseover',
724
+ 'mouseout'
725
+ ];
726
+ function smoothDrawing(c) {
727
+ const { self, treemap } = c;
728
+ const currentNode = self.currentNode;
729
+ if (currentNode) {
730
+ const lloc = new Set();
731
+ visit([
732
+ currentNode
733
+ ], (node)=>{
734
+ const [x, y, w, h] = node.layout;
735
+ const { rectGap, titleHeight } = node.decorator;
736
+ lloc.add(x + '-' + y);
737
+ lloc.add(x + '-' + (y + h - rectGap));
738
+ lloc.add(x + '-' + (y + titleHeight));
739
+ lloc.add(x + w - rectGap + '-' + (y + titleHeight));
740
+ });
741
+ const startTime = Date.now();
742
+ const animationDuration = 300;
743
+ const draw = ()=>{
744
+ if (self.forceDestroy) {
745
+ return;
746
+ }
747
+ const elapsed = Date.now() - startTime;
748
+ const progress = Math.min(elapsed / animationDuration, 1);
749
+ const easedProgress = easing.cubicIn(progress) || 0.1;
750
+ let allTasksCompleted = true;
751
+ treemap.reset();
752
+ etoile.traverse([
753
+ treemap.elements[0]
754
+ ], (graph)=>{
755
+ const key = `${graph.x}-${graph.y}`;
756
+ if (lloc.has(key)) {
757
+ applyForOpacity(graph, 1, 0.7, easedProgress);
758
+ if (progress < 1) {
759
+ allTasksCompleted = false;
760
+ }
761
+ }
762
+ });
763
+ applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
764
+ treemap.update();
765
+ if (!allTasksCompleted) {
766
+ window.requestAnimationFrame(draw);
767
+ }
768
+ };
769
+ if (!self.isAnimating) {
770
+ self.isAnimating = true;
771
+ window.requestAnimationFrame(draw);
772
+ }
773
+ } else {
774
+ treemap.reset();
775
+ applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
776
+ treemap.update();
777
+ }
778
+ }
779
+ function applyZoomEvent(ctx) {
780
+ ctx.treemap.event.on('zoom', (node)=>{
781
+ const root = null;
782
+ if (ctx.self.isDragging) return;
783
+ onZoom(ctx, node, root);
784
+ });
785
+ }
786
+ function getOffset(el) {
787
+ let e = 0;
788
+ let f = 0;
789
+ if (document.documentElement.getBoundingClientRect && el.getBoundingClientRect) {
790
+ const { top, left } = el.getBoundingClientRect();
791
+ e = top;
792
+ f = left;
793
+ } else {
794
+ for(let elt = el; elt; elt = el.offsetParent){
795
+ e += el.offsetLeft;
796
+ f += el.offsetTop;
797
+ }
798
+ }
799
+ return [
800
+ e + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
801
+ f + Math.max(document.documentElement.scrollTop, document.body.scrollTop)
802
+ ];
803
+ }
804
+ function captureBoxXY(c, evt, a, d, translateX, translateY) {
805
+ const boundingClientRect = c.getBoundingClientRect();
806
+ if (evt instanceof MouseEvent) {
807
+ const [e, f] = getOffset(c);
808
+ return {
809
+ x: (evt.clientX - boundingClientRect.left - e - translateX) / a,
810
+ y: (evt.clientY - boundingClientRect.top - f - translateY) / d
811
+ };
812
+ }
813
+ return {
814
+ x: 0,
815
+ y: 0
816
+ };
817
+ }
818
+ function bindPrimitiveEvent(ctx, evt, bus) {
819
+ const { treemap, self } = ctx;
820
+ const c = treemap.render.canvas;
821
+ const handler = (e)=>{
822
+ const { x, y } = captureBoxXY(c, e, self.scaleRatio, self.scaleRatio, self.translateX, self.translateY);
823
+ const event = {
824
+ native: e,
825
+ module: findRelativeNode(c, {
826
+ x,
827
+ y
828
+ }, treemap.layoutNodes)
829
+ };
830
+ // @ts-expect-error
831
+ bus.emit(evt, event);
832
+ };
833
+ c.addEventListener(evt, handler);
834
+ return handler;
835
+ }
836
+ class SelfEvent extends RegisterModule {
837
+ currentNode;
838
+ isAnimating;
839
+ forceDestroy;
840
+ scaleRatio;
841
+ translateX;
842
+ translateY;
843
+ layoutWidth;
844
+ layoutHeight;
845
+ isDragging;
846
+ draggingState;
847
+ event;
848
+ constructor(){
849
+ super();
850
+ this.currentNode = null;
851
+ this.isAnimating = false;
852
+ this.forceDestroy = false;
853
+ this.isDragging = false;
854
+ this.scaleRatio = 1;
855
+ this.translateX = 0;
856
+ this.translateY = 0;
857
+ this.layoutWidth = 0;
858
+ this.layoutHeight = 0;
859
+ this.draggingState = {
860
+ x: 0,
861
+ y: 0
862
+ };
863
+ this.event = new Event();
864
+ }
865
+ ondragstart(metadata) {
866
+ const { native } = metadata;
867
+ if (isScrollWheelOrRightButtonOnMouseupAndDown(native)) {
868
+ return;
869
+ }
870
+ const x = native.offsetX;
871
+ const y = native.offsetY;
872
+ this.self.isDragging = true;
873
+ this.self.draggingState = {
874
+ x,
875
+ y
876
+ };
877
+ }
878
+ ondragmove(metadata) {
879
+ if (!this.self.isDragging) {
880
+ if ('zoom' in this.treemap.event.eventCollections) {
881
+ const condit = this.treemap.event.eventCollections.zoom.length > 0;
882
+ if (!condit) {
883
+ applyZoomEvent(this);
884
+ }
885
+ }
886
+ return;
887
+ }
888
+ // @ts-expect-error
889
+ this.self.event.off('mousemove', this.self.onmousemove);
890
+ this.treemap.event.off('zoom');
891
+ this.self.forceDestroy = true;
892
+ const { native } = metadata;
893
+ const x = native.offsetX;
894
+ const y = native.offsetY;
895
+ const { x: lastX, y: lastY } = this.self.draggingState;
896
+ const drawX = x - lastX;
897
+ const drawY = y - lastY;
898
+ this.self.translateX += drawX;
899
+ this.self.translateY += drawY;
900
+ this.self.draggingState = {
901
+ x,
902
+ y
903
+ };
904
+ this.treemap.reset();
905
+ applyGraphTransform(this.treemap.elements, this.self.translateX, this.self.translateY, this.self.scaleRatio);
906
+ this.treemap.update();
907
+ }
908
+ ondragend(metadata) {
909
+ if (!this.self.isDragging) {
910
+ return;
911
+ }
912
+ this.self.isDragging = false;
913
+ this.self.draggingState = {
914
+ x: 0,
915
+ y: 0
916
+ };
917
+ this.self.event.bindWithContext(this)('mousemove', this.self.onmousemove);
918
+ }
919
+ onmousemove(metadata) {
920
+ const { self } = this;
921
+ if (self.isDragging) {
922
+ return;
923
+ }
924
+ const { module: node } = metadata;
925
+ self.forceDestroy = false;
926
+ if (self.currentNode !== node) {
927
+ self.currentNode = node;
928
+ self.isAnimating = false;
929
+ }
930
+ smoothDrawing(this);
931
+ }
932
+ onmouseout() {
933
+ const { self } = this;
934
+ self.currentNode = null;
935
+ self.forceDestroy = true;
936
+ self.isDragging = false;
937
+ smoothDrawing(this);
938
+ }
939
+ onwheel(metadata) {
940
+ const { self, treemap } = this;
941
+ // @ts-expect-error
942
+ const wheelDelta = metadata.native.wheelDelta;
943
+ const absWheelDelta = Math.abs(wheelDelta);
944
+ const offsetX = metadata.native.offsetX;
945
+ const offsetY = metadata.native.offsetY;
946
+ if (wheelDelta === 0) {
947
+ return;
948
+ }
949
+ self.forceDestroy = true;
950
+ self.isAnimating = true;
951
+ treemap.reset();
952
+ const factor = absWheelDelta > 3 ? 1.4 : absWheelDelta > 1 ? 1.2 : 1.1;
953
+ const delta = wheelDelta > 0 ? factor : 1 / factor;
954
+ self.scaleRatio *= delta;
955
+ const translateX = offsetX - (offsetX - self.translateX) * delta;
956
+ const translateY = offsetY - (offsetY - self.translateY) * delta;
957
+ self.translateX = translateX;
958
+ self.translateY = translateY;
959
+ applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
960
+ treemap.update();
961
+ self.forceDestroy = false;
962
+ self.isAnimating = false;
963
+ }
964
+ init(app, treemap, render) {
965
+ const event = this.event;
966
+ const nativeEvents = [];
967
+ const methods = [
968
+ {
969
+ name: 'on',
970
+ fn: ()=>event.bindWithContext(treemap.api).bind(event)
971
+ },
972
+ {
973
+ name: 'off',
974
+ fn: ()=>event.off.bind(event)
975
+ },
976
+ {
977
+ name: 'emit',
978
+ fn: ()=>event.emit.bind(event)
979
+ }
980
+ ];
981
+ RegisterModule.mixin(app, methods);
982
+ const selfEvents = [
983
+ ...primitiveEvents,
984
+ 'wheel'
985
+ ];
986
+ selfEvents.forEach((evt)=>{
987
+ nativeEvents.push(bindPrimitiveEvent({
988
+ treemap,
989
+ self: this
990
+ }, evt, event));
991
+ });
992
+ const selfEvt = event.bindWithContext({
993
+ treemap,
994
+ self: this
995
+ });
996
+ selfEvt('mousedown', this.ondragstart);
997
+ selfEvt('mousemove', this.ondragmove);
998
+ selfEvt('mouseup', this.ondragend);
999
+ // highlight
1000
+ selfEvt('mousemove', this.onmousemove);
1001
+ selfEvt('mouseout', this.onmouseout);
1002
+ // wheel
1003
+ selfEvt('wheel', this.onwheel);
1004
+ applyZoomEvent({
1005
+ treemap,
1006
+ self: this
1007
+ });
1008
+ treemap.event.on('cleanup:selfevent', ()=>{
1009
+ this.currentNode = null;
1010
+ this.isAnimating = false;
1011
+ this.scaleRatio = 1;
1012
+ this.translateX = 0;
1013
+ this.translateY = 0;
1014
+ this.layoutWidth = treemap.render.canvas.width;
1015
+ this.layoutHeight = treemap.render.canvas.height;
1016
+ this.isDragging = false;
1017
+ this.draggingState = {
1018
+ x: 0,
1019
+ y: 0
1020
+ };
1021
+ });
1022
+ }
1023
+ }
1024
+ function estimateZoomingArea(node, root, w, h) {
1025
+ const defaultSizes = [
1026
+ w,
1027
+ h,
1028
+ 1
1029
+ ];
1030
+ if (root === node) {
1031
+ return defaultSizes;
1032
+ }
1033
+ const viewArea = w * h;
1034
+ let area = viewArea;
1035
+ let parent = node.node.parent;
1036
+ let totalWeight = node.node.weight;
1037
+ while(parent){
1038
+ const siblings = parent.groups || [];
1039
+ let siblingWeightSum = 0;
1040
+ for (const sibling of siblings){
1041
+ siblingWeightSum += sibling.weight;
1042
+ }
1043
+ area *= siblingWeightSum / totalWeight;
1044
+ totalWeight = parent.weight;
1045
+ parent = parent.parent;
1046
+ }
1047
+ const maxScaleFactor = 2.5;
1048
+ const minScaleFactor = 0.3;
1049
+ const scaleFactor = Math.max(minScaleFactor, Math.min(maxScaleFactor, Math.sqrt(area / viewArea)));
1050
+ return [
1051
+ w * scaleFactor,
1052
+ h * scaleFactor
1053
+ ];
1054
+ }
1055
+ function applyGraphTransform(graphs, translateX, translateY, scale) {
1056
+ etoile.traverse(graphs, (graph)=>{
1057
+ graph.x = graph.x * scale + translateX;
1058
+ graph.y = graph.y * scale + translateY;
1059
+ graph.scaleX = scale;
1060
+ graph.scaleY = scale;
1061
+ });
1062
+ }
1063
+ function onZoom(ctx, node, root) {
1064
+ if (!node) return;
1065
+ const { treemap, self } = ctx;
1066
+ let isAnimating = false;
1067
+ const c = treemap.render.canvas;
1068
+ const boundingClientRect = c.getBoundingClientRect();
1069
+ const [w, h] = estimateZoomingArea(node, root, boundingClientRect.width, boundingClientRect.height);
1070
+ resetLayout(treemap, w, h);
1071
+ const module = findRelativeNodeById(node.node.id, treemap.layoutNodes);
1072
+ if (module) {
1073
+ const [mx, my, mw, mh] = module.layout;
1074
+ const scale = Math.min(boundingClientRect.width / mw, boundingClientRect.height / mh);
1075
+ const translateX = boundingClientRect.width / 2 - (mx + mw / 2) * scale;
1076
+ const translateY = boundingClientRect.height / 2 - (my + mh / 2) * scale;
1077
+ const initialScale = self.scaleRatio;
1078
+ const initialTranslateX = self.translateX;
1079
+ const initialTranslateY = self.translateY;
1080
+ const startTime = Date.now();
1081
+ const animationDuration = 300;
1082
+ const draw = ()=>{
1083
+ if (self.forceDestroy) {
1084
+ return;
1085
+ }
1086
+ const elapsed = Date.now() - startTime;
1087
+ const progress = Math.min(elapsed / animationDuration, 1);
1088
+ const easedProgress = easing.cubicInOut(progress);
1089
+ const scaleRatio = initialScale + (scale - initialScale) * easedProgress;
1090
+ self.translateX = initialTranslateX + (translateX - initialTranslateX) * easedProgress;
1091
+ self.translateY = initialTranslateY + (translateY - initialTranslateY) * easedProgress;
1092
+ self.scaleRatio = scaleRatio;
1093
+ treemap.reset();
1094
+ applyGraphTransform(treemap.elements, self.translateX, self.translateY, scaleRatio);
1095
+ treemap.update();
1096
+ if (progress < 1) {
1097
+ window.requestAnimationFrame(draw);
1098
+ } else {
1099
+ self.layoutWidth = w;
1100
+ self.layoutHeight = h;
1101
+ isAnimating = false;
1102
+ }
1103
+ };
1104
+ if (!isAnimating) {
1105
+ isAnimating = true;
1106
+ window.requestAnimationFrame(draw);
1107
+ }
1108
+ }
1109
+ root = node;
1110
+ }
1111
+ // Only works for mouseup and mousedown events
1112
+ function isScrollWheelOrRightButtonOnMouseupAndDown(e) {
1113
+ return e.which === 2 || e.which === 3;
1114
+ }
1115
+
1116
+ const defaultRegistries = [
1117
+ registerModuleForSchedule(new SelfEvent())
1118
+ ];
1119
+ function charCodeWidth(c, ch) {
1120
+ return c.measureText(String.fromCharCode(ch)).width;
1121
+ }
1122
+ function evaluateOptimalFontSize(c, text, width, fontRange, fontFamily, height) {
1123
+ height = Math.floor(height);
1124
+ let optimalFontSize = fontRange.min;
1125
+ for(let fontSize = fontRange.min; fontSize <= fontRange.max; fontSize++){
1126
+ c.font = `${fontSize}px ${fontFamily}`;
1127
+ let textWidth = 0;
1128
+ const textHeight = fontSize;
1129
+ let i = 0;
1130
+ while(i < text.length){
1131
+ const codePointWidth = charCodeWidth(c, text.charCodeAt(i));
1132
+ textWidth += codePointWidth;
1133
+ i++;
1134
+ }
1135
+ if (textWidth >= width) {
1136
+ const overflow = textWidth - width;
1137
+ const ratio = overflow / textWidth;
1138
+ const newFontSize = Math.abs(Math.floor(fontSize - fontSize * ratio));
1139
+ optimalFontSize = newFontSize || fontRange.min;
1140
+ break;
1141
+ }
1142
+ if (textHeight >= height) {
1143
+ const overflow = textHeight - height;
1144
+ const ratio = overflow / textHeight;
1145
+ const newFontSize = Math.abs(Math.floor(fontSize - fontSize * ratio));
1146
+ optimalFontSize = newFontSize || fontRange.min;
1147
+ break;
1148
+ }
1149
+ optimalFontSize = fontSize;
1150
+ }
1151
+ return optimalFontSize;
1152
+ }
1153
+ function getSafeText(c, text, width) {
1154
+ const ellipsisWidth = c.measureText('...').width;
1155
+ if (width < ellipsisWidth) {
1156
+ return false;
1157
+ }
1158
+ let textWidth = 0;
1159
+ let i = 0;
1160
+ while(i < text.length){
1161
+ const codePointWidth = charCodeWidth(c, text.charCodeAt(i));
1162
+ textWidth += codePointWidth;
1163
+ i++;
1164
+ }
1165
+ if (textWidth < width) {
1166
+ return {
1167
+ text,
1168
+ width: textWidth
1169
+ };
1170
+ }
1171
+ return {
1172
+ text: '...',
1173
+ width: ellipsisWidth
1174
+ };
1175
+ }
1176
+ function createFillBlock(color, x, y, width, height) {
1177
+ return new Rect({
1178
+ width,
1179
+ height,
1180
+ x,
1181
+ y,
1182
+ style: {
1183
+ fill: color,
1184
+ opacity: 1
1185
+ }
1186
+ });
1187
+ }
1188
+ function createTitleText(text, x, y, font, color) {
1189
+ return new Text({
1190
+ text,
1191
+ x,
1192
+ y,
1193
+ style: {
1194
+ fill: color,
1195
+ textAlign: 'center',
1196
+ baseline: 'middle',
1197
+ font,
1198
+ lineWidth: 1
1199
+ }
1200
+ });
1201
+ }
1202
+ function resetLayout(treemap, w, h) {
1203
+ treemap.layoutNodes = squarify(treemap.data, {
1204
+ w,
1205
+ h,
1206
+ x: 0,
1207
+ y: 0
1208
+ }, treemap.decorator.layout);
1209
+ treemap.reset();
1210
+ }
1211
+ // https://www.typescriptlang.org/docs/handbook/mixins.html
1212
+ class Schedule extends etoile.Schedule {
1213
+ }
1214
+ class TreemapLayout extends Schedule {
1215
+ data;
1216
+ layoutNodes;
1217
+ decorator;
1218
+ bgBox;
1219
+ fgBox;
1220
+ constructor(...args){
1221
+ super(...args);
1222
+ this.data = [];
1223
+ this.layoutNodes = [];
1224
+ this.bgBox = new Box();
1225
+ this.fgBox = new Box();
1226
+ this.decorator = Object.create(null);
1227
+ }
1228
+ drawBackgroundNode(node) {
1229
+ for (const child of node.children){
1230
+ this.drawBackgroundNode(child);
1231
+ }
1232
+ const [x, y, w, h] = node.layout;
1233
+ const { rectGap, titleHeight } = node.decorator;
1234
+ const fill = this.decorator.color.mappings[node.node.id];
1235
+ if (node.children.length) {
1236
+ const box = new Box();
1237
+ box.add(createFillBlock(fill, x, y, w, titleHeight), createFillBlock(fill, x, y + h - rectGap, w, rectGap), createFillBlock(fill, x, y + titleHeight, rectGap, h - titleHeight - rectGap), createFillBlock(fill, x + w - rectGap, y + titleHeight, rectGap, h - titleHeight - rectGap));
1238
+ this.bgBox.add(box);
1239
+ } else {
1240
+ this.bgBox.add(createFillBlock(fill, x, y, w, h));
1241
+ }
1242
+ }
1243
+ drawForegroundNode(node) {
1244
+ for (const child of node.children){
1245
+ this.drawForegroundNode(child);
1246
+ }
1247
+ const [x, y, w, h] = node.layout;
1248
+ const { rectBorderWidth, titleHeight, rectGap } = node.decorator;
1249
+ const { fontSize, fontFamily, color } = this.decorator.font;
1250
+ const rect = new Rect({
1251
+ x: x + 0.5,
1252
+ y: y + 0.5,
1253
+ width: w,
1254
+ height: h,
1255
+ style: {
1256
+ stroke: '#222',
1257
+ lineWidth: rectBorderWidth
1258
+ }
1259
+ });
1260
+ this.fgBox.add(rect);
1261
+ this.render.ctx.textBaseline = 'middle';
1262
+ const optimalFontSize = evaluateOptimalFontSize(this.render.ctx, node.node.label, w - rectGap * 2, fontSize, fontFamily, node.children.length ? Math.round(titleHeight / 2) + rectGap : h);
1263
+ this.render.ctx.font = `${optimalFontSize}px ${fontFamily}`;
1264
+ if (h > titleHeight) {
1265
+ const result = getSafeText(this.render.ctx, node.node.label, w - rectGap * 2);
1266
+ if (!result) return;
1267
+ const { text, width } = result;
1268
+ const textX = x + Math.round((w - width) / 2);
1269
+ let textY = y + Math.round(h / 2);
1270
+ if (node.children.length) {
1271
+ textY = y + Math.round(titleHeight / 2);
1272
+ }
1273
+ this.fgBox.add(createTitleText(text, textX, textY, `${optimalFontSize}px ${fontFamily}`, color));
1274
+ } else {
1275
+ const ellipsisWidth = 3 * charCodeWidth(this.render.ctx, 46);
1276
+ const textX = x + Math.round((w - ellipsisWidth) / 2);
1277
+ const textY = y + Math.round(h / 2);
1278
+ this.fgBox.add(createTitleText('...', textX, textY, `${optimalFontSize}px ${fontFamily}`, color));
1279
+ }
1280
+ }
1281
+ reset() {
1282
+ this.bgBox.destory();
1283
+ this.fgBox.destory();
1284
+ this.remove(this.bgBox, this.fgBox);
1285
+ for (const node of this.layoutNodes){
1286
+ this.drawBackgroundNode(node);
1287
+ this.drawForegroundNode(node);
1288
+ }
1289
+ this.add(this.bgBox, this.fgBox);
1290
+ }
1291
+ get api() {
1292
+ return {
1293
+ zoom: (node)=>{
1294
+ this.event.emit('zoom', node);
1295
+ }
1296
+ };
1297
+ }
1298
+ }
1299
+ function createTreemap() {
1300
+ let treemap = null;
1301
+ let root = null;
1302
+ const uses = [];
1303
+ const context = {
1304
+ init,
1305
+ dispose,
1306
+ setOptions,
1307
+ resize,
1308
+ use
1309
+ };
1310
+ function init(el) {
1311
+ treemap = new TreemapLayout(el);
1312
+ root = el;
1313
+ }
1314
+ function dispose() {
1315
+ if (root && treemap) {
1316
+ treemap.destory();
1317
+ root.removeChild(root.firstChild);
1318
+ root = null;
1319
+ treemap = null;
1320
+ }
1321
+ }
1322
+ function resize() {
1323
+ if (!treemap || !root) return;
1324
+ const { width, height } = root.getBoundingClientRect();
1325
+ treemap.render.initOptions({
1326
+ height,
1327
+ width,
1328
+ devicePixelRatio: window.devicePixelRatio
1329
+ });
1330
+ treemap.event.emit('cleanup:selfevent');
1331
+ resetLayout(treemap, width, height);
1332
+ treemap.update();
1333
+ }
1334
+ function setOptions(options) {
1335
+ if (!treemap) {
1336
+ throw new Error('Treemap not initialized');
1337
+ }
1338
+ treemap.data = bindParentForModule(options.data || []);
1339
+ for (const registry of defaultRegistries){
1340
+ registry(context, treemap, treemap.render);
1341
+ }
1342
+ for (const use of uses){
1343
+ use(treemap);
1344
+ }
1345
+ resize();
1346
+ }
1347
+ function use(key, register) {
1348
+ switch(key){
1349
+ case 'decorator':
1350
+ uses.push((treemap)=>register(treemap));
1351
+ break;
1352
+ }
1353
+ }
1354
+ return context;
1355
+ }
1356
+
1357
+ const defaultLayoutOptions = {
1358
+ titleAreaHeight: {
1359
+ max: 80,
1360
+ min: 20
1361
+ },
1362
+ rectGap: 5,
1363
+ rectBorderRadius: 0.5,
1364
+ rectBorderWidth: 1.5
1365
+ };
1366
+ const defaultFontOptions = {
1367
+ color: '#000',
1368
+ fontSize: {
1369
+ max: 38,
1370
+ min: 7
1371
+ },
1372
+ fontFamily: 'sans-serif'
1373
+ };
1374
+ function presetDecorator(app) {
1375
+ Object.assign(app.decorator, {
1376
+ layout: defaultLayoutOptions,
1377
+ font: defaultFontOptions,
1378
+ color: colorMappings(app)
1379
+ });
1380
+ }
1381
+ function colorDecorator(node, state) {
1382
+ const depth = getNodeDepth(node);
1383
+ let baseHue = 0;
1384
+ let sweepAngle = Math.PI * 2;
1385
+ const totalHueRange = Math.PI;
1386
+ if (node.parent) {
1387
+ sweepAngle = node.weight / node.parent.weight * sweepAngle;
1388
+ baseHue = state.hue + sweepAngle / Math.PI * 180;
1389
+ }
1390
+ baseHue += sweepAngle;
1391
+ const depthHueOffset = depth + totalHueRange / 10;
1392
+ const finalHue = baseHue + depthHueOffset / 2;
1393
+ const saturation = 0.6 + 0.4 * Math.max(0, Math.cos(finalHue));
1394
+ const lightness = 0.5 + 0.2 * Math.max(0, Math.cos(finalHue + Math.PI * 2 / 3));
1395
+ state.hue = baseHue;
1396
+ return {
1397
+ mode: 'hsl',
1398
+ desc: {
1399
+ h: finalHue,
1400
+ s: Math.round(saturation * 100),
1401
+ l: Math.round(lightness * 100)
1402
+ }
1403
+ };
1404
+ }
1405
+ function evaluateColorMappingByNode(node, state) {
1406
+ const colorMappings = {};
1407
+ if (node.groups && Array.isArray(node.groups)) {
1408
+ for (const child of node.groups){
1409
+ Object.assign(colorMappings, evaluateColorMappingByNode(child, state));
1410
+ }
1411
+ }
1412
+ if (node.id) {
1413
+ colorMappings[node.id] = colorDecorator(node, state);
1414
+ }
1415
+ return colorMappings;
1416
+ }
1417
+ function colorMappings(app) {
1418
+ const colorMappings = {};
1419
+ const state = {
1420
+ hue: 0
1421
+ };
1422
+ for (const node of app.data){
1423
+ Object.assign(colorMappings, evaluateColorMappingByNode(node, state));
1424
+ }
1425
+ return {
1426
+ mappings: colorMappings
1427
+ };
1428
+ }
1429
+
1430
+ export { c2m, createTreemap, defaultFontOptions, defaultLayoutOptions, flatten as flattenModule, presetDecorator, sortChildrenByKey };