Html5 canvas fillRect coordinates and sizes

Source: Internet
Author: User

Comments: I learned about canvas in html5 today and found that the coordinates and sizes of fillRect are always incorrect. After studying it for a long time, I found that the width and height of the canvas must be inline in the canvas label before applying fillRect (100,100,100,100) the first two 100 are the coordinates, and the last two 100 are the width and height.

Today, I learned about the canvas of html5 and found that the coordinates and sizes of fillRect were not correct. After studying it for a long time, I found that the width and height of the canvas must be inline in the canvas label. Depressed for half a day.

Incorrect Method 1:

The Code is as follows:
<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> Document </title>
<Style>
# Mycanvas {
Width: 200px;
Height: 200px;
Background: yellow;
}
</Style>
</Head>
<Body>
<Canvas id = 'mycanvas '> </canvas>
<Script>
Var c = document. getElementById ('mycanvas ');
Var ctx = c. getContext ("2d ");
Ctx. fillStyle = '# f36 ';
Ctx. fillRect (100,100,100,100 );
</Script>
</Body>
</Html>

Incorrect Method 2:

The Code is as follows:
<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> Document </title>
</Head>
<Body>
<Canvas id = 'mycanvas 'style = 'width: 200px; height: 200px; background: yellow'> </canvas>
<Script>
Var c = document. getElementById ('mycanvas ');
Var ctx = c. getContext ("2d ");
Ctx. fillStyle = '# f36 ';
Ctx. fillRect (100,100,100,100 );
</Script>
</Body>
</Html>

Display result:

The correct method:

The Code is as follows:
<! Doctype html>
<Html lang = "en">
<Head>
<Meta charset = "UTF-8">
<Title> Document </title>
</Head>
<Body>
<Canvas id = 'mycanvas 'width = '200px 'height = '200px' style = 'background: yellow'> </canvas>
<Script>
Var c = document. getElementById ('mycanvas ');
Var ctx = c. getContext ("2d ");
Ctx. fillStyle = '# f36 ';
Ctx. fillRect (100,100,100,100 );
</Script>
</Body>
</Html>

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.