Skip to content

Commit b5009b2

Browse files
committed
New 3D tensor mode
1 parent bd66a80 commit b5009b2

14 files changed

Lines changed: 1751 additions & 178 deletions

CLAUDE.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ This file provides guidance to Claude Code when working with code in this reposi
44

55
## Project Overview
66

7-
This is **Interactive Linear Algebra** - an educational web application for teaching vector operations through visual experimentation. Students can draw vectors on a 2D Cartesian plane and perform operations (addition, subtraction, scalar multiplication, dot products, projections) with smooth animated visualizations.
7+
This is **Interactive Linear Algebra** - an educational web application for teaching linear algebra concepts through visual experimentation. The application supports three interaction modes:
8+
9+
- **Vector Mode**: Draw vectors on a 2D Cartesian plane and perform operations (addition, subtraction, scalar multiplication, dot products, projections) with smooth animated visualizations
10+
- **Matrix Mode**: Visualize 2×2 matrix transformations and their effects on basis vectors
11+
- **Tensor Mode**: Explore tensors of different ranks (0-3) in an interactive 3D space with drag-to-rotate and scroll-to-zoom controls
812

913
This application also serves as a reference implementation of the **Bespoke framework** - a reusable set of generalized CSS and JavaScript components designed for building embedded educational applications.
1014

@@ -38,22 +42,30 @@ No build step required - this is pure HTML/CSS/JavaScript with no frameworks or
3842

3943
### Core Components
4044

41-
1. **Vector Class** (`client/linear-algebra.js`)
45+
1. **Vector Class** (`client/core/vector.js`)
4246
- Encapsulates mathematical vector operations
4347
- Methods: add, subtract, scale, dot, normalize, project, reflect
4448

45-
2. **LinearAlgebraApp Class** (`client/linear-algebra.js`)
46-
- Main application controller
47-
- Manages canvas rendering, user interactions, and animations
48-
- Configuration-driven design via CONFIG object
49+
2. **Mode System** (`client/core/mode-manager.js`, `client/modes/`)
50+
- Three modes: VectorMode, MatrixMode, TensorMode
51+
- Each mode is a controller managing its own UI, canvas rendering, and interactions
52+
- Modes subscribe to theme changes and clean up properly on destroy
53+
- See `client/modes/AGENTS.md` for detailed implementation contracts
54+
55+
3. **TensorMode** (`client/modes/tensor-mode.js`, `client/modes/tensor-canvas-3d.js`)
56+
- Visualizes tensors of ranks 0-3 in interactive 3D space
57+
- Uses `TensorCanvas3D` for 3D rendering with mouse drag rotation and scroll zoom
58+
- Supports scalar, vector, matrix, and 3D tensor inputs
59+
- See `client/modes/AGENTS.md` for lifecycle and teardown requirements
4960

50-
3. **HelpModal** (`client/help-modal.js`)
61+
4. **HelpModal** (`client/help-modal.js`)
5162
- Reusable modal component with theme support
5263
- Singleton pattern with event handling
64+
- Mode-specific help content loaded from `help-content-*.html` files
5365

54-
4. **Status Management** (`client/app.js`)
55-
- WebSocket client for real-time updates
66+
5. **Status Management** (`client/core/status.js`, `client/app.js`)
5667
- Standardized status messaging system
68+
- WebSocket client for real-time updates (optional)
5769

5870
### File Loading Order
5971

@@ -116,10 +128,21 @@ client/
116128
├── help-modal.js # Reusable help modal
117129
├── app.js # WebSocket & status handling
118130
├── linear-algebra.js # Main app logic
119-
├── linear-algebra.css # App-specific styles
131+
├── config.json # Application configuration (modes, operations)
132+
├── core/ # Shared services
133+
│ ├── mode-manager.js # Mode switching system
134+
│ ├── theme-service.js # Theme management
135+
│ ├── coordinate-system.js # Coordinate system
136+
│ └── ... # Other core services
137+
├── modes/ # Mode controllers
138+
│ ├── vector-mode.js # Vector mode controller
139+
│ ├── matrix-mode.js # Matrix mode controller
140+
│ ├── tensor-mode.js # Tensor mode controller
141+
│ └── tensor-canvas-3d.js # 3D canvas renderer for tensor mode
142+
├── help-content-*.html # Mode-specific help content
120143
└── index.html # Main HTML structure
121144
122145
server.js # Development server
123-
test-integration.html # Component testing page
124146
AGENTS.md # Bespoke framework implementation guide
147+
client/modes/AGENTS.md # Mode implementation contracts
125148
```

README.md

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ An interactive educational tool for learning vector operations through visual ex
1111
- **Real-time Calculations**: View coordinates, magnitude, and angle for each vector
1212
- **Animated Operations**: Smooth transitions showing vector transformations
1313
- **Formula Display**: Mathematical formulas and calculations shown for each operation
14+
- **Tensor Visualization**: Interactive 3D visualization of tensors (scalars, vectors, matrices, 3D tensors)
1415

1516
### Supported Operations
1617
-**Vector Addition** (v₁ + v₂)
@@ -20,9 +21,19 @@ An interactive educational tool for learning vector operations through visual ex
2021
-**Magnitude Calculation** (||v||)
2122
-**Angle Measurement** (in degrees from positive x-axis)
2223

24+
### Modes
25+
26+
The application supports three interaction modes:
27+
28+
1. **Vector Mode**: Draw and manipulate vectors on a 2D Cartesian plane with animated operations
29+
2. **Matrix Mode**: Visualize 2×2 matrix transformations and their effects on basis vectors
30+
3. **Tensor Mode**: Explore tensors of different ranks (0-3) in an interactive 3D space
31+
32+
Modes can be enabled/disabled via the `enabledModes` array in `client/config.json`. The default mode is set via the `mode` property in the config file.
33+
2334
### User Interface
24-
- **Left Sidebar**: Control panel with vector information and operation buttons
25-
- **Main Canvas**: Full-screen 2D Cartesian plane with grid
35+
- **Left Sidebar**: Control panel with mode-specific information and operation buttons
36+
- **Main Canvas**: Full-screen coordinate plane (2D for vector/matrix modes, 3D for tensor mode)
2637
- **No Scrolling**: Fully responsive, single-page layout
2738
- **Help System**: Comprehensive help modal with instructions and FAQ
2839

@@ -34,19 +45,31 @@ An interactive educational tool for learning vector operations through visual ex
3445
```
3546
Server runs at `http://localhost:3000`
3647

37-
2. **Create vectors**:
38-
- Click and drag anywhere on the grid
39-
- First vector (v₁) will be red
40-
- Second vector (v₂) will be blue
41-
42-
3. **Perform operations**:
43-
- Use buttons in the left sidebar
44-
- Watch animated results
45-
- Read formulas in the Results section
46-
47-
4. **Clear and experiment**:
48-
- Clear individual vectors or all at once
49-
- Try different vector configurations
48+
2. **Choose a mode**:
49+
- Use the mode switcher buttons (if multiple modes are enabled)
50+
- Default mode is set in `client/config.json` (`mode` property)
51+
- Available modes: Vector, Matrix, Tensor
52+
53+
3. **Vector Mode**:
54+
- Click and drag anywhere on the grid to create vectors
55+
- First vector (v₁) will be red, second vector (v₂) will be blue
56+
- Use buttons in the left sidebar to perform operations
57+
- Watch animated results and read formulas in the Results section
58+
59+
4. **Matrix Mode**:
60+
- Enter values in the 2×2 matrix input grid
61+
- See how the matrix transforms basis vectors î and ĵ
62+
- Toggle determinant visualization to see area scaling
63+
64+
5. **Tensor Mode**:
65+
- Select a tensor rank (0-3) using the rank buttons
66+
- Rank 0: Scalar (single value)
67+
- Rank 1: Vector (x, y components)
68+
- Rank 2: Matrix (2×2 grid)
69+
- Rank 3: 3D Tensor (2×2×2 cube)
70+
- Enter values in the input fields to modify tensor components
71+
- Drag to rotate the 3D visualization, scroll to zoom
72+
- See detailed help in `help-content-tensor.html`
5073

5174
## Project Structure
5275

@@ -58,8 +81,26 @@ learn-bespoke-linear-algebra/
5881
│ ├── linear-algebra.css # App-specific styles
5982
│ ├── linear-algebra.js # Main application logic
6083
│ ├── help-modal.js # Help modal component
61-
│ ├── help-content-template.html # Help content
62-
│ └── app.js # WebSocket/status handling
84+
│ ├── help-content-template.html # Help content template
85+
│ ├── help-content-vector.html # Vector mode help content
86+
│ ├── help-content-matrix.html # Matrix mode help content
87+
│ ├── help-content-tensor.html # Tensor mode help content
88+
│ ├── app.js # WebSocket/status handling
89+
│ ├── config.json # Application configuration
90+
│ ├── core/ # Shared services
91+
│ │ ├── config.js # Config service
92+
│ │ ├── mode-manager.js # Mode switching
93+
│ │ ├── theme-service.js # Theme management
94+
│ │ ├── coordinate-system.js # Coordinate system
95+
│ │ └── ... # Other core services
96+
│ ├── modes/ # Mode controllers
97+
│ │ ├── vector-mode.js # Vector mode controller
98+
│ │ ├── matrix-mode.js # Matrix mode controller
99+
│ │ ├── tensor-mode.js # Tensor mode controller
100+
│ │ ├── tensor-canvas-3d.js # 3D canvas renderer
101+
│ │ └── ... # Other mode files
102+
│ └── entities/ # Data models
103+
│ └── matrix.js # Matrix class
63104
├── server.js # Development server
64105
├── package.json # Dependencies
65106
└── README.md # This file
@@ -125,6 +166,49 @@ Modern browsers with Canvas support:
125166
- Firefox (latest)
126167
- Safari (latest)
127168

169+
## Manual Testing Checklist
170+
171+
Use this checklist to verify all modes work correctly after changes:
172+
173+
### Mode Switching
174+
- [ ] Mode switcher buttons appear/disappear based on `enabledModes` config
175+
- [ ] Switching between modes updates the UI correctly
176+
- [ ] Each mode displays its own sidebar content
177+
- [ ] Canvas renders correctly for each mode
178+
179+
### Vector Mode
180+
- [ ] Click and drag creates vectors on the canvas
181+
- [ ] Vector operations (add, subtract, scalar multiply, dot product) work
182+
- [ ] Animations play smoothly
183+
- [ ] Results panel displays calculation formulas
184+
- [ ] Status remains "Ready" after operations
185+
186+
### Matrix Mode
187+
- [ ] Matrix input grid accepts numeric values
188+
- [ ] Basis vectors update when matrix changes
189+
- [ ] Determinant visualization toggles correctly (if enabled)
190+
- [ ] Reset button restores identity matrix
191+
- [ ] Status remains "Ready" after operations
192+
193+
### Tensor Mode
194+
- [ ] Rank buttons (0-3) update UI inputs correctly
195+
- [ ] Rank toggle updates 3D render visualization
196+
- [ ] Scalar input (rank 0) updates single cube visualization
197+
- [ ] Vector inputs (rank 1) update two-cube visualization
198+
- [ ] Matrix inputs (rank 2) update 2×2 grid visualization
199+
- [ ] Tensor3D inputs (rank 3) update 2×2×2 cube visualization
200+
- [ ] Drag interaction rotates 3D view smoothly
201+
- [ ] Scroll/zoom interaction works correctly
202+
- [ ] Reset button restores default tensor values
203+
- [ ] Help button shows tensor help content when tensor mode is active
204+
- [ ] Status remains "Ready" after all interactions
205+
206+
### Theme and Cleanup
207+
- [ ] Theme changes update all mode visualizations correctly
208+
- [ ] Switching away from a mode cleans up properly (no memory leaks)
209+
- [ ] Event listeners are removed when modes are destroyed
210+
- [ ] Canvas interaction handlers are cleaned up (especially tensor mode 3D canvas)
211+
128212
---
129213

130214
## Template Components (Bespoke Framework)

client/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"mode": "matrix",
2+
"mode": "tensor",
3+
"enabledModes": ["vector", "matrix", "tensor"],
34
"vectorMode": {
45
"maxVectors": 2,
56
"operationGroups": {

client/core/config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Default configuration
1111
const DEFAULT_CONFIG = {
1212
mode: 'vector',
13+
enabledModes: ['vector', 'matrix', 'tensor'],
1314
vectorMode: {
1415
maxVectors: 2,
1516
operationGroups: {
@@ -56,7 +57,10 @@
5657

5758
if (!response.ok) {
5859
console.warn('config.json not found, using default configuration');
59-
const config = { ...DEFAULT_CONFIG };
60+
const config = {
61+
...DEFAULT_CONFIG,
62+
enabledModes: [...DEFAULT_CONFIG.enabledModes]
63+
};
6064
configCache = config;
6165

6266
if (window.StatusService) {
@@ -71,6 +75,9 @@
7175
// Merge with defaults
7276
const config = {
7377
mode: userConfig.mode || DEFAULT_CONFIG.mode,
78+
enabledModes: Array.isArray(userConfig.enabledModes) && userConfig.enabledModes.length > 0
79+
? userConfig.enabledModes
80+
: DEFAULT_CONFIG.enabledModes,
7481
vectorMode: {
7582
...DEFAULT_CONFIG.vectorMode,
7683
...(userConfig.vectorMode || userConfig), // Backward compatible
@@ -99,7 +106,10 @@
99106
return config;
100107
} catch (error) {
101108
console.warn('Failed to load config.json, using default configuration:', error);
102-
const config = { ...DEFAULT_CONFIG };
109+
const config = {
110+
...DEFAULT_CONFIG,
111+
enabledModes: [...DEFAULT_CONFIG.enabledModes]
112+
};
103113
configCache = config;
104114

105115
if (window.StatusService) {

client/core/help.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99

1010
/**
1111
* Load help content from template file
12-
* @param {string} mode - Mode name ('vector' or 'matrix')
12+
* @param {string} mode - Mode name ('vector', 'matrix', or 'tensor')
1313
* @returns {Promise<string>} Promise that resolves to help content HTML
1414
*/
1515
async function loadHelpContent(mode = 'vector') {
1616
// Determine filename based on mode
17-
const filename = mode === 'matrix' ? 'help-content-matrix.html' : 'help-content-vector.html';
17+
let filename = 'help-content-vector.html';
18+
if (mode === 'matrix') {
19+
filename = 'help-content-matrix.html';
20+
} else if (mode === 'tensor') {
21+
filename = 'help-content-tensor.html';
22+
}
1823

1924
// Return cached promise if it exists
2025
if (helpContentCache[mode]) {
@@ -43,7 +48,7 @@
4348
* Modes can append additional content using appendHelpContent()
4449
* @param {Object} options - Initialization options
4550
* @param {string} options.triggerSelector - CSS selector for help button
46-
* @param {string} [options.mode] - Mode name ('vector' or 'matrix'), defaults to 'vector'
51+
* @param {string} [options.mode] - Mode name ('vector', 'matrix', or 'tensor'), defaults to 'vector'
4752
* @param {string} [options.additionalContent] - Additional HTML content to append
4853
* @param {string} [options.theme] - Theme mode ('auto', 'light', 'dark')
4954
* @returns {Promise<void>}
@@ -98,7 +103,7 @@
98103
* Append additional content to the help modal
99104
* Useful for mode-specific help sections
100105
* @param {string} content - HTML content to append
101-
* @param {string} [mode] - Mode name ('vector' or 'matrix'), defaults to 'vector'
106+
* @param {string} [mode] - Mode name ('vector', 'matrix', or 'tensor'), defaults to 'vector'
102107
* @returns {Promise<void>}
103108
*/
104109
async function appendHelpContent(content, mode = 'vector') {

0 commit comments

Comments
 (0)