Use Raphael to draw a ring progress bar on the webpage

Source: Internet
Author: User

We have seen too many bar progress bars. It is relatively simple to implement. It is always rectangular, and it is not very nice to place it in a square area. With the emergence of css3, ring-like progress bars are increasingly used. However, since IE6/7/8 does not support css3, we can only implement it in other ways. This article uses Raphael to draw a picture. This component encapsulates svg and vml in a unified manner and draws based on different technologies in the browser, So IE can also be used.

First:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250H2B-0.png "border =" 0 "alt =" "/>


Good results, right? The code is not complex:

 
 
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <Html xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head>
  5. <Title> circular progress bar </title>
  6. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  7. <Script src = "jquery-1.4.2.js"> </script>
  8. <Script src = "raphael-min.js"> </script>
  9. <Style>
  10. # Txt {
  11. Width: 74px;
  12. Height: 74px;
  13. Line-height: 74px;
  14. Position: absolute;
  15. Margin-top:-74px;
  16. Text-align: center;
  17. Color: #9e9fa3;
  18. Font-size: 18px;
  19. Font-family: Arial;
  20. }
  21. </Style>
  22. <Script>
  23.  
  24. Var demo = {
  25. Paper: null,
  26.  
  27. Init: function (){
  28. // Initialize the Raphael canvas
  29. This. paper = Raphael ("bg", 74, 74 );
  30.  
  31. // Draw the basemap first
  32. This. paper. image ("progressBg.png", 0, 0, 74, 74 );
  33.  
  34. // Progress ratio: 0 to 1. In this example, we will draw 65%
  35. // Note that the following algorithm does not support painting 100%. You need to draw the image according to 99.99%.
  36. Var percent = 0.65,
  37. DrawPercent = percent> = 1? 0.9999: percent;
  38.  
  39. // Start to calculate the position of each point, as shown in the following figure.
  40. // R1 indicates the inner circle radius and r2 indicates the outer circle radius.
  41. Var r1 = 26, r2 = 31, PI = Math. PI,
  42. P1 = {
  43. X: 37,
  44. Y: 69
  45. },
  46. P4 = {
  47. X: p1.x,
  48. Y: p1.y-r2 + r1
  49. },
  50. P2 = {
  51. X: p1.x + r2 * Math. sin (2 * PI * (1-drawPercent )),
  52. Y: p1.y-r2 + r2 * Math. cos (2 * PI * (1-drawPercent ))
  53. },
  54. P3 = {
  55. X: p4.x + r1 * Math. sin (2 * PI * (1-drawPercent )),
  56. Y: p4.y-r1 + r1 * Math. cos (2 * PI * (1-drawPercent ))
  57. },
  58. Path = [
  59. 'M', p1.x, '', p1.y,
  60. 'A', r2, '', r2, '0', percent> 0.5? 1: 0, '1', p2.x, '', p2.y,
  61. 'L', p3.x, '', p3.y,
  62. 'A', r1, '', r1, '0', percent> 0.5? 1: 0, '0', p4.x, '', p4.y,
  63. 'Z'
  64. ]. Join ('');
  65.  
  66. // Draw a graph using the path method, which consists of two arcs and two straight lines. The algorithm for drawing an arc is shown below:
  67. This. paper. path (path)
  68. // Fill in gradient, from # 3f0b3f to # ff66ff
  69. . Attr ({"stroke-width": 0.5, "stroke": "# c32ec3", "fill": "90-# 3f0b3f-# ff66ff "});
  70.  
  71. // Display progress text
  72. $ ("# Txt"). text (Math. round (percent * 100) + "% ");
  73. }
  74.  
  75. };
  76.  
  77. $ (Function (){
  78. Demo. init ();
  79. });
  80. </Script>
  81. </Head>
  82. <Body>
  83.  
  84. <! -- Host the graphic subject -->
  85. <Div id = "bg"> </div>
  86. <! -- Carry progress text -->
  87. <Div id = "txt"> </div>
  88.  
  89. </Body>
  90. </Html>


The progress bar consists of two parts: the basemap progressBg.png and the 74 × 74 png. Use the Raphael image method to draw the progress bar first:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250L102-1.png "border =" 0 "alt =" "/>

The second step is to use Raphael to draw the progress part, which consists of two arcs and two line segments. For details, see:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/12250M527-2.png "border =" 0 "alt =" "/>

First, draw a clockwise arc from p1 to p2, then a straight line to p3, then draw an arc clockwise to p4, and then straight line back p1. Then fill it with gradient. Take 65% as an example. The path to describe the image is as follows (basically the svg syntax ):

 
 
  1. M37 69
  2. A31 31 0 1 1 62.079526825623375 19.778657178933337
  3. L58.03444185374863 22.7175834403957
  4. A26 26 0 1 0 37 64

It looks a little scary. In fact, it can be simply divided into several parts:

◆ The first line indicates moving to (37, 69) as the starting point, that is, p1;
◆ The second line indicates drawing an arc, that is, the section between p1 and p2;
◆ The third line indicates drawing a straight line, that is, the section between p2 and p3;
◆ The fourth line indicates that a circle is drawn, which is between p3 and p4;
◆ The fifth line indicates a closed image, which is equivalent to a connection from p4 back to p1;

In this case, the arc parameters are complex. The parameters are as follows:

 
 
  1. A  rx ry x-axis-rotation large-arc-flag sweep-flag x y 

Taking the first arc as an example, the parameters are described as follows:

◆ Rx and ry: the length of the semi-axis and semi-short axis of the arc. We use 31 because we draw a positive circle;
◆ X-axix-rotation: the angle between the x-axis and the horizontal direction of the arc segment. Because we have drawn a positive circle, this parameter is useless and is set to 0;
◆ Large-arc-flag: large angle or small angle. Simply put, it is the half side of the circle, and 1 indicates the large angle. Because we drew an arc of 65% and a relatively large half, we entered 1. The program makes a judgment;
◆ Sweep-flag: The direction around the center. 1 indicates clockwise, and 0 indicates counterclockwise. The first arc is clockwise and the second arc is clockwise;
◆ X and y: the end coordinate of the arc;

For more information, refer to this article: Workshop.

PS: If you don't need a gradient, just draw a rough arc, which is much simpler.
PPS: you do not have to draw a positive circle. You can also change it to an elliptical progress bar.

 

This article from the rabbit nest blog, please be sure to keep this source http://boytnt.blog.51cto.com/966121/1074215

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.