Spaces:
Running
Running
File size: 5,615 Bytes
60aea95 |
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 |
// Function to enhance the SVG content by adding styles and data attributes
function enhanceSVGContent(originalContent) {
const parser = new DOMParser();
const doc = parser.parseFromString(originalContent, 'image/svg+xml');
// Create a style element with hover effects and insert it as the first child of the SVG
const styleElement = doc.createElementNS('http://www.w3.org/2000/svg', 'style');
styleElement.textContent = `
path[data-element-type="layer"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="layer"]:hover {
fill: #b197fc !important;
transform: translate(0, -2px);
}
path[data-element-type="layer-updated"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="layer-updated"]:hover {
fill:rgb(103, 56, 244) !important;
transform: scale(1.02);
transform: translate(0, -2px);
}
path[data-element-type="gradient"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="gradient"]:hover {
fill: #f06595 !important;
transform: translate(0, -2px);
}
path[data-element-type="forward"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="forward"]:hover {
stroke: #0c8599 !important;
stroke-width: 4 !important;
}
path[data-element-type="backward"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="backward"]:hover {
stroke: #e8590c !important;
stroke-width: 4 !important;
}
path[data-element-type="optimization"] {
transition: all 0.3s;
cursor: pointer;
}
path[data-element-type="optimization"]:hover {
stroke: #087f5b !important;
stroke-width: 4 !important;
}
`;
doc.documentElement.insertBefore(styleElement, doc.documentElement.firstChild);
// Process neural network layers (purple nodes)
doc.querySelectorAll('path[fill="#d0bfff"]').forEach((node, index) => {
node.setAttribute('data-element-id', `layer-${index}`);
node.setAttribute('data-element-type', 'layer');
});
doc.querySelectorAll('path[fill="#9775fa"]').forEach((node, index) => {
node.setAttribute('data-element-id', `layer-updated-${index}`);
node.setAttribute('data-element-type', 'layer-updated');
});
// Process gradient nodes (pink nodes)
doc.querySelectorAll('path[fill="#f783ac"]').forEach((node, index) => {
node.setAttribute('data-element-id', `gradient-${index}`);
node.setAttribute('data-element-type', 'gradient');
});
// Process arrows by matching stroke colors
const arrowTypes = {
'#15aabf': 'forward',
'#fd7e14': 'backward',
'#099268': 'optimization'
};
Object.entries(arrowTypes).forEach(([color, type]) => {
doc.querySelectorAll(`path[stroke="${color}"]`).forEach((arrow, index) => {
arrow.setAttribute('data-element-id', `${type}-${index}`);
arrow.setAttribute('data-element-type', type);
});
});
// Make the SVG responsive
doc.documentElement.setAttribute('width', '100%');
doc.documentElement.setAttribute('height', '100%');
doc.documentElement.setAttribute('preserveAspectRatio', 'xMidYMid meet');
return new XMLSerializer().serializeToString(doc);
}
// Function to load an SVG file via fetch
async function loadSVG(url, containerId) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const svgText = await response.text();
const enhancedSVG = enhanceSVGContent(svgText);
document.getElementById(containerId).innerHTML = enhancedSVG;
} catch (error) {
console.error('Error loading SVG:', error);
document.getElementById(containerId).innerHTML = '<p>Error loading SVG.</p>';
}
}
// Load the SVG file (adjust the path if needed)
loadSVG('../assets/images/activation_recomputation.svg', 'svg-activation_recomputation');
// Set up event listeners to display a description of the hovered element
const svgContainer3 = document.getElementById('svg-activation_recomputation');
svgContainer3.addEventListener('mouseover', function (event) {
const target = event.target;
if (target.tagName.toLowerCase() === 'path' && target.hasAttribute('data-element-id')) {
const elementId = target.getAttribute('data-element-id');
const elementType = target.getAttribute('data-element-type');
const descriptions = {
layer: 'Neural Network Layer',
'layer-updated': 'Neural Network Layer (updated)',
gradient: 'Gradient Update Layer',
forward: 'Forward Pass Connection',
backward: 'Backward Pass Connection',
optimization: 'Optimization Step'
};
const description = descriptions[elementType] || elementType;
document.getElementById('svg-activation_recomputation-info').textContent = `Hovering over: ${description} (${elementId})`;
}
});
svgContainer3.addEventListener('mouseout', function () {
document.getElementById('svg-activation_recomputation-info').textContent = 'Hover over the network elements to see their details';
});
|