-
Notifications
You must be signed in to change notification settings - Fork 16
/
visualization.js
959 lines (846 loc) · 34.1 KB
/
visualization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
class HubAndSpokeVisualization {
constructor() {
this.svg = null;
this.width = 0;
this.height = 0;
this.centerX = 0;
this.centerY = 0;
this.hubRadius = 10;
this.userRadius = 5;
this.dotRadius = 3;
this.positionVariation = 0;
this.hubPositionVariation = 0;
this.motionTime = 1000;
this.hubTime = 500;
this.transactionRate = 2;
this.seed = Math.random();
this.isRunning = false;
this.transactionInterval = null;
this.transactionCounter = 0;
this.rebalanceRate = 1;
this.broadcastStyle = 'ripple';
this.depositories = [];
this.pendingL1Transactions = new Map();
this.rebalanceInterval = null;
this.onchainTxSize = 8;
this.numDepositories = 3;
this.depositoryPosition = 'top';
this.blockCounter = 0;
this.blockSize = 12;
this.blockPadding = 2;
this.blockchains = new Map();
this.depositorySpots = new Map();
this.blockCounters = new Map();
this.l2TPS = 1.0; // Default L2 TPS (formerly transactionRate)
this.l1TPS = 1.0; // Default L1 TPS (formerly rebalanceRate)
this.colorSchemes = {
'anthropic': {
hub: '#1A1A1A',
user: '#2D2D2D',
transaction: '#FFFFFF',
onchainTx: '#FFD700',
depository: '#666666',
ripple: '#FFD700'
},
'modern': {
hub: '#0066FF',
user: '#4D4D4D',
transaction: '#FFFFFF',
onchainTx: '#00CC99',
depository: '#333333',
ripple: '#00CC99'
},
'classic': {
hub: '#FFD700',
user: '#4169E1',
transaction: '#FFFFFF',
onchainTx: '#FFD700',
depository: '#666666',
ripple: '#FFD700'
}
};
this.currentColorScheme = 'anthropic';
this.loadStateFromHash();
this.init();
}
loadStateFromHash() {
try {
if (location.hash) {
const state = JSON.parse(decodeURIComponent(location.hash.slice(1)));
// Load TPS values
if (state.l2TPS !== undefined) {
this.l2TPS = state.l2TPS;
const l2Control = document.getElementById('l2TPSControl');
const l2Value = document.getElementById('l2TPSValue');
if (l2Control) l2Control.value = this.l2TPS;
if (l2Value) l2Value.textContent = this.l2TPS;
}
if (state.l1TPS !== undefined) {
this.l1TPS = state.l1TPS;
const l1Control = document.getElementById('l1TPSControl');
const l1Value = document.getElementById('l1TPSValue');
if (l1Control) l1Control.value = this.l1TPS;
if (l1Value) l1Value.textContent = this.l1TPS;
}
// ... rest of state loading ...
}
} catch (e) {
console.error('Failed to load state from hash:', e);
}
}
saveStateToHash() {
const state = {
hubRadius: this.hubRadius,
userRadius: this.userRadius,
dotRadius: this.dotRadius,
positionVariation: this.positionVariation,
hubPositionVariation: this.hubPositionVariation,
motionTime: this.motionTime,
hubTime: this.hubTime,
l2TPS: this.l2TPS,
l1TPS: this.l1TPS,
broadcastStyle: this.broadcastStyle,
colorScheme: this.currentColorScheme,
onchainTxSize: this.onchainTxSize,
numDepositories: this.numDepositories,
depositoryPosition: this.depositoryPosition,
blockSize: this.blockSize,
numHubs: parseInt(document.getElementById('hubsControl').value),
numUsers: parseInt(document.getElementById('usersControl').value)
};
location.hash = encodeURIComponent(JSON.stringify(state));
}
getDepositoryPositions() {
const positions = [];
const spacing = 150;
const numDepositories = this.numDepositories;
switch (this.depositoryPosition) {
case 'top':
for (let i = 0; i < numDepositories; i++) {
positions.push({
x: this.width/2 + (i - (numDepositories-1)/2) * spacing,
y: 100
});
}
break;
case 'bottom':
for (let i = 0; i < numDepositories; i++) {
positions.push({
x: this.width/2 + (i - (numDepositories-1)/2) * spacing,
y: this.height - 100
});
}
break;
case 'left':
for (let i = 0; i < numDepositories; i++) {
positions.push({
x: 100,
y: this.height/2 + (i - (numDepositories-1)/2) * spacing
});
}
break;
case 'right':
for (let i = 0; i < numDepositories; i++) {
positions.push({
x: this.width - 100,
y: this.height/2 + (i - (numDepositories-1)/2) * spacing
});
}
break;
}
return positions;
}
init() {
// Get container dimensions
const container = document.getElementById('visualization');
if (!container) {
console.error('Visualization container not found');
return;
}
this.width = container.clientWidth;
this.height = container.clientHeight;
this.centerX = this.width / 2;
this.centerY = this.height / 2;
// Ensure all controls exist before trying to set their values
const requiredControls = [
'hubsControl', 'hubsValue',
'usersControl', 'usersValue'
// Remove blockSize from required controls for now
];
const missingControls = requiredControls.filter(id => !document.getElementById(id));
if (missingControls.length > 0) {
console.error('Missing controls:', missingControls);
return;
}
// Set initial values for controls with proper defaults
const state = location.hash ?
JSON.parse(decodeURIComponent(location.hash.slice(1))) :
{ numHubs: 3, numUsers: 20 }; // Ensure default values
// Update all control values with proper defaults
document.getElementById('hubsControl').value = state.numHubs ?? 3;
document.getElementById('hubsValue').textContent = state.numHubs ?? 3;
document.getElementById('usersControl').value = state.numUsers ?? 20;
document.getElementById('usersValue').textContent = state.numUsers ?? 20;
// Optional block size control initialization
const blockSizeControl = document.getElementById('blockSizeControl');
const blockSizeValue = document.getElementById('blockSizeValue');
if (blockSizeControl && blockSizeValue) {
blockSizeControl.value = this.blockSize;
blockSizeValue.textContent = this.blockSize;
}
// Initial render
this.updateVisualization();
// Initialize controls
this.initializeControls();
// Add transaction control
const transactionButton = document.getElementById('transactionControl');
if (transactionButton) {
transactionButton.addEventListener('click', () => this.toggleTransactions());
}
// Start transactions after a small delay
setTimeout(() => {
this.toggleTransactions();
this.startRebalanceTransactions();
}, 100);
// Add legend
this.createLegend();
}
initializeControls() {
// Update transaction rate slider limits
const rateControl = document.getElementById('rebalanceRateControl');
if (rateControl) {
rateControl.min = "0.1";
rateControl.max = "100";
rateControl.step = "0.1";
}
// Add depository position control
const positionControl = document.getElementById('depositoryPositionControl');
if (positionControl) {
positionControl.addEventListener('change', (e) => {
this.depositoryPosition = e.target.value;
// Clear existing transactions
this.svg.selectAll('.rebalance').remove();
this.depositories.forEach(d => {
this.depositorySpots.get(d.id).clear();
d.pendingTx = 0;
});
this.updateVisualization();
this.saveStateToHash();
});
}
// Update values display and handle changes
const controls = {
'motionTime': (val) => this.motionTime = parseInt(val),
'hubTime': (val) => this.hubTime = parseInt(val),
'rate': (val) => {
this.transactionRate = parseFloat(val);
if (this.isRunning) {
this.startTransactions();
}
},
'rebalanceRate': (val) => {
this.rebalanceRate = parseFloat(val);
if (this.isRunning) {
this.startRebalanceTransactions();
}
},
'hubs': (val) => {
const numHubs = parseInt(val);
document.getElementById('hubsValue').textContent = numHubs;
this.updateVisualization();
},
'users': (val) => {
const numUsers = parseInt(val);
document.getElementById('usersValue').textContent = numUsers;
this.updateVisualization();
},
'variation': (val) => {
this.positionVariation = parseInt(val);
this.updateVisualization();
},
'hubVariation': (val) => {
this.hubPositionVariation = parseInt(val);
this.updateVisualization();
},
'hubSize': (val) => {
this.hubRadius = parseInt(val);
this.updateVisualization();
},
'userSize': (val) => {
this.userRadius = parseInt(val);
this.updateVisualization();
},
'dotSize': (val) => {
this.dotRadius = parseInt(val);
this.updateVisualization();
},
'blockSize': (val) => {
this.blockSize = parseInt(val);
// Reset depositories when block size changes
this.depositories.forEach(d => {
this.depositorySpots.get(d.id).clear();
d.pendingTx = 0;
});
// Clear all pending transactions
this.svg.selectAll('.rebalance').remove();
this.updateVisualization();
}
};
// Add listeners for all controls
Object.keys(controls).forEach(control => {
const slider = document.getElementById(`${control}Control`);
const value = document.getElementById(`${control}Value`);
if (slider && value) {
slider.addEventListener('input', (e) => {
value.textContent = e.target.value;
controls[control](e.target.value);
this.saveStateToHash();
});
}
});
// Add broadcast style control listener
const broadcastStyleControl = document.getElementById('broadcastStyleControl');
if (broadcastStyleControl) {
broadcastStyleControl.addEventListener('change', (e) => {
this.broadcastStyle = e.target.value;
this.saveStateToHash();
});
}
// Add specific handler for rebalance rate
const rebalanceControl = document.getElementById('rebalanceRateControl');
if (rebalanceControl) {
rebalanceControl.addEventListener('input', (e) => {
this.rebalanceRate = parseFloat(e.target.value);
document.getElementById('rebalanceRateValue').textContent = this.rebalanceRate;
// Restart with new rate if running
if (this.isRunning) {
this.startRebalanceTransactions();
}
this.saveStateToHash();
});
}
// Add specific handlers for depository and onchain tx controls
const depositoryControl = document.getElementById('depositoriesControl');
if (depositoryControl) {
depositoryControl.addEventListener('input', (e) => {
this.numDepositories = parseInt(e.target.value);
document.getElementById('depositoriesValue').textContent = this.numDepositories;
this.updateVisualization();
this.saveStateToHash();
});
}
const onchainSizeControl = document.getElementById('onchainSizeControl');
if (onchainSizeControl) {
onchainSizeControl.addEventListener('input', (e) => {
this.onchainTxSize = parseInt(e.target.value);
document.getElementById('onchainSizeValue').textContent = this.onchainTxSize;
// Clear existing transactions and update visualization
this.svg.selectAll('.rebalance').remove();
this.depositories.forEach(d => {
this.depositorySpots.get(d.id).clear();
d.pendingTx = 0;
});
this.updateVisualization();
this.saveStateToHash();
});
}
// Add color scheme control
const colorSchemeControl = document.getElementById('colorSchemeControl');
if (colorSchemeControl) {
colorSchemeControl.addEventListener('change', (e) => {
this.currentColorScheme = e.target.value;
this.updateVisualization();
this.saveStateToHash();
});
}
// Update TPS controls
const l2TPSControl = document.getElementById('l2TPSControl');
if (l2TPSControl) {
l2TPSControl.addEventListener('input', (e) => {
this.l2TPS = parseFloat(e.target.value);
document.getElementById('l2TPSValue').textContent = this.l2TPS;
if (this.isRunning) {
this.startTransactions();
}
this.saveStateToHash();
});
}
const l1TPSControl = document.getElementById('l1TPSControl');
if (l1TPSControl) {
l1TPSControl.addEventListener('input', (e) => {
this.l1TPS = parseFloat(e.target.value);
document.getElementById('l1TPSValue').textContent = this.l1TPS;
if (this.isRunning) {
this.startRebalanceTransactions();
}
this.saveStateToHash();
});
}
}
updateVisualization() {
// First, completely clear the visualization container
d3.select('#visualization').selectAll('svg').remove();
// Create new SVG
this.svg = d3.select('#visualization')
.append('svg')
.attr('width', this.width)
.attr('height', this.height);
const numHubs = parseInt(document.getElementById('hubsControl').value);
const numUsers = parseInt(document.getElementById('usersControl').value);
// Create hubs
this.hubs = Array.from({length: numHubs}, (_, i) => {
const angle = 2 * Math.PI * i / numHubs;
const variation = this.hubPositionVariation;
const distance = 100 + (Math.random() - 0.5) * variation;
return {
id: i,
isHub: true,
x: this.centerX + distance * Math.cos(angle),
y: this.centerY + distance * Math.sin(angle)
};
});
// Create users with position variation
this.users = Array.from({length: numUsers}, (_, i) => {
const angle = 2 * Math.PI * i / numUsers;
const variation = this.positionVariation;
const distance = 200 + (Math.random() - 0.5) * variation;
return {
id: i,
isHub: false,
x: this.centerX + distance * Math.cos(angle),
y: this.centerY + distance * Math.sin(angle)
};
});
// Draw connections
this.drawConnections();
// Draw hubs
this.svg.selectAll('.hub')
.data(this.hubs)
.join('circle')
.attr('class', 'hub')
.attr('cx', d => d.x)
.attr('cy', d => d.y)
.attr('r', this.hubRadius)
.attr('fill', '#9C27B0')
.attr('filter', 'url(#glow)');
// Draw users
this.svg.selectAll('.user')
.data(this.users)
.join('circle')
.attr('class', 'user')
.attr('cx', d => d.x)
.attr('cy', d => d.y)
.attr('r', this.userRadius)
.attr('fill', '#2196F3');
// Add glow filter
const defs = this.svg.append('defs');
const filter = defs.append('filter')
.attr('id', 'glow');
filter.append('feGaussianBlur')
.attr('stdDeviation', '3')
.attr('result', 'coloredBlur');
const feMerge = filter.append('feMerge');
feMerge.append('feMergeNode')
.attr('in', 'coloredBlur');
feMerge.append('feMergeNode')
.attr('in', 'SourceGraphic');
// Add L1 separation line
const lineY = this.height * 0.15;
this.svg.append('line')
.attr('x1', 0)
.attr('y1', lineY)
.attr('x2', this.width)
.attr('y2', lineY)
.attr('stroke', '#666')
.attr('stroke-width', 2)
.attr('stroke-dasharray', '5,5');
// Get depository positions and create depositories
const positions = this.getDepositoryPositions();
this.depositories = positions.map((pos, i) => ({
id: i,
x: pos.x,
y: pos.y,
pendingTx: 0
}));
// Reset block counters and initialize counter displays
this.blockCounters = new Map();
this.depositories.forEach(d => {
this.blockCounters.set(d.id, 0);
// Add initial counter display
this.svg.append('text')
.attr('class', `depository-${d.id}-counter`)
.attr('x', d.x)
.attr('y', d.y - 40)
.attr('text-anchor', 'middle')
.attr('fill', 'white')
.attr('font-size', '16px')
.text('#0');
});
// Initialize spots tracking
this.depositorySpots = new Map();
this.depositories.forEach(d => {
this.depositorySpots.set(d.id, new Set());
});
// Draw depositories with larger boxes to fit 12 transactions
this.svg.selectAll('.depository')
.data(this.depositories)
.join('g')
.attr('class', 'depository')
.attr('transform', d => `translate(${d.x},${d.y})`)
.call(g => {
g.selectAll('*').remove();
// Draw larger depository box
g.append('rect')
.attr('x', -35)
.attr('y', -25)
.attr('width', 120) // Wider to fit 6 transactions horizontally
.attr('height', 50) // Taller to fit 2 rows
.attr('fill', 'none')
.attr('stroke', '#666')
.attr('stroke-width', 1);
});
}
drawConnections() {
// Draw hub-to-hub connections
for (let i = 0; i < this.hubs.length; i++) {
for (let j = i + 1; j < this.hubs.length; j++) {
this.svg.append('line')
.attr('x1', this.hubs[i].x)
.attr('y1', this.hubs[i].y)
.attr('x2', this.hubs[j].x)
.attr('y2', this.hubs[j].y)
.attr('stroke', 'rgba(255, 255, 255, 0.2)')
.attr('stroke-width', 1);
}
}
// Draw user-to-hub connections
this.users.forEach(user => {
// Connect to nearest 1-4 hubs
const numConnections = Math.floor(Math.random() * 4) + 1;
const hubDistances = this.hubs.map((hub, index) => ({
index,
distance: Math.hypot(hub.x - user.x, hub.y - user.y)
})).sort((a, b) => a.distance - b.distance);
for (let i = 0; i < Math.min(numConnections, this.hubs.length); i++) {
const hub = this.hubs[hubDistances[i].index];
this.svg.append('line')
.attr('x1', user.x)
.attr('y1', user.y)
.attr('x2', hub.x)
.attr('y2', hub.y)
.attr('stroke', 'rgba(255, 255, 255, 0.5)')
.attr('stroke-width', 1);
}
});
}
toggleTransactions() {
const button = document.getElementById('transactionControl');
this.isRunning = !this.isRunning;
if (this.isRunning) {
button.textContent = 'Stop Transactions';
button.classList.add('active');
this.startTransactions();
this.startRebalanceTransactions();
} else {
button.textContent = 'Start Transactions';
button.classList.remove('active');
if (this.transactionInterval) clearInterval(this.transactionInterval);
if (this.rebalanceInterval) clearInterval(this.rebalanceInterval);
}
}
startTransactions() {
if (this.transactionInterval) {
clearInterval(this.transactionInterval);
}
const interval = Math.max(20, Math.floor(1000 / this.l2TPS));
this.transactionInterval = setInterval(() => {
if (this.isRunning) this.createTransaction();
}, interval);
}
stopTransactions() {
this.isRunning = false;
if (this.transactionInterval) {
clearInterval(this.transactionInterval);
this.transactionInterval = null;
}
}
createTransaction() {
const source = this.users[Math.floor(Math.random() * this.users.length)];
const hub = this.hubs[Math.floor(Math.random() * this.hubs.length)];
const dot = this.svg.append('circle')
.attr('class', 'transaction')
.attr('cx', source.x)
.attr('cy', source.y)
.attr('r', this.dotRadius)
.attr('fill', '#FFF');
// First leg: user to hub using motionTime
dot.transition()
.duration(this.motionTime)
.attr('cx', hub.x)
.attr('cy', hub.y)
.on('end', () => {
// Second leg: hub to destination using hubTime
const dest = this.users[Math.floor(Math.random() * this.users.length)];
dot.transition()
.duration(this.hubTime)
.attr('cx', dest.x)
.attr('cy', dest.y)
.on('end', function() {
d3.select(this).remove();
});
});
}
startRebalanceTransactions() {
if (this.rebalanceInterval) {
clearInterval(this.rebalanceInterval);
}
const interval = Math.max(20, Math.floor(1000 / this.l1TPS));
this.rebalanceInterval = setInterval(() => {
if (this.isRunning) this.createRebalanceTransaction();
}, interval);
}
findAvailableDepositorySpot() {
// Randomly shuffle depositories
const shuffledDepositories = [...this.depositories]
.sort(() => Math.random() - 0.5);
for (const depository of shuffledDepositories) {
const takenSpots = this.depositorySpots.get(depository.id);
if (takenSpots.size < 12) { // Always check against 12 spots
// Find first available spot
for (let i = 0; i < 12; i++) { // Check all 12 possible spots
if (!takenSpots.has(i)) {
return { depository, spotIndex: i };
}
}
}
}
return null;
}
getSpotPosition(spotIndex) {
// Fill horizontally first (6 per row, 2 rows)
const row = Math.floor(spotIndex / 6); // 0 for first row, 1 for second row
const col = spotIndex % 6; // 0-5 for positions in each row
return {
x: -30 + (col * (this.onchainTxSize + 5)), // 5px padding between transactions
y: -15 + (row * (this.onchainTxSize + 5)) // 5px padding between rows
};
}
createRebalanceTransaction() {
const spot = this.findAvailableDepositorySpot();
if (!spot) return;
const { depository, spotIndex } = spot;
// Only proceed if block isn't full
if (this.depositorySpots.get(depository.id).size >= this.blockSize) {
return;
}
// Check if spot is already taken
if (this.depositorySpots.get(depository.id).has(spotIndex)) {
return;
}
const source = this.hubs[Math.floor(Math.random() * this.hubs.length)];
const position = this.getSpotPosition(spotIndex);
const colors = this.colorSchemes[this.currentColorScheme];
// Reserve spot before creating transaction
this.depositorySpots.get(depository.id).add(spotIndex);
const square = this.svg.append('rect')
.attr('class', 'rebalance')
.datum({ depositoryId: depository.id, spotIndex: spotIndex })
.attr('x', source.x - this.onchainTxSize/2)
.attr('y', source.y - this.onchainTxSize/2)
.attr('width', this.onchainTxSize)
.attr('height', this.onchainTxSize)
.attr('fill', colors.onchainTx);
square.transition()
.duration(this.motionTime)
.attr('x', depository.x + position.x)
.attr('y', depository.y + position.y)
.on('end', () => {
depository.pendingTx++;
// Check if block is exactly full
if (this.depositorySpots.get(depository.id).size === this.blockSize) {
this.broadcastBlock(depository);
}
});
}
broadcastBlock(depository) {
if (this.depositorySpots.get(depository.id).size !== 12) {
return;
}
const currentCount = this.blockCounters.get(depository.id) || 0;
this.blockCounters.set(depository.id, currentCount + 1);
// Update counter first
const counterText = this.svg.select(`.depository-${depository.id}-counter`);
if (counterText.empty()) {
this.svg.append('text')
.attr('class', `depository-${depository.id}-counter`)
.attr('x', depository.x)
.attr('y', depository.y - 40)
.attr('text-anchor', 'middle')
.attr('fill', 'white')
.attr('font-size', '16px')
.text(`#${currentCount + 1}`);
} else {
counterText.text(`#${currentCount + 1}`);
}
// Create broadcast effect first
switch(this.broadcastStyle) {
case 'ripple':
this.createRippleEffect(depository);
break;
case 'directed':
this.createDirectedRipples(depository);
break;
case 'flash':
this.createFlashEffect(depository);
break;
case 'rays':
this.createRaysEffect(depository);
break;
case 'pulse':
this.createPulseEffect(depository);
break;
}
// Then remove transactions after a small delay
setTimeout(() => {
// Remove all transactions in this block
this.svg.selectAll('.rebalance')
.filter(d => d && d.depositoryId === depository.id)
.remove();
// Reset depository state
depository.pendingTx = 0;
this.depositorySpots.get(depository.id).clear();
}, 50); // Small delay to ensure ripple starts first
}
createLegend() {
const legend = this.svg.append('g')
.attr('class', 'legend')
.attr('transform', `translate(20, ${this.height - 150})`);
const legendItems = [
{ label: 'User', type: 'circle', fill: '#2196F3', r: this.userRadius },
{ label: 'Hub', type: 'circle', fill: '#9C27B0', r: this.hubRadius },
{ label: 'Depository', type: 'rect', fill: '#444', width: 30, height: 20 },
{ label: 'L1 Transaction', type: 'rect', fill: '#FFD700', size: this.onchainTxSize },
{ label: 'L2 Transaction', type: 'circle', fill: '#FFF', r: this.dotRadius }
];
const itemHeight = 25;
legendItems.forEach((item, i) => {
const g = legend.append('g')
.attr('transform', `translate(0, ${i * itemHeight})`);
if (item.type === 'circle') {
g.append('circle')
.attr('r', item.r)
.attr('cx', 10)
.attr('cy', 10)
.attr('fill', item.fill);
} else {
g.append('rect')
.attr('width', item.width || item.size)
.attr('height', item.height || item.size)
.attr('x', item.width ? 0 : (10 - item.size/2))
.attr('y', item.height ? 0 : (10 - item.size/2))
.attr('fill', item.fill);
}
g.append('text')
.attr('x', 30)
.attr('y', 15)
.attr('fill', 'white')
.text(item.label);
});
// Add semi-transparent background
const bbox = legend.node().getBBox();
legend.insert('rect', ':first-child')
.attr('x', -10)
.attr('y', -10)
.attr('width', bbox.width + 20)
.attr('height', bbox.height + 20)
.attr('fill', 'rgba(0, 0, 0, 0.7)')
.attr('rx', 5);
}
createRippleEffect(depository) {
const colors = this.colorSchemes[this.currentColorScheme];
const maxDimension = Math.max(this.width, this.height) * 1.5;
const ripple = this.svg.append('circle')
.attr('cx', depository.x)
.attr('cy', depository.y)
.attr('r', 10)
.attr('fill', 'none')
.attr('stroke', colors.ripple)
.attr('stroke-width', 2)
.style('opacity', 1);
ripple.transition()
.duration(this.hubTime)
.attr('r', maxDimension)
.style('opacity', 0)
.remove();
}
createDirectedRipples(depository) {
const colors = this.colorSchemes[this.currentColorScheme];
[...this.hubs, ...this.users].forEach(target => {
const line = this.svg.append('line')
.attr('x1', depository.x)
.attr('y1', depository.y)
.attr('x2', depository.x)
.attr('y2', depository.y)
.attr('stroke', colors.ripple)
.attr('stroke-width', 2)
.style('opacity', 1);
line.transition()
.duration(this.hubTime)
.attr('x2', target.x)
.attr('y2', target.y)
.style('opacity', 0)
.remove();
});
}
createFlashEffect(depository) {
const flash = this.svg.append('circle')
.attr('cx', depository.x)
.attr('cy', depository.y)
.attr('r', 30)
.attr('fill', '#FFD700')
.style('opacity', 0.8);
flash.transition()
.duration(this.hubTime / 2)
.style('opacity', 0)
.remove();
}
createRaysEffect(depository) {
const numRays = 8;
const rayLength = 50;
for (let i = 0; i < numRays; i++) {
const angle = (2 * Math.PI * i) / numRays;
const endX = depository.x + rayLength * Math.cos(angle);
const endY = depository.y + rayLength * Math.sin(angle);
const ray = this.svg.append('line')
.attr('x1', depository.x)
.attr('y1', depository.y)
.attr('x2', depository.x)
.attr('y2', depository.y)
.attr('stroke', '#FFD700')
.attr('stroke-width', 2);
ray.transition()
.duration(this.hubTime)
.attr('x2', endX)
.attr('y2', endY)
.style('opacity', 0)
.remove();
}
}
createPulseEffect(depository) {
const pulse = this.svg.append('circle')
.attr('cx', depository.x)
.attr('cy', depository.y)
.attr('r', 20)
.attr('fill', '#FFD700')
.style('opacity', 0.5);
pulse.transition()
.duration(this.hubTime)
.attr('r', 40)
.style('opacity', 0)
.remove();
}
}
// Move initialization to DOMContentLoaded event
window.addEventListener('DOMContentLoaded', () => {
new HubAndSpokeVisualization();
});