forked from nsjcy/frontEnd/nsjcy

liyj
2020-02-03 7a4d973140ee0168f509098b52745c8a7947ca74
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function Line(user, option) {
    this.user = user;
    this.type = 'line';
    this.seq = option.seq; // seq 唯一编号
    this.show = option.show; // 是否展示
    this.select = false; // 是否被选了
    this.color = option.color;
    this.time = parseInt(+ new Date() / 1000, 10);
    this.thin = option.thin;
    this.startSeq = option.seq; // 开始点seq
    this.endSeq = 0; // 结束点seq
  this.belongSeq = option.belongSeq;
    // 每一个点  {x, y, seq}
    this.lines = [
        {
            x: option.x,
            y: option.y,
      belongSeq: option.belongSeq,
            seq: option.seq
        }
    ];
    this.border = {
        maxX: option.x,
        maxY: option.y,
        minX: option.x,
        minY: option.y
    }
}
 
Line.prototype.setBorder = function(x, y) {
    if (x + this.thin > this.border.maxX) {
        this.border.maxX = x + this.thin;
    }
    if (x - this.thin < this.border.minX) {
        this.border.minX = x - this.thin;
    }
    if (y + this.thin > this.border.maxY) {
        this.border.maxY = y + this.thin;
    }
    if (y - this.thin < this.border.minY) {
        this.border.minY = y - this.thin;
    }
}
 
Line.prototype.sort = function () {
  // 给lines排序
  this.lines.sort(function (a, b) {
    return a.seq - b.seq;
  })
}
 
function dealColor(color) {
    let temp = parseInt(color).toString(16);
    temp = temp.substr(0, temp.length - 2);
    temp = '000000' + temp;
    temp = temp.substring(temp.length - 6, temp.length);
    return hexToRgba(temp);
}
 
function hexToRgba(hex) {
    let rgb = [];
    hex.replace(/../g, function(color) {
        rgb.push(parseInt(color, 0x10)); //按16进制将字符串转换为数字
    });
    return "rgba(" + rgb.join(",") + ",1)";
}
 
function formatColor(color) {
    let temp = rgbaToHex(color);
    return parseInt(temp + 'ff', 16);
}
 
function rgbaToHex(rgba) {
    let color = rgba.toString().match(/\d+/g);
    let hex = '';
    for (let i = 0; i < 3; i++) {
        hex += ("0" + Number(color[i]).toString(16)).slice(-2);
    }
    return hex;
}
 
function Graph(user, option) {
    this.user = user;
    this.type = 'graph';
    this.graph = option.graph; // line:直线 circle:圆 rect: 矩形
    this.seq = option.seq; // seq 唯一编号
    this.show = option.show; // 是否展示
    this.select = false; // 是否被选了
    this.time = parseInt(+ new Date() / 1000, 10);
    this.color = option.color;
    this.thin = option.thin;
    this.solid = option.solid || false; // 是否实心
    // 记录开始点与结束点
    this.startPoint = {
        x: option.beginPoint.x,
        y: option.beginPoint.y,
        seq: option.beginPoint.seq
    };
    this.endPoint = {
        x: option.endPoint.x,
        y: option.endPoint.y,
        seq: option.endPoint.seq
    };
}
 
module.exports.Line = Line;
module.exports.Graph = Graph;
module.exports.dealColor = dealColor;
module.exports.formatColor = formatColor;