Home  >  Article  >  Web Front-end  >  Using js to draw sinusoidal curve_javascript skills

Using js to draw sinusoidal curve_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:20:491626browse

Mathematical formula: y=Asin(ωx φ) k

Sample: http://www.zhaojz.com.cn/demo/draw7.html

JS function declaration:

Copy code The code is as follows:

//Draw a sine curve
//dot origin
//amplitude Amplitude -- A
//initialPhase initial 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"; //Color of curve
var max = len*2*Math.PI/w; //The maximum value of x
//var x = -2*Math.PI/w/3;
var x = 0; //Initial value of x
var pre = [dot[0] x, dot[1] (amplitude*Math.sin(palstance*x initialPhase) setover)]; //Initial value of y
for(;x < max;x =5){ //Draw a line every five units
      var cur = [dot[0] x, dot[1] (amplitude*Math.sin(palstance*x initialPhase) setover)];
         drawLine(pre, cur, {color: color}); // Draw line
          pre = cur;
}
var d = Math.PI/(2*w);
for(var x =0;x < max;x =d){//Plot 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);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn