Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { View, ART } from 'react-native';
  4.  
  5. const { Surface, Shape, Path, Group } = ART;
  6.  
  7. export default class CircularProgress extends React.Component {
  8.  
  9. circlePath(cx, cy, r, startDegree, endDegree) {
  10.  
  11. let p = Path();
  12. p.path.push(0, cx + r, cy);
  13. p.path.push(4, cx, cy, r, startDegree * Math.PI / 180, endDegree * Math.PI / 180, 1);
  14.  
  15. return p;
  16. }
  17.  
  18. extractFill(fill) {
  19. if (fill < 1) {
  20. return 0;
  21. } else if (fill > 100) {
  22. return 100;
  23. }
  24. return fill;
  25. }
  26.  
  27. render() {
  28. const { size, width, tintColor, backgroundColor, style, rotation, linecap, arcSweepAngle, children, capWidth, capColor, strokeCap } = this.props;
  29. const borderWidth = capWidth > width ? capWidth : width;
  30. const radius = (size-borderWidth)/2;
  31. const center = size/2;
  32. const backgroundPath = this.circlePath(size / 2, size / 2, size / 2 - width / 2, 0, arcSweepAngle * .9999);
  33.  
  34. const fill = this.extractFill(this.props.fill);
  35. const circlePath = this.circlePath(size / 2, size / 2, size / 2 - width / 2, 0, (arcSweepAngle * .9999) * fill / 100);
  36.  
  37. const radian = Math.PI * fill/50;
  38. const capX = radius * Math.cos(radian) + center;
  39. const capY = radius * Math.sin(radian) + center;
  40.  
  41. return (
  42. <View style={style}>
  43. <Surface
  44. width={size}
  45. height={size}>
  46. <Group rotation={rotation+(360-arcSweepAngle)/2} originX={center} originY={center}>
  47. <Shape d={backgroundPath}
  48. stroke={backgroundColor}
  49. strokeWidth={width}
  50. strokeCap={linecap}/>
  51. <Shape d={circlePath}
  52. stroke={tintColor}
  53. strokeWidth={width}
  54. strokeCap={linecap}
  55. />
  56. {
  57. fill <= 100 &&
  58. <Shape
  59. width={300}
  60. d={this.circlePath(capX, capY, capWidth/4, 0, 360)}
  61. stroke={capColor}
  62. strokeWidth={capWidth}
  63. />
  64. }
  65. </Group>
  66.  
  67. </Surface>
  68. {
  69. children && children(fill)
  70. }
  71. </View>
  72. )
  73. }
  74. }
  75.  
  76. CircularProgress.propTypes = {
  77. size: PropTypes.number.isRequired,
  78. fill: PropTypes.number.isRequired,
  79. width: PropTypes.number.isRequired,
  80. capColor: PropTypes.string,
  81. capWidth: PropTypes.number,
  82. tintColor: PropTypes.string,
  83. backgroundColor: PropTypes.string,
  84. rotation: PropTypes.number,
  85. linecap: PropTypes.string,
  86. children: PropTypes.func,
  87. arcSweepAngle: PropTypes.number,
  88. }
  89.  
  90. CircularProgress.defaultProps = {
  91. tintColor: 'black',
  92. backgroundColor: '#e4e4e4',
  93. rotation: 90,
  94. linecap: 'round',
  95. arcSweepAngle: 360,
  96. capColor: 'black',
  97. capWidth: 0
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement