Use HTML5 Canvas to draw rounded rectangle and some related application examples, html5canvas

Source: Internet
Author: User

Use HTML5 Canvas to draw rounded rectangle and some related application examples, html5canvas

The rounded rectangle consists of four sections and four 1/4 arcs.

Because we want to write a function instead of a fixed rounded rectangle, the parameters required by the function are listed here. After analysis, the code is directly typed out.

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> rounded rectangle </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. DrawRoundRect (context, 200,100,400,400, 50 );
  26. Context. strokeStyle = "# 0078AA ";
  27. Context. stroke ();
  28. }
  29. Function drawRoundRect (cxt, x, y, width, height, radius ){
  30. Cxt. beginPath ();
  31. Cxt. arc (x + radius, y + radius, radius, Math. PI, Math. PI * 3/2 );
  32. Cxt. lineTo (width-radius + x, y );
  33. Cxt. arc (width-radius + x, radius + y, radius, Math. PI * 3/2, Math. PI * 2 );
  34. Cxt. lineTo (width + x, height + y-radius );
  35. Cxt. arc (width-radius + x, height-radius + y, radius, 0, Math. PI * 1/2 );
  36. Cxt. lineTo (radius + x, height + y );
  37. Cxt. arc (radius + x, height-radius + y, radius, Math. PI * 1/2, Math. PI );
  38. Cxt. closePath ();
  39. }
  40. </Script>
  41. </Body>
  42. </Html>

Running result:

We recommend that you create a rounded rectangle by yourself, which helps you grasp the path.

Next we will use this function to do something else.

Draw a 2048 game interface
I will not explain the code too much. I suggest you study the code yourself and try it out first. I am using hard encoding, so it is not very good. You can try to optimize it.

Copy the content to the clipboard using JavaScript Code
  1. <! DOCTYPE html>
  2. <Html lang = "zh">
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> 2048 game page </title>
  6. <Style>
  7. Body {background: url ("./images/bg3.jpg") repeat ;}
  8. # Canvas {border: 1px solid # aaaaaa; display: block; margin: 50px auto ;}
  9. </Style>
  10. </Head>
  11. <Body>
  12. <Div id = "canvas-warp">
  13. <Canvas id = "canvas">
  14. Does your browser support Canvas ?! Just change one !!
  15. </Canvas>
  16. </Div>
  17. <Script>
  18. Window. onload = function (){
  19. Var canvas = document. getElementById ("canvas ");
  20. Canvas. width = 800;
  21. Canvas. height = 600;
  22. Var context = canvas. getContext ("2d ");
  23. Context. fillStyle = "# FFF ";
  24. Context. fillRect (0, 0, 800,600 );
  25. DrawRoundRect (context, 200,100,400,400, 5 );
  26. Context. fillStyle = "# AA7B41 ";
  27. Context. strokeStyle = "# 0078AA ";
  28. Context. stroke ();
  29. Context. fill ();
  30. For (var I = 1; I <= 4; I ++ ){
  31. For (var j = 1; j <= 4; j ++ ){
  32. DrawRoundRect (context, 200 + 16 * I + 80 * (I-1), 100 + 16 * j + 80 * (j-1), 80, 80, 5 );
  33. Context. fillStyle = "# CCBFB4 ";
  34. Context. strokeStyle = "# 0078AA ";
  35. Context. stroke ();
  36. Context. fill ();
  37. }
  38. }
  39. }
  40. Function drawRoundRect (cxt, x, y, width, height, radius ){
  41. Cxt. beginPath ();
  42. Cxt. arc (x + radius, y + radius, radius, Math. PI, Math. PI * 3/2 );
  43. Cxt. lineTo (width-radius + x, y );
  44. Cxt. arc (width-radius + x, radius + y, radius, Math. PI * 3/2, Math. PI * 2 );
  45. Cxt. lineTo (width + x, height + y-radius );
  46. Cxt. arc (width-radius + x, height-radius + y, radius, 0, Math. PI * 1/2 );
  47. Cxt. lineTo (radius + x, height + y );
  48. Cxt. arc (radius + x, height-radius + y, radius, Math. PI * 1/2, Math. PI );
  49. Cxt. closePath ();
  50. }
  51. </Script>
  52. </Body>
  53. </Html>

Running result:

After the function of this rounded rectangle is written, it can be encapsulated into the JS file by itself, and any good functions can be stored in the future, this file is a set of its own graphics library and game engine. Isn't it really cool?

In fact, game production is the main purpose of Canvas, but you must know that every game designer is an artist.


Drawing dialog box
You can try to use Canvas to draw a chat interface for practice and consolidation.

Here we use some knowledge to draw a rectangle, draw a rounded rectangle, draw a multi-line image, and fill the color. We haven't mentioned some Canvas text APIs yet, so you only need to be able to draw a rough interface. The Canvas API can be drawn out.

In fact, the above conversation is generated-"interface builder webpage version", which can be described as a derivative artifact. Is it really cool?

This is only the first version that took two days to write during the summer vacation. It has not been released yet. When I write this section, the page is still being optimized. You can try it on your own, or pay attention to and refer to my github: interface builder for this small project. This section does not repeat the interface code.

Now, I have learned all the basic Canvas drawing APIs. You can use your paint brush to make full use of it!

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.