A small C++ / WebAssembly project for visualizing magnetic fields in the browser. The simulation is written mostly in C++ and rendered with OpenGL-style code compiled to WebGL using Emscripten.
https://magnetic-field-simulator.netlify.app/
The project shows a magnetic field visualization with animated particles and shader-based rendering.
C++ code is compiled into WebAssembly, then loaded by the browser through field.js and field.wasm.
The general idea is:
C++ source code → Emscripten → WebAssembly + JavaScript → browser visualizationMagneticFieldSimulation/
├── shaders/
│ ├── field_line.frag
│ ├── particle.frag
│ └── particle.vert
│
├── src/
│ ├── magnets/
│ ├── physics/
│ ├── render/
│ ├── exports.cpp
│ ├── main.cpp
│ └── types.h
│
├── web/
│ ├── field.js
│ ├── field.wasm
│ ├── index.html
│ └── style.css
│
├── build.ps1
├── build.sh
├── CMakeLists.txt
├── netlify.toml
└── README.mdContains the C++ source code of the simulation.
main.cppstarts the application and the render loop.exports.cppexposes selected C++ functions to JavaScript.types.hstores shared types used in the project.render/contains OpenGL helper classes such as VAO, VBO, EBO, shaders and renderer code.physics/andmagnets/are prepared for the magnetic field calculations and magnet models.
Contains GLSL shader files used for rendering particles and field lines.
Contains the browser version of the project.
index.htmlloads the simulation.style.csscontains the page styling.field.jsandfield.wasmare generated by Emscripten after building.
To build the project, you need:
- Emscripten SDK
- Python 3 for running a local web server
- Git Bash or PowerShell on Windows
If Emscripten is installed in C:/dev/emsdk, first activate it:
source /c/dev/emsdk/emsdk_env.shThen build the project:
bash build.shAfter a successful build, the files should appear in the web/ folder:
web/field.js
web/field.wasmThe project must be opened through a local server, not by double-clicking index.html.
From the project folder run:
cd web
python -m http.server 8080Then open in the browser:
http://localhost:8080On Windows you can also use:
C:\dev\emsdk\emsdk_env.ps1
.\build.ps1
.\run_server.ps1Then open:
http://localhost:8080The generated field.js and field.wasm files are build outputs.
If the C++ code or shaders are changed, the project should be rebuilt with build.sh or build.ps1.