To achieve a responsive design that ensures the SVG and ASCII content maintain their proportionality while fitting into the available viewport, you can utilize CSS transforms to scale the elements. This way, we can avoid messing with the coordinates and sizes directly in your original JavaScript code, which would keep everything aligned correctly.
Here's the full updated code that accomplishes this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trans-Dimensional Quantum ASCII/SVG Nexus</title>
<style>
* {
font-size: 20px !important;
line-height: 20px;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1c1c1c;
color: #ffffff;
font-family: monospace;
white-space: pre;
overflow: hidden; /* Prevent scrollbars */
}
.container {
position: relative;
width: 100%;
height: 100%;
transform: scale(0.9); /* Adjust scaling for embedment */
transform-origin: top left; /* Scale from top-left corner */
}
pre {
position: absolute;
z-index: 1;
top: 0;
left: 0;
}
svg {
position: absolute;
width: 200px;
height: 400px;
top: 0;
left: 0;
transform: scale(0.9); /* Ensure SVG scales with container */
transform-origin: top left; /* Scale from top-left corner */
}
#quantumAsciiMatrix {
z-index: 1;
}
</style>
</head>
<body>
<div class="container">
<pre id="quantumAsciiMatrix"></pre>
<svg id="vectorialManifestation" viewBox="0 0 200 400">
<text id="protonQuantization" x="100" y="100" text-anchor="middle" dominant-baseline="middle" fill="red" font-family="monospace" font-size="20">O</text>
<text id="neutronQuantization" x="100" y="100" text-anchor="middle" dominant-baseline="middle" fill="black" opacity="0.5" font-family="monospace" font-size="20">:</text>
</svg>
</div>
<script>
const quantumAsciiMatrixElement = document.getElementById('quantumAsciiMatrix');
const vectorialManifestationElement = document.getElementById('vectorialManifestation');
const protonQuantizationElement = document.getElementById('protonQuantization');
const neutronQuantizationElement = document.getElementById('neutronQuantization');
const hyperDimensionalStrataRows = 20;
const hyperDimensionalStrataCols = 17;
let protonCoordX = 8;
let protonCoordY = hyperDimensionalStrataRows - 1;
const interruptiveTopology = [];
const obstructiveCount = 5;
const modulationInterval = 500;
function initializeObstructions() {
for (let i = 0; i < obstructiveCount; i++) {
interruptiveTopology.push({
coordX: Math.floor(Math.random() * hyperDimensionalStrataCols),
coordY: Math.floor(Math.random() * hyperDimensionalStrataRows)
});
}
}
function renderQuantumMatrix() {
const asciiMatrix = Array(hyperDimensionalStrataRows).fill('').map(() => Array(hyperDimensionalStrataCols).fill(' '));
interruptiveTopology.forEach(obstacle => {
if (obstacle.coordY < hyperDimensionalStrataRows) {
asciiMatrix[obstacle.coordY][obstacle.coordX] = ':';
}
});
const isProtonActive = interruptiveTopology.some(obstacle =>
obstacle.coordX === protonCoordX &&
obstacle.coordY === protonCoordY
);
asciiMatrix[protonCoordY][protonCoordX] = isProtonActive ? ' ' : 'O';
let outputRepresentation = '';
for (let row of asciiMatrix) {
outputRepresentation += '|' + row.join('') + '|\n';
}
quantumAsciiMatrixElement.innerHTML = outputRepresentation;
protonQuantizationElement.setAttribute('x', protonCoordX * 11.76 + 7);
protonQuantizationElement.setAttribute('y', protonCoordY * 20 + 10);
vectorialManifestationElement.style.display = isProtonActive ? 'block' : 'none';
}
function updateObstructions() {
interruptiveTopology.forEach(obstacle => {
if (obstacle.coordY < hyperDimensionalStrataRows - 1) {
obstacle.coordY++;
} else {
obstacle.coordY = 0;
obstacle.coordX = Math.floor(Math.random() * hyperDimensionalStrataCols);
}
});
}
window.addEventListener('keydown', (event) => {
switch (event.key) {
case 'ArrowLeft':
protonCoordX = Math.max(protonCoordX - 1, 0);
break;
case 'ArrowRight':
protonCoordX = Math.min(protonCoordX + 1, hyperDimensionalStrataCols - 1);
break;
case 'ArrowUp':
protonCoordY = Math.max(protonCoordY - 1, 0);
break;
case 'ArrowDown':
protonCoordY = Math.min(protonCoordY + 1, hyperDimensionalStrataRows - 1);
break;
}
renderQuantumMatrix();
});
setInterval(() => {
updateObstructions();
renderQuantumMatrix();
}, modulationInterval);
initializeObstructions();
renderQuantumMatrix();
</script>
</body>
</html>
Key Modifications:
-
Container: Wrapped both the SVG and the ASCII representation inside a .container div to manage the scaling uniformly.
-
CSS Transform: Applied a scaling transformation to both the container and the SVG to ensure they fit within the viewport without altering their internal dimensions or coordinates. The scaling may be adjusted as per the desired fit.
-
Positioning: Ensured the pre and svg elements position absolutely relative to their containing div.
This approach keeps the SVG and ASCII matrix proportional and ensures they react responsively to resizing of the viewport. Adjust the scale value in the CSS as needed to fit your specific layout preferences.