Tips for drawing an ellipse in html5 canvas and keeping the line width uniform

Source: Internet
Author: User

Comments: Drawing an ellipse in a Canvas is a common requirement, but this method has not been implemented in most browsers currently. Therefore, the arc or arcTo method is often used to draw an ellipse in combination with scale deformation, if you are interested, you can understand it and hope it can help you.

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.

Sample Code:

The Code is as follows:
<Canvas width = "400" height = "300"> </canvas>
<Script>
Var ctx = documentquerySelector ('canvas ') getContext ('2d ');
CtxlineWidth = "10 ";
Ctxscale (1, 2 );
Ctxarc (150,150,100, 0, MathPI * 2, false );
Ctxstroke ();
</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:

The Code is as follows:
[Code]
<Canvas width = "400" height = "300"> </canvas>
<Script>
Var ctx = documentquerySelector ('canvas ') getContext ('2d ');
CtxlineWidth = "10 ";
Ctxsave ();
Ctxscale (1, 2 );
Ctxarc (150,150,100, 0, MathPI * 2, false );
Ctxrestore ();
Ctxstroke ();
</Script>

[/Code]


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.


Related Article

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.