Solution to inconsistent border styles when HTML5 canvas draws a rectangle

Source: Internet
Author: User

Comments: HTML5 painting finds that the border drawn by the function that draws a rectangle has a style inconsistency problem. Let's share our ideas on solving the problem with you. Do not miss out if you are interested.

In the past two days, I used my colleague HTML5 to draw pictures and found that the border drawn by the function that draws a rectangle in the image had a style inconsistency problem. Finally, I found the crux of the problem through some exclusion methods, I will share it with you.

First, the code for drawing a rectangle in HTML5 is attached:

The Code is as follows:
<Canvas id = "myCanvas" width = "578" height = "200"> </canvas>
<Script>
Var canvas = document. getElementById ('mycanvas ');
Var context = canvas. getContext ('2d ');

Context. beginPath ();

Context. rect (188.0, 50,200,100.375 );
Context. fillStyle = 'white ';
Context. fill ();
Context. lineWidth = 1;
Context. strokeStyle = 'red ';
Context. stroke ();
</Script>

You will find the border style is different after the execution, obviously the bottom is very fine. The color is also slightly different.

After you change context. rect (188.0, 50,200,100.375) to context. rect (188.0, 50,200,100), the style is completely consistent.
This shows that when you draw a rectangle, if the parameters in the rectangle are not integers, it will easily lead to border problems. Therefore, we suggest you use them after rounding.


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.