Skip to content

Commit cc1b44e

Browse files
uploaded simulator files
1 parent eaa7c37 commit cc1b44e

7 files changed

Lines changed: 1437 additions & 2 deletions

File tree

README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
1-
# DiffractionGrating
2-
Interactive web simulation of diffraction grating
1+
# Diffraction Grating Simulator
2+
3+
An interactive, web-based simulator that provides a real-time visualization of the diffraction pattern produced by a reflective or transmissive grating. This tool allows users to explore the principles of optical diffraction by adjusting a comprehensive set of parameters for both the incident light and the grating itself.
4+
5+
## Live Demo
6+
7+
Experience the simulator live at: **[visuphy.github.io/DiffractionGrating](https://visuphy.github.io/DiffractionGrating)**
8+
9+
## Screenshot
10+
11+
![Screenshot of the Diffraction Grating Simulator](Screenshot.png)
12+
13+
## Features
14+
15+
* **Dual Light Modes:** Simulate diffraction for both **Monochromatic** and **White Light** sources.
16+
* **Comprehensive Beam Controls:**
17+
* Adjust the **Wavelength (λ)** of monochromatic light with a dynamic range.
18+
* Set the **Bandwidth (Δλ)** to observe the effect of spectral line width.
19+
* Modify the **Angle of Incidence (α)** of the light source.
20+
* **Detailed Grating Properties:**
21+
* Control **Grating Spacing (d)** or, alternatively, the **Groove Density** (lines/mm).
22+
* Select the number of **Diffraction Orders (±m)** to display.
23+
* Define the number of **Illuminated Grooves (N)** to see its effect on resolving power.
24+
* Switch between **Reflective** and **Transmissive** grating types.
25+
* **Interactive Analysis:**
26+
* In monochromatic mode, **click on any diffraction order** to view its key quantitative properties.
27+
* Displayed data includes the precise diffraction angle, angular dispersion, temporal dispersion (GVD), and the system's resolving power.
28+
* **Littrow Condition Display:** The simulator calculates and displays the wavelength and angle required to satisfy the Littrow condition for m=1 based on the current setup.
29+
30+
## How to Use
31+
32+
1. **Launch the Simulator:** Open the [live demo link](https://visuphy.github.io/DiffractionGrating) in your browser.
33+
2. **Select Light Properties:**
34+
* Choose between `Monochromatic` or `White Light`.
35+
* If monochromatic, use the sliders or input boxes to set the Wavelength and Bandwidth.
36+
* Adjust the Angle of Incidence for the incoming beam.
37+
3. **Configure Grating Properties:**
38+
* Use the sliders or input fields to set the Grating Spacing or Groove Density. Note that changing one will automatically update the other.
39+
* Set the maximum number of diffraction orders and illuminated grooves.
40+
* Select whether the grating is `Reflective` or `Transmissive`.
41+
4. **Analyze the Results:**
42+
* Observe the diffraction pattern on the canvas.
43+
* For detailed analysis, switch to `Monochromatic` mode and click on a specific diffracted ray (e.g., `m = 1`). The calculated physical properties for that order will appear in the top-left corner of the canvas.
44+
45+
## Technical Details
46+
47+
This simulator is built entirely with client-side web technologies:
48+
49+
* **HTML5:** For the structure and layout of the user interface.
50+
* **CSS3:** For styling the controls and ensuring a responsive design.
51+
* **JavaScript:** For the core simulation logic, physics calculations, and rendering on the HTML5 Canvas.
52+
53+
No external libraries or frameworks are required, ensuring the simulator is lightweight and fast-loading.
54+
55+
## Contributing
56+
57+
Feedback and contributions are welcome! If you encounter any bugs, have suggestions for new features, or would like to improve the code, please feel free to:
58+
59+
* **Report an issue:** Open an issue on the [GitHub Issues page](https://github.com/visuphy/DiffractionGrating/issues).
60+
* **Submit a pull request:** Fork the repository, make your changes, and submit a pull request for review.
61+
62+
## License
63+
64+
This project is licensed under the **MIT License**. See the `LICENSE` file for more details.
65+
66+
## Acknowledgments
67+
68+
* **Original Author:** [Hussein-Tofaili](https://github.com/Hussein-Tofaili)
69+
* This project is maintained by [VisuPhy](https://github.com/visuphy).

Screenshot.png

170 KB
Loading

diffractiongrating.html

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Diffraction Grating Simulator</title>
7+
<link rel="icon" type="image/png" href="favicon.png">
8+
9+
<!-- Font Awesome CDN for icons -->
10+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />
11+
12+
<!-- Google Fonts -->
13+
<link rel="preconnect" href="https://fonts.googleapis.com">
14+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
15+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
16+
17+
<!-- Link to local and shared styles -->
18+
<link rel="stylesheet" href="style.css">
19+
<link rel="stylesheet" href="/header.css">
20+
<link rel="stylesheet" href="/footer.css">
21+
</head>
22+
<body>
23+
24+
<!-- This div will be populated by layout.js with the shared header content -->
25+
<div id="header-placeholder"></div>
26+
27+
<!-- This H1 block contains the title and info specific to this page -->
28+
<h1>
29+
<!-- Wrapper for title and copyright -->
30+
<div class="h1-title-group">
31+
<span class="h1-title">Diffraction Grating Simulator</span>
32+
<div class="h1-copyright">
33+
© 2025 <a href="https://github.com/visuphy" target="_blank" title="Visit VisuPhy on GitHub">VisuPhy</a>
34+
</div>
35+
</div>
36+
<!-- Container for the info links -->
37+
<div class="h1-info">
38+
<div class="info-item">
39+
<span class="info-label">About and discussion</span>
40+
<div class="info-content">
41+
<a href="." target="_blank" title="About and Discussion">
42+
<i class="fa-solid fa-file-invoice"></i>
43+
</a>
44+
</div>
45+
</div>
46+
<div class="info-item">
47+
<span class="info-label">Source</span>
48+
<div class="info-content">
49+
<a href="https://github.com/visuphy/DiffractionGrating" target="_blank" title="View Source on GitHub">
50+
<i class="fab fa-github"></i>
51+
</a>
52+
</div>
53+
</div>
54+
<div class="info-item">
55+
<span class="info-label">Original Author</span>
56+
<div class="info-content">
57+
<a href="https://github.com/Hussein-Tofaili" target="_blank">Hussein-Tofaili</a>
58+
</div>
59+
</div>
60+
<div class="info-item">
61+
<span class="info-label">Other Contributors</span>
62+
<div class="info-content">
63+
<span style="color: #555;">none</span>
64+
</div>
65+
</div>
66+
</div>
67+
</h1>
68+
69+
<!-- Main content container, now holding the simulator -->
70+
<div class="main-content">
71+
<p class="page-description">Simulates the diffraction pattern of light from a reflective or transmissive grating.</p>
72+
73+
<main class="app-layout">
74+
<div class="controls-container">
75+
<!-- First Row: Incident Beam Properties -->
76+
<div class="control-row">
77+
<div class="row-title">Incident Beam Properties</div>
78+
79+
<div class="control-group">
80+
<label>Light Type</label>
81+
<div class="radio-group">
82+
<div>
83+
<input type="radio" id="monochromatic" name="lightType" value="monochromatic" checked>
84+
<label for="monochromatic">Monochromatic</label>
85+
</div>
86+
<div>
87+
<input type="radio" id="whiteLight" name="lightType" value="whiteLight">
88+
<label for="whiteLight">White Light</label>
89+
</div>
90+
</div>
91+
</div>
92+
93+
<div id="monoControls" class="mono-controls-group">
94+
<div class="control-group">
95+
<div class="label-input-group">
96+
<label for="wavelength">Wavelength (λ):</label>
97+
<input type="number" id="wavelengthInput" value="550" step="1" class="value-input">
98+
<label for="wavelengthInput">nm</label>
99+
</div>
100+
<input type="range" id="wavelength" min="380" max="750" value="550">
101+
<div class="input-group">
102+
<input type="number" id="minWavelength" value="380">
103+
<label>to</label>
104+
<input type="number" id="maxWavelength" value="750">
105+
<button id="updateWavelengthRange">Set</button>
106+
</div>
107+
</div>
108+
<div class="control-group">
109+
<div class="label-input-group">
110+
<label for="bandwidth">Bandwidth (Δλ):</label>
111+
<input type="number" id="bandwidthInput" value="0" step="1" class="value-input">
112+
<label for="bandwidthInput">nm</label>
113+
</div>
114+
<input type="range" id="bandwidth" min="0" max="100" value="0" step="1">
115+
</div>
116+
</div>
117+
118+
<div class="control-group">
119+
<div class="label-input-group">
120+
<label for="angle">Angle of Incidence (α):</label>
121+
<input type="number" id="angleInput" value="30" step="1" class="value-input">
122+
<label for="angleInput">°</label>
123+
</div>
124+
<input type="range" id="angle" min="-90" max="90" value="30">
125+
</div>
126+
</div>
127+
128+
<!-- Second Row: Grating Properties -->
129+
<div class="control-row">
130+
<div class="row-title">Grating Properties</div>
131+
132+
<div class="control-group">
133+
<div class="label-input-group">
134+
<label for="gratingSpacing">Grating Spacing (d):</label>
135+
<input type="number" id="gratingSpacingInput" value="2000" step="10" class="value-input">
136+
<label for="gratingSpacingInput">nm</label>
137+
</div>
138+
<input type="range" id="gratingSpacing" min="500" max="5000" value="2000">
139+
</div>
140+
141+
<div class="control-group">
142+
<div class="label-input-group">
143+
<label for="grooveDensity">Groove Density:</label>
144+
<input type="number" id="grooveDensityInput" value="500" step="10" class="value-input">
145+
<label for="grooveDensityInput">l/mm</label>
146+
</div>
147+
<input type="range" id="grooveDensity" min="200" max="2000" value="500">
148+
</div>
149+
150+
<div class="control-group">
151+
<div class="label-input-group">
152+
<label for="orders">Diffraction Orders (±m):</label>
153+
<input type="number" id="ordersInput" value="3" step="1" class="value-input">
154+
</div>
155+
<input type="range" id="orders" min="1" max="20" value="3">
156+
</div>
157+
158+
<div class="control-group">
159+
<div class="label-input-group">
160+
<label for="grooves">Illuminated Grooves (N):</label>
161+
<input type="number" id="groovesInput" value="1000" step="100" class="value-input">
162+
</div>
163+
<input type="range" id="grooves" min="100" max="20000" value="1000" step="100">
164+
</div>
165+
166+
<div class="control-group">
167+
<label>Grating Type</label>
168+
<div class="radio-group">
169+
<div>
170+
<input type="radio" id="reflective" name="gratingType" value="reflective" checked>
171+
<label for="reflective">Reflective</label>
172+
</div>
173+
<div>
174+
<input type="radio" id="transmissive" name="gratingType" value="transmissive">
175+
<label for="transmissive">Transmissive</label>
176+
</div>
177+
</div>
178+
</div>
179+
</div>
180+
</div>
181+
182+
<div class="canvas-wrapper">
183+
<canvas id="simulationCanvas" width="800" height="600"></canvas>
184+
<p class="canvas-hint">Hint: Click on a diffraction order (in monochromatic mode) to view its properties.</p>
185+
<p class="canvas-hint">The sign of the diffraction orders and the sign of angles depends on your convention. </p>
186+
</div>
187+
</main>
188+
</div>
189+
190+
<!-- This div will be populated by layout.js with the shared footer content -->
191+
<div id="footer-placeholder"></div>
192+
193+
<!-- Scripts -->
194+
<script src="/layout.js" defer></script>
195+
<script src="script.js"></script>
196+
</body>
197+
</html>

index.html

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Diffraction Grating Simulator - Introduction</title>
7+
<link rel="icon" type="image/png" href="https://raw.githubusercontent.com/visuphy/visuphy.github.io/refs/heads/main/favicon.png">
8+
9+
<!-- Font Awesome CDN -->
10+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />
11+
12+
<!-- Intro Page Styles -->
13+
<link rel="stylesheet" href="intro_style.css">
14+
15+
<!-- Shared Header & Footer Styles -->
16+
<link rel="stylesheet" href="/header.css">
17+
<link rel="stylesheet" href="/footer.css">
18+
</head>
19+
<body>
20+
21+
<!-- Dynamic Header Include -->
22+
<div id="header-placeholder"></div>
23+
24+
<!-- Main Introduction Content -->
25+
<main class="intro-container">
26+
27+
<h1>Welcome to the Diffraction Grating Simulator</h1>
28+
29+
<!-- Simulator Preview with Play Button -->
30+
<div class="simulator-preview">
31+
<img src="Screenshot.png" alt="Screenshot of the Plarization 3D Simulator">
32+
<a href="diffractiongrating.html" class="play-link" title="Launch Simulator">
33+
<div class="play-button">
34+
<i class="fas fa-play"></i>
35+
</div>
36+
</a>
37+
</div>
38+
39+
<!-- Information Section (Modified) -->
40+
<section class="info-section">
41+
<p>
42+
<span class="author-info">
43+
Original Author: <a href="https://github.com/Hussein-Tofaili" target="_blank">Hussein-Tofaili</a>
44+
</span>
45+
<span class="info-separator">|</span>
46+
<span class="source-info">
47+
Source Code: <a href="https://github.com/visuphy/DiffractionGrating" target="_blank">
48+
<i class="fab fa-github"></i> GitHub Repository
49+
</a>
50+
</span>
51+
</p>
52+
<!-- Removed the "About & Discussion" paragraph -->
53+
</section>
54+
55+
<!-- Description Section -->
56+
<section class="description">
57+
<h2>About This Simulator</h2>
58+
<p>
59+
Explore the principles of optical diffraction with this interactive and intuitive simulator. This tool provides a real-time visualization of the diffraction pattern generated when light interacts with a grating.</p>
60+
<p>
61+
You can adjust a comprehensive set of parameters, including the properties of the incident light—such as wavelength, bandwidth, and angle—and the physical characteristics of the grating, like groove density and type (reflective or transmissive).</p>
62+
<p>
63+
For a deeper analysis in monochromatic mode, simply click on a diffraction order to display its key quantitative properties, including the precise diffraction angle, angular and temporal dispersion, and the system's resolving power.
64+
</p>
65+
We appreciate your feedback! If you encounter any issues or have suggestions for improvement, please report them on our <a href="https://github.com/visuphy/DiffractionGrating/issues" target="_blank" rel="noopener noreferrer">GitHub Issues page</a>.
66+
</p>
67+
<p>
68+
Click the play button above to launch the simulator!
69+
</p>
70+
</section>
71+
72+
<!-- Comments Section -->
73+
<section class="comments-section">
74+
<h2>Comments & Feedback</h2>
75+
<script src="https://giscus.app/client.js"
76+
data-repo="visuphy/DiffractionGrating"
77+
data-repo-id="R_kgDOPnb5sQ"
78+
data-category="Comments"
79+
data-category-id="DIC_kwDOPnb5sc4CuziT"
80+
data-mapping="pathname"
81+
data-strict="0"
82+
data-reactions-enabled="1"
83+
data-emit-metadata="0"
84+
data-input-position="bottom"
85+
data-theme="light"
86+
data-lang="en"
87+
crossorigin="anonymous"
88+
async>
89+
</script>
90+
<noscript>Please enable JavaScript to view the comments powered by Giscus.</noscript>
91+
</section>
92+
93+
</main>
94+
95+
<!-- Dynamic Footer Include -->
96+
<div id="footer-placeholder"></div>
97+
98+
<!-- Script to include Header/Footer -->
99+
<script src="/layout.js" defer></script>
100+
</body>
101+
</html>

0 commit comments

Comments
 (0)