Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcgm1978/c7d985b956cb0b8b8da9815974d4c0f7 to your computer and use it in GitHub Desktop.
Save qcgm1978/c7d985b956cb0b8b8da9815974d4c0f7 to your computer and use it in GitHub Desktop.
Double-slit experiment simulation
<ul id="jsi-canvas" class="g_canvas">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
const $canvas = document.getElementById('jsi-canvas');
setInterval(() => {
const $targets = $canvas.querySelectorAll('li');
const positiveRand = Math.random() * 1;
const negativeRand = Math.random() * -1;
const indexRand = (positiveRand + negativeRand) * 3;
const targetIndex = parseInt(indexRand, 10) + 2;
$targets[targetIndex].insertAdjacentHTML(
'beforeend',
`
<div class="g_photon"
style="
top: ${Math.random() * 100}%;
left: ${Math.random() * 100}%;
">
</div>
`
);
}, 1);
.g_canvas {
overflow: hidden;
display: flex;
justify-content: space-around;
width: 100vw;
height: 100vh;
background-color: #333333;
> li {
position: relative;
height: 100%;
&:nth-child(1),
&:nth-child(5) {
width: calc(10vw / 3);
}
&:nth-child(2),
&:nth-child(4) {
width: calc(10vw / 3 * 2);
}
&:nth-child(3) {
width: 10vw;
}
}
}
.g_photon {
position: absolute;
width: 1px;
height: 1px;
background-color: #FFFFFF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment