-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.html
158 lines (142 loc) · 4.22 KB
/
lines.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head>
<title>Lines</title>
</head>
<body>
<canvas style="border-style:solid;padding: 0;" id="canv"></canvas>
<pre id="coords"></pre>
<script>
var c = document.getElementById("canv");
var ctx = c.getContext("2d");
class Square {
constructor(x, y, wh, ctxt){
this.xpos = x;
this.ypos = y;
this.size = wh;
this.ctx = ctxt;
this.selected = false;
this.pointerx = 0;
this.pointery = 0;
this.left_line = null;
this.right_line = null;
}
draw(){
this.ctx.beginPath();
this.ctx.rect(this.xpos, this.ypos, this.size, this.size);
this.ctx.stroke();
}
pointer_in(ptrx, ptry){
if(ptrx >= this.xpos && ptrx <= this.xpos + this.size && ptry >= this.ypos && ptry <= this.ypos + this.size){
return true;
} else {
return false;
}
}
assign_left_line(line){
line.ctx = this.ctx
this.left_line = line;
this.update_left_line();
}
update_left_line(){
this.left_line.xend = this.xpos + (this.size / 2);
this.left_line.yend = this.ypos + (this.size / 2);
}
assign_right_line(line){
line.ctx = this.ctx
this.right_line = line;
this.update_right_line();
}
update_right_line(){
this.right_line.xpos = this.xpos + (this.size / 2);
this.right_line.ypos = this.ypos + (this.size / 2);
}
select_square(ptrx, ptry){
this.selected = true;
this.pointerx = ptrx - this.xpos;
this.pointery = ptry - this.ypos;
}
drag_square(ptrx, ptry){
this.xpos = ptrx - this.pointerx;
this.ypos = ptry - this.pointery;
if(this.left_line != null){
this.update_left_line();
}
if(this.right_line != null){
this.update_right_line();
}
}
};
class Line {
constructor(x, y, ye, xe, ctxt){
this.xpos = x;
this.ypos = y;
this.xend = xe;
this.yend = ye;
this.ctx = ctxt;
}
draw(){
this.ctx.beginPath();
this.ctx.moveTo(this.xpos, this.ypos);
this.ctx.lineTo(this.xend, this.yend);
this.ctx.stroke();
}
}
let sq1 = new Square(10, 10, 30, ctx);
let sq2 = new Square(100, 20, 30, ctx);
let sq3 = new Square(200, 100, 30, ctx);
let ln1 = new Line();
let ln2 = new Line();
let ln3 = new Line();
sq1.draw(); sq2.draw(); sq3.draw();
sq1.assign_right_line(ln1);
sq2.assign_left_line(ln1);
sq2.assign_right_line(ln2);
sq3.assign_left_line(ln2);
sq3.assign_right_line(ln3);
sq1.assign_left_line(ln3);
ln1.draw(); ln2.draw(); ln3.draw();
let squares = [sq1, sq2, sq3];
let lines = [ln1, ln2];
console.log(sq1.pointer_in(20,20));
let rect = ctx.canvas.getBoundingClientRect();
c.addEventListener('mousedown', e => {
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
squares.forEach(
function(square){
if(square.pointer_in(x, y)){
square.select_square(x, y);
}
}
);
});
c.addEventListener('mousemove', e => {
var output = "";
let x = e.clientX - rect.left;
let y = e.clientY - rect.top;
if(sq1.pointer_in(x,y)){
output = "[" + x + ", " + y + "]";
} else {
output = x + ", " + y ;
}
ctx.clearRect(0, 0, c.width, c.height);
squares.forEach(
function(square){
if(square.selected){
square.drag_square(x, y);
}
}
);
sq1.draw(); sq2.draw(); sq3.draw();
ln1.draw(); ln2.draw(); ln3.draw();
document.getElementById("coords").innerHTML = output;
});
window.addEventListener('mouseup', e => {
sq1.selected = false;
sq2.selected = false;
sq3.selected = false;
});
</script>
</body>
</html>