|
|
|
|
|
|
|
function enhanceSVGContent2(originalContent) { |
|
const parser = new DOMParser(); |
|
const doc = parser.parseFromString(originalContent, 'image/svg+xml'); |
|
|
|
|
|
const styleElement = doc.createElementNS('http://www.w3.org/2000/svg', 'style'); |
|
styleElement.textContent = ` |
|
/* Memory Block (free memory) */ |
|
path[data-element-type="memory-block"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="memory-block"]:hover { |
|
fill: #a5d6a7 !important; /* slightly darker than original */ |
|
transform: translate(0, -2px); |
|
} |
|
|
|
/* Memory Block (updated) */ |
|
path[data-element-type="memory-block-updated"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="memory-block-updated"]:hover { |
|
fill: #81c784 !important; |
|
transform: scale(1.02) translate(0, -2px); |
|
} |
|
|
|
/* Stack Segment */ |
|
path[data-element-type="stack"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="stack"]:hover { |
|
fill: #ffd54f !important; |
|
transform: translate(0, -2px); |
|
} |
|
|
|
/* Read Operation Arrow */ |
|
path[data-element-type="read"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="read"]:hover { |
|
stroke: #1e88e5 !important; |
|
stroke-width: 4 !important; |
|
} |
|
|
|
/* Write Operation Arrow */ |
|
path[data-element-type="write"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="write"]:hover { |
|
stroke: #d32f2f !important; |
|
stroke-width: 4 !important; |
|
} |
|
|
|
/* Cache Operation Arrow */ |
|
path[data-element-type="cache"] { |
|
transition: all 0.3s; |
|
cursor: pointer; |
|
} |
|
path[data-element-type="cache"]:hover { |
|
stroke: #fbc02d !important; |
|
stroke-width: 4 !important; |
|
} |
|
`; |
|
doc.documentElement.insertBefore(styleElement, doc.documentElement.firstChild); |
|
|
|
|
|
doc.querySelectorAll('path[fill="#c8e6c9"]').forEach((node, index) => { |
|
node.setAttribute('data-element-id', `memory-block-${index}`); |
|
node.setAttribute('data-element-type', 'memory-block'); |
|
}); |
|
|
|
|
|
doc.querySelectorAll('path[fill="#a5d6a7"]').forEach((node, index) => { |
|
node.setAttribute('data-element-id', `memory-block-updated-${index}`); |
|
node.setAttribute('data-element-type', 'memory-block-updated'); |
|
}); |
|
|
|
|
|
doc.querySelectorAll('path[fill="#ffe082"]').forEach((node, index) => { |
|
node.setAttribute('data-element-id', `stack-${index}`); |
|
node.setAttribute('data-element-type', 'stack'); |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
const arrowTypes = { |
|
'#42a5f5': 'read', |
|
'#ef5350': 'write', |
|
'#ffca28': 'cache' |
|
}; |
|
|
|
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); |
|
}); |
|
}); |
|
|
|
|
|
doc.documentElement.setAttribute('width', '100%'); |
|
doc.documentElement.setAttribute('height', '100%'); |
|
doc.documentElement.setAttribute('preserveAspectRatio', 'xMidYMid meet'); |
|
|
|
return new XMLSerializer().serializeToString(doc); |
|
} |
|
|
|
|
|
async function loadSVG(url, containerId) { |
|
try { |
|
console.log('Loading SVG from:', url); |
|
const response = await fetch(url); |
|
if (!response.ok) { |
|
throw new Error(`HTTP error! Status: ${response.status}`); |
|
} |
|
const svgText = await response.text(); |
|
const enhancedSVG = enhanceSVGContent2(svgText); |
|
document.getElementById(containerId).innerHTML = enhancedSVG; |
|
} catch (error) { |
|
console.error('Error loading SVG:', error); |
|
document.getElementById(containerId).innerHTML = '<p>Error loading SVG.</p>'; |
|
} |
|
} |
|
|
|
|
|
loadSVG('../assets/images/first_steps_memory_profile.svg', 'svg-first_steps_memory_profile'); |
|
|
|
|
|
const svgContainer2 = document.getElementById('svg-first_steps_memory_profile'); |
|
|
|
svgContainer2.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 = { |
|
'memory-block': 'Memory Block', |
|
'memory-block-updated': 'Memory Block (updated)', |
|
'stack': 'Stack Segment', |
|
'read': 'Memory Read', |
|
'write': 'Memory Write', |
|
'cache': 'Cache Operation' |
|
}; |
|
const description = descriptions[elementType] || elementType; |
|
document.getElementById('svg-first_steps_memory_profile-info').textContent = `Hovering over: ${description} (${elementId})`; |
|
} |
|
}); |
|
|
|
svgContainer2.addEventListener('mouseout', function() { |
|
document.getElementById('svg-first_steps_memory_profile-info').textContent = 'Hover over the elements to see their details'; |
|
}); |
|
|