Last active
April 10, 2017 14:03
-
-
Save mvbattan/2c36db8f27f8691955bd8474620ba6e5 to your computer and use it in GitHub Desktop.
PointsPath.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { View } from 'react-native'; | |
import { dist, angle, pointPropTypes, add } from './pointUtils'; | |
import { keyGen } from './keyUtils'; | |
function toLine(pointA, pointB, color, opacity) { | |
return ( | |
<View | |
key={keyGen(pointA, pointB)} | |
style={[ | |
createLine( | |
dist(pointA, pointB), | |
angle(pointA, pointB), | |
color, | |
opacity, | |
pointA | |
) | |
]} | |
/> | |
); | |
} | |
function drawPoint(point, color) { | |
return ( | |
<View | |
key={keyGen(point, color)} | |
style={createPoint(point, color)} | |
/> | |
); | |
} | |
function PointsPath({ color, pointList, opacity, startingPoint }) { | |
return ( | |
<View style={styles.container}> | |
{[...pointList.map((elem, i) => { | |
if (i === pointList.length - 1) { | |
return null; | |
} | |
return toLine( | |
add(pointList[i], startingPoint), | |
add(pointList[i + 1], startingPoint), | |
color, | |
opacity | |
); | |
}), | |
...pointList.map((elem) => { | |
return drawPoint(add(elem, startingPoint), color); | |
})]} | |
</View> | |
); | |
} | |
PointsPath.propTypes = { | |
pointList: React.PropTypes.arrayOf(React.PropTypes.shape(pointPropTypes)), | |
color: React.PropTypes.string.isRequired, | |
opacity: React.PropTypes.number.isRequired, | |
startingPoint: React.PropTypes.shape(pointPropTypes) | |
}; | |
export const createLine = (dist, angle, color, opacity, startingPoint) => { | |
return { | |
backgroundColor: color, | |
height: 4, | |
width: dist, | |
bottom: dist * Math.sin(angle) / 2 + startingPoint.y, | |
left: -dist * (1 - Math.cos(angle)) / 2 + startingPoint.x, | |
position: 'absolute', | |
opacity, | |
transform: [ | |
{ rotate: `${(-1) * angle} rad` } | |
] | |
}; | |
}; | |
export const createPoint = (coordinates, color, size = 8) => { | |
return { | |
backgroundColor: color, | |
left: coordinates.x - 3, | |
bottom: coordinates.y - 2, | |
position: 'absolute', | |
borderRadius: 50, | |
width: size, | |
height: size | |
}; | |
}; | |
export default PointsPath; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment