在画布上画正弦波

BRD*_*oid 6 html canvas sine-wave

我试图在画布上绘制一个简单的正弦波,但我没有把它弄好.这是我想要的输出,如图所示.

到目前为止我得到的是http://jsfiddle.net/RaoBurugula/gmhg61s6/4/

HTML

 <canvas id="myCanvas" width="360" height="360" style="border:1px solid #d3d3d3;">
Run Code Online (Sandbox Code Playgroud)

JS

 var c = document.getElementById("myCanvas");
 var ctx = c.getContext("2d");
 var i;
 for(i=0; i<360; i+= 20){
    ctx.moveTo(i+5,180);
    ctx.lineTo(i,180);

 }
 ctx.stroke();
 var counter = 0, x=0,y=180;

 //100 iterations
 var increase = 90/180*Math.PI ;
 for(i=0; i<=180; i+=10){

 ctx.moveTo(x,y);
 x = i;
y=  180 - Math.sin(counter);
counter += increase;

ctx.lineTo(x,y);
alert( " x : " + x + " y : " + y) ;
}
ctx.stroke();
Run Code Online (Sandbox Code Playgroud)

我想要的输出

我想要的输出

San*_*fon 7

老问题,但非常普遍的兴趣。

这里的所有答案都非常有用,但没有一个有很好的代码注释,所以我创建了一个片段来解释每个点发生的事情并为了清晰起见绘制了许多变量。感谢大家帮助我理解,我希望这对其他人有所帮助。

var c = document.getElementById("canvas"); // Grab canvas object
var ctx = c.getContext("2d"); // Define canvas context
var w=c.width; // Canvas width => Frequency is relative to this
var h=c.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;i<x;i++){ // Loop from left side to current x
    var y = calcSineY(x); // Calculate y value from x
  	ctx.moveTo(i,y); // Where to start drawing
    ctx.lineTo(x,y); // Where to draw to
  }
  ctx.stroke(); // Draw
  
  // draw amplitude bar at current point
  ctx.beginPath(); // Draw a new path
  ctx.strokeStyle = "red"; // Pick a color
  for(var i=0;i<x;i++){ // Loop from left side to current x
    var y = calcSineY(x); // Calculate y value from x
  	ctx.moveTo(x,h); // Where to start drawing
    ctx.lineTo(x,y); // Where to draw to
  }
  ctx.stroke(); // Draw
  
  // draw area below y
  ctx.beginPath(); // Draw a new path
  ctx.strokeStyle = "orange"; // Pick a color
  for(var i=0;i<x;i++){ // Loop from left side to current x
    if(i/3==Math.round(i/3)) { // Draw only one line each 3 pixels
      var y = calcSineY(i); // Calculate y value from x
      ctx.moveTo(i,h); // Where to start drawing
      ctx.lineTo(i,y); // Where to draw to
    }
  }
  ctx.stroke(); // Draw
  
  // draw sin curve point to point until x
  ctx.beginPath(); // Draw a new path
  ctx.strokeStyle = "black"; // Pick a color
  for(var i=0;i<x;i++){ // Loop from left side to current x
    var y = calcSineY(i); // Calculate y value from x
    ctx.lineTo(i,y); // Where to draw to
  }
  ctx.stroke(); // Draw
}
// Define initial value of x positiom (leftmost side of cnanvas)
var x=0;
// Start time interval
var interval = setInterval(function(){
	drawSine(x); // Call draww function every cycle
	x++; // Increment x by 1
	if(x>w){
	    x=0; // x cannot be more than canvas with, so back to 0
        f++; // increment frequency for demonstration
    }
},10); // Loop every 10 milliseconds
Run Code Online (Sandbox Code Playgroud)
<canvas id="canvas" width="320" height="120" style="border:1px solid #999;">
Run Code Online (Sandbox Code Playgroud)


Guf*_*ffa 5

您所增加counter的值过高,请减小它:

var increase = 90/180*Math.PI / 9;
Run Code Online (Sandbox Code Playgroud)

绘制图的整个宽度而不是一半:

for(i=0; i<=360; i+=10){
Run Code Online (Sandbox Code Playgroud)

您需要更高的振幅:

y =  180 - Math.sin(counter) * 120;
Run Code Online (Sandbox Code Playgroud)

演示:http//jsfiddle.net/Guffa/gmhg61s6/5/


bar*_*pie 5

使用bezierCurveTo,这只是一个示例,您应该调整参数以获得漂亮的正弦曲线

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(50,50);
ctx.bezierCurveTo(120,-100,200,250,250,50);
ctx.bezierCurveTo(300,-100,350,250,430,50);
ctx.lineWidth = 5;
ctx.strokeStyle = '#003300';
ctx.stroke();
Run Code Online (Sandbox Code Playgroud)
<canvas id="myCanvas" width="550" height="360" style="border:1px solid #d3d3d3;">
Run Code Online (Sandbox Code Playgroud)