-
Notifications
You must be signed in to change notification settings - Fork 7
/
Lines.js
76 lines (63 loc) · 1.74 KB
/
Lines.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @file 手动解锁的格状视图的线区
* @author Leo Wang([email protected])
*/
import React, {Component} from 'react'
import {
Animated,
View,
StyleSheet
} from 'react-native';
var styles = require('./styles');
class Lines extends Component {
constructor(props) {
super(props);
this.state = {
lines: [{from: [0, 0], to: [30, 100]}]
}
}
getLines() {
var lines = this.state.lines;
return lines.map(this.getLine.bind(this));
}
getLine(lineData) {
var rx = lineData.to[0] - lineData.from[0];
var ry = lineData.to[1] - lineData.from[1];
var length = Math.sqrt(rx * rx + ry * ry);
var degree = this.getDegree(lineData.from, lineData.to);
return (
<View style={[
styles.line,
{
left: lineData.from[0],
top: lineData.from[1],
height: length,
transform: [
{rotate: degree + 'deg'}
]
}
]} />
);
}
getDegree(from, to){
var rx = to[0] - from[0];
var ry = to[1] - from[1];
var radius = Math.sqrt(rx * rx + ry * ry);
var helperPoint = {
x: from[0],
y: from[1] - radius
}
var dx = helperPoint[0] - to[0];
var dy = helperPoint[1] - to[1];
var d = Math.sqrt(dx * dx + dy * dy) / 2;
return Math.round(2 * Math.asin(d/radius) * (dx > 0 ? -1 : 1) / Math.PI * 180);
}
render() {
return (
<View style={styles.lines}>
{this.getLines()}
</View>
);
}
}
module.exports = Lines;