-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjects.js
More file actions
209 lines (167 loc) · 4.64 KB
/
Copy pathobjects.js
File metadata and controls
209 lines (167 loc) · 4.64 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* class that represents a dot on the 2d plane
*/
class Dot {
constructor(x, y) {
// the absolute pixel values of the dot on the plane
this.x = x;
this.y = y;
// the relative values on the canvas
this.x_ratio = x / document.body.clientWidth;
this.y_ratio = y / document.body.clientHeight;
}
get_x() {
return this.x_ratio * document.body.clientWidth;
}
get_y() {
return this.y_ratio * document.body.clientHeight;
}
set_x(x) {
this.x = x;
this.x_ratio = x / document.body.clientWidth;
}
set_y(y) {
this.y = y;
this.y_ratio = y / document.body.clientHeight;
}
}
/**
* class that represents a line on the plane
*/
class Line {
constructor(s, e) {
// starting point
this.s = s;
// ending point
this.e = e;
}
get_color() {
return main_stroke_color;
}
}
/**
* super class for geometric objects on the plane
*/
class Geometrics {
constructor(center, content, type) {
// type identifier (e.g. "square")
this.type = type;
// center of the object
this.center = center;
// wether the object is selected
this.selected = false;
// wether the object has been grabbed
this.moveable = false;
// the content displayed on the object
this.content = content;
}
get_x() {
return this.center.get_x();
}
get_y() {
return this.center.get_y();
}
set_x(x) {
this.center.set_x(x);
}
set_y(y) {
this.center.set_y(y);
}
select() {
this.selected = true;
}
deselect() {
this.selected = false;
this.moveable = false;
}
get_content() {
return this.content;
}
get_color() {
if (this.selected) {
return selection_color;
} else {
return main_stroke_color;
}
}
}
/**
* class for circle object
*/
class Circle extends Geometrics {
constructor(center, content) {
super(center, content, "circle");
}
/**
* check if a given point lies inside the circle
* @param {Number} x the x coordinate of the point
* @param {Number} y the y coordinate of the point
* @returns wether the point lies inside the circle
*/
in(x,y) {
return Math.sqrt(Math.pow((this.get_x() - x),2) + Math.pow((this.get_y() - y),2)) < radius / 2;
}
}
/**
* class for circle object
*/
class Square extends Geometrics {
constructor(center, content, text) {
super(center, content, "square");
// wether the square is a text box
this.text = text;
}
is_text() {
return this.text;
}
/**
* check if a given point lies inside the square
* @param {Number} x the x coordinate of the point
* @param {Number} y the y coordinate of the point
* @returns wether the point lies inside the square
*/
in(x,y) {
if (this.text) {
return (Math.abs(this.get_x() - x) < square_w / 3) && (Math.abs(this.get_y() - y) < square_h / 4);
} else {
return ((Math.abs(this.get_x() - x) < square_w) && (Math.abs(this.get_y() - y) < square_h));
}
}
}
/**
* class for a grouping of multiple objects
*/
class Selection {
constructor() {
// list of elements in group
this.elements = [];
// coordinate where the group can be dragged by
this.hold_point = new Dot(0,0);
}
/**
* add object into grouping
* @param {Number} id the index of the object in the global objects list
* @param {Geometrics} input the object to be put in
*/
add(id, input) {
// set hold point on last abd push object into list
this.hold_point = input.center;
this.elements.push([id, [input, 0, 0]]);
// for all other objects in the selection: reset their difference to the hold point
for (var i = 0; i < this.elements.length; i++) {
// get current position of object
var elem_x = this.elements[i][1][0].center.x;
var elem_y = this.elements[i][1][0].center.y;
// set distance to hold point
this.elements[i][1][1] = elem_x - this.hold_point.x;
this.elements[i][1][2] = elem_y - this.hold_point.y;
}
}
/**
* check if the selection is a real selection or just 1 object
* @returns wether the selection contains more then 1 object
*/
valid() {
return this.elements.length > 1;
}
}