-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec2.js
174 lines (140 loc) · 4.23 KB
/
vec2.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
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
class Vec2 {
constructor(x=0, y = null) {
if (x.hasOwnProperty("x")) {
y = x.y;
x = x.x;
}
else if (typeof x === "string" && x.indexOf(",") !== -1) {
const split = x.split(",");
x = split[0];
y = split[1];
}
this.x = parseFloat(x);
this.y = parseFloat(y === null ? x : y);
}
add(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(this.x + vec2.x, this.y + vec2.y);
}
sub(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(this.x - vec2.x, this.y - vec2.y);
}
mul(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(this.x * vec2.x, this.y * vec2.y);
}
div(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(this.x / vec2.x, this.y / vec2.y);
}
equals(x, y) {
const vec2 = new Vec2(x, y);
return this.x === vec2.x && this.y === vec2.y;
}
min(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(Math.min(this.x, vec2.x), Math.min(this.y, vec2.y));
}
max(x, y) {
const vec2 = new Vec2(x, y);
return new Vec2(Math.max(this.x, vec2.x), Math.max(this.y, vec2.y));
}
distance(x, y) {
const vec2 = new Vec2(x, y);
return this.sub(vec2).length();
}
round() {
return new Vec2(Math.round(this.x), Math.round(this.y));
}
ceil() {
return new Vec2(Math.ceil(this.x), Math.ceil(this.y));
}
floor() {
return new Vec2(Math.floor(this.x), Math.floor(this.y));
}
length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
toString() {
return this.x + "," + this.y;
}
}
let _Vec2GlobalStuff = {};
_Vec2GlobalStuff.previousTouchVec2 = null;
_Vec2GlobalStuff.pinchDistance = null;
// Touch isn't supported by Jest jsdom environment
// https://github.com/jsdom/jsdom/issues/2152
if (window.Touch !== undefined) {
Touch.prototype.vec2 = function () {
return new Vec2(
this.pageX,
this.pageY
);
}
}
Event.prototype.pinchAmount = function () {
if (this.touches && this.touches.length === 2) {
const a = this.touches.item(0).vec2();
const b = this.touches.item(1).vec2();
const distance = a.distance(b);
const oldDistance = _Vec2GlobalStuff.pinchDistance;
const amount = distance - oldDistance;
_Vec2GlobalStuff.pinchDistance = distance;
if (oldDistance !== null) {
return amount;
}
} else {
_Vec2GlobalStuff.pinchDistance = null;
}
return 0;
}
Event.prototype.vec2 = function () {
if (this.touches) {
if (this.touches.length === 0) {
return _Vec2GlobalStuff.previousTouchVec2;
}
let vec2 = new Vec2(0);
for (const touch of this.touches) {
vec2 = vec2.add(touch.vec2());
}
vec2 = vec2.div(this.touches.length);
_Vec2GlobalStuff.previousTouchVec2 = vec2;
return vec2;
}
return new Vec2(
this.pageX,
this.pageY
);
};
Element.prototype.getPos = function (topLeft = false) {
let pos = new Vec2(this.style.left || 0, this.style.top || 0);
if (!topLeft) pos = pos.add(this.getFullSize().div(2));
return pos;
}
Element.prototype.getGlobalPos = function (topLeft = false) {
const rect = this.getBoundingClientRect();
let pos = new Vec2(rect.left + this.clientLeft, rect.top + this.clientTop);
if (!topLeft) pos = pos.add(this.getFullSize().div(2));
return pos;
}
Element.prototype.setPos = function (x, y, topLeft = false) {
let vec2 = new Vec2(x, y);
if (!topLeft) {
vec2 = vec2.sub(this.getFullSize().div(2))
}
this.style.left = vec2.x + "px";
this.style.top = vec2.y + "px";
}
Element.prototype.getSize = function () {
return new Vec2(this.style.width || this.clientWidth, this.style.height || this.clientHeight);
}
Element.prototype.getFullSize = function () {
return new Vec2(this.offsetWidth, this.offsetHeight);
}
Element.prototype.setSize = function (x, y) {
let vec2 = new Vec2(x, y);
this.style.width = vec2.x + "px";
this.style.height = vec2.y + "px";
}
module.exports = Vec2;