how to draw ellipse in javascript canvas

How to Draw Ellipse in JavaScript Canvas

If you are looking to draw an ellipse in JavaScript canvas, then you have come to the right place. Drawing an ellipse is not difficult, but it does require some knowledge of the canvas API. In this blog post, we will show you how to draw an ellipse in JavaScript canvas with step-by-step instructions.

Method 1: Using the arc() Method

The easiest way to draw an ellipse in JavaScript canvas is by using the arc() method. The arc() method draws a circular or elliptical arc with a given center point, radius, start angle, and end angle. To draw an ellipse, we need to use the arc() method twice - once for the horizontal radius and once for the vertical radius.


const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const horizontalRadius = 100;
const verticalRadius = 50;

ctx.beginPath();
ctx.arc(centerX, centerY, horizontalRadius, 0, 2 * Math.PI);
ctx.stroke();

ctx.beginPath();
ctx.arc(centerX, centerY, verticalRadius, 0, 2 * Math.PI);
ctx.stroke();

In the above code, we first get the canvas element and its context. We then calculate the center point of the canvas by dividing its width and height by 2. We also set the horizontal and vertical radii of the ellipse. Then, we use the arc() method twice to draw the ellipse - once for the horizontal radius and once for the vertical radius.

Method 2: Using Bezier Curves

Another way to draw an ellipse in JavaScript canvas is by using Bezier curves. Bezier curves are a mathematical way to define smooth curves. To draw an ellipse using Bezier curves, we need to calculate the control points for the curve. We can use the following formula to calculate the control points:


const kappa = 0.5522847498;
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const horizontalRadius = 100;
const verticalRadius = 50;
const offset = horizontalRadius * kappa;

ctx.beginPath();
ctx.moveTo(centerX - horizontalRadius, centerY);
ctx.bezierCurveTo(
    centerX - horizontalRadius, centerY - offset,
    centerX - offset, centerY - verticalRadius,
    centerX, centerY - verticalRadius
);
ctx.bezierCurveTo(
    centerX + offset, centerY - verticalRadius,
    centerX + horizontalRadius, centerY - offset,
    centerX + horizontalRadius, centerY
);
ctx.bezierCurveTo(
    centerX + horizontalRadius, centerY + offset,
    centerX + offset, centerY + verticalRadius,
    centerX, centerY + verticalRadius
);
ctx.bezierCurveTo(
    centerX - offset, centerY + verticalRadius,
    centerX - horizontalRadius, centerY + offset,
    centerX - horizontalRadius, centerY
);
ctx.closePath();
ctx.stroke();

In the above code, we first define a constant called kappa, which is used to calculate the control points for the curve. We then calculate the center point of the canvas and the horizontal and vertical radii of the ellipse. We also calculate an offset value using the horizontal radius and kappa. This offset value is used to calculate the control points for the curves.

We then use the bezierCurveTo() method four times to draw the ellipse using four Bezier curves. The first two calls to bezierCurveTo() draw the left half of the ellipse, while the last two calls draw the right half of the ellipse. We then use the closePath() method to connect the last point back to the first point and stroke the path to draw the ellipse.

Conclusion

In this blog post, we have shown you two ways to draw an ellipse in JavaScript canvas - using the arc() method and using Bezier curves. Both of these methods are effective, but they have different pros and cons. The arc() method is easier to use and understand, but it can produce jagged edges at high eccentricities. On the other hand, Bezier curves can produce smoother edges, but they require more calculations and may be harder to understand for beginners.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe