div.js
3.36 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
/**
* 2D Vector.
* Container for x and y coordinates.
*/
class Vector {
constructor(x,y) {
this.x = x;
this.y = y;
}
}
class Text {
/**
* Both arguments should be real coordinates and not a percentage based on parent element
* @param {Vector} position
* @param {Vector?} size
*/
constructor(text, position) {
this.text = text;
this.position = position;
}
/**
* Compiles the true position coordinates based on percentage relative to parent element
* Assumes the elements grow to the right and down
* @param {Vector} position
*/
getDrawCoordinates(position) {
return new Vector(
(this.size.x * position.x / 100) + this.position.x,
(this.size.y * position.y / 100) + this.position.y,
);
}
}
/**
* This class does NOT regulate if its children go out of its cofinements.
* It does NOT resize or realocate its children to fit, in any way.
* Its only purpose is to provide percentage based relative coordinates.
*/
class Div {
/**
* Both arguments should be real coordinates and not a percentage based on parent element
* @param {Vector} position
* @param {Vector} size
*/
constructor(position, size) {
this.position = position;
this.size = size;
this.children = {};
}
addChild(id, position, size) {
if (position.x < 0 || position.x > 100)
console.error("position.x has to be an int between 0 and 100");
if (position.y < 0 || position.y > 100)
console.error("position.y has to be an int between 0 and 100");
if (size.x < 0 || size.x > 100)
console.error("size.x has to be an int between 0 and 100");
if (size.y < 0 || size.y > 100)
console.error("size.y has to be an int between 0 and 100");
this.children[id] = new Div (
this.getDrawCoordinates(position),
this.getSize(size)
);
return this.children[id];
}
addText(id, position) {
if (position.x < 0 || position.x > 100)
console.error("position.x has to be an int between 0 and 100");
if (position.y < 0 || position.y > 100)
console.error("position.y has to be an int between 0 and 100");
this.children[id] = new Text(position);
}
/**
* Compiles the true position coordinates based on percentage relative to parent element
* Assumes the elements grow to the right and down
* @param {Vector} position
*/
getDrawCoordinates(position) {
return new Vector (
(this.size.x * position.x / 100) + this.position.x,
(this.size.y * position.y / 100) + this.position.y,
);
}
/**
* Compiles the true size based on percentage relative to parent element
* Assumes the elements grow to the right and down
* @param {Vector?} size
*/
getSize(size) {
if (size === null) return null;
return new Vector (
this.size.x * size.x / 100,
this.size.y * size.y / 100,
);
}
/**
* Debug method to see the confines of the Div
* @param {jsPDF} pdf
*/
drawDivBox(pdf) {
/** Upper **/
pdf.line(
this.position.x,
this.position.y,
this.position.x + this.size.x,
this.position.y
);
/** Lower **/
pdf.line(
this.position.x,
this.position.y + this.size.y,
this.position.x + this.size.x,
this.position.y + this.size.y
);
/** Left **/
pdf.line(
this.position.x,
this.position.y,
this.position.x,
this.position.y + this.size.y
);
/** Right **/
pdf.line(
this.position.x + this.size.x,
this.position.y,
this.position.x + this.size.x,
this.position.y + this.size.y
);
}
}