Graphic transformation of HTML5 canvas Basic Drawing, html5canvas

Source: Internet
Author: User

Graphic transformation of HTML5 canvas Basic Drawing, html5canvas

<Canvas> </canvas>It is a new label added in HTML5 for drawing graphics. In fact, this label is the same as other labels. Its special feature is that this label can obtain a CanvasRenderingContext2D object, we can use JavaScript scripts to control this object for plotting.

<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.

Graph Conversion

Translation:Context. translate (x, y). The receiving parameters are the source point, which translates x in the x axis and y in the y axis.

Scaling:Context. scale (x, y). The receiving parameters are x-axis scaled proportionally, and y-axis scaled proportionally.

Rotation:Context. rotate (angle), the receiving parameter is the angle of the coordinate axis rotation.

It should be noted that after the graph is changed, the next drawing is followed by the last state, so if you need to return to the initial state, you need to use context. save (); and context. restore (); to save and restore the current state:

Copy the content to the clipboard using JavaScript Code
  1. Var canvas = document. getElementById ("canvas ");
  2. Var context = canvas. getContext ("2d ");
  3. // Translate ()
  4. Context. save ();
  5. Context. fillStyle = "# 1424DE ";
  6. Context. translate (10, 10 );
  7. Context. fillRect (0, 0, 200,200 );
  8. Context. restore ();
  9. // Scale ()
  10. Context. save ();
  11. Context. fillStyle = "# F5270B ";
  12. Context. scale (0.5, 0.5 );
  13. Context. fillRect (200,200, 50 );
  14. Context. restore ();
  15. // Rotate ()
  16. Context. save ();
  17. Context. fillStyle = "# 18EB0F ";
  18. Context. rotate (Math. PI/4 );
  19. Context. fillRect (200,200, 10 );
  20. Context. restore ();

The effect is as follows:

Another thing related to graphic transformation is matrix transformation: context. transform (a, B, c, d, e, f, g ). The parameters are described as follows:

A horizontal scaling (1 by default)
B horizontal skew (0 by default)
C vertical skew (0 by default)
D Vertical Scaling (1 by default)
E horizontal displacement (0 by default)
F vertical displacement (0 by default)

You can verify the effects of each parameter on your own. I will not describe it here.

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.