Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x | import { useRef, useEffect, useCallback, useState } from 'react';
import { useSimulation, useSimulationDispatch } from '../context/SimulationContext';
import { useParameters } from '../context/ParametersContext';
import { useAnimationFrame } from '../hooks/useAnimationFrame';
import { useKeyboard } from '../hooks/useKeyboard';
import {
drawAtom,
drawFireball,
drawBond,
drawBondWithLength,
drawVelocityVector,
drawAtomInfo,
drawMotionTrail
} from '../utils/renderer';
import './SimulationCanvas.css';
/**
* Simulation Canvas Component
* Renders the molecular dynamics simulation using HTML5 Canvas
* Supports zoom, pan, atom selection, dragging, multi-select, touch, and recording
*/
function SimulationCanvas() {
const canvasRef = useRef(null);
const containerRef = useRef(null);
const simulation = useSimulation();
const parameters = useParameters();
const dispatch = useSimulationDispatch();
const keys = useKeyboard();
// Local state for interactions
const [isPanning, setIsPanning] = useState(false);
const [lastMousePos, setLastMousePos] = useState({ x: 0, y: 0 });
const [isBoxSelecting, setIsBoxSelecting] = useState(false);
const [boxSelectStart, setBoxSelectStart] = useState({ x: 0, y: 0 });
const [boxSelectEnd, setBoxSelectEnd] = useState({ x: 0, y: 0 });
const [touchState, setTouchState] = useState({ touches: [], lastDist: 0 });
// Initialize simulation on mount
useEffect(() => {
dispatch({ type: 'INITIALIZE' });
}, [dispatch]);
// Update canvas size based on container
useEffect(() => {
const canvas = canvasRef.current;
Iif (!canvas) return;
const updateSize = () => {
const container = canvas.parentElement;
const width = container.clientWidth;
const height = Math.min(container.clientWidth * 0.75, 600);
canvas.width = width;
canvas.height = height;
dispatch({
type: 'SET_SIZE',
payload: {
x: width / simulation.scale,
y: height / simulation.scale,
z: height / simulation.scale,
},
});
};
updateSize();
window.addEventListener('resize', updateSize);
return () => window.removeEventListener('resize', updateSize);
}, [dispatch, simulation.scale]);
// Keyboard shortcuts
useEffect(() => {
const handleKeyDown = (e) => {
// Space to pause/resume
if (e.code === 'Space' && e.target === document.body) {
e.preventDefault();
dispatch({ type: 'TOGGLE_PAUSE' });
}
// R to reset
if (e.code === 'KeyR' && e.target === document.body && !e.ctrlKey) {
dispatch({ type: 'RESET_SIMULATION' });
}
// Ctrl+Z to undo
if (e.code === 'KeyZ' && e.ctrlKey && !e.shiftKey) {
e.preventDefault();
dispatch({ type: 'UNDO' });
}
// Ctrl+Shift+Z or Ctrl+Y to redo
if ((e.code === 'KeyZ' && e.ctrlKey && e.shiftKey) || (e.code === 'KeyY' && e.ctrlKey)) {
e.preventDefault();
dispatch({ type: 'REDO' });
}
// F for fullscreen
if (e.code === 'KeyF' && e.target === document.body) {
dispatch({ type: 'TOGGLE_FULLSCREEN' });
}
// V for velocity vectors
if (e.code === 'KeyV' && e.target === document.body) {
dispatch({ type: 'TOGGLE_VELOCITY_VECTORS' });
}
// L for labels
if (e.code === 'KeyL' && e.target === document.body) {
dispatch({ type: 'TOGGLE_ATOM_LABELS' });
}
// B for bonds
if (e.code === 'KeyB' && e.target === document.body) {
dispatch({ type: 'TOGGLE_BONDS' });
}
// Delete to remove selected atom(s)
if (e.code === 'Delete') {
if (simulation.selectedAtomIds?.length > 0) {
dispatch({ type: 'PUSH_UNDO' });
dispatch({ type: 'DELETE_SELECTED_ATOMS' });
} else if (simulation.selectedAtomId !== null) {
dispatch({ type: 'PUSH_UNDO' });
dispatch({ type: 'REMOVE_ATOM', payload: { id: simulation.selectedAtomId } });
dispatch({ type: 'SELECT_ATOM', payload: null });
}
}
// Ctrl+C to copy
if (e.code === 'KeyC' && e.ctrlKey) {
e.preventDefault();
dispatch({ type: 'COPY_ATOMS' });
}
// Ctrl+V to paste
if (e.code === 'KeyV' && e.ctrlKey) {
e.preventDefault();
const canvas = canvasRef.current;
if (canvas) {
const rect = canvas.getBoundingClientRect();
dispatch({
type: 'PASTE_ATOMS',
payload: { x: simulation.size.x / 2, y: simulation.size.y / 2 }
});
}
}
// Escape to clear selection/measurement
if (e.code === 'Escape') {
dispatch({ type: 'CLEAR_SELECTION' });
dispatch({ type: 'CLEAR_MEASUREMENT' });
}
// M for measurement mode
if (e.code === 'KeyM' && e.target === document.body) {
dispatch({ type: 'SET_MEASUREMENT_MODE', payload: simulation.measurementMode ? null : 'distance' });
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [dispatch, simulation.selectedAtomId, simulation.selectedAtomIds, simulation.measurementMode, simulation.size]);
// Handle keyboard input for player movement
useEffect(() => {
Iif (simulation.playerId === null) return;
const playerAtom = simulation.atoms.find(a => a.id === simulation.playerId);
Iif (!playerAtom) return;
let forceX = 0;
let forceY = 0;
Iif (keys.ArrowLeft) forceX -= simulation.thrust;
Iif (keys.ArrowRight) forceX += simulation.thrust;
Iif (keys.ArrowUp) forceY -= simulation.thrust;
Iif (keys.ArrowDown) forceY += simulation.thrust;
// Always update force (including zero when no keys pressed)
dispatch({
type: 'SET_PLAYER_FORCE',
payload: { x: forceX, y: forceY },
});
// Add fireball effect when force is applied
Iif (forceX !== 0 || forceY !== 0) {
const v = Math.sqrt(forceX * forceX + forceY * forceY);
const vx = -forceX / v * 0.1;
const vy = -forceY / v * 0.1;
dispatch({
type: 'ADD_FIREBALL',
payload: {
id: Date.now(),
pos: {
x: playerAtom.pos.x + vx * playerAtom.radius,
y: playerAtom.pos.y + vy * playerAtom.radius,
},
vel: { x: vx + (Math.random() - 0.5) * 0.2, y: vy - Math.random() * 0.4 },
radius: 0.5,
time: 0,
lifetime: 30,
},
});
}
}, [keys, simulation.playerId, simulation.atoms, simulation.thrust, dispatch]);
// Mouse wheel for zoom (only if not locked)
const handleWheel = useCallback((e) => {
if (simulation.lockZoom) {
// Don't prevent default if zoom is locked - let page scroll normally
return;
}
e.preventDefault();
const delta = e.deltaY > 0 ? 0.9 : 1.1;
dispatch({ type: 'SET_ZOOM', payload: simulation.zoom * delta });
}, [dispatch, simulation.zoom, simulation.lockZoom]);
// Get canvas coordinates from mouse event
const getCanvasCoords = useCallback((e) => {
const canvas = canvasRef.current;
const rect = canvas.getBoundingClientRect();
const x = (e.clientX - rect.left - simulation.pan.x) / (simulation.scale * simulation.zoom);
const y = (e.clientY - rect.top - simulation.pan.y) / (simulation.scale * simulation.zoom);
return { x, y };
}, [simulation.scale, simulation.zoom, simulation.pan]);
// Find atom at position
const findAtomAtPos = useCallback((canvasX, canvasY) => {
for (const atom of simulation.atoms) {
const dx = atom.pos.x - canvasX;
const dy = atom.pos.y - canvasY;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < atom.radius * 1.5) {
return atom;
}
}
return null;
}, [simulation.atoms]);
// Mouse down handler
const handleMouseDown = useCallback((e) => {
const coords = getCanvasCoords(e);
const atom = findAtomAtPos(coords.x, coords.y);
if (e.button === 0) { // Left click
// Measurement mode
if (simulation.measurementMode === 'distance' && atom) {
dispatch({ type: 'ADD_MEASUREMENT_ATOM', payload: atom.id });
return;
}
// Shift+click for multi-select
if (e.shiftKey) {
if (atom) {
if (simulation.selectedAtomIds?.includes(atom.id)) {
dispatch({ type: 'REMOVE_FROM_SELECTION', payload: atom.id });
} else {
dispatch({ type: 'ADD_TO_SELECTION', payload: atom.id });
}
} else {
// Start box selection
setIsBoxSelecting(true);
setBoxSelectStart(coords);
setBoxSelectEnd(coords);
}
} else if (atom) {
dispatch({ type: 'SELECT_ATOM', payload: atom.id });
dispatch({ type: 'SET_DRAGGING_ATOM', payload: atom.id });
dispatch({ type: 'CLEAR_SELECTION' }); // Clear multi-selection
} else {
dispatch({ type: 'SELECT_ATOM', payload: null });
dispatch({ type: 'CLEAR_SELECTION' });
}
} else if (e.button === 1 || e.button === 2) { // Middle or right click for pan
setIsPanning(true);
setLastMousePos({ x: e.clientX, y: e.clientY });
}
}, [getCanvasCoords, findAtomAtPos, dispatch]);
// Mouse move handler
const handleMouseMove = useCallback((e) => {
if (isPanning) {
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
dispatch({
type: 'SET_PAN',
payload: { x: simulation.pan.x + dx, y: simulation.pan.y + dy }
});
setLastMousePos({ x: e.clientX, y: e.clientY });
} else if (simulation.draggingAtomId !== null) {
const coords = getCanvasCoords(e);
dispatch({
type: 'UPDATE_ATOM_POSITION',
payload: { id: simulation.draggingAtomId, position: { x: coords.x, y: coords.y } },
});
}
}, [isPanning, lastMousePos, simulation.pan, simulation.draggingAtomId, dispatch, getCanvasCoords]);
// Mouse up handler
const handleMouseUp = useCallback(() => {
setIsPanning(false);
if (simulation.draggingAtomId !== null) {
dispatch({ type: 'SET_DRAGGING_ATOM', payload: null });
}
}, [simulation.draggingAtomId, dispatch]);
// Double click to add atom
const handleDoubleClick = useCallback((e) => {
const coords = getCanvasCoords(e);
dispatch({ type: 'PUSH_UNDO' });
dispatch({
type: 'ADD_ATOM',
payload: { x: coords.x, y: coords.y, z: 0, atomType: simulation.selectedAtomType }
});
}, [getCanvasCoords, dispatch, simulation.selectedAtomType]);
// Context menu prevention
const handleContextMenu = useCallback((e) => {
e.preventDefault();
}, []);
// Render function
const render = useCallback(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
const {
atoms, bonds, fireballs, clearScreen, scale, size, showBonds,
zoom, pan, selectedAtomId, showVelocityVectors, showAtomLabels,
showBondLengths, theme, colorByVelocity, showMotionTrails, positionHistory
} = simulation;
// Apply zoom and pan transforms
ctx.save();
// Clear canvas with theme-appropriate color
const bgColor = theme === 'light' ? '#f0f4f8' : '#0a0a12';
if (clearScreen) {
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
// 2D rendering
// Apply transformations
ctx.translate(pan.x, pan.y);
ctx.scale(zoom, zoom);
// Draw boundary
ctx.strokeStyle = theme === 'light' ? 'rgba(0, 0, 0, 0.3)' : 'rgba(255, 255, 255, 0.3)';
ctx.lineWidth = 2 / zoom;
ctx.strokeRect(2 / zoom, 2 / zoom, size.x * scale - 4 / zoom, size.y * scale - 4 / zoom);
// Draw bonds first (behind atoms)
if (showBonds && bonds) {
bonds.forEach(bond => {
const atom1 = atoms.find(a => a.id === bond.atom1Id);
const atom2 = atoms.find(a => a.id === bond.atom2Id);
if (atom1 && atom2) {
if (showBondLengths) {
drawBondWithLength(ctx, atom1, atom2, bond.order, scale, true);
} else {
drawBond(ctx, atom1, atom2, bond.order, scale);
}
}
});
}
// Draw motion trails (behind atoms)
if (showMotionTrails) {
atoms.forEach(atom => {
const history = positionHistory[atom.id];
if (history && history.length > 1) {
const trailColor = atom.color || '#888888';
drawMotionTrail(ctx, history, trailColor, scale, atom.radius);
}
});
}
// Draw atoms
atoms.forEach(atom => {
drawAtom(ctx, atom, scale, atom.id === simulation.playerId, showAtomLabels, colorByVelocity);
// Draw velocity vectors if enabled
if (showVelocityVectors) {
drawVelocityVector(ctx, atom, scale, zoom);
}
});
// Draw selection highlight
if (selectedAtomId !== null) {
const selectedAtom = atoms.find(a => a.id === selectedAtomId);
if (selectedAtom) {
const screenPos = { x: scale * selectedAtom.pos.x, y: scale * selectedAtom.pos.y };
const radius = scale * selectedAtom.radius;
ctx.strokeStyle = 'rgba(100, 200, 255, 0.9)';
ctx.lineWidth = 3 / zoom;
ctx.setLineDash([5 / zoom, 5 / zoom]);
ctx.beginPath();
ctx.arc(screenPos.x, screenPos.y, radius + 8 / zoom, 0, Math.PI * 2);
ctx.stroke();
ctx.setLineDash([]);
}
}
// Draw fireballs
fireballs.forEach(fireball => {
drawFireball(ctx, fireball, scale);
});
ctx.restore();
// Draw atom info tooltip (outside transform)
if (selectedAtomId !== null) {
const selectedAtom = atoms.find(a => a.id === selectedAtomId);
if (selectedAtom) {
drawAtomInfo(ctx, selectedAtom, scale * zoom, canvas.width);
}
}
// Draw info overlay
if (parameters.isLoaded) {
ctx.fillStyle = 'rgba(0, 255, 100, 0.8)';
ctx.font = '12px monospace';
ctx.fillText('ReaxFF Parameters Loaded ✓', 10, 20);
}
// Atom count warning
if (atoms.length >= simulation.atomCountWarning) {
ctx.fillStyle = 'rgba(255, 200, 50, 0.9)';
ctx.font = '12px monospace';
ctx.fillText(`⚠ ${atoms.length} atoms (may affect performance)`, 10, canvas.height - 10);
}
// Update status
if (atoms.length > 0) {
const player = atoms.find(a => a.id === simulation.playerId);
if (player) {
dispatch({
type: 'SET_STATUS',
payload: `${player.symbol || 'H'} @ (${player.pos.x.toFixed(1)}, ${player.pos.y.toFixed(1)})`,
});
}
}
}, [simulation, parameters.isLoaded, dispatch]);
// Update simulation state
const update = useCallback(() => {
if (simulation.isPaused) return;
// Run physics simulation step (respecting time multiplier)
for (let i = 0; i < Math.ceil(simulation.timeStepMultiplier); i++) {
dispatch({ type: 'PHYSICS_STEP' });
}
// Update position history for motion trails
if (simulation.showMotionTrails) {
dispatch({ type: 'UPDATE_POSITION_HISTORY' });
}
// Update fireballs
const updatedFireballs = simulation.fireballs
.map(fb => ({
...fb,
pos: {
x: fb.pos.x + fb.vel.x,
y: fb.pos.y + fb.vel.y,
},
radius: fb.radius + 0.033,
time: fb.time + 1,
}))
.filter(fb => fb.time < fb.lifetime);
dispatch({ type: 'UPDATE_FIREBALLS', payload: updatedFireballs });
}, [simulation.isPaused, simulation.fireballs, simulation.timeStepMultiplier, simulation.showMotionTrails, dispatch]);
// Screenshot function
const takeScreenshot = useCallback(() => {
const canvas = canvasRef.current;
if (!canvas) return null;
return canvas.toDataURL('image/png');
}, []);
// Expose screenshot function
useEffect(() => {
Eif (containerRef.current) {
containerRef.current.takeScreenshot = takeScreenshot;
}
}, [takeScreenshot]);
// Animation loop
useAnimationFrame(() => {
update();
render();
});
return (
<div
ref={containerRef}
className={`canvas-container ${simulation.isFullscreen ? 'fullscreen' : ''}`}
>
{/* View Controls Toolbar */}
<div className="view-controls-toolbar">
<button
className={`toolbar-btn ${simulation.lockZoom ? 'active' : ''}`}
onClick={() => dispatch({ type: 'TOGGLE_LOCK_ZOOM' })}
title={simulation.lockZoom ? 'Unlock Zoom (scroll disabled)' : 'Lock Zoom (scroll enabled)'}
>
{simulation.lockZoom ? '🔒' : '🔓'}
</button>
<button
className="toolbar-btn"
onClick={() => dispatch({ type: 'SET_ZOOM', payload: simulation.zoom * 1.2 })}
title="Zoom In"
disabled={simulation.lockZoom}
>
➕
</button>
<button
className="toolbar-btn"
onClick={() => dispatch({ type: 'SET_ZOOM', payload: simulation.zoom * 0.8 })}
title="Zoom Out"
disabled={simulation.lockZoom}
>
➖
</button>
<button
className="toolbar-btn"
onClick={() => dispatch({ type: 'RESET_VIEW' })}
title="Reset View"
>
🏠
</button>
</div>
<canvas
ref={canvasRef}
className="simulation-canvas"
aria-label="Molecular Dynamics Simulation"
onWheel={handleWheel}
onMouseDown={handleMouseDown}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
onDoubleClick={handleDoubleClick}
onContextMenu={handleContextMenu}
/>
{simulation.isFullscreen && (
<button
className="exit-fullscreen-btn"
onClick={() => dispatch({ type: 'TOGGLE_FULLSCREEN' })}
>
Exit Fullscreen (F)
</button>
)}
<div className="canvas-zoom-indicator">
{(simulation.zoom * 100).toFixed(0)}%
</div>
</div>
);
}
export default SimulationCanvas;
|