Draw a sine curve using js, and draw a sine from js

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.