forked from sderosiaux/react-line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React from 'react';
export default class Line extends React.Component {
static propTypes = {
from: React.PropTypes.shape({
x: React.PropTypes.number.isRequired,
y: React.PropTypes.number.isRequired,
}),
to: React.PropTypes.shape({
x: React.PropTypes.number.isRequired,
y: React.PropTypes.number.isRequired,
}),
style: React.PropTypes.string
};
render() {
let from = this.props.from;
let to = this.props.to;
if (to.x < from.x) {
from = this.props.to;
to = this.props.from;
}
const len = Math.sqrt(Math.pow(from.x - to.x, 2) + Math.pow(from.y - to.y, 2));
const angle = Math.atan((to.y - from.y) / (to.x - from.x));
const style = {
position: 'absolute',
transform: `translate(${from.x - .5 * len * (1 - Math.cos(angle))}px, ${from.y + .5 * len * Math.sin(angle)}px) rotate(${angle}rad)`,
width: `${len}px`,
height: `${0}px`,
borderBottom: this.props.style || '1px solid black'
};
return <div style={style}></div>;
}
}