Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const canvas = document.getElementById('waveCanvas');
+ const ctx = canvas.getContext('2d');
+ const frequencyInput = document.getElementById('frequency');
+ const harmonicsInput = document.getElementById('harmonics');
+ const amplitudeInput = document.getElementById('amplitude');
+ const waveButtons = document.querySelectorAll('.wave-selector button');
+
+ let currentWave = 'sine';
+ let animationId = null;
+
+ function setCanvasSize() {
+ canvas.width = canvas.offsetWidth;
+ canvas.height = canvas.offsetHeight;
+ }
+
+ function drawWave(timestamp) {
+ const frequency = parseFloat(frequencyInput.value);
+ const harmonics = parseInt(harmonicsInput.value);
+ const amplitude = parseFloat(amplitudeInput.value);
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+ ctx.beginPath();
+ ctx.strokeStyle = getComputedStyle(document.documentElement)
+ .getPropertyValue('--secondary').trim();
+ ctx.lineWidth = 2;
+
+ for (let x = 0; x < canvas.width; x++) {
+ let y = 0;
+ for (let h = 1; h <= harmonics; h++) {
+ const freq = frequency * h;
+ const time = timestamp / 1000;
+ if (currentWave === 'sine') {
+ y += Math.sin(2 * Math.PI * freq * (x / canvas.width) + time) / h;
+ } else {
+ y += Math.cos(2 * Math.PI * freq * (x / canvas.width) + time) / h;
+ }
+ }
+ y = (y * amplitude * canvas.height / 4) + (canvas.height / 2);
+
+ if (x === 0) {
+ ctx.moveTo(x, y);
+ } else {
+ ctx.lineTo(x, y);
+ }
+ }
+
+ ctx.stroke();
+ animationId = requestAnimationFrame(drawWave);
+ }
+
+ function updateOutput(input) {
+ const output = input.nextElementSibling;
+ output.value = input.value + (input.id === 'frequency' ? ' Hz' : '');
+ }
+
+ // Event Listeners
+ window.addEventListener('resize', setCanvasSize);
+
+ [frequencyInput, harmonicsInput, amplitudeInput].forEach(input => {
+ input.addEventListener('input', () => updateOutput(input));
+ });
+
+ waveButtons.forEach(button => {
+ button.addEventListener('click', () => {
+ waveButtons.forEach(btn => btn.classList.remove('active'));
+ button.classList.add('active');
+ currentWave = button.dataset.wave;
+ });
+ });
+
+ // Initialize
+ setCanvasSize();
+ requestAnimationFrame(drawWave);
+ });