@litecanvas/utils 0.12.0 → 0.13.0
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/dist/actor.js +16 -9
- package/dist/actor.min.js +1 -1
- package/dist/all.js +143 -90
- package/dist/all.min.js +2 -2
- package/dist/math.js +1 -1
- package/dist/vector.js +140 -87
- package/dist/vector.min.js +1 -1
- package/package.json +1 -1
- package/src/actor/index.js +9 -9
- package/src/math/README.md +15 -0
- package/src/math/range.js +4 -4
- package/src/vector/README.md +62 -75
- package/src/vector/index.js +196 -73
package/dist/vector.js
CHANGED
|
@@ -15,29 +15,39 @@
|
|
|
15
15
|
UP: () => UP,
|
|
16
16
|
Vector: () => Vector,
|
|
17
17
|
ZERO: () => ZERO,
|
|
18
|
-
isvector: () => isvector,
|
|
19
18
|
vec: () => vec,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
19
|
+
vecAbs: () => vecAbs,
|
|
20
|
+
vecAdd: () => vecAdd,
|
|
21
|
+
vecAngle: () => vecAngle,
|
|
22
|
+
vecCeil: () => vecCeil,
|
|
23
|
+
vecClamp: () => vecClamp,
|
|
24
|
+
vecCross: () => vecCross,
|
|
25
|
+
vecDist: () => vecDist,
|
|
26
|
+
vecDist2: () => vecDist2,
|
|
27
|
+
vecDiv: () => vecDiv,
|
|
28
|
+
vecDot: () => vecDot,
|
|
29
|
+
vecEq: () => vecEq,
|
|
30
|
+
vecFloor: () => vecFloor,
|
|
31
|
+
vecIsZero: () => vecIsZero,
|
|
32
|
+
vecLerp: () => vecLerp,
|
|
33
|
+
vecLimit: () => vecLimit,
|
|
34
|
+
vecMag: () => vecMag,
|
|
35
|
+
vecMag2: () => vecMag2,
|
|
36
|
+
vecMove: () => vecMove,
|
|
37
|
+
vecMult: () => vecMult,
|
|
38
|
+
vecNorm: () => vecNorm,
|
|
39
|
+
vecRand: () => vecRand,
|
|
40
|
+
vecReflect: () => vecReflect,
|
|
41
|
+
vecRotate: () => vecRotate,
|
|
42
|
+
vecRound: () => vecRound,
|
|
43
|
+
vecSet: () => vecSet,
|
|
44
|
+
vecSetMag: () => vecSetMag,
|
|
45
|
+
vecSub: () => vecSub
|
|
40
46
|
});
|
|
47
|
+
var sqrt = Math.sqrt;
|
|
48
|
+
var cos = Math.cos;
|
|
49
|
+
var sin = Math.sin;
|
|
50
|
+
var PI2 = 2 * Math.PI;
|
|
41
51
|
var Vector = class {
|
|
42
52
|
/** @type {number} */
|
|
43
53
|
x;
|
|
@@ -58,102 +68,145 @@
|
|
|
58
68
|
return `Vector (${this.x}, ${this.y})`;
|
|
59
69
|
}
|
|
60
70
|
};
|
|
61
|
-
var
|
|
62
|
-
var
|
|
63
|
-
if (
|
|
64
|
-
|
|
71
|
+
var isVector = (v) => v instanceof Vector;
|
|
72
|
+
var vec = (x = 0, y = x) => {
|
|
73
|
+
if (isVector(x)) {
|
|
74
|
+
y = x.y;
|
|
75
|
+
x = x.x;
|
|
76
|
+
}
|
|
77
|
+
return new Vector(x, y);
|
|
78
|
+
};
|
|
79
|
+
var vecEq = (v, x, y = x) => {
|
|
80
|
+
if (isVector(x)) {
|
|
81
|
+
return vecEq(v, x.x, x.y);
|
|
65
82
|
}
|
|
66
83
|
return v.x === x && v.y === y;
|
|
67
84
|
};
|
|
68
|
-
var
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
vecset(v, x.x, x.y);
|
|
85
|
+
var vecSet = (v, x, y = x) => {
|
|
86
|
+
if (isVector(x)) {
|
|
87
|
+
vecSet(v, x.x, x.y);
|
|
72
88
|
} else {
|
|
73
89
|
v.x = x;
|
|
74
90
|
v.y = y;
|
|
75
91
|
}
|
|
92
|
+
return v;
|
|
76
93
|
};
|
|
77
|
-
var
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
} else {
|
|
81
|
-
v.x += x;
|
|
82
|
-
v.y += y;
|
|
94
|
+
var vecAdd = (v, x, y = x) => {
|
|
95
|
+
if (isVector(x)) {
|
|
96
|
+
return vecAdd(v, x.x, x.y);
|
|
83
97
|
}
|
|
98
|
+
v.x += x;
|
|
99
|
+
v.y += y;
|
|
100
|
+
return v;
|
|
84
101
|
};
|
|
85
|
-
var
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
} else {
|
|
89
|
-
v.x -= x;
|
|
90
|
-
v.y -= y;
|
|
102
|
+
var vecSub = (v, x, y = x) => {
|
|
103
|
+
if (isVector(x)) {
|
|
104
|
+
return vecSub(v, x.x, x.y);
|
|
91
105
|
}
|
|
106
|
+
v.x -= x;
|
|
107
|
+
v.y -= y;
|
|
108
|
+
return v;
|
|
92
109
|
};
|
|
93
|
-
var
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
} else {
|
|
97
|
-
v.x *= x;
|
|
98
|
-
v.y *= y;
|
|
110
|
+
var vecMult = (v, x, y = x) => {
|
|
111
|
+
if (isVector(x)) {
|
|
112
|
+
return vecMult(v, x.x, x.y);
|
|
99
113
|
}
|
|
114
|
+
v.x *= x;
|
|
115
|
+
v.y *= y;
|
|
116
|
+
return v;
|
|
100
117
|
};
|
|
101
|
-
var
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
} else {
|
|
105
|
-
v.x /= x;
|
|
106
|
-
v.y /= y;
|
|
118
|
+
var vecDiv = (v, x, y = x) => {
|
|
119
|
+
if (isVector(x)) {
|
|
120
|
+
return vecDiv(v, x.x, x.y);
|
|
107
121
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
v.x /= x;
|
|
123
|
+
v.y /= y;
|
|
124
|
+
return v;
|
|
125
|
+
};
|
|
126
|
+
var vecRotate = (v, radians) => {
|
|
127
|
+
const c = cos(radians), s = sin(radians);
|
|
128
|
+
v.x = c * v.x - s * v.y;
|
|
129
|
+
v.y = s * v.x + c * v.y;
|
|
130
|
+
return v;
|
|
131
|
+
};
|
|
132
|
+
var vecReflect = (v, normal) => {
|
|
133
|
+
const normalCopy = vecNorm(vec(normal));
|
|
134
|
+
return vecSub(v, vecMult(normalCopy, 2 * vecDot(v, normalCopy)));
|
|
135
|
+
};
|
|
136
|
+
var vecSetMag = (v, value) => {
|
|
137
|
+
vecNorm(v);
|
|
138
|
+
vecMult(v, value);
|
|
139
|
+
return v;
|
|
140
|
+
};
|
|
141
|
+
var vecMag = (v) => sqrt(v.x * v.x + v.y * v.y);
|
|
142
|
+
var vecMag2 = (v) => v.x * v.x + v.y * v.y;
|
|
143
|
+
var vecNorm = (v) => {
|
|
144
|
+
const length = vecMag(v);
|
|
118
145
|
if (length > 0) {
|
|
119
|
-
|
|
146
|
+
vecDiv(v, length);
|
|
120
147
|
}
|
|
148
|
+
return v;
|
|
121
149
|
};
|
|
122
|
-
var
|
|
123
|
-
const sq =
|
|
150
|
+
var vecLimit = (v, max = 1) => {
|
|
151
|
+
const sq = vecMag2(v);
|
|
124
152
|
if (sq > max * max) {
|
|
125
|
-
|
|
126
|
-
|
|
153
|
+
vecDiv(v, sqrt(sq));
|
|
154
|
+
vecMult(v, max);
|
|
127
155
|
}
|
|
156
|
+
return v;
|
|
128
157
|
};
|
|
129
|
-
var
|
|
158
|
+
var vecDist = (a, b) => {
|
|
130
159
|
const dx = a.x - b.x;
|
|
131
160
|
const dy = a.y - b.y;
|
|
132
|
-
return
|
|
161
|
+
return sqrt(dx * dx + dy * dy);
|
|
133
162
|
};
|
|
134
|
-
var
|
|
163
|
+
var vecDist2 = (a, b) => {
|
|
135
164
|
const dx = a.x - b.x;
|
|
136
165
|
const dy = a.y - b.y;
|
|
137
166
|
return dx * dx + dy * dy;
|
|
138
167
|
};
|
|
139
|
-
var
|
|
140
|
-
var
|
|
141
|
-
var
|
|
142
|
-
var
|
|
168
|
+
var vecAngle = (v) => Math.atan2(v.y, v.x);
|
|
169
|
+
var vecDot = (a, b) => a.x * b.x + a.y * b.y;
|
|
170
|
+
var vecCross = (a, b) => a.x * b.y - a.y * b.x;
|
|
171
|
+
var vecLerp = (a, b, t) => {
|
|
143
172
|
a.x += (b.x - a.x) * t || 0;
|
|
144
173
|
a.y += (b.y - a.y) * t || 0;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
var
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
};
|
|
174
|
+
return a;
|
|
175
|
+
};
|
|
176
|
+
var vecRand = (minlength = 1, maxlength = minlength, randomFn = globalThis.rand || Math.random) => {
|
|
177
|
+
const angle = randomFn() * PI2;
|
|
178
|
+
const radius = randomFn() * (maxlength - minlength) + minlength;
|
|
179
|
+
return vec(cos(angle) * radius, sin(angle) * radius);
|
|
180
|
+
};
|
|
181
|
+
var vecAbs = (v) => {
|
|
182
|
+
v.x = Math.abs(v.x);
|
|
183
|
+
v.y = Math.abs(v.y);
|
|
184
|
+
return v;
|
|
185
|
+
};
|
|
186
|
+
var vecCeil = (v) => {
|
|
187
|
+
v.x = Math.ceil(v.x);
|
|
188
|
+
v.y = Math.ceil(v.y);
|
|
189
|
+
return v;
|
|
190
|
+
};
|
|
191
|
+
var vecFloor = (v) => {
|
|
192
|
+
v.x = Math.floor(v.x);
|
|
193
|
+
v.y = Math.floor(v.y);
|
|
194
|
+
return v;
|
|
195
|
+
};
|
|
196
|
+
var vecRound = (v) => {
|
|
197
|
+
v.x = Math.round(v.x);
|
|
198
|
+
v.y = Math.round(v.y);
|
|
199
|
+
return v;
|
|
200
|
+
};
|
|
201
|
+
var vecClamp = (v, min, max) => {
|
|
202
|
+
if (v.x < min.x) v.x = min.x;
|
|
203
|
+
if (v.x > max.x) v.x = max.x;
|
|
204
|
+
if (v.y < min.y) v.y = min.y;
|
|
205
|
+
if (v.y > max.y) v.y = max.y;
|
|
206
|
+
return v;
|
|
207
|
+
};
|
|
208
|
+
var vecMove = (v, to, delta = 1) => vecAdd(v, to.x * delta, to.y * delta);
|
|
209
|
+
var vecIsZero = (v) => vecEq(v, ZERO);
|
|
157
210
|
var ZERO = /* @__PURE__ */ vec(0, 0);
|
|
158
211
|
var ONE = /* @__PURE__ */ vec(1, 1);
|
|
159
212
|
var UP = /* @__PURE__ */ vec(0, -1);
|
package/dist/vector.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var
|
|
1
|
+
(()=>{var C=Object.defineProperty;var E=(t,o)=>{for(var r in o)C(t,r,{get:o[r],enumerable:!0})};var a={};E(a,{DOWN:()=>J,LEFT:()=>K,ONE:()=>k,RIGHT:()=>B,UP:()=>z,Vector:()=>s,ZERO:()=>q,vec:()=>c,vecAbs:()=>j,vecAdd:()=>i,vecAngle:()=>P,vecCeil:()=>w,vecClamp:()=>H,vecCross:()=>U,vecDist:()=>L,vecDist2:()=>N,vecDiv:()=>y,vecDot:()=>T,vecEq:()=>u,vecFloor:()=>F,vecIsZero:()=>W,vecLerp:()=>Z,vecLimit:()=>A,vecMag:()=>R,vecMag2:()=>D,vecMove:()=>V,vecMult:()=>x,vecNorm:()=>h,vecRand:()=>$,vecReflect:()=>S,vecRotate:()=>O,vecRound:()=>G,vecSet:()=>g,vecSetMag:()=>b,vecSub:()=>l});var p=Math.sqrt,f=Math.cos,d=Math.sin,I=2*Math.PI,s=class{x;y;constructor(o=0,r=o){this.x=o,this.y=r}toString(){return`Vector (${this.x}, ${this.y})`}},n=t=>t instanceof s,c=(t=0,o=t)=>(n(t)&&(o=t.y,t=t.x),new s(t,o)),u=(t,o,r=o)=>n(o)?u(t,o.x,o.y):t.x===o&&t.y===r,g=(t,o,r=o)=>(n(o)?g(t,o.x,o.y):(t.x=o,t.y=r),t),i=(t,o,r=o)=>n(o)?i(t,o.x,o.y):(t.x+=o,t.y+=r,t),l=(t,o,r=o)=>n(o)?l(t,o.x,o.y):(t.x-=o,t.y-=r,t),x=(t,o,r=o)=>n(o)?x(t,o.x,o.y):(t.x*=o,t.y*=r,t),y=(t,o,r=o)=>n(o)?y(t,o.x,o.y):(t.x/=o,t.y/=r,t),O=(t,o)=>{let r=f(o),e=d(o);return t.x=r*t.x-e*t.y,t.y=e*t.x+r*t.y,t},S=(t,o)=>{let r=h(c(o));return l(t,x(r,2*T(t,r)))},b=(t,o)=>(h(t),x(t,o),t),R=t=>p(t.x*t.x+t.y*t.y),D=t=>t.x*t.x+t.y*t.y,h=t=>{let o=R(t);return o>0&&y(t,o),t},A=(t,o=1)=>{let r=D(t);return r>o*o&&(y(t,p(r)),x(t,o)),t},L=(t,o)=>{let r=t.x-o.x,e=t.y-o.y;return p(r*r+e*e)},N=(t,o)=>{let r=t.x-o.x,e=t.y-o.y;return r*r+e*e},P=t=>Math.atan2(t.y,t.x),T=(t,o)=>t.x*o.x+t.y*o.y,U=(t,o)=>t.x*o.y-t.y*o.x,Z=(t,o,r)=>(t.x+=(o.x-t.x)*r||0,t.y+=(o.y-t.y)*r||0,t),$=(t=1,o=t,r=globalThis.rand||Math.random)=>{let e=r()*I,M=r()*(o-t)+t;return c(f(e)*M,d(e)*M)},j=t=>(t.x=Math.abs(t.x),t.y=Math.abs(t.y),t),w=t=>(t.x=Math.ceil(t.x),t.y=Math.ceil(t.y),t),F=t=>(t.x=Math.floor(t.x),t.y=Math.floor(t.y),t),G=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t),H=(t,o,r)=>(t.x<o.x&&(t.x=o.x),t.x>r.x&&(t.x=r.x),t.y<o.y&&(t.y=o.y),t.y>r.y&&(t.y=r.y),t),V=(t,o,r=1)=>i(t,o.x*r,o.y*r),W=t=>u(t,q),q=c(0,0),k=c(1,1),z=c(0,-1),B=c(1,0),J=c(0,1),K=c(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},a);})();
|
package/package.json
CHANGED
package/src/actor/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Vector, vec
|
|
1
|
+
import { Vector, vec } from "../vector/index.js"
|
|
2
2
|
import "litecanvas"
|
|
3
3
|
|
|
4
4
|
export const ANCHOR_CENTER = vec(0.5, 0.5)
|
|
@@ -37,7 +37,7 @@ export class Actor {
|
|
|
37
37
|
constructor(sprite, position, anchor = ANCHOR_TOP_LEFT) {
|
|
38
38
|
this.sprite = sprite
|
|
39
39
|
this.pos = position || vec(0)
|
|
40
|
-
this._o =
|
|
40
|
+
this._o = vec(anchor) // clone the anchor vector
|
|
41
41
|
this._s = vec(1, 1)
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -98,6 +98,13 @@ export class Actor {
|
|
|
98
98
|
return this.sprite.height * this._s.y
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* @retuns {Vector}
|
|
103
|
+
*/
|
|
104
|
+
get scale() {
|
|
105
|
+
return this._s
|
|
106
|
+
}
|
|
107
|
+
|
|
101
108
|
/**
|
|
102
109
|
* @returns {number[]}
|
|
103
110
|
*/
|
|
@@ -109,13 +116,6 @@ export class Actor {
|
|
|
109
116
|
return [x, y, w, h]
|
|
110
117
|
}
|
|
111
118
|
|
|
112
|
-
/**
|
|
113
|
-
* @retuns {Vector}
|
|
114
|
-
*/
|
|
115
|
-
get scale() {
|
|
116
|
-
return this._s
|
|
117
|
-
}
|
|
118
|
-
|
|
119
119
|
/**
|
|
120
120
|
* Update the transformation matrix, sets the opacity and draw the actor sprite image.
|
|
121
121
|
*
|
package/src/math/README.md
CHANGED
|
@@ -61,3 +61,18 @@ function draw() {
|
|
|
61
61
|
circfill(CENTERX, CENTERY + y, 50, 4)
|
|
62
62
|
}
|
|
63
63
|
```
|
|
64
|
+
|
|
65
|
+
## range
|
|
66
|
+
|
|
67
|
+
Returns a sequence of numbers from `0` to `size - 1`.
|
|
68
|
+
|
|
69
|
+
Syntax: `range(size: number): number[]`
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
import { range } from "@litecanvas/utils"
|
|
73
|
+
|
|
74
|
+
// prints 0 1 2 3 4
|
|
75
|
+
for (let i of range(5)) {
|
|
76
|
+
console.log(i)
|
|
77
|
+
}
|
|
78
|
+
```
|
package/src/math/range.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Returns a sequence of numbers from 0 to
|
|
2
|
+
* Returns a sequence of numbers from `0` to `size - 1`.
|
|
3
3
|
*
|
|
4
|
-
* @param {number}
|
|
4
|
+
* @param {number} size the amount of numbers
|
|
5
5
|
* @returns {number[]}
|
|
6
6
|
* @example
|
|
7
|
-
* // print 0 1 2 3 4
|
|
7
|
+
* // print 0 1 2 3 4
|
|
8
8
|
* for(let i of range(5)) console.log(i)
|
|
9
9
|
*/
|
|
10
|
-
export default (
|
|
10
|
+
export default (size) => Array.from(Array(size).keys())
|
package/src/vector/README.md
CHANGED
|
@@ -6,9 +6,9 @@ A class to describe a two-dimensional vector.
|
|
|
6
6
|
|
|
7
7
|
## vec
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Copy a vector or create a new one.
|
|
10
10
|
|
|
11
|
-
Syntax: `vec(x: number = 0, y: number = 0): Vector`
|
|
11
|
+
Syntax to create: `vec(x: number = 0, y: number = 0): Vector`
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
14
|
import { vec, Vector } from "@litecanvas/utils"
|
|
@@ -20,18 +20,14 @@ console.log(position.x) // outputs 50
|
|
|
20
20
|
console.log(position.y) // outputs 25
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Creates a copy/clone of a given vector.
|
|
26
|
-
|
|
27
|
-
Syntax: `veccopy(v: Vector): Vector`
|
|
23
|
+
Syntax to copy: `vec(other: Vector): Vector`
|
|
28
24
|
|
|
29
25
|
```js
|
|
30
|
-
import { vec
|
|
26
|
+
import { vec } from "@litecanvas/utils"
|
|
31
27
|
|
|
32
28
|
const a = vec(1, 1)
|
|
33
29
|
const b = a // `b`` is a references of `a`
|
|
34
|
-
const c =
|
|
30
|
+
const c = vec(a) // `c` is a copy of `a`
|
|
35
31
|
|
|
36
32
|
c.x = 99 // this changes only `c`
|
|
37
33
|
b.x = 0 // this changes `a` and `b`
|
|
@@ -41,145 +37,136 @@ console.log(b) // outputs "Vector (0, 1)
|
|
|
41
37
|
console.log(c) // outputs "Vector (99, 1)
|
|
42
38
|
```
|
|
43
39
|
|
|
44
|
-
##
|
|
40
|
+
## vecSet
|
|
45
41
|
|
|
46
|
-
Assigns new values to a vector.
|
|
42
|
+
Assigns new values to a vector and returns the vector (first argument).
|
|
47
43
|
|
|
48
|
-
Syntax: `
|
|
44
|
+
Syntax: `vecSet(v: Vector, x: number, y: number = x): Vector`
|
|
49
45
|
|
|
50
46
|
```js
|
|
51
|
-
import { vec,
|
|
47
|
+
import { vec, vecSet } from "@litecanvas/utils"
|
|
52
48
|
|
|
53
49
|
const a = vec(0, 0)
|
|
54
|
-
|
|
50
|
+
vecSet(a, 10, 20)
|
|
55
51
|
|
|
56
52
|
console.log(a.x) // outputs 10
|
|
57
53
|
console.log(a.y) // outputs 20
|
|
58
54
|
```
|
|
59
55
|
|
|
60
|
-
##
|
|
56
|
+
## vecAdd
|
|
61
57
|
|
|
62
|
-
Add values to a vector.
|
|
58
|
+
Add values to a vector and returns the vector (first argument).
|
|
63
59
|
|
|
64
|
-
Syntax: `
|
|
60
|
+
Syntax: `vecAdd(v: Vector, x: number, y: number = x): Vector`
|
|
65
61
|
|
|
66
62
|
```js
|
|
67
|
-
import { vec,
|
|
63
|
+
import { vec, vecAdd } from "@litecanvas/utils"
|
|
68
64
|
|
|
69
65
|
const a = vec(10, 10)
|
|
70
|
-
|
|
66
|
+
vecAdd(a, 50, 90)
|
|
71
67
|
|
|
72
|
-
console.log(a.x) // outputs
|
|
73
|
-
console.log(a.y) // outputs
|
|
68
|
+
console.log(a.x) // outputs 60
|
|
69
|
+
console.log(a.y) // outputs 100
|
|
74
70
|
```
|
|
75
71
|
|
|
76
|
-
##
|
|
72
|
+
## vecSub
|
|
77
73
|
|
|
78
|
-
Subtracts values from to a vector.
|
|
74
|
+
Subtracts values from to a vector and returns the vector (first argument).
|
|
79
75
|
|
|
80
|
-
Syntax: `
|
|
76
|
+
Syntax: `vecSub(v: Vector, x: number, y: number = x): Vector`
|
|
81
77
|
|
|
82
|
-
##
|
|
78
|
+
## vecMult
|
|
83
79
|
|
|
84
|
-
Multiplies (scale) a vector.
|
|
80
|
+
Multiplies (scale) a vector and returns the vector (first argument).
|
|
85
81
|
|
|
86
|
-
Syntax: `
|
|
82
|
+
Syntax: `vecMult(v: Vector, x: number, y: number = x): Vector`
|
|
87
83
|
|
|
88
|
-
##
|
|
84
|
+
## vecDiv
|
|
89
85
|
|
|
90
|
-
Divides a vector.
|
|
86
|
+
Divides a vector and returns the vector (first argument).
|
|
91
87
|
|
|
92
|
-
Syntax: `vecdiv(v: Vector, x: number, y: number = x):
|
|
88
|
+
Syntax: `vecdiv(v: Vector, x: number, y: number = x): Vector`
|
|
93
89
|
|
|
94
|
-
##
|
|
90
|
+
## vecRotate
|
|
95
91
|
|
|
96
|
-
Rotates a vector by an angle (in radians) without changing its magnitude.
|
|
92
|
+
Rotates a vector by an angle (in radians) without changing its magnitude and returns the vector (first argument).
|
|
97
93
|
|
|
98
|
-
Syntax: `
|
|
94
|
+
Syntax: `vecRotate(v: Vector, radians: number): Vector`
|
|
99
95
|
|
|
100
|
-
##
|
|
96
|
+
## vecMag
|
|
101
97
|
|
|
102
98
|
Returns the magnitude (length) of the vector.
|
|
103
99
|
|
|
104
|
-
Syntax: `
|
|
100
|
+
Syntax: `vecMag(v: Vector): number`
|
|
105
101
|
|
|
106
|
-
##
|
|
102
|
+
## vecMag2
|
|
107
103
|
|
|
108
104
|
Returns the magnitude (length) of the vector squared.
|
|
109
105
|
|
|
110
|
-
Syntax: `
|
|
106
|
+
Syntax: `vecMag2(v: Vector): number`
|
|
111
107
|
|
|
112
|
-
##
|
|
108
|
+
## vecNorm
|
|
113
109
|
|
|
114
|
-
Scales the values of a vector so that its magnitude is 1.
|
|
110
|
+
Scales the values of a vector so that its magnitude is 1 and returns the vector (first argument).
|
|
115
111
|
|
|
116
|
-
Syntax: `
|
|
112
|
+
Syntax: `vecNorm(v: Vector): Vector`
|
|
117
113
|
|
|
118
|
-
##
|
|
114
|
+
## vecLimit
|
|
119
115
|
|
|
120
|
-
Limits (clamp) a vector's magnitude to a maximum value.
|
|
116
|
+
Limits (clamp) a vector's magnitude to a maximum value and returns the vector (first argument).
|
|
121
117
|
|
|
122
|
-
Syntax: `
|
|
118
|
+
Syntax: `vecLimit(v: Vector, max: number): Vector`
|
|
123
119
|
|
|
124
|
-
##
|
|
120
|
+
## vecDist
|
|
125
121
|
|
|
126
122
|
Returns the distance between two points represented by vectors.
|
|
127
123
|
|
|
128
|
-
Syntax: `
|
|
124
|
+
Syntax: `vecDist(a: Vector, b: Vector): number`
|
|
129
125
|
|
|
130
|
-
##
|
|
126
|
+
## vecDist2
|
|
131
127
|
|
|
132
128
|
Returns the distance between two points represented by vectors squared.
|
|
133
129
|
|
|
134
|
-
Syntax: `
|
|
130
|
+
Syntax: `vecDist2(a: Vector, b: Vector): number`
|
|
135
131
|
|
|
136
|
-
##
|
|
132
|
+
## vecAngle
|
|
137
133
|
|
|
138
134
|
Calculates the angle a vector makes with the positive x-axis.
|
|
139
135
|
|
|
140
|
-
Syntax: `
|
|
136
|
+
Syntax: `vecAngle(v: Vector): number`
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const a = vecAngle(vec(1, 0)) // outputs 0 (zero)
|
|
140
|
+
const b = vecAngle(vec(0, 1)) // outputs ~1.571 (same as Math.PI / 2)
|
|
141
|
+
```
|
|
141
142
|
|
|
142
|
-
##
|
|
143
|
+
## vecDot
|
|
143
144
|
|
|
144
145
|
Calculates the dot product of two vectors.
|
|
145
146
|
|
|
146
|
-
Syntax: `
|
|
147
|
+
Syntax: `vecDot(a: Vector, b: Vector): number`
|
|
147
148
|
|
|
148
|
-
##
|
|
149
|
+
## vecCross
|
|
149
150
|
|
|
150
151
|
Calculates the dot product of two vectors.
|
|
151
152
|
|
|
152
|
-
Syntax: `
|
|
153
|
+
Syntax: `vecCross(a: Vector, b: Vector): number`
|
|
153
154
|
|
|
154
|
-
##
|
|
155
|
+
## vecLerp
|
|
156
|
+
|
|
157
|
+
Calculates new vector values that are proportionally the same distance
|
|
158
|
+
between two vectors and returns the first vector (first argument).
|
|
155
159
|
|
|
156
|
-
Calculates new vector values that are proportionally the same distance between two vectors.
|
|
157
160
|
The `atm` parameter is the amount to interpolate between the old vector and the new vector:
|
|
158
161
|
`0.0` keeps all values equal to the old vector's, `0.5` is halfway between, and `1.0` sets all
|
|
159
162
|
values equal to the new vector's.
|
|
160
163
|
|
|
161
|
-
Syntax: `
|
|
164
|
+
Syntax: `vecLerp(a: Vector, b: Vector, atm: number): Lerp`
|
|
162
165
|
|
|
163
|
-
##
|
|
166
|
+
## vecRand
|
|
164
167
|
|
|
165
168
|
Creates a vector with random direction and (optional) length.
|
|
166
169
|
|
|
167
|
-
|
|
168
|
-
You can set `vecconfig.random` to set your own "random" function.
|
|
169
|
-
|
|
170
|
-
Syntax: `vecrand(minlength: number = 1, maxlength: number = minlength): Vector`
|
|
170
|
+
By default the `randomFn` (third argument) is the `globalThis.rand` (from litecanvas globally instantiated) or the native `Math.random`.
|
|
171
171
|
|
|
172
|
-
|
|
173
|
-
import litecanvas from "litecanvas"
|
|
174
|
-
import { vecrand, vecconfig } from "@litecanvas/utils"
|
|
175
|
-
|
|
176
|
-
const engine = litecanvas({
|
|
177
|
-
loop: { init },
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
function init() {
|
|
181
|
-
// make sure to use the RNG of this litecanvas instance
|
|
182
|
-
vecconfig.random = engine.rand
|
|
183
|
-
v = vecrand()
|
|
184
|
-
}
|
|
185
|
-
```
|
|
172
|
+
Syntax: `vecRand(minlength: number = 1, maxlength: number = minlength, randomFn?): Vector`
|