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>