How to draw an oval in html5 canvas?
To quickly draw an oval in HTML5 canvas, you can use the ellipse
method:
This renders an oval at the coordinates (100, 75)
with a horizontal radius
of 50
and a vertical radius
of 35
.
Alternative methods
There's more than one way to skin a cat... or in our case, draw an oval. Here's some alternatives when ellipse
is not universally supported or you need more precise control over your shape.
Bezier curves - Now you're drawing with power
Bezier curves give you more control over the ellipse drawing:
Quadratic curves - Because math is cool
The quadraticCurveTo() method is another valid approach:
Circle scaling - The Beauty and The Beast method
A non-uniform scaling transformation can also turn a circle into an oval:
The save()
and restore()
functions help maintain the canvas state during the transformation process.
Advanced transformations
Here we will handle common pitfalls, like undesired skewing, and we'll also learn how to draw part of an oval.
Scaling circles into ovals
The scale()
transformation will turn a boring circle into an exciting oval:
Adding rotation to ovals - Spin to win
Include the rotation parameter to make your oval dance:
Provide insights with partial ovals
Sometimes, less is more. Drawing just part of an oval can make data more understandable:
Was this article helpful?