Mathematical formula: y=Asin(ωx+φ)+k
Example: http://www.zhaojz.com.cn/demo/draw7.html
Declaration of JS function:
The code copy is as follows:
//Draw a sinusoidal curve
//dot Origin
//amplitude amplitude-- A
//initialPhase first phase -- φ
//setover offset-- k
//palstance angular velocity-- ω
//len number of cycles
function drawSinusoid(dot, amplitude, initialPhase,palstance,setover,len,opts){
var color = opts&&opts.color?opts.color:"DarkRed"; //The color of the curve
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 value 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 lines
pre = cur;
}
var d = Math.PI/(2*w);
for(var x =0;x < max;x+=d){//Scan the point
var cur = [dot[0]+x, dot[1]+(amplitude*Math.sin(palstance*x+initialPhase)+setover)];
drawPoint({
pw:3,ph:3,color:'DarkRed',point: cur
});
}
var pend = [dot[0]+max, dot[1]+(amplitude*Math.sin(palstance*max+initialPhase)+setover)];
drawPoint({
pw:3,ph:3,color:'DarkRed',point: pend
});
drawLine(pre, pend);
}