Draw Sine Wave Combination of HTML & CSS

Ticker

10/recent/ticker-posts

Draw Sine Wave Combination of HTML & CSS

Sine Wave Animation HTML & CSS


<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<style type="text/css">
canvas {
  border: 5px solid rgb(0, 255, 76);
  margin: 30px;
}</style>
<input onchange="init()" id="f" type="range" min="0" max="100" value="3" step="1">
<output for="f" id="output_f">20</output>
<canvas width="350px" height="200px"></canvas>
<script type="text/javascript">
// special thanks to stackoverflow.com
var canvas = document.querySelector("canvas"),
  ctx = canvas.getContext("2d"),
  cHeight = canvas.height,
  cWidth = canvas.width,
  frequency = document.querySelector("#f").value,
  amplitude = 80,
  x = 0,
  y = cHeight / 2,
  point_y = 0;
  var t;
window.onload = init;
function init() {
  document.querySelector("#f").addEventListener("input", function() {
    frequency = this.value;
    document.querySelector("#output_f").value = frequency;
  }, false);
x=0
drawSine();
t=setInterval(aaa,10)
}
function drawSine() {
  ctx.clearRect(0, 0, cWidth, cHeight);
  ctx.beginPath();
  ctx.moveTo(0, y);
  ctx.strokeStyle = "Blue";
  ctx.lineTo(cWidth, y);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.strokeStyle = "red";
  ctx.closePath();
}
function aaa(){
  x++
  point_y = amplitude * -Math.sin((frequency / 95.33) * x) + y;
  ctx.lineTo(x, point_y);
  if(x>=340) clearInterval(t)
  ctx.stroke();}
</script>
</body>
</html> 


OUTPUT



Post a Comment

44 Comments

  1. Wonderful 😊😊😊😊

    ReplyDelete
  2. Fabulous 😊😊😊

    ReplyDelete
  3. Aarju
    Very nice😊😊😊😊

    ReplyDelete