q5 1.9.21 → 1.9.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -26
- package/package.json +20 -10
- package/q5-server.js +3 -2
- package/q5.js +89 -12
- package/q5.min.js +1 -1
- package/src/q5-2d-canvas.js +257 -0
- package/src/q5-2d-drawing.js +508 -0
- package/src/q5-2d-image.js +572 -0
- package/src/q5-2d-text.js +216 -0
- package/src/q5-ai.js +65 -0
- package/src/q5-color.js +202 -0
- package/src/q5-core.js +273 -0
- package/src/q5-display.js +85 -0
- package/src/q5-dom.js +6 -0
- package/src/q5-input.js +157 -0
- package/src/q5-math.js +505 -0
- package/src/q5-sensors.js +96 -0
- package/src/q5-sound.js +19 -0
- package/src/q5-util.js +33 -0
- package/src/q5-vector.js +225 -0
- package/index.html +0 -12
package/src/q5-vector.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
Q5.modules.vector = ($) => {
|
|
2
|
+
$.createVector = (x, y, z) => new Q5.Vector(x, y, z, $);
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
Q5.Vector = class {
|
|
6
|
+
constructor(_x, _y, _z, _$) {
|
|
7
|
+
this.x = _x || 0;
|
|
8
|
+
this.y = _y || 0;
|
|
9
|
+
this.z = _z || 0;
|
|
10
|
+
this._$ = _$ || window;
|
|
11
|
+
this._cn = null;
|
|
12
|
+
this._cnsq = null;
|
|
13
|
+
}
|
|
14
|
+
set(_x, _y, _z) {
|
|
15
|
+
this.x = _x || 0;
|
|
16
|
+
this.y = _y || 0;
|
|
17
|
+
this.z = _z || 0;
|
|
18
|
+
}
|
|
19
|
+
copy() {
|
|
20
|
+
return new Q5.Vector(this.x, this.y, this.z);
|
|
21
|
+
}
|
|
22
|
+
_arg2v(x, y, z) {
|
|
23
|
+
if (x.x !== undefined) return x;
|
|
24
|
+
if (y !== undefined) {
|
|
25
|
+
return { x, y, z: z || 0 };
|
|
26
|
+
}
|
|
27
|
+
return { x: x, y: x, z: x };
|
|
28
|
+
}
|
|
29
|
+
_calcNorm() {
|
|
30
|
+
this._cnsq = this.x * this.x + this.y * this.y + this.z * this.z;
|
|
31
|
+
this._cn = Math.sqrt(this._cnsq);
|
|
32
|
+
}
|
|
33
|
+
add() {
|
|
34
|
+
let u = this._arg2v(...arguments);
|
|
35
|
+
this.x += u.x;
|
|
36
|
+
this.y += u.y;
|
|
37
|
+
this.z += u.z;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
rem() {
|
|
41
|
+
let u = this._arg2v(...arguments);
|
|
42
|
+
this.x %= u.x;
|
|
43
|
+
this.y %= u.y;
|
|
44
|
+
this.z %= u.z;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
sub() {
|
|
48
|
+
let u = this._arg2v(...arguments);
|
|
49
|
+
this.x -= u.x;
|
|
50
|
+
this.y -= u.y;
|
|
51
|
+
this.z -= u.z;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
mult() {
|
|
55
|
+
let u = this._arg2v(...arguments);
|
|
56
|
+
this.x *= u.x;
|
|
57
|
+
this.y *= u.y;
|
|
58
|
+
this.z *= u.z;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
div() {
|
|
62
|
+
let u = this._arg2v(...arguments);
|
|
63
|
+
if (u.x) this.x /= u.x;
|
|
64
|
+
else this.x = 0;
|
|
65
|
+
if (u.y) this.y /= u.y;
|
|
66
|
+
else this.y = 0;
|
|
67
|
+
if (u.z) this.z /= u.z;
|
|
68
|
+
else this.z = 0;
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
mag() {
|
|
72
|
+
this._calcNorm();
|
|
73
|
+
return this._cn;
|
|
74
|
+
}
|
|
75
|
+
magSq() {
|
|
76
|
+
this._calcNorm();
|
|
77
|
+
return this._cnsq;
|
|
78
|
+
}
|
|
79
|
+
dot() {
|
|
80
|
+
let u = this._arg2v(...arguments);
|
|
81
|
+
return this.x * u.x + this.y * u.y + this.z * u.z;
|
|
82
|
+
}
|
|
83
|
+
dist() {
|
|
84
|
+
let u = this._arg2v(...arguments);
|
|
85
|
+
let x = this.x - u.x;
|
|
86
|
+
let y = this.y - u.y;
|
|
87
|
+
let z = this.z - u.z;
|
|
88
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
89
|
+
}
|
|
90
|
+
cross() {
|
|
91
|
+
let u = this._arg2v(...arguments);
|
|
92
|
+
let x = this.y * u.z - this.z * u.y;
|
|
93
|
+
let y = this.z * u.x - this.x * u.z;
|
|
94
|
+
let z = this.x * u.y - this.y * u.x;
|
|
95
|
+
this.x = x;
|
|
96
|
+
this.y = y;
|
|
97
|
+
this.z = z;
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
normalize() {
|
|
101
|
+
this._calcNorm();
|
|
102
|
+
let n = this._cn;
|
|
103
|
+
if (n != 0) {
|
|
104
|
+
this.x /= n;
|
|
105
|
+
this.y /= n;
|
|
106
|
+
this.z /= n;
|
|
107
|
+
}
|
|
108
|
+
this._cn = 1;
|
|
109
|
+
this._cnsq = 1;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
limit(m) {
|
|
113
|
+
this._calcNorm();
|
|
114
|
+
let n = this._cn;
|
|
115
|
+
if (n > m) {
|
|
116
|
+
let t = m / n;
|
|
117
|
+
this.x *= t;
|
|
118
|
+
this.y *= t;
|
|
119
|
+
this.z *= t;
|
|
120
|
+
this._cn = m;
|
|
121
|
+
this._cnsq = m * m;
|
|
122
|
+
}
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
setMag(m) {
|
|
126
|
+
this._calcNorm();
|
|
127
|
+
let n = this._cn;
|
|
128
|
+
let t = m / n;
|
|
129
|
+
this.x *= t;
|
|
130
|
+
this.y *= t;
|
|
131
|
+
this.z *= t;
|
|
132
|
+
this._cn = m;
|
|
133
|
+
this._cnsq = m * m;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
heading() {
|
|
137
|
+
return this._$.atan2(this.y, this.x);
|
|
138
|
+
}
|
|
139
|
+
rotate(ang) {
|
|
140
|
+
let costh = this._$.cos(ang);
|
|
141
|
+
let sinth = this._$.sin(ang);
|
|
142
|
+
let vx = this.x * costh - this.y * sinth;
|
|
143
|
+
let vy = this.x * sinth + this.y * costh;
|
|
144
|
+
this.x = vx;
|
|
145
|
+
this.y = vy;
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
angleBetween() {
|
|
149
|
+
let u = this._arg2v(...arguments);
|
|
150
|
+
let o = Q5.Vector.cross(this, u);
|
|
151
|
+
let ang = this._$.atan2(o.mag(), this.dot(u));
|
|
152
|
+
return ang * Math.sign(o.z || 1);
|
|
153
|
+
}
|
|
154
|
+
lerp() {
|
|
155
|
+
let args = [...arguments];
|
|
156
|
+
let u = this._arg2v(...args.slice(0, -1));
|
|
157
|
+
let amt = args[args.length - 1];
|
|
158
|
+
this.x += (u.x - this.x) * amt;
|
|
159
|
+
this.y += (u.y - this.y) * amt;
|
|
160
|
+
this.z += (u.z - this.z) * amt;
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
reflect(n) {
|
|
164
|
+
n.normalize();
|
|
165
|
+
return this.sub(n.mult(2 * this.dot(n)));
|
|
166
|
+
}
|
|
167
|
+
array() {
|
|
168
|
+
return [this.x, this.y, this.z];
|
|
169
|
+
}
|
|
170
|
+
equals(u, epsilon) {
|
|
171
|
+
epsilon ??= Number.EPSILON || 0;
|
|
172
|
+
return Math.abs(u.x - this.x) < epsilon && Math.abs(u.y - this.y) < epsilon && Math.abs(u.z - this.z) < epsilon;
|
|
173
|
+
}
|
|
174
|
+
fromAngle(th, l) {
|
|
175
|
+
if (l === undefined) l = 1;
|
|
176
|
+
this._cn = l;
|
|
177
|
+
this._cnsq = l * l;
|
|
178
|
+
this.x = l * this._$.cos(th);
|
|
179
|
+
this.y = l * this._$.sin(th);
|
|
180
|
+
this.z = 0;
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
fromAngles(th, ph, l) {
|
|
184
|
+
if (l === undefined) l = 1;
|
|
185
|
+
this._cn = l;
|
|
186
|
+
this._cnsq = l * l;
|
|
187
|
+
const cosph = this._$.cos(ph);
|
|
188
|
+
const sinph = this._$.sin(ph);
|
|
189
|
+
const costh = this._$.cos(th);
|
|
190
|
+
const sinth = this._$.sin(th);
|
|
191
|
+
this.x = l * sinth * sinph;
|
|
192
|
+
this.y = -l * costh;
|
|
193
|
+
this.z = l * sinth * cosph;
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
196
|
+
random2D() {
|
|
197
|
+
this._cn = this._cnsq = 1;
|
|
198
|
+
return this.fromAngle(Math.random() * Math.PI * 2);
|
|
199
|
+
}
|
|
200
|
+
random3D() {
|
|
201
|
+
this._cn = this._cnsq = 1;
|
|
202
|
+
return this.fromAngles(Math.random() * Math.PI * 2, Math.random() * Math.PI * 2);
|
|
203
|
+
}
|
|
204
|
+
toString() {
|
|
205
|
+
return `[${this.x}, ${this.y}, ${this.z}]`;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
Q5.Vector.add = (v, u) => v.copy().add(u);
|
|
209
|
+
Q5.Vector.cross = (v, u) => v.copy().cross(u);
|
|
210
|
+
Q5.Vector.dist = (v, u) => Math.hypot(v.x - u.x, v.y - u.y, v.z - u.z);
|
|
211
|
+
Q5.Vector.div = (v, u) => v.copy().div(u);
|
|
212
|
+
Q5.Vector.dot = (v, u) => v.copy().dot(u);
|
|
213
|
+
Q5.Vector.equals = (v, u, epsilon) => v.equals(u, epsilon);
|
|
214
|
+
Q5.Vector.lerp = (v, u, amt) => v.copy().lerp(u, amt);
|
|
215
|
+
Q5.Vector.limit = (v, m) => v.copy().limit(m);
|
|
216
|
+
Q5.Vector.heading = (v) => this._$.atan2(v.y, v.x);
|
|
217
|
+
Q5.Vector.magSq = (v) => v.x * v.x + v.y * v.y + v.z * v.z;
|
|
218
|
+
Q5.Vector.mag = (v) => Math.sqrt(Q5.Vector.magSq(v));
|
|
219
|
+
Q5.Vector.mult = (v, u) => v.copy().mult(u);
|
|
220
|
+
Q5.Vector.normalize = (v) => v.copy().normalize();
|
|
221
|
+
Q5.Vector.rem = (v, u) => v.copy().rem(u);
|
|
222
|
+
Q5.Vector.sub = (v, u) => v.copy().sub(u);
|
|
223
|
+
for (let k of ['fromAngle', 'fromAngles', 'random2D', 'random3D']) {
|
|
224
|
+
Q5.Vector[k] = (u, v, t) => new Q5.Vector()[k](u, v, t);
|
|
225
|
+
}
|