/* Basic Reset & Body Style */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: sans-serif;
line-height: 1.6;
padding: 20px;
background-color: #f4f4f4;
}
h1, h2 {
margin-bottom: 15px;
color: #333;
text-align: center;
}
/* Main Layout Container */
.container {
display: flex;
flex-wrap: wrap; /* Allow wrapping on smaller screens */
gap: 20px;
justify-content: space-around; /* Distribute space */
align-items: flex-start; /* Align items to the top */
max-width: 1200px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
/* Diagram Area */
#diagram-area {
position: relative; /* Needed for absolute positioning of boxes and LeaderLine */
height: 250px; /* Adjust as needed */
width: 100%; /* Take full width in its flex item */
min-width: 300px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 20px; /* Space below if wrapping */
flex: 1 1 400px; /* Flex properties: grow, shrink, basis */
}
.box {
position: absolute; /* Position boxes precisely */
background-color: #e0e0ff;
border: 1px solid #a0a0cc;
padding: 15px;
border-radius: 5px;
min-width: 100px;
text-align: center;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
cursor: default; /* Indicate non-interactive */
}
/* Specific Box Positions (Adjust these!) */
#box1 { top: 50px; left: 30px; }
#box2 { top: 100px; left: 200px; }
#box3 { top: 50px; left: 370px; }
/* Controls Area */
#controls-area {
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
flex: 1 1 300px; /* Flex properties */
}
.slider-group {
margin-bottom: 15px;
}
.slider-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.slider-group input[type="range"] {
width: calc(100% - 60px); /* Adjust width to fit next to value display */
margin-right: 10px;
vertical-align: middle;
}
.slider-group span {
display: inline-block;
min-width: 40px; /* Ensure space for value */
text-align: right;
font-family: monospace;
vertical-align: middle;
color: #555;
}
.buttons {
margin-top: 20px;
text-align: center;
}
.buttons button {
padding: 10px 20px;
margin: 0 10px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
transition: background-color 0.3s ease;
}
#start-stop-button {
background-color: #4CAF50; /* Green */
color: white;
}
#start-stop-button:hover {
background-color: #45a049;
}
#start-stop-button.running {
background-color: #f44336; /* Red */
}
#start-stop-button.running:hover {
background-color: #da190b;
}
#reset-button {
background-color: #ff9800; /* Orange */
color: white;
}
#reset-button:hover {
background-color: #e68a00;
}
#status {
margin-top: 15px;
font-style: italic;
color: #666;
text-align: center;
}
/* Graph Area */
#graph-area {
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
flex: 2 1 400px; /* Let graph take more space */
min-width: 300px; /* Ensure minimum width */
}
#simulation-chart {
max-width: 100%; /* Make chart responsive within its container */
height: auto;
}
/* LeaderLine default arrow style (can be overridden in JS) */
.leader-line {
z-index: 1; /* Ensure arrows are behind if needed, adjust if they overlap boxes */
}
document.addEventListener('DOMContentLoaded', () => {
// --- DOM Elements ---
const box1 = document.getElementById('box1');
const box2 = document.getElementById('box2');
const box3 = document.getElementById('box3');
const slider1 = document.getElementById('slider1');
const slider1ValueDisplay = document.getElementById('slider1-value');
const slider2 = document.getElementById('slider2');
const slider2ValueDisplay = document.getElementById('slider2-value');
const startStopButton = document.getElementById('start-stop-button');
const resetButton = document.getElementById('reset-button');
const statusDisplay = document.getElementById('status');
const chartCanvas = document.getElementById('simulation-chart');
// --- Simulation State ---
let simulationRunning = false;
let simulationIntervalId = null;
let simulationTime = 0;
const MAX_GRAPH_POINTS = 50; // Limit points on graph for performance
const initialSliderValues = {
slider1: 50,
slider2: 100,
};
let currentSliderValues = { ...initialSliderValues };
// --- Arrows (LeaderLine) ---
let arrow1, arrow2;
function drawArrows() {
// Remove existing arrows if they exist
if (arrow1) arrow1.remove();
if (arrow2) arrow2.remove();
// Create new arrows
arrow1 = new LeaderLine(box1, box2, {
color: 'rgba(0, 0, 200, 0.7)',
size: 3,
path: 'fluid', // Or 'straight', 'arc', 'grid'
endPlug: 'arrow1'
});
arrow2 = new LeaderLine(box2, box3, {
color: 'rgba(0, 0, 200, 0.7)',
size: 3,
path: 'fluid',
endPlug: 'arrow1'
});
}
// Initial Arrow Draw
drawArrows();
// Redraw arrows on window resize
window.addEventListener('resize', () => {
if (arrow1 && arrow2) {
try {
arrow1.position();
arrow2.position();
} catch (e) {
console.warn("Could not reposition arrows, redrawing.", e);
// If position fails (e.g., element removed/hidden temporarily), redraw
drawArrows();
}
}
});
// --- Chart (Chart.js) ---
const ctx = chartCanvas.getContext('2d');
const simulationChart = new Chart(ctx, {
type: 'line',
data: {
labels: [], // Time steps
datasets: [{
label: 'Simulated Value',
data: [], // Calculated values
borderColor: 'rgb(75, 192, 192)',
tension: 0.1, // Makes the line slightly curved
fill: false
}]
},
options: {
scales: {
x: {
title: { display: true, text: 'Time Step' }
},
y: {
title: { display: true, text: 'Value' },
beginAtZero: true // Or adjust as needed
}
},
animation: {
duration: 0 // Disable animation for faster updates during simulation
},
maintainAspectRatio: false // Allow chart to fill container height/width better
}
});
// --- Slider Event Listeners ---
slider1.addEventListener('input', (event) => {
const value = event.target.value;
currentSliderValues.slider1 = parseInt(value, 10);
slider1ValueDisplay.textContent = value;
});
slider2.addEventListener('input', (event) => {
const value = event.target.value;
currentSliderValues.slider2 = parseInt(value, 10);
slider2ValueDisplay.textContent = `${value} ms delay`;
// If simulation is running, restart it with the new speed
if (simulationRunning) {
stopSimulation();
startSimulation();
}
});
// --- Button Event Listeners ---
startStopButton.addEventListener('click', () => {
if (simulationRunning) {
stopSimulation();
} else {
startSimulation();
}
});
resetButton.addEventListener('click', () => {
resetSimulation();
});
// --- Simulation Logic ---
function runSimulationStep() {
simulationTime++;
// ** YOUR SIMULATION LOGIC GOES HERE **
// Example: Calculate a value based on sliders and time
// Let slider1 affect amplitude and time affect the cycle.
const amplitude = currentSliderValues.slider1 / 10; // Scale slider value
// Use a sine wave for a simple cyclical pattern
const calculatedValue = amplitude * Math.sin(simulationTime * 0.5) + 50; // Base value 50
// Update Box Content (Optional Example)
box2.textContent = `Processing: ${calculatedValue.toFixed(1)}`;
// Update Graph
addDataToChart(simulationTime, calculatedValue);
}
function addDataToChart(label, data) {
simulationChart.data.labels.push(label);
simulationChart.data.datasets[0].data.push(data);
// Limit the number of data points on the graph
if (simulationChart.data.labels.length > MAX_GRAPH_POINTS) {
simulationChart.data.labels.shift(); // Remove oldest label
simulationChart.data.datasets[0].data.shift(); // Remove oldest data point
}
simulationChart.update(); // Redraw the chart
}
function startSimulation() {
if (simulationRunning) return; // Already running
const simulationSpeedMs = currentSliderValues.slider2; // Get speed from slider
simulationIntervalId = setInterval(runSimulationStep, simulationSpeedMs);
simulationRunning = true;
startStopButton.textContent = 'Stop';
startStopButton.classList.add('running');
statusDisplay.textContent = `Status: Running (Update every ${simulationSpeedMs}ms)`;
console.log("Simulation Started");
}
function stopSimulation() {
if (!simulationRunning) return; // Already stopped
clearInterval(simulationIntervalId);
simulationIntervalId = null;
simulationRunning = false;
startStopButton.textContent = 'Start';
startStopButton.classList.remove('running');
statusDisplay.textContent = 'Status: Stopped';
console.log("Simulation Stopped");
}
function resetSimulation() {
stopSimulation();
console.log("Resetting Simulation");
simulationTime = 0;
// Reset sliders to initial values
currentSliderValues = { ...initialSliderValues };
slider1.value = initialSliderValues.slider1;
slider1ValueDisplay.textContent = initialSliderValues.slider1;
slider2.value = initialSliderValues.slider2;
slider2ValueDisplay.textContent = `${initialSliderValues.slider2} ms delay`;
// Reset graph
simulationChart.data.labels = [];
simulationChart.data.datasets[0].data = [];
simulationChart.update();
// Reset box content (optional)
box1.textContent = 'Input Source';
box2.textContent = 'Processing Unit';
box3.textContent = 'Output Display';
// Reset status
statusDisplay.textContent = 'Status: Idle (Reset)';
}
// --- Initial Setup ---
function initializeFramework() {
// Set initial slider display values
slider1ValueDisplay.textContent = initialSliderValues.slider1;
slider2ValueDisplay.textContent = `${initialSliderValues.slider2} ms delay`;
// Initial Reset just sets things up cleanly
resetSimulation();
console.log("Framework Initialized");
}
// Run initialization
initializeFramework();
}); // End DOMContentLoaded