Draw a sine curve using js, and draw a sine from js
Math formula: y = Asin (ω x + PHI) + k
Example: http://www.zhaojz.com.cn/demo/draw7.html
Declaration of JS functions:
Copy codeThe Code is as follows:
// Draw a sine curve
// Dot Origin
// Amplitude --
// Initial Phase of initialPhase -- Phi
// Setover offset -- k
// Palstance angular velocity -- ω
// Len cycle count
Function drawSinusoid (dot, amplase, initialPhase, palstance, setover, len, opts ){
Var color = opts & opts. color? Opts. color: "DarkRed"; // curve color
Var max = len * 2 * Math. PI/w; // The maximum value of x
// Var x =-2 * Math. PI/w/3;
Var x = 0; // The initial value of x
Var pre = [dot [0] + x, dot [1] + (amplitude * Math. sin (palstance * x + initialPhase) + setover)]; // The initial values of y
For (; x <max; x + = 5) {// draw a line for every five units
Var cur = [dot [0] + x, dot [1] + (amplitude * Math. sin (palstance * x + initialPhase) + setover)];
DrawLine (pre, cur, {color: color}); // draw a line
Pre = cur;
}
Var d = Math. PI/(2 * w );
For (var x = 0; x <max; x + = d) {// trace
Var cur = [dot [0] + x, dot [1] + (amplitude * Math. sin (palstance * x + initialPhase) + setover)];
DrawPoint ({
Pw: 3, ph: 3, color: 'dark', point: cur
});
}
Var pend = [dot [0] + max, dot [1] + (amplitude * Math. sin (palstance * max + initialPhase) + setover)];
DrawPoint ({
Pw: 3, ph: 3, color: 'dark', point: pend
});
DrawLine (pre, pend );
}