Changed around line 1
+ const canvas = document.getElementById('signal-canvas');
+ const ctx = canvas.getContext('2d');
+ const frequencyInput = document.getElementById('frequency');
+ const harmonicsInput = document.getElementById('harmonics');
+ const signalTypeSelect = document.getElementById('signal-type');
+ const generateButton = document.getElementById('generate');
+
+ function generateSignal() {
+ const frequency = parseFloat(frequencyInput.value);
+ const harmonics = parseInt(harmonicsInput.value);
+ const signalType = signalTypeSelect.value;
+
+ const width = canvas.width;
+ const height = canvas.height;
+ ctx.clearRect(0, 0, width, height);
+ ctx.beginPath();
+ ctx.strokeStyle = '#ff6f61';
+ ctx.lineWidth = 2;
+
+ for (let x = 0; x < width; x++) {
+ let y = 0;
+ for (let n = 1; n <= harmonics; n++) {
+ const amplitude = 1 / n;
+ const phase = (2 * Math.PI * frequency * n * x) / width;
+ y += amplitude * (signalType === 'sin' ? Math.sin(phase) : Math.cos(phase));
+ }
+ y = (y * height / 4) + height / 2;
+ if (x === 0) {
+ ctx.moveTo(x, y);
+ } else {
+ ctx.lineTo(x, y);
+ }
+ }
+
+ ctx.stroke();
+ }
+
+ generateButton.addEventListener('click', generateSignal);
+ window.addEventListener('resize', () => {
+ canvas.width = canvas.offsetWidth;
+ canvas.height = canvas.offsetHeight;
+ generateSignal();
+ });
+
+ canvas.width = canvas.offsetWidth;
+ canvas.height = canvas.offsetHeight;
+ generateSignal();