File size: 52,492 Bytes
0b2295a a895648 0b2295a fd832fc a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 fd832fc 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a fd832fc 0b2295a fd832fc 0b2295a fd832fc 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a a895648 0b2295a f02caca a895648 f02caca a895648 f02caca a895648 f02caca a895648 f02caca a895648 f02caca a895648 |
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 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 |
// Initialize drag and drop functionality
function initializeDragAndDrop() {
const nodeItems = document.querySelectorAll('.node-item');
const canvas = document.getElementById('network-canvas');
let draggedNode = null;
let offsetX, offsetY;
let isDragging = false;
let isConnecting = false;
let startNode = null;
let connectionLine = null;
let nodeCounter = {};
// Anti-duplication system
const recentlyCreated = {
nodeIds: new Set(),
dragStartTime: 0,
isDropHandled: false,
inProgress: false,
timestamp: 0
};
// Track layers for proper architecture building
let networkLayers = {
layers: [],
connections: []
};
// Helper function to format numbers with K, M, B suffixes
function formatNumber(num) {
if (num === 0) return '0';
if (!num) return 'N/A';
if (num >= 1e9) return (num / 1e9).toFixed(2) + 'B';
if (num >= 1e6) return (num / 1e6).toFixed(2) + 'M';
if (num >= 1e3) return (num / 1e3).toFixed(2) + 'K';
return num.toString();
}
// Add event listeners to draggable items with extra safety
nodeItems.forEach(item => {
// Clean dragstart handler with precise lifecycle
item.addEventListener('dragstart', function(e) {
// Clean up previous state
recentlyCreated.isDropHandled = false;
recentlyCreated.inProgress = true;
recentlyCreated.dragStartTime = Date.now();
const nodeType = this.getAttribute('data-type');
// Persist data in multiple ways to ensure transfer
e.dataTransfer.setData('text/plain', nodeType);
e.dataTransfer.setData('application/x-neural-node-type', nodeType);
// Extra backup properties
try {
e.dataTransfer.nodeType = nodeType;
e.dataTransfer._neural_type = nodeType;
} catch (err) {
// Some browsers restrict properties on dataTransfer
}
draggedNode = this;
// Set ghost image
const ghost = this.cloneNode(true);
ghost.style.opacity = '0.5';
document.body.appendChild(ghost);
e.dataTransfer.setDragImage(ghost, 0, 0);
setTimeout(() => {
document.body.removeChild(ghost);
}, 0);
// Event cleanup handler
const dragEndHandler = function() {
setTimeout(() => {
recentlyCreated.inProgress = false;
draggedNode = null;
}, 100);
// Remove this one-time handler
item.removeEventListener('dragend', dragEndHandler);
};
// Add one-time dragend handler
item.addEventListener('dragend', dragEndHandler);
});
});
// Safe dragover handler
function handleDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
// Canvas events
canvas.addEventListener('dragover', handleDragOver);
// One-time drop handler for each operation
canvas.addEventListener('drop', function dropHandler(e) {
e.preventDefault();
// Multi-layer protection against duplicate drops
if (recentlyCreated.isDropHandled) {
return; // Already handled this drop
}
const now = Date.now();
// Debounce protection
if (now - recentlyCreated.timestamp < 500) {
return;
}
// Set state to prevent multiple processing
recentlyCreated.isDropHandled = true;
recentlyCreated.timestamp = now;
// Safety check for drag operation
if (!recentlyCreated.inProgress || !draggedNode || !draggedNode.classList.contains('node-item')) {
return;
}
// Try multiple ways to get the node type
let nodeType = null;
try {
// Try standard method first
nodeType = e.dataTransfer.getData('text/plain');
// Try backup methods if needed
if (!nodeType) {
nodeType = e.dataTransfer.getData('application/x-neural-node-type');
}
if (!nodeType && e.dataTransfer.nodeType) {
nodeType = e.dataTransfer.nodeType;
}
if (!nodeType && e.dataTransfer._neural_type) {
nodeType = e.dataTransfer._neural_type;
}
if (!nodeType && draggedNode) {
nodeType = draggedNode.getAttribute('data-type');
}
} catch (err) {
// Error handling for dataTransfer access
}
if (!nodeType) {
return;
}
// Calculate position relative to canvas
const canvasRect = canvas.getBoundingClientRect();
const x = e.clientX - canvasRect.left - 75;
const y = e.clientY - canvasRect.top - 30;
// Ensure position is within canvas bounds
const posX = Math.max(0, Math.min(canvasRect.width - 150, x));
const posY = Math.max(0, Math.min(canvasRect.height - 100, y));
// Generate a unique ID for the node that includes a timestamp to avoid collision
const layerId = `${nodeType}-${Date.now()}-${Math.floor(Math.random() * 10000)}`;
// Skip if this node ID was recently created (extremely unlikely due to timestamp)
if (recentlyCreated.nodeIds.has(layerId)) {
return;
}
recentlyCreated.nodeIds.add(layerId);
// Limit the size of the recently created set
if (recentlyCreated.nodeIds.size > 10) {
const iterator = recentlyCreated.nodeIds.values();
recentlyCreated.nodeIds.delete(iterator.next().value);
}
// Increment counter for this node type
nodeCounter[nodeType] = (nodeCounter[nodeType] || 0) + 1;
// Generate a unique ID for the node
const layerId = `${nodeType}-${Date.now()}-${Math.floor(Math.random() * 1000)}`;
// Create the node element
const canvasNode = document.createElement('div');
canvasNode.className = `canvas-node ${nodeType}-node`;
canvasNode.setAttribute('data-type', nodeType);
canvasNode.setAttribute('data-id', layerId);
canvasNode.style.position = 'absolute';
canvasNode.style.left = `${posX}px`;
canvasNode.style.top = `${posY}px`;
// Get default config for this node type
const nodeConfig = window.neuralNetwork.createNodeConfig(nodeType);
// Create node content with input and output shape information
let nodeName, inputShape, outputShape, parameters;
switch(nodeType) {
case 'input':
nodeName = 'Input Layer';
inputShape = 'N/A';
outputShape = '[' + nodeConfig.shape.join(' × ') + ']';
parameters = nodeConfig.parameters;
break;
case 'hidden':
const hiddenCount = document.querySelectorAll('.canvas-node[data-type="hidden"]').length;
nodeConfig.units = hiddenCount === 0 ? 128 : 64;
nodeName = `Hidden Layer ${hiddenCount + 1}`;
// Input shape will be updated when connections are made
inputShape = 'Connect input';
outputShape = `[${nodeConfig.units}]`;
parameters = 'Connect input to calculate';
break;
case 'output':
nodeName = 'Output Layer';
inputShape = 'Connect input';
outputShape = `[${nodeConfig.units}]`;
parameters = 'Connect input to calculate';
break;
case 'conv':
const convCount = document.querySelectorAll('.canvas-node[data-type="conv"]').length;
nodeConfig.filters = 32 * (convCount + 1);
nodeName = `Conv2D ${convCount + 1}`;
inputShape = 'Connect input';
outputShape = 'Depends on input';
// Create parameter string
parameters = `Kernel: ${nodeConfig.kernelSize.join('×')}\nStride: ${nodeConfig.strides.join('×')}\nPadding: ${nodeConfig.padding}`;
break;
case 'pool':
const poolCount = document.querySelectorAll('.canvas-node[data-type="pool"]').length;
nodeName = `Pooling ${poolCount + 1}`;
inputShape = 'Connect input';
outputShape = 'Depends on input';
parameters = `Pool size: ${nodeConfig.poolSize.join('×')}\nStride: ${nodeConfig.strides.join('×')}\nPadding: ${nodeConfig.padding}`;
break;
default:
nodeName = 'Unknown Layer';
inputShape = 'N/A';
outputShape = 'N/A';
parameters = 'N/A';
}
// Create node content
const nodeContent = document.createElement('div');
nodeContent.className = 'node-content';
// Add shape information in a structured way
const shapeInfo = document.createElement('div');
shapeInfo.className = 'shape-info';
shapeInfo.innerHTML = `
<div class="shape-row"><span class="shape-label">Input:</span> <span class="input-shape">${inputShape}</span></div>
<div class="shape-row"><span class="shape-label">Output:</span> <span class="output-shape">${outputShape}</span></div>
`;
// Add parameters section
const paramsSection = document.createElement('div');
paramsSection.className = 'params-section';
paramsSection.innerHTML = `
<div class="params-details">${parameters}</div>
<div class="node-parameters">Params: ${nodeConfig.parameters !== undefined ? formatNumber(nodeConfig.parameters) : '?'}</div>
`;
// Assemble content
nodeContent.appendChild(shapeInfo);
nodeContent.appendChild(paramsSection);
// Add dimensions section to show shapes compactly
const dimensionsSection = document.createElement('div');
dimensionsSection.className = 'node-dimensions';
// Set dimensions text based on node type
let dimensionsText = '';
switch(nodeType) {
case 'input':
dimensionsText = nodeConfig.shape.join(' × ');
break;
case 'hidden':
case 'output':
dimensionsText = nodeConfig.units.toString();
break;
case 'conv':
if (nodeConfig.inputShape && nodeConfig.outputShape) {
dimensionsText = `${nodeConfig.inputShape.join('×')} → ${nodeConfig.outputShape.join('×')}`;
} else {
dimensionsText = `? → ${nodeConfig.filters} filters`;
}
break;
case 'pool':
if (nodeConfig.inputShape && nodeConfig.outputShape) {
dimensionsText = `${nodeConfig.inputShape.join('×')} → ${nodeConfig.outputShape.join('×')}`;
} else {
dimensionsText = `? → ?`;
}
break;
case 'linear':
dimensionsText = `${nodeConfig.inputFeatures} → ${nodeConfig.outputFeatures}`;
break;
}
dimensionsSection.textContent = dimensionsText;
// Add node title for clearer identification
const nodeTitle = document.createElement('div');
nodeTitle.className = 'node-title';
nodeTitle.textContent = nodeName;
// Add node controls (edit and delete buttons)
const nodeControls = document.createElement('div');
nodeControls.className = 'node-controls';
const editButton = document.createElement('button');
editButton.className = 'node-edit-btn';
editButton.innerHTML = '✎';
editButton.title = 'Edit Layer';
const deleteButton = document.createElement('button');
deleteButton.className = 'node-delete-btn';
deleteButton.innerHTML = '×';
deleteButton.title = 'Delete Layer';
nodeControls.appendChild(editButton);
nodeControls.appendChild(deleteButton);
// Add connection ports
const portIn = document.createElement('div');
portIn.className = 'node-port port-in';
const portOut = document.createElement('div');
portOut.className = 'node-port port-out';
// Assemble the node with the new structure
canvasNode.appendChild(nodeTitle);
canvasNode.appendChild(nodeControls);
canvasNode.appendChild(dimensionsSection);
canvasNode.appendChild(nodeContent);
canvasNode.appendChild(portIn);
canvasNode.appendChild(portOut);
// Store node data attributes for easier access
canvasNode.setAttribute('data-name', nodeName);
canvasNode.setAttribute('data-dimensions', dimensionsText);
// Add node to the canvas
canvas.appendChild(canvasNode);
// Store node configuration
canvasNode.layerConfig = nodeConfig;
// Add event listeners for node manipulation
canvasNode.addEventListener('mousedown', startDrag);
// Update port event listeners for the new class names
portIn.addEventListener('mousedown', (e) => {
e.stopPropagation();
});
portOut.addEventListener('mousedown', (e) => {
e.stopPropagation();
startConnection(canvasNode, e);
});
// Double-click to edit node properties
canvasNode.addEventListener('dblclick', () => {
openLayerEditor(canvasNode);
});
// Right-click to delete
canvasNode.addEventListener('contextmenu', (e) => {
e.preventDefault();
deleteNode(canvasNode);
});
// Add click event for edit button
editButton.addEventListener('click', (e) => {
e.stopPropagation();
openLayerEditor(canvasNode);
});
// Add click event for delete button
deleteButton.addEventListener('click', (e) => {
e.stopPropagation();
deleteNode(canvasNode);
});
// Add to network layers for architecture building
networkLayers.layers.push({
id: layerId,
type: nodeType,
name: nodeName,
position: { x: posX, y: posY },
dimensions: dimensionsText,
config: nodeConfig,
parameters: nodeConfig.parameters || 0
});
// Notify about network changes
document.dispatchEvent(new CustomEvent('networkUpdated', {
detail: networkLayers
}));
updateConnections();
// Hide the canvas hint after adding a node
const canvasHint = document.querySelector('.canvas-hint');
if (canvasHint) {
canvasHint.style.display = 'none';
}
// Reset states and references
draggedNode = null;
recentlyCreated.inProgress = false;
// Force cleanup any stray global variables after a short delay
setTimeout(() => {
if (window.draggedNode) {
delete window.draggedNode;
}
recentlyCreated.isDropHandled = false;
}, 100);
}
// Delete a node and its associated connections
function deleteNode(node) {
if (!node) return;
const nodeId = node.getAttribute('data-id');
// Remove all connections to/from this node
const connections = document.querySelectorAll(`.connection[data-source="${nodeId}"], .connection[data-target="${nodeId}"]`);
connections.forEach(connection => {
if (connection.parentNode) {
connection.parentNode.removeChild(connection);
}
});
// Remove from networkLayers.connections
networkLayers.connections = networkLayers.connections.filter(conn =>
conn.source !== nodeId && conn.target !== nodeId
);
// Remove from networkLayers.layers
const layerIndex = networkLayers.layers.findIndex(layer => layer.id === nodeId);
if (layerIndex !== -1) {
networkLayers.layers.splice(layerIndex, 1);
}
// Remove the node from the DOM
if (node.parentNode) {
node.parentNode.removeChild(node);
}
// Show the canvas hint if no nodes left
if (document.querySelectorAll('.canvas-node').length === 0) {
const canvasHint = document.querySelector('.canvas-hint');
if (canvasHint) {
canvasHint.style.display = 'block';
}
}
// Update connections to remove orphaned ones
updateConnections();
// Notify about network changes
document.dispatchEvent(new CustomEvent('networkUpdated', {
detail: networkLayers
}));
}
// Start dragging an existing node on the canvas
function startDrag(e) {
console.log('[DEBUG] startDrag called', e.target);
if (isConnecting) return;
// Only start drag if not clicking on buttons or ports
if (e.target.closest('.node-controls') || e.target.closest('.node-port')) {
return;
}
isDragging = true;
// Make sure we get the canvas-node, even if we clicked on a child element
const target = e.target.closest('.canvas-node');
if (!target) {
console.error('[ERROR] No canvas-node found in startDrag');
return;
}
const rect = target.getBoundingClientRect();
// Calculate offset
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
// Add event listeners to document, not the element
document.addEventListener('mousemove', dragNode);
document.addEventListener('mouseup', stopDrag);
// Reference to the dragged node
draggedNode = target;
// Make the dragged node appear on top
draggedNode.style.zIndex = "100";
// Add dragging class for visual feedback
draggedNode.classList.add('dragging');
// Add dragging class to body for consistent cursor
document.body.classList.add('node-dragging');
// Prevent default behavior
e.preventDefault();
console.log(`[DEBUG] Started dragging node: ${target.getAttribute('data-id')}`);
}
// Drag node on the canvas
function dragNode(e) {
if (!isDragging || !draggedNode) {
console.log('[WARN] dragNode called but not in dragging state');
return;
}
const canvasRect = canvas.getBoundingClientRect();
let x = e.clientX - canvasRect.left - offsetX;
let y = e.clientY - canvasRect.top - offsetY;
// Constrain to canvas with better boundary checks
const nodeWidth = draggedNode.offsetWidth || 150; // Default width if not set
const nodeHeight = draggedNode.offsetHeight || 100; // Default height if not set
// Ensure the node stays completely within the canvas
x = Math.max(0, Math.min(canvasRect.width - nodeWidth, x));
y = Math.max(0, Math.min(canvasRect.height - nodeHeight, y));
// Apply position with fixed sizing to prevent layout expansion
draggedNode.style.position = 'absolute';
draggedNode.style.left = `${x}px`;
draggedNode.style.top = `${y}px`;
draggedNode.style.width = `${nodeWidth}px`; // Maintain fixed width
// Update node position in network layers
const nodeId = draggedNode.getAttribute('data-id');
const layerIndex = networkLayers.layers.findIndex(layer => layer.id === nodeId);
if (layerIndex !== -1) {
networkLayers.layers[layerIndex].position = { x, y };
}
// Force update all connections immediately to make them responsive
updateConnections();
}
// Stop dragging
function stopDrag(e) {
if (!isDragging) {
return;
}
console.log('[DEBUG] stopDrag called');
// Always clean up event listeners
document.removeEventListener('mousemove', dragNode);
document.removeEventListener('mouseup', stopDrag);
isDragging = false;
// Remove dragging class from body
document.body.classList.remove('node-dragging');
// Reset z-index and remove dragging class if node exists
if (draggedNode) {
draggedNode.style.zIndex = "10";
draggedNode.classList.remove('dragging');
// Trigger connections update one more time
updateConnections();
// Clear the reference
const nodeId = draggedNode.getAttribute('data-id');
console.log(`[DEBUG] Stopped dragging node: ${nodeId}`);
draggedNode = null;
}
}
// Start creating a connection between nodes
function startConnection(node, e) {
isConnecting = true;
startNode = node;
// Create a temporary line
connectionLine = document.createElement('div');
connectionLine.className = 'connection temp-connection';
// Get start position (center of the port)
const portOut = node.querySelector('.node-port.port-out');
const portRect = portOut.getBoundingClientRect();
const canvasRect = canvas.getBoundingClientRect();
const startX = portRect.left + portRect.width / 2 - canvasRect.left;
const startY = portRect.top + portRect.height / 2 - canvasRect.top;
// Position the line
connectionLine.style.left = `${startX}px`;
connectionLine.style.top = `${startY}px`;
connectionLine.style.width = '0px';
connectionLine.style.transform = 'rotate(0deg)';
// Add active class to the starting port
portOut.classList.add('active-port');
// Highlight valid target ports
highlightValidConnectionTargets(node);
canvas.appendChild(connectionLine);
// Add event listeners for drawing the line
document.addEventListener('mousemove', drawConnection);
document.addEventListener('mouseup', cancelConnection);
e.preventDefault();
}
// Highlight valid targets for connection
function highlightValidConnectionTargets(sourceNode) {
const sourceType = sourceNode.getAttribute('data-type');
const sourceId = sourceNode.getAttribute('data-id');
document.querySelectorAll('.canvas-node').forEach(node => {
if (node !== sourceNode) {
const nodeType = node.getAttribute('data-type');
const nodeId = node.getAttribute('data-id');
const isValidTarget = isValidConnection(sourceType, nodeType, sourceId, nodeId);
const portIn = node.querySelector('.node-port.port-in');
if (portIn) {
if (isValidTarget) {
portIn.classList.add('valid-target');
} else {
portIn.classList.add('invalid-target');
}
}
}
});
}
// Remove highlights from all ports
function removePortHighlights() {
document.querySelectorAll('.node-port.port-in, .node-port.port-out').forEach(port => {
port.classList.remove('active-port', 'valid-target', 'invalid-target');
});
}
// Check if a connection between two node types is valid
function isValidConnection(sourceType, targetType, sourceId, targetId) {
// Basic hierarchy validation
if (sourceType === 'output' || targetType === 'input') {
return false; // Output can't have outgoing connections, Input can't have incoming
}
// Prevent cycles
const existingConnection = networkLayers.connections.find(
conn => conn.target === sourceId && conn.source === targetId
);
if (existingConnection) {
return false;
}
// Specific connection rules
switch(sourceType) {
case 'input':
return ['hidden', 'conv'].includes(targetType);
case 'conv':
return ['conv', 'pool', 'hidden'].includes(targetType);
case 'pool':
return ['conv', 'hidden'].includes(targetType);
case 'hidden':
return ['hidden', 'output'].includes(targetType);
default:
return false;
}
}
// Draw the connection line as mouse moves
function drawConnection(e) {
if (!isConnecting || !connectionLine) return;
const canvasRect = canvas.getBoundingClientRect();
const portOut = startNode.querySelector('.node-port.port-out');
const portRect = portOut.getBoundingClientRect();
// Calculate start and end points
const startX = portRect.left + portRect.width / 2 - canvasRect.left;
const startY = portRect.top + portRect.height / 2 - canvasRect.top;
const endX = e.clientX - canvasRect.left;
const endY = e.clientY - canvasRect.top;
// Calculate length and angle
const length = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
const angle = Math.atan2(endY - startY, endX - startX) * 180 / Math.PI;
// Update line
connectionLine.style.width = `${length}px`;
connectionLine.style.transform = `rotate(${angle}deg)`;
// Highlight the port under cursor
document.querySelectorAll('.canvas-node').forEach(node => {
if (node !== startNode) {
const portIn = node.querySelector('.node-port.port-in');
if (portIn) {
const portInRect = portIn.getBoundingClientRect();
// Check if mouse is over the input port
if (e.clientX >= portInRect.left && e.clientX <= portInRect.right &&
e.clientY >= portInRect.top && e.clientY <= portInRect.bottom) {
portIn.classList.add('port-hover');
} else {
portIn.classList.remove('port-hover');
}
}
}
});
}
// Cancel connection creation
function cancelConnection(e) {
if (!isConnecting) return;
// Find if we're over a valid input port
let targetNode = null;
document.querySelectorAll('.canvas-node').forEach(node => {
if (node !== startNode) {
const portIn = node.querySelector('.node-port.port-in');
if (portIn) {
const portRect = portIn.getBoundingClientRect();
if (e.clientX >= portRect.left && e.clientX <= portRect.right &&
e.clientY >= portRect.top && e.clientY <= portRect.bottom) {
// Check if this would be a valid connection
const sourceType = startNode.getAttribute('data-type');
const targetType = node.getAttribute('data-type');
const sourceId = startNode.getAttribute('data-id');
const targetId = node.getAttribute('data-id');
if (isValidConnection(sourceType, targetType, sourceId, targetId)) {
targetNode = node;
}
}
}
}
});
// If we found a valid target, create the connection
if (targetNode) {
endConnection(targetNode);
} else {
// Otherwise, remove the temporary line
if (connectionLine && connectionLine.parentNode) {
connectionLine.parentNode.removeChild(connectionLine);
}
}
// Remove all port highlights
removePortHighlights();
document.querySelectorAll('.node-port').forEach(port => {
port.classList.remove('port-hover');
});
// Reset variables
isConnecting = false;
startNode = null;
connectionLine = null;
// Remove event listeners
document.removeEventListener('mousemove', drawConnection);
document.removeEventListener('mouseup', cancelConnection);
}
// End creating a connection
function endConnection(targetNode) {
if (!isConnecting || !connectionLine || !startNode) return;
const sourceType = startNode.getAttribute('data-type');
const targetType = targetNode.getAttribute('data-type');
const sourceId = startNode.getAttribute('data-id');
const targetId = targetNode.getAttribute('data-id');
// Check if this is a valid connection
if (isValidConnection(sourceType, targetType, sourceId, targetId)) {
// Remove the temporary line
if (connectionLine && connectionLine.parentNode) {
connectionLine.parentNode.removeChild(connectionLine);
}
// Create a permanent connection line
const connection = document.createElement('div');
connection.className = 'connection';
connection.setAttribute('data-source', sourceId);
connection.setAttribute('data-target', targetId);
// Add to canvas
canvas.appendChild(connection);
// Position the connection
const sourcePort = startNode.querySelector('.node-port.port-out');
const targetPort = targetNode.querySelector('.node-port.port-in');
if (sourcePort && targetPort) {
const sourceRect = sourcePort.getBoundingClientRect();
const targetRect = targetPort.getBoundingClientRect();
const canvasRect = canvas.getBoundingClientRect();
const startX = sourceRect.left + sourceRect.width / 2 - canvasRect.left;
const startY = sourceRect.top + sourceRect.height / 2 - canvasRect.top;
const endX = targetRect.left + targetRect.width / 2 - canvasRect.left;
const endY = targetRect.top + targetRect.height / 2 - canvasRect.top;
const length = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
const angle = Math.atan2(endY - startY, endX - startX) * 180 / Math.PI;
connection.style.left = `${startX}px`;
connection.style.top = `${startY}px`;
connection.style.width = `${length}px`;
connection.style.transform = `rotate(${angle}deg)`;
}
// Update network layers
const sourceLayerIndex = networkLayers.layers.findIndex(layer => layer.id === sourceId);
const targetLayerIndex = networkLayers.layers.findIndex(layer => layer.id === targetId);
if (sourceLayerIndex !== -1 && targetLayerIndex !== -1) {
networkLayers.connections.push({
source: sourceId,
target: targetId
});
// Initialize connections array if it doesn't exist
if (!networkLayers.layers[sourceLayerIndex].connections) {
networkLayers.layers[sourceLayerIndex].connections = [];
}
if (!networkLayers.layers[targetLayerIndex].connections) {
networkLayers.layers[targetLayerIndex].connections = [];
}
// Add connection to layers
networkLayers.layers[sourceLayerIndex].connections.push(targetId);
networkLayers.layers[targetLayerIndex].connections.push(sourceId);
// Update target node using the source node's configuration
const sourceConfig = networkLayers.layers[sourceLayerIndex].config;
if (sourceConfig && sourceConfig.outputShape) {
// Update target node with source's output shape
if (!targetNode.layerConfig) {
targetNode.layerConfig = {};
}
// Set input shape of target to output shape of source
targetNode.layerConfig.inputShape = [...sourceConfig.outputShape];
// Update parameters using our helper function
updateNodeParameters(targetNode, targetType, sourceConfig);
// Recursively update downstream nodes
updateDownstreamNodes(targetId);
// Force update all parameters in the network for complete synchronization
forceUpdateNetworkParameters();
}
}
// Notify about network changes
document.dispatchEvent(new CustomEvent('networkUpdated', {
detail: networkLayers
}));
}
// Reset variables
isConnecting = false;
startNode = null;
connectionLine = null;
// Remove event listeners
document.removeEventListener('mousemove', drawConnection);
document.removeEventListener('mouseup', cancelConnection);
}
// Update connections when nodes are moved
function updateConnections(specificNodeId = null) {
console.log(`[DEBUG] updateConnections called ${specificNodeId ? 'for node: ' + specificNodeId : 'for all connections'}`);
// Get all connections or just those related to the specified node
let connections;
if (specificNodeId) {
connections = document.querySelectorAll(`.connection[data-source="${specificNodeId}"], .connection[data-target="${specificNodeId}"]`);
} else {
connections = document.querySelectorAll('.connection:not(.temp-connection)');
}
console.log(`[DEBUG] Updating ${connections.length} connections`);
connections.forEach(connection => {
const sourceId = connection.getAttribute('data-source');
const targetId = connection.getAttribute('data-target');
const sourceNode = document.querySelector(`.canvas-node[data-id="${sourceId}"]`);
const targetNode = document.querySelector(`.canvas-node[data-id="${targetId}"]`);
if (sourceNode && targetNode) {
const sourcePort = sourceNode.querySelector('.node-port.port-out');
const targetPort = targetNode.querySelector('.node-port.port-in');
if (sourcePort && targetPort) {
const canvasRect = canvas.getBoundingClientRect();
const sourceRect = sourcePort.getBoundingClientRect();
const targetRect = targetPort.getBoundingClientRect();
const startX = sourceRect.left + sourceRect.width / 2 - canvasRect.left;
const startY = sourceRect.top + sourceRect.height / 2 - canvasRect.top;
const endX = targetRect.left + targetRect.width / 2 - canvasRect.left;
const endY = targetRect.top + targetRect.height / 2 - canvasRect.top;
const length = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
const angle = Math.atan2(endY - startY, endX - startX) * 180 / Math.PI;
connection.style.left = `${startX}px`;
connection.style.top = `${startY}px`;
connection.style.width = `${length}px`;
connection.style.transform = `rotate(${angle}deg)`;
}
} else {
// If either node is missing, remove the connection
if (connection.parentNode) {
console.log(`[DEBUG] Removing orphaned connection between ${sourceId} and ${targetId}`);
connection.parentNode.removeChild(connection);
// Remove from the connections array
const connIndex = networkLayers.connections.findIndex(conn =>
conn.source === sourceId && conn.target === targetId
);
if (connIndex !== -1) {
networkLayers.connections.splice(connIndex, 1);
}
}
}
});
}
// Helper function to update a node's parameters and display
function updateNodeParameters(node, nodeType, sourceConfig) {
if (!node || !nodeType || !sourceConfig) return;
const nodeId = node.getAttribute('data-id');
// Ensure node's layerConfig exists
if (!node.layerConfig) {
node.layerConfig = {};
}
// Ensure input shape is set in the layer config
if (sourceConfig.outputShape) {
node.layerConfig.inputShape = [...sourceConfig.outputShape];
// For specific layer types, calculate output shape based on input shape
switch(nodeType) {
case 'hidden':
node.layerConfig.outputShape = [node.layerConfig.units];
break;
case 'output':
node.layerConfig.outputShape = [node.layerConfig.units];
break;
case 'conv':
// Call neural network module to calculate output shape
if (window.neuralNetwork && window.neuralNetwork.calculateOutputShape) {
node.layerConfig.outputShape = window.neuralNetwork.calculateOutputShape(
'conv',
node.layerConfig.inputShape,
node.layerConfig
);
}
break;
case 'pool':
// Call neural network module to calculate output shape
if (window.neuralNetwork && window.neuralNetwork.calculateOutputShape) {
node.layerConfig.outputShape = window.neuralNetwork.calculateOutputShape(
'pool',
node.layerConfig.inputShape,
node.layerConfig
);
}
break;
}
}
// Calculate parameters using the neural network module
let newParams = 0;
if (window.neuralNetwork && window.neuralNetwork.calculateParameters) {
newParams = window.neuralNetwork.calculateParameters(
nodeType,
node.layerConfig,
sourceConfig
);
} else {
// Fallback parameter calculation if neuralNetwork module is not available
switch(nodeType) {
case 'hidden':
if (node.layerConfig.inputShape && node.layerConfig.units) {
// Parameters = (input_size * units) + units (weights + biases)
const inputSize = node.layerConfig.inputShape[0];
newParams = (inputSize * node.layerConfig.units) + node.layerConfig.units;
}
break;
case 'output':
if (node.layerConfig.inputShape && node.layerConfig.units) {
// Parameters = (input_size * units) + units (weights + biases)
const inputSize = node.layerConfig.inputShape[0];
newParams = (inputSize * node.layerConfig.units) + node.layerConfig.units;
}
break;
case 'conv':
if (node.layerConfig.inputShape && node.layerConfig.filters && node.layerConfig.kernelSize) {
// Parameters = (kernel_height * kernel_width * input_channels * filters) + filters
const inputChannels = node.layerConfig.inputShape.length > 2 ? node.layerConfig.inputShape[2] : 1;
newParams = (node.layerConfig.kernelSize[0] * node.layerConfig.kernelSize[1] *
inputChannels * node.layerConfig.filters) + node.layerConfig.filters;
}
break;
case 'pool':
// Pooling layers don't have trainable parameters
newParams = 0;
break;
}
}
// Update parameter count in both the node object and network model
if (newParams !== undefined) {
// Update the node object
node.layerConfig.parameters = newParams;
// Update the network model
const layerIndex = networkLayers.layers.findIndex(layer => layer.id === nodeId);
if (layerIndex !== -1) {
networkLayers.layers[layerIndex].parameters = newParams;
if (networkLayers.layers[layerIndex].config) {
networkLayers.layers[layerIndex].config.parameters = newParams;
// Also update output shape in model
if (node.layerConfig.outputShape) {
networkLayers.layers[layerIndex].config.outputShape = [...node.layerConfig.outputShape];
}
}
}
// Force update the display
const paramsDisplay = node.querySelector('.node-parameters');
if (paramsDisplay) {
paramsDisplay.textContent = `Params: ${formatNumber(newParams)}`;
}
}
// Update input shape display
if (node.layerConfig.inputShape) {
const inputShapeDisplay = node.querySelector('.input-shape');
if (inputShapeDisplay) {
inputShapeDisplay.textContent = `[${node.layerConfig.inputShape.join(' × ')}]`;
}
}
// Update output shape display
if (node.layerConfig.outputShape) {
const outputShapeDisplay = node.querySelector('.output-shape');
if (outputShapeDisplay) {
outputShapeDisplay.textContent = `[${node.layerConfig.outputShape.join(' × ')}]`;
}
}
// Update the dimensions display
updateNodeDimensions(node);
// Force a rerender of this node to ensure all changes are displayed
setTimeout(() => {
// Minimal DOM update to force re-rendering
const originalDisplay = node.style.display;
node.style.display = 'none';
// Force reflow
void node.offsetHeight;
node.style.display = originalDisplay;
}, 10);
}
// Update node dimensions display
function updateNodeDimensions(node) {
if (!node || !node.layerConfig) return;
const nodeType = node.getAttribute('data-type');
const dimensionsSection = node.querySelector('.node-dimensions');
if (!dimensionsSection) return;
let dimensionsText = '';
// Generate appropriate dimensions text based on node type
switch (nodeType) {
case 'input':
if (node.layerConfig.shape) {
dimensionsText = node.layerConfig.shape.join(' × ');
}
break;
case 'hidden':
case 'output':
dimensionsText = node.layerConfig.units ? node.layerConfig.units.toString() : '?';
break;
case 'conv':
if (node.layerConfig.inputShape && node.layerConfig.outputShape) {
dimensionsText = `${node.layerConfig.inputShape.join('×')} → ${node.layerConfig.outputShape.join('×')}`;
} else if (node.layerConfig.filters) {
dimensionsText = `? → ${node.layerConfig.filters} filters`;
}
break;
case 'pool':
if (node.layerConfig.inputShape && node.layerConfig.outputShape) {
dimensionsText = `${node.layerConfig.inputShape.join('×')} → ${node.layerConfig.outputShape.join('×')}`;
} else {
dimensionsText = `? → ?`;
}
break;
case 'linear':
if (node.layerConfig.inputFeatures && node.layerConfig.outputFeatures) {
dimensionsText = `${node.layerConfig.inputFeatures} → ${node.layerConfig.outputFeatures}`;
}
break;
}
if (dimensionsText) {
dimensionsSection.textContent = dimensionsText;
node.setAttribute('data-dimensions', dimensionsText);
}
}
// Recursively update nodes downstream from the given node ID
function updateDownstreamNodes(nodeId) {
// Get all connections that start from this node
const outgoingConnections = networkLayers.connections.filter(conn => conn.source === nodeId);
outgoingConnections.forEach(conn => {
const targetId = conn.target;
const targetNode = document.querySelector(`.canvas-node[data-id="${targetId}"]`);
const sourceNode = document.querySelector(`.canvas-node[data-id="${nodeId}"]`);
if (targetNode && sourceNode) {
const targetType = targetNode.getAttribute('data-type');
const sourceType = sourceNode.getAttribute('data-type');
// Skip if source or target type is invalid
if (!targetType || !sourceType) return;
// Find the indices in the layers array
const sourceIndex = networkLayers.layers.findIndex(layer => layer.id === nodeId);
const targetIndex = networkLayers.layers.findIndex(layer => layer.id === targetId);
if (sourceIndex !== -1 && targetIndex !== -1) {
const sourceConfig = networkLayers.layers[sourceIndex].config;
// Update the target node with the source's output shape
if (sourceConfig && sourceConfig.outputShape) {
// Set input shape of target
if (!targetNode.layerConfig) {
targetNode.layerConfig = {};
}
targetNode.layerConfig.inputShape = [...sourceConfig.outputShape];
networkLayers.layers[targetIndex].config.inputShape = [...sourceConfig.outputShape];
// Update parameters
updateNodeParameters(targetNode, targetType, sourceConfig);
// Continue updating downstream
updateDownstreamNodes(targetId);
}
}
}
});
}
// Force update all network connections and parameters
function forceUpdateNetworkParameters() {
// First, identify root nodes (nodes with no incoming connections)
const targetIds = new Set(networkLayers.connections.map(conn => conn.target));
const rootNodeIds = networkLayers.layers
.filter(layer => !targetIds.has(layer.id))
.map(layer => layer.id);
// Update from each root node
rootNodeIds.forEach(nodeId => {
updateDownstreamNodes(nodeId);
});
// After updating all parameters, notify about the network changes
document.dispatchEvent(new CustomEvent('networkUpdated', {
detail: networkLayers
}));
}
// Get the current network architecture
function getNetworkArchitecture() {
return networkLayers;
}
// Clear all nodes from the canvas
function clearAllNodes() {
// Clear all nodes and connections
document.querySelectorAll('.canvas-node, .connection').forEach(el => {
el.parentNode.removeChild(el);
});
// Reset network layers
networkLayers = {
layers: [],
connections: []
};
// Reset layer counter
window.neuralNetwork.resetLayerCounter();
// Show the canvas hint
const canvasHint = document.querySelector('.canvas-hint');
if (canvasHint) {
canvasHint.style.display = 'block';
}
// Trigger network updated event
const event = new CustomEvent('networkUpdated', { detail: networkLayers });
document.dispatchEvent(event);
}
// Open layer editor modal
function openLayerEditor(node) {
if (!node) return;
const nodeId = node.getAttribute('data-id');
const nodeType = node.getAttribute('data-type');
const nodeName = node.getAttribute('data-name');
const dimensions = node.getAttribute('data-dimensions');
// Trigger custom event with the node object
const event = new CustomEvent('openLayerEditor', {
detail: {
id: nodeId,
type: nodeType,
name: nodeName,
dimensions: dimensions,
node: node // Pass the node object
}
});
document.dispatchEvent(event);
}
// Create SVG container for connections
function createSVGContainer() {
const svgContainer = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svgContainer.classList.add('svg-container');
svgContainer.style.position = 'absolute';
svgContainer.style.top = '0';
svgContainer.style.left = '0';
svgContainer.style.width = '100%';
svgContainer.style.height = '100%';
svgContainer.style.pointerEvents = 'none';
svgContainer.style.zIndex = '5';
canvas.appendChild(svgContainer);
return svgContainer;
}
// Export functions
window.dragDrop = {
getNetworkArchitecture,
clearAllNodes,
updateConnections
};
// Expose the drag functions to the window for debugging
window.startDrag = startDrag;
window.dragNode = dragNode;
window.stopDrag = stopDrag;
window.deleteNode = deleteNode;
} |