Drawing an ellipse in a canvas is a common requirement. The ellipse method is added to the new HTML canvas 2D context W3C draft, but this method has not been implemented by most browsers currently, so we need to use the arc or arcto method combined with scale deformation to draw an ellipse.
ExampleCode:
<Canvas width = "400" Height = "300"> </canvas> <SCRIPT> var CTX = document. queryselector ('canvas '). getcontext ('2d '); CTX. linewidth = "10"; CTX. scale (1, 0.2); CTX. arc( 150,150,100, 0, math. pI * 2, false); CTX. stroke (); </SCRIPT>
A little wrong, because the line width is uneven, stroke is also affected by scale.
It requires a little skill to correct this problem.
Sample Code:
<Canvas width = "400" Height = "300"> </canvas> <SCRIPT> var CTX = document. queryselector ('canvas '). getcontext ('2d '); CTX. linewidth = "10"; CTX. save (); CTX. scale (1, 0.2); CTX. arc( 150,150,100, 0, math. pI * 2, false); CTX. restore (); CTX. stroke (); </SCRIPT>
Now it's even and perfect.
Tip: Save the canvas state first, then zoom in and out, call the path command, restore the canvas state, and draw the stroke.
The key point is to save and zoom, restore first and then stroke.