document.addEventListener("DOMContentLoaded", () => { const canvas = document.getElementById("background"); if (canvas.getContext) { const ctx = canvas.getContext("2d"); // Source - https://stackoverflow.com/a/58673385 // Posted by Sanxofon, modified by community. See post 'Timeline' for change history // Retrieved 2026-02-15, License - CC BY-SA 4.0 var w = canvas.width; // Canvas width => Frequency is relative to this var h = canvas.height/2; // Canvas height over two => Amplitude: Volume var f=1; // How many cycles per canvas width => Frequency: Tone & Speed // Calculates y position from x function calcSineY(x) { // This is the meat (unles you are vegan) // Note that: // h is the amplitude of the wave // x is the current x value we get every time interval // 2 * PI is the length of one cycle (full circumference) // f/w is the frequency fraction return h - h * Math.sin( x * 2 * Math.PI * (f/w) ); } function drawSine(x){ ctx.clearRect(0, 0, w, h*2); //draw x axis ctx.beginPath(); // Draw a new path ctx.strokeStyle = "green"; // Pick a color ctx.moveTo(0,h); // Where to start drawing ctx.lineTo(w,h); // Where to draw to ctx.stroke(); // Draw // // draw horizontal line of current amplitude // ctx.beginPath(); // Draw a new path // ctx.moveTo(0,h); // Where to start drawing // ctx.strokeStyle = "gray"; // Pick a color // for(var i=0;iw){ // x=0; // x cannot be more than canvas with, so back to 0 // f++; // increment frequency for demonstration // } },10); // Loop every 10 milliseconds } else { } });