-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d181f19
commit e632d1d
Showing
6 changed files
with
165 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Sim 2 - Interference</title> | ||
|
||
</head> | ||
<body> | ||
<canvas width="1000px" height="500px"></canvas> | ||
|
||
<div class="frequency"> | ||
<input type="range" min="0.05" max="1" step="0.05" value="0.5" class="slider" id="frequencyInput"> | ||
Frequency: <span id="frequencyValue">0.05</span> | ||
</div> | ||
<div class="amplitude"> | ||
<input type="range" min="0.1" max="1" step="0.1" value="0.2" class="slider" id="amplitudeInput"> | ||
Amplitude: <span class="amplitudeValue">0.2</span> | ||
</div> | ||
|
||
<script type="module" src="static/js/sim2.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Pointer { | ||
// This is an x coordinate restricted pointer | ||
constructor(cvs, c, x, y) { | ||
this.cvs = cvs; | ||
this.c = c; | ||
this.x = x; | ||
this.y = y; | ||
|
||
this.length = 40; | ||
this.minY = this.length; | ||
this.maxY = cvs.height - this.length; | ||
} | ||
|
||
draw = () => { | ||
this.c.beginPath(); | ||
this.c.moveTo(this.x, this.y); | ||
this.c.lineTo(this.x + 0.866 * this.length, this.y + 0.5 * this.length); | ||
this.c.lineTo(this.x + 0.866 * this.length, this.y - 0.5 * this.length); | ||
this.c.lineTo(this.x, this.y); | ||
this.c.closePath(); | ||
|
||
this.c.fillStyle = "#179e7e" | ||
this.c.fill(); | ||
} | ||
|
||
set setLength(length) { | ||
this.length = length; | ||
} | ||
} | ||
|
||
let dragging = -1; | ||
const pointers = []; | ||
|
||
function createPointer(pointer) { | ||
pointers.push(pointer); | ||
} | ||
|
||
document.addEventListener("mousedown", e => { | ||
for (let i = 0; i < pointers.length; i++) { | ||
// use math check in triangle | ||
// lazy to code | ||
} | ||
dragging = 0; | ||
}); | ||
|
||
document.addEventListener("mouseup", e => { dragging = -1; }); | ||
|
||
document.addEventListener("mousemove", e => { | ||
if (dragging === -1) return; | ||
const pointer = pointers[dragging]; | ||
pointer.y = Math.max(Math.min(pointer.y + e.movementY, pointer.maxY), pointer.minY); | ||
}); | ||
|
||
export { Pointer, createPointer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
import { WaveDisplay } from "./shared/waves.js"; | ||
import { Pointer, createPointer } from "./shared/pointer.js"; | ||
|
||
const cvs = document.querySelector('canvas'); | ||
const c = cvs.getContext('2d'); | ||
const frequencyInput = document.getElementById("frequencyInput"); | ||
const amplitudeInput = document.getElementById("amplitudeInput"); | ||
|
||
const s1 = new WaveDisplay(cvs, c); | ||
s1.setRect(100, 0.3 * cvs.height, cvs.width - 100, 0.5 * cvs.height); | ||
s1.frequency = 0.5; | ||
s1.amplitude = 0.2 | ||
|
||
const s2 = new WaveDisplay(cvs, c); | ||
s1.load(100); | ||
s2.load(100); | ||
s2.setRect(100, 0.7 * cvs.height, cvs.width - 100, 0.5 * cvs.height); | ||
s2.frequency = 0.5; | ||
s2.amplitude = 0.2; | ||
|
||
const pointer = new Pointer(cvs, c, cvs.width - 80, 0.5 * cvs.height); | ||
createPointer(pointer); | ||
const animate = () => { | ||
requestAnimationFrame(animate) | ||
c.clearRect(0, 0, cvs.width, cvs.height); | ||
s1.update(); | ||
s2.update(); | ||
s1.setRect(100, 0.3 * cvs.height, pointer.x - 30, pointer.y); | ||
s2.setRect(100, 0.7 * cvs.height, pointer.x - 30, pointer.y); | ||
pointer.draw(); | ||
} | ||
|
||
frequencyInput.oninput = () => { | ||
document.getElementById("frequencyValue").innerText = frequencyInput.value; | ||
s1.frequency = frequencyInput.value; | ||
s2.frequency = frequencyInput.value; | ||
} | ||
|
||
amplitudeInput.oninput = () => { | ||
document.getElementById("amplitudeInput").innerText = amplitudeInput.value; | ||
s1.amplitude = amplitudeInput.value; | ||
s2.amplitude = amplitudeInput.value; | ||
} | ||
|
||
animate(); |