Draw a rectangle for HTML5 canvas Basic Drawing, html5canvas

Source: Internet
Author: User

Draw a rectangle for HTML5 canvas Basic Drawing, html5canvas

<Canvas> </canvas>It is just a container for drawing graphics. In addition to attributes such as id, class, and style, there are also attributes of height and width. There are three steps to draw an element on the <canvas> element:

1. Obtain the DOM object corresponding to the <canvas> element. This is a Canvas object;
2. Call the getContext () method of the Canvas object to obtain a CanvasRenderingContext2D object;
3. Call the CanvasRenderingContext2D object for plotting.

Draw rectangular rect (), fillRect (), and strokeRect ()

• Context. rect (x, y, width, height): defines only the path of the rectangle;
• Context. fillRect (x, y, width, height): draws a filled rectangle;
• Context. strokeRect (x, y, width, height): draws a rectangular border directly;

Copy the content to the clipboard using JavaScript Code
  1. <Script type = "text/javascript">
  2. Var canvas = document. getElementById ("canvas ");
  3. Var context = canvas. getContext ("2d ");
  4. // Use the rect Method
  5. Context. rect (10, 10, 190,190 );
  6. Context. lineWidth = 2;
  7. Context. fillStyle = "# 3EE4CB ";
  8. Context. strokeStyle = "# F5270B ";
  9. Context. fill ();
  10. Context. stroke ();
  11. // Use the fillRect Method
  12. Context. fillStyle = "# 1424DE ";
  13. Context. fillRect (190,190 );
  14. // Use the strokeRect Method
  15. Context. strokeStyle = "# F5270B ";
  16. Context. strokeRect (values, 10, 190,190 );
  17. // Use both the strokeRect method and fillRect Method
  18. Context. fillStyle = "# 1424DE ";
  19. Context. strokeStyle = "# F5270B ";
  20. Context. strokeRect (190,190, 10 );
  21. Context. fillRect (610,10, 190,190 );
  22. </Script>

Here we need to explain two points: the first point is the sequence of drawing between stroke () and fill (). If fill () is drawn after, when the stroke border is large, it will obviously overwrite the border drawn by stroke (). The second point: when setting the fillStyle or strokeStyle attribute, you can use "rgba (0.2, 0) ". The last parameter of this setting is transparency.

There is also an area related to rectangular painting: Clear the rectangular area: context. clearRect (x, y, width, height ).
The receiving parameters are respectively the starting position of the rectangle and the width and length of the rectangle.
In the above Code, add:

Context. clearRect (600,100, 60 );

You can get the following results:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.