@litecanvas/utils 0.12.1 → 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/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
- vecadd: () => vecadd,
21
- vecconfig: () => vecconfig,
22
- veccopy: () => veccopy,
23
- veccross: () => veccross,
24
- vecdir: () => vecdir,
25
- vecdist: () => vecdist,
26
- vecdist2: () => vecdist2,
27
- vecdiv: () => vecdiv,
28
- vecdot: () => vecdot,
29
- veceq: () => veceq,
30
- veclerp: () => veclerp,
31
- veclimit: () => veclimit,
32
- vecmag: () => vecmag,
33
- vecmag2: () => vecmag2,
34
- vecmult: () => vecmult,
35
- vecnorm: () => vecnorm,
36
- vecrand: () => vecrand,
37
- vecrot: () => vecrot,
38
- vecset: () => vecset,
39
- vecsub: () => vecsub
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 vec = (x = 0, y = x) => new Vector(x, y);
62
- var veceq = (v, x, y = x) => {
63
- if (isvector(x)) {
64
- return veceq(v, x.x, x.y);
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 veccopy = (v) => vec(v.x, v.y);
69
- var vecset = (v, x, y = x) => {
70
- if (isvector(x)) {
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 vecadd = (v, x, y = x) => {
78
- if (isvector(x)) {
79
- vecadd(v, x.x, x.y);
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 vecsub = (v, x, y = x) => {
86
- if (isvector(x)) {
87
- vecsub(v, x.x, x.y);
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 vecmult = (v, x, y = x) => {
94
- if (isvector(x)) {
95
- vecmult(v, x.x, x.y);
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 vecdiv = (v, x, y = x) => {
102
- if (isvector(x)) {
103
- vecdiv(v, x.x, x.y);
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
- var vecrot = (v, radians) => {
110
- const cos = Math.cos(radians), sin = Math.sin(radians);
111
- v.x = cos * v.x - sin * v.y;
112
- v.y = sin * v.x + cos * v.y;
113
- };
114
- var vecmag = (v) => Math.sqrt(v.x * v.x + v.y * v.y);
115
- var vecmag2 = (v) => v.x * v.x + v.y * v.y;
116
- var vecnorm = (v) => {
117
- const length = vecmag(v);
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
- vecdiv(v, length);
146
+ vecDiv(v, length);
120
147
  }
148
+ return v;
121
149
  };
122
- var veclimit = (v, max) => {
123
- const sq = vecmag2(v);
150
+ var vecLimit = (v, max = 1) => {
151
+ const sq = vecMag2(v);
124
152
  if (sq > max * max) {
125
- vecdiv(v, Math.sqrt(sq));
126
- vecmult(v, max);
153
+ vecDiv(v, sqrt(sq));
154
+ vecMult(v, max);
127
155
  }
156
+ return v;
128
157
  };
129
- var vecdist = (a, b) => {
158
+ var vecDist = (a, b) => {
130
159
  const dx = a.x - b.x;
131
160
  const dy = a.y - b.y;
132
- return Math.sqrt(dx * dx + dy * dy);
161
+ return sqrt(dx * dx + dy * dy);
133
162
  };
134
- var vecdist2 = (a, b) => {
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 vecdir = (v) => Math.atan2(v.y, v.x);
140
- var vecdot = (a, b) => a.x * b.x + a.y * b.y;
141
- var veccross = (a, b) => a.x * b.y - a.y * b.x;
142
- var veclerp = (a, b, t) => {
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
- var vecrand = (minlength = 1, maxlength = minlength) => {
147
- const angle = vecconfig.random() * 2 * Math.PI;
148
- const radius = vecconfig.random() * (maxlength - minlength) + minlength;
149
- return vec(Math.cos(angle) * radius, Math.sin(angle) * radius);
150
- };
151
- var isvector = (v) => v instanceof Vector;
152
- var vecconfig = {
153
- random: () => {
154
- return globalThis.rand ? rand() : Math.random();
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);
@@ -1 +1 @@
1
- (()=>{var M=Object.defineProperty;var g=(t,o)=>{for(var s in o)M(t,s,{get:o[s],enumerable:!0})};var i={};g(i,{DOWN:()=>G,LEFT:()=>H,ONE:()=>w,RIGHT:()=>F,UP:()=>D,Vector:()=>n,ZERO:()=>j,isvector:()=>r,vec:()=>c,vecadd:()=>l,vecconfig:()=>y,veccopy:()=>m,veccross:()=>R,vecdir:()=>N,vecdist:()=>E,vecdist2:()=>I,vecdiv:()=>x,vecdot:()=>P,veceq:()=>a,veclerp:()=>U,veclimit:()=>O,vecmag:()=>u,vecmag2:()=>f,vecmult:()=>p,vecnorm:()=>T,vecrand:()=>$,vecrot:()=>q,vecset:()=>d,vecsub:()=>h});var n=class{x;y;constructor(o=0,s=o){this.x=o,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},c=(t=0,o=t)=>new n(t,o),a=(t,o,s=o)=>r(o)?a(t,o.x,o.y):t.x===o&&t.y===s,m=t=>c(t.x,t.y),d=(t,o,s=o)=>{r(o)?d(t,o.x,o.y):(t.x=o,t.y=s)},l=(t,o,s=o)=>{r(o)?l(t,o.x,o.y):(t.x+=o,t.y+=s)},h=(t,o,s=o)=>{r(o)?h(t,o.x,o.y):(t.x-=o,t.y-=s)},p=(t,o,s=o)=>{r(o)?p(t,o.x,o.y):(t.x*=o,t.y*=s)},x=(t,o,s=o)=>{r(o)?x(t,o.x,o.y):(t.x/=o,t.y/=s)},q=(t,o)=>{let s=Math.cos(o),e=Math.sin(o);t.x=s*t.x-e*t.y,t.y=e*t.x+s*t.y},u=t=>Math.sqrt(t.x*t.x+t.y*t.y),f=t=>t.x*t.x+t.y*t.y,T=t=>{let o=u(t);o>0&&x(t,o)},O=(t,o)=>{let s=f(t);s>o*o&&(x(t,Math.sqrt(s)),p(t,o))},E=(t,o)=>{let s=t.x-o.x,e=t.y-o.y;return Math.sqrt(s*s+e*e)},I=(t,o)=>{let s=t.x-o.x,e=t.y-o.y;return s*s+e*e},N=t=>Math.atan2(t.y,t.x),P=(t,o)=>t.x*o.x+t.y*o.y,R=(t,o)=>t.x*o.y-t.y*o.x,U=(t,o,s)=>{t.x+=(o.x-t.x)*s||0,t.y+=(o.y-t.y)*s||0},$=(t=1,o=t)=>{let s=y.random()*2*Math.PI,e=y.random()*(o-t)+t;return c(Math.cos(s)*e,Math.sin(s)*e)},r=t=>t instanceof n,y={random:()=>globalThis.rand?rand():Math.random()},j=c(0,0),w=c(1,1),D=c(0,-1),F=c(1,0),G=c(0,1),H=c(-1,0);globalThis.utils=Object.assign(globalThis.utils||{},i);})();
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.12.1",
3
+ "version": "0.13.0",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
@@ -1,4 +1,4 @@
1
- import { Vector, vec, veccopy } from "../vector/index.js"
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 = veccopy(anchor)
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
  *
@@ -6,9 +6,9 @@ A class to describe a two-dimensional vector.
6
6
 
7
7
  ## vec
8
8
 
9
- Creates a vector.
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
- ## veccopy
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, veccopy } from "@litecanvas/utils"
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 = veccopy(a) // `c` is a copy of `a`
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
- ## vecset
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: `vecset(v: Vector, x: number, y: number = x): void`
44
+ Syntax: `vecSet(v: Vector, x: number, y: number = x): Vector`
49
45
 
50
46
  ```js
51
- import { vec, vecset } from "@litecanvas/utils"
47
+ import { vec, vecSet } from "@litecanvas/utils"
52
48
 
53
49
  const a = vec(0, 0)
54
- vecset(a, 10, 20)
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
- ## vecadd
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: `vecadd(v: Vector, x: number, y: number = x): void`
60
+ Syntax: `vecAdd(v: Vector, x: number, y: number = x): Vector`
65
61
 
66
62
  ```js
67
- import { vec, vecadd } from "@litecanvas/utils"
63
+ import { vec, vecAdd } from "@litecanvas/utils"
68
64
 
69
65
  const a = vec(10, 10)
70
- vecadd(a, 5, 20)
66
+ vecAdd(a, 50, 90)
71
67
 
72
- console.log(a.x) // outputs 15
73
- console.log(a.y) // outputs 30
68
+ console.log(a.x) // outputs 60
69
+ console.log(a.y) // outputs 100
74
70
  ```
75
71
 
76
- ## vecsub
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: `vecsub(v: Vector, x: number, y: number = x): void`
76
+ Syntax: `vecSub(v: Vector, x: number, y: number = x): Vector`
81
77
 
82
- ## vecmult
78
+ ## vecMult
83
79
 
84
- Multiplies (scale) a vector.
80
+ Multiplies (scale) a vector and returns the vector (first argument).
85
81
 
86
- Syntax: `vecmult(v: Vector, x: number, y: number = x): void`
82
+ Syntax: `vecMult(v: Vector, x: number, y: number = x): Vector`
87
83
 
88
- ## vecdiv
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): void`
88
+ Syntax: `vecdiv(v: Vector, x: number, y: number = x): Vector`
93
89
 
94
- ## vecrot
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: `vecrot(v: Vector, radians: number): void`
94
+ Syntax: `vecRotate(v: Vector, radians: number): Vector`
99
95
 
100
- ## vecmag
96
+ ## vecMag
101
97
 
102
98
  Returns the magnitude (length) of the vector.
103
99
 
104
- Syntax: `vecmag(v: Vector): number`
100
+ Syntax: `vecMag(v: Vector): number`
105
101
 
106
- ## vecmag2
102
+ ## vecMag2
107
103
 
108
104
  Returns the magnitude (length) of the vector squared.
109
105
 
110
- Syntax: `vecmag2(v: Vector): number`
106
+ Syntax: `vecMag2(v: Vector): number`
111
107
 
112
- ## vecnorm
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: `vecnorm(v: Vector): void`
112
+ Syntax: `vecNorm(v: Vector): Vector`
117
113
 
118
- ## veclimit
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: `veclimit(v: Vector, max: number): void`
118
+ Syntax: `vecLimit(v: Vector, max: number): Vector`
123
119
 
124
- ## vecdist
120
+ ## vecDist
125
121
 
126
122
  Returns the distance between two points represented by vectors.
127
123
 
128
- Syntax: `vecdist(a: Vector, b: Vector): number`
124
+ Syntax: `vecDist(a: Vector, b: Vector): number`
129
125
 
130
- ## vecdist2
126
+ ## vecDist2
131
127
 
132
128
  Returns the distance between two points represented by vectors squared.
133
129
 
134
- Syntax: `vecdist2(a: Vector, b: Vector): number`
130
+ Syntax: `vecDist2(a: Vector, b: Vector): number`
135
131
 
136
- ## vecdir
132
+ ## vecAngle
137
133
 
138
134
  Calculates the angle a vector makes with the positive x-axis.
139
135
 
140
- Syntax: `vecdir(v: Vector): number`
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
- ## vecdot
143
+ ## vecDot
143
144
 
144
145
  Calculates the dot product of two vectors.
145
146
 
146
- Syntax: `vecdot(a: Vector, b: Vector): number`
147
+ Syntax: `vecDot(a: Vector, b: Vector): number`
147
148
 
148
- ## veccross
149
+ ## vecCross
149
150
 
150
151
  Calculates the dot product of two vectors.
151
152
 
152
- Syntax: `veccross(a: Vector, b: Vector): number`
153
+ Syntax: `vecCross(a: Vector, b: Vector): number`
153
154
 
154
- ## veclerp
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: `veclerp(a: Vector, b: Vector, atm: number): void`
164
+ Syntax: `vecLerp(a: Vector, b: Vector, atm: number): Lerp`
162
165
 
163
- ## vecrand
166
+ ## vecRand
164
167
 
165
168
  Creates a vector with random direction and (optional) length.
166
169
 
167
- If the `litecanvas#rand()` not is globally explosed, uses `Math.random()`.
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
- ```js
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`