From 327cce9a33fe1dedbbf9943c5bb2d5aa464cc5da Mon Sep 17 00:00:00 2001 From: Jacob Lauritzen Date: Wed, 13 Mar 2019 11:08:50 +0800 Subject: [PATCH] Fix #176, code style --- example/App.js | 34 +++++++++--------------- src/CircularProgress.js | 57 ++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 49 deletions(-) diff --git a/example/App.js b/example/App.js index 98e65a9..abf4aac 100644 --- a/example/App.js +++ b/example/App.js @@ -8,7 +8,7 @@ export default class App extends React.Component { state = { isMoving: false, pointsDelta: 0, - points: 325 + points: 325, }; componentWillMount() { this._panResponder = PanResponder.create({ @@ -34,31 +34,26 @@ export default class App extends React.Component { this.setState({ isMoving: false, points: points > 0 ? Math.min(points, MAX_POINTS) : 0, - pointsDelta: 0 + pointsDelta: 0, }); }, }); } render() { - const fill = this.state.points / MAX_POINTS * 100; + const fill = (this.state.points / MAX_POINTS) * 100; return ( - + - {(fill) => ( - - { Math.round(MAX_POINTS * fill / 100) } - - )} + {fill => {Math.round((MAX_POINTS * fill) / 100)}} - { this.state.pointsDelta >= 0 && '+' } - { this.state.pointsDelta } + {this.state.pointsDelta >= 0 && '+'} + {this.state.pointsDelta} ); @@ -95,29 +90,24 @@ export default class App extends React.Component { const styles = StyleSheet.create({ points: { - backgroundColor: 'transparent', - position: 'absolute', - top: 72, - left: 56, - width: 90, textAlign: 'center', color: '#7591af', fontSize: 50, - fontWeight: "100" + fontWeight: '100', }, container: { flex: 1, justifyContent: 'space-between', alignItems: 'center', backgroundColor: '#152d44', - padding: 50 + padding: 50, }, pointsDelta: { color: '#4c6479', fontSize: 50, - fontWeight: "100" + fontWeight: '100', }, pointsDeltaActive: { color: '#fff', - } + }, }); diff --git a/src/CircularProgress.js b/src/CircularProgress.js index 02efb19..04384ba 100644 --- a/src/CircularProgress.js +++ b/src/CircularProgress.js @@ -5,21 +5,18 @@ import { Svg, Path, G } from 'react-native-svg'; export default class CircularProgress extends React.PureComponent { polarToCartesian(centerX, centerY, radius, angleInDegrees) { - var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0; + var angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0; return { - x: centerX + (radius * Math.cos(angleInRadians)), - y: centerY + (radius * Math.sin(angleInRadians)) + x: centerX + radius * Math.cos(angleInRadians), + y: centerY + radius * Math.sin(angleInRadians), }; } - circlePath(x, y, radius, startAngle, endAngle){ + circlePath(x, y, radius, startAngle, endAngle) { var start = this.polarToCartesian(x, y, radius, endAngle * 0.9999); var end = this.polarToCartesian(x, y, radius, startAngle); var largeArcFlag = endAngle - startAngle <= 180 ? '0' : '1'; - var d = [ - 'M', start.x, start.y, - 'A', radius, radius, 0, largeArcFlag, 0, end.x, end.y - ]; + var d = ['M', start.x, start.y, 'A', radius, radius, 0, largeArcFlag, 0, end.x, end.y]; return d.join(' '); } @@ -40,31 +37,41 @@ export default class CircularProgress extends React.PureComponent { children, } = this.props; - const backgroundPath = this.circlePath(size / 2, size / 2, size / 2 - width / 2, 0, arcSweepAngle); - const circlePath = this.circlePath(size / 2, size / 2, size / 2 - width / 2, 0, arcSweepAngle * this.clampFill(fill) / 100); - const offset = size - (width * 2); + const maxWidthCircle = backgroundWidth ? Math.max(width, backgroundWidth) : width; + + const backgroundPath = this.circlePath( + size / 2, + size / 2, + size / 2 - maxWidthCircle / 2, + 0, + arcSweepAngle + ); + const circlePath = this.circlePath( + size / 2, + size / 2, + size / 2 - maxWidthCircle / 2, + 0, + (arcSweepAngle * this.clampFill(fill)) / 100 + ); + const offset = size - maxWidthCircle * 2; const childContainerStyle = { position: 'absolute', - left: width, - top: width, + left: maxWidthCircle, + top: maxWidthCircle, width: offset, height: offset, borderRadius: offset / 2, alignItems: 'center', justifyContent: 'center', - overflow: 'hidden' + overflow: 'hidden', }; return ( - - - { backgroundColor && ( + + + {backgroundColor && ( - {children && ( - - {children(fill)} - - )} + {children && {children(fill)}} ); } @@ -112,5 +115,5 @@ CircularProgress.defaultProps = { tintColor: 'black', rotation: 90, lineCap: 'butt', - arcSweepAngle: 360 + arcSweepAngle: 360, };