Created
September 11, 2023 10:26
-
-
Save iceener/2626f66078f8d52d72448e1663e577d9 to your computer and use it in GitHub Desktop.
Wave Function on Canvas
This file contains hidden or 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
class Wave { | |
constructor(canvasId) { | |
this.canvas = document.getElementById(canvasId); | |
this.ctx = this.canvas.getContext('2d'); | |
this.canvas.width = window.innerWidth; | |
this.canvas.height = window.innerHeight; | |
} | |
drawWave() { | |
let waveLength = 0.01; | |
let amplitude = 100; | |
let frequency = 0.01; | |
this.ctx.beginPath(); | |
for(let i = 0; i < this.canvas.width; i++) { | |
let y = this.canvas.height / 2 + amplitude * Math.sin(waveLength * i + frequency); | |
this.ctx.lineTo(i, y); | |
} | |
this.ctx.strokeStyle = '#f0f0f0'; | |
this.ctx.lineWidth = 5; | |
this.ctx.stroke(); | |
} | |
} | |
window.onload = () => { | |
const wave = new Wave('wave'); | |
wave.drawWave(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment