A sine curve on Canvas is created by converting each angle to radians, evaluating Math.sin(), scaling the result, and joining the points with line segments.
const radians = degrees * Math.PI / 180;
const y = centerY - amplitude * Math.sin(radians);
ctx.beginPath();
for (let degree = 0; degree <= 540; degree += 2) {
const y = centerY - amplitude * Math.sin(degree * Math.PI / 180);
if (degree === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.stroke();
Math.sin() expects radians. See JavaScript Math.sin() for additional examples.
Slow-motion sine curve | Cosine curve | Sine and cosine together | Slow sine and cosine
No. Convert degrees to radians first.
From -1 to 1.
Canvas y coordinates increase downward.
The amplitude multiplier.
Increase the angle range beyond 360 degrees.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.