-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.js
52 lines (51 loc) · 1.22 KB
/
line.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
class Line {
constructor(firstPoint, secondPoint, color) {
this.color = color;
this.vertices = [
firstPoint.x,
firstPoint.y,
0,
secondPoint.x,
secondPoint.y,
0,
];
this.xformMatrix = new Float32Array([
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
]);
}
drawOnCanvas() {
let vertex_buffer = getVerticesBuffer(this.vertices);
let vertShader = getVertexShader();
let fragShader = getFragmentShader(this.color);
let shaderProgram = getShaderProgram(vertShader, fragShader);
transformObject(shaderProgram, this.xformMatrix);
bindVertexBuffer(shaderProgram, vertex_buffer);
gl.drawArrays(gl.LINES, 0, 2);
}
resizeLine(xCursor, yCursor) {
let midX = (this.vertices[0] + this.vertices[3]) / 2;
let midY = (this.vertices[1] + this.vertices[4]) / 2;
let length = Math.abs(xCursor - midX);
let height = Math.abs(yCursor - midY);
this.vertices[0] = midX - length / 2;
this.vertices[3] = midX + length / 2;
this.vertices[1] = midY - height / 2;
this.vertices[4] = midY + height / 2;
renderAll();
}
}