@litecanvas/utils 0.9.0 → 0.10.1
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/all.js +6 -6
- package/dist/all.min.js +1 -1
- package/dist/camera.js +6 -6
- package/dist/camera.min.js +1 -1
- package/package.json +1 -1
- package/src/camera/README.md +96 -0
- package/src/camera/index.js +6 -6
package/dist/all.js
CHANGED
|
@@ -142,11 +142,11 @@
|
|
|
142
142
|
constructor(engine = null) {
|
|
143
143
|
engine = engine || globalThis;
|
|
144
144
|
this._engine = engine;
|
|
145
|
-
this.
|
|
145
|
+
this.resize(engine.WIDTH || 0, engine.HEIGHT || 0);
|
|
146
146
|
this.x = this.width / 2;
|
|
147
147
|
this.y = this.height / 2;
|
|
148
148
|
}
|
|
149
|
-
|
|
149
|
+
resize(width, height) {
|
|
150
150
|
this.width = width;
|
|
151
151
|
this.height = height;
|
|
152
152
|
}
|
|
@@ -201,8 +201,8 @@
|
|
|
201
201
|
output.y = s * x + c * y + this.y;
|
|
202
202
|
return output;
|
|
203
203
|
}
|
|
204
|
-
shake(
|
|
205
|
-
if (this.shaking
|
|
204
|
+
shake(amplitude = 1, duration = 0.3) {
|
|
205
|
+
if (this.shaking) return;
|
|
206
206
|
this._shake.removeListener = this._engine.listen("update", (dt) => {
|
|
207
207
|
this._shake.x = this._engine.randi(-amplitude, amplitude);
|
|
208
208
|
this._shake.y = this._engine.randi(-amplitude, amplitude);
|
|
@@ -213,13 +213,13 @@
|
|
|
213
213
|
});
|
|
214
214
|
}
|
|
215
215
|
unshake() {
|
|
216
|
-
if (this.shaking
|
|
216
|
+
if (this.shaking) {
|
|
217
217
|
this._shake.removeListener();
|
|
218
218
|
this._shake.removeListener = null;
|
|
219
219
|
this._shake.x = this._shake.y = 0;
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
|
-
shaking() {
|
|
222
|
+
get shaking() {
|
|
223
223
|
return this._shake.removeListener !== null;
|
|
224
224
|
}
|
|
225
225
|
};
|
package/dist/all.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{var v=Object.defineProperty;var F=(e,t)=>{for(var s in t)v(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var M={};F(M,{ANCHOR_BOT_LEFT:()=>nt,ANCHOR_BOT_RIGHT:()=>at,ANCHOR_CENTER:()=>it,ANCHOR_TOP_LEFT:()=>L,ANCHOR_TOP_RIGHT:()=>rt,Actor:()=>I,Camera:()=>p,DOWN:()=>st,Grid:()=>g,LEFT:()=>ot,ONE:()=>Q,RIGHT:()=>et,TypedGrid:()=>w,UP:()=>tt,Vector:()=>u,ZERO:()=>K,diff:()=>P,fract:()=>D,intersection:()=>x,isvector:()=>c,resolve:()=>C,vec:()=>i,vecadd:()=>z,vecconfig:()=>H,veccopy:()=>E,veccross:()=>U,vecdir:()=>N,vecdist:()=>$,vecdist2:()=>q,vecdiv:()=>_,vecdot:()=>V,veceq:()=>S,veclerp:()=>Z,veclimit:()=>j,vecmag:()=>Y,vecmag2:()=>W,vecmult:()=>b,vecnorm:()=>B,vecrand:()=>J,vecrot:()=>G,vecset:()=>X,vecsub:()=>R,wave:()=>O});var P=(e,t)=>Math.abs(t-e)||0;var D=e=>e%1||0;var O=(e,t,s,o=Math.sin)=>e+(o(s)+1)/2*(t-e);var x=(e,t,s,o,n,r,a,h)=>{let l=Math.max(e,n),A=Math.min(e+s,n+a)-l,d=Math.max(t,r),m=Math.min(t+o,r+h)-d;return[l,d,A,m]};var C=(e,t,s,o,n,r,a,h)=>{let[l,A,d,m]=x(e,t,s,o,n,r,a,h),f="",y=e,T=t;return d<m?e<n?(f="right",y=n-s):(f="left",y=n+a):t<r?(f="bottom",T=r-o):(f="top",T=r+h),{direction:f,x:y,y:T}};var p=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.
|
|
1
|
+
(()=>{var v=Object.defineProperty;var F=(e,t)=>{for(var s in t)v(e,s,{get:t[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var M={};F(M,{ANCHOR_BOT_LEFT:()=>nt,ANCHOR_BOT_RIGHT:()=>at,ANCHOR_CENTER:()=>it,ANCHOR_TOP_LEFT:()=>L,ANCHOR_TOP_RIGHT:()=>rt,Actor:()=>I,Camera:()=>p,DOWN:()=>st,Grid:()=>g,LEFT:()=>ot,ONE:()=>Q,RIGHT:()=>et,TypedGrid:()=>w,UP:()=>tt,Vector:()=>u,ZERO:()=>K,diff:()=>P,fract:()=>D,intersection:()=>x,isvector:()=>c,resolve:()=>C,vec:()=>i,vecadd:()=>z,vecconfig:()=>H,veccopy:()=>E,veccross:()=>U,vecdir:()=>N,vecdist:()=>$,vecdist2:()=>q,vecdiv:()=>_,vecdot:()=>V,veceq:()=>S,veclerp:()=>Z,veclimit:()=>j,vecmag:()=>Y,vecmag2:()=>W,vecmult:()=>b,vecnorm:()=>B,vecrand:()=>J,vecrot:()=>G,vecset:()=>X,vecsub:()=>R,wave:()=>O});var P=(e,t)=>Math.abs(t-e)||0;var D=e=>e%1||0;var O=(e,t,s,o=Math.sin)=>e+(o(s)+1)/2*(t-e);var x=(e,t,s,o,n,r,a,h)=>{let l=Math.max(e,n),A=Math.min(e+s,n+a)-l,d=Math.max(t,r),m=Math.min(t+o,r+h)-d;return[l,d,A,m]};var C=(e,t,s,o,n,r,a,h)=>{let[l,A,d,m]=x(e,t,s,o,n,r,a,h),f="",y=e,T=t;return d<m?e<n?(f="right",y=n-s):(f="left",y=n+a):t<r?(f="bottom",T=r-o):(f="top",T=r+h),{direction:f,x:y,y:T}};var p=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null){t=t||globalThis,this._engine=t,this.resize(t.WIDTH||0,t.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s}start(t=!1){let s=this.width/2,o=this.height/2;this._engine.push(),this._engine.translate(s,o),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),t&&this._engine.cliprect(this.x,this.y,this.width,this.height)}end(){this._engine.pop()}lookAt(t,s){this.x=t,this.y=s}move(t,s){this.x+=t,this.y+=s}zoom(t){this.scale*=t}zoomTo(t){this.scale=t}rotate(t){this.rotation+=t}rotateTo(t){this.rotation=t}getWorldPoint(t,s,o){let n=Math.cos(-this.rotation),r=Math.sin(-this.rotation);return t=(t-this.width/2)/this.scale,s=(s-this.height/2)/this.scale,o=o||{},o.x=n*t-r*s+this.x,o.y=r*t+n*s+this.y,o}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",o=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=o,s<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};var g=class e{_w;_h;_c;constructor(t,s,o=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=o}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,o){this._c[this.pointToIndex(t,s)]=o}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}get length(){return this._w*this._h}pointToIndex(t,s){return this.clampX(~~t)+this.clampY(~~s)*this._w}indexToPointX(t){return t%this._w}indexToPointY(t){return Math.floor(t/this._w)}forEach(t,s=!1){let o=s?this.length-1:0,n=s?-1:this.length,r=s?-1:1;for(;o!==n;){let a=this.indexToPointX(o),h=this.indexToPointY(o),l=this._c[o];if(t(a,h,l,this)===!1)break;o+=r}}fill(t){this.forEach((s,o)=>{this.set(s,o,t)})}clone(){return e.fromArray(this._w,this._h,this._c)}clampX(t){return k(t,0,this._w-1)}clampY(t){return k(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let o=[];return this.forEach((n,r,a)=>{o[r]=o[r]||"",o[r]+=a+t}),o.join(`
|
|
2
2
|
`)}},w=class e extends g{constructor(t,s,o=Uint8Array){super(t,s,null),this._c=new o(this._w*this._h)}has(t,s){return this.get(t,s)!==0}clone(){let t=new e(this._w,this._h,this._c.constructor);return this.forEach((s,o,n)=>{t.set(s,o,n)}),t}};function k(e,t,s){return e<t?t:e>s?s:e}var u=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},i=(e=0,t=e)=>new u(e,t),S=(e,t,s=t)=>c(t)?S(e,t.x,t.y):e.x===t&&e.y===s,E=e=>i(e.x,e.y),X=(e,t,s=t)=>{c(t)?X(e,t.x,t.y):(e.x=t,e.y=s)},z=(e,t,s=t)=>{c(t)?z(e,t.x,t.y):(e.x+=t,e.y+=s)},R=(e,t,s=t)=>{c(t)?R(e,t.x,t.y):(e.x-=t,e.y-=s)},b=(e,t,s=t)=>{c(t)?b(e,t.x,t.y):(e.x*=t,e.y*=s)},_=(e,t,s=t)=>{c(t)?_(e,t.x,t.y):(e.x/=t,e.y/=s)},G=(e,t)=>{let s=Math.cos(t),o=Math.sin(t);e.x=s*e.x-o*e.y,e.y=o*e.x+s*e.y},Y=e=>Math.sqrt(e.x*e.x+e.y*e.y),W=e=>e.x*e.x+e.y*e.y,B=e=>{let t=Y(e);t>0&&_(e,t)},j=(e,t)=>{let s=W(e);s>t*t&&(_(e,Math.sqrt(s)),b(e,t))},$=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return Math.sqrt(s*s+o*o)},q=(e,t)=>{let s=e.x-t.x,o=e.y-t.y;return s*s+o*o},N=e=>Math.atan2(e.y,e.x),V=(e,t)=>e.x*t.x+e.y*t.y,U=(e,t)=>e.x*t.y-e.y*t.x,Z=(e,t,s)=>{e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0},J=(e=1,t=e)=>{let s=H.random()*2*Math.PI,o=H.random()*(t-e)+e;return i(Math.cos(s)*o,Math.sin(s)*o)},c=e=>e instanceof u,H={random:()=>globalThis.rand?rand():Math.random()},K=i(0,0),Q=i(1,1),tt=i(0,-1),et=i(1,0),st=i(0,1),ot=i(-1,0);var it=i(.5,.5),L=i(0,0),rt=i(1,0),nt=i(0,1),at=i(1,1),I=class{sprite;pos;_o;_s;angle=0;opacity=1;hidden=!1;constructor(t,s,o=L){this.sprite=t,this.pos=s||i(0),this._o=E(o),this._s=i(1,1)}set x(t){this.pos.x=t}get x(){return this.pos.x}set y(t){this.pos.y=t}get y(){return this.pos.y}set anchor(t){this._o.x=t.x,this._o.y=t.y}get anchor(){return this._o}get width(){return this.sprite.width*this._s.y}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}draw(t=globalThis){if(this.hidden||this.opacity<=0)return;t.push(),this.transform(t);let s=this.sprite.width*this.anchor.x,o=this.sprite.height*this.anchor.y;t.image(-s,-o,this.sprite),t.pop()}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(this.angle),t.scale(this._s.x,this._s.y),t.alpha(this.opacity)}};globalThis.utils=Object.assign(globalThis.utils||{},M);})();
|
package/dist/camera.js
CHANGED
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
constructor(engine = null) {
|
|
36
36
|
engine = engine || globalThis;
|
|
37
37
|
this._engine = engine;
|
|
38
|
-
this.
|
|
38
|
+
this.resize(engine.WIDTH || 0, engine.HEIGHT || 0);
|
|
39
39
|
this.x = this.width / 2;
|
|
40
40
|
this.y = this.height / 2;
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
resize(width, height) {
|
|
43
43
|
this.width = width;
|
|
44
44
|
this.height = height;
|
|
45
45
|
}
|
|
@@ -94,8 +94,8 @@
|
|
|
94
94
|
output.y = s * x + c * y + this.y;
|
|
95
95
|
return output;
|
|
96
96
|
}
|
|
97
|
-
shake(
|
|
98
|
-
if (this.shaking
|
|
97
|
+
shake(amplitude = 1, duration = 0.3) {
|
|
98
|
+
if (this.shaking) return;
|
|
99
99
|
this._shake.removeListener = this._engine.listen("update", (dt) => {
|
|
100
100
|
this._shake.x = this._engine.randi(-amplitude, amplitude);
|
|
101
101
|
this._shake.y = this._engine.randi(-amplitude, amplitude);
|
|
@@ -106,13 +106,13 @@
|
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
unshake() {
|
|
109
|
-
if (this.shaking
|
|
109
|
+
if (this.shaking) {
|
|
110
110
|
this._shake.removeListener();
|
|
111
111
|
this._shake.removeListener = null;
|
|
112
112
|
this._shake.x = this._shake.y = 0;
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
shaking() {
|
|
115
|
+
get shaking() {
|
|
116
116
|
return this._shake.removeListener !== null;
|
|
117
117
|
}
|
|
118
118
|
};
|
package/dist/camera.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var h=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(i=null){i=i||globalThis,this._engine=i,this.
|
|
1
|
+
(()=>{globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let e in globalThis.utils)e!=="global"&&(globalThis[e]=globalThis.utils[e])};var h=class{_engine=null;x=0;y=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(i=null){i=i||globalThis,this._engine=i,this.resize(i.WIDTH||0,i.HEIGHT||0),this.x=this.width/2,this.y=this.height/2}resize(i,s){this.width=i,this.height=s}start(i=!1){let s=this.width/2,t=this.height/2;this._engine.push(),this._engine.translate(s,t),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y),i&&this._engine.cliprect(this.x,this.y,this.width,this.height)}end(){this._engine.pop()}lookAt(i,s){this.x=i,this.y=s}move(i,s){this.x+=i,this.y+=s}zoom(i){this.scale*=i}zoomTo(i){this.scale=i}rotate(i){this.rotation+=i}rotateTo(i){this.rotation=i}getWorldPoint(i,s,t){let n=Math.cos(-this.rotation),a=Math.sin(-this.rotation);return i=(i-this.width/2)/this.scale,s=(s-this.height/2)/this.scale,t=t||{},t.x=n*i-a*s+this.x,t.y=a*i+n*s+this.y,t}shake(i=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",t=>{this._shake.x=this._engine.randi(-i,i),this._shake.y=this._engine.randi(-i,i),s-=t,s<=0&&this.unshake()}))}unshake(){this.shaking&&(this._shake.removeListener(),this._shake.removeListener=null,this._shake.x=this._shake.y=0)}get shaking(){return this._shake.removeListener!==null}};globalThis.utils=Object.assign(globalThis.utils||{},{Camera:h});})();
|
package/package.json
CHANGED
package/src/camera/README.md
CHANGED
|
@@ -24,3 +24,99 @@ function draw() {
|
|
|
24
24
|
// draw your UI here
|
|
25
25
|
}
|
|
26
26
|
```
|
|
27
|
+
|
|
28
|
+
## Properties
|
|
29
|
+
|
|
30
|
+
- `x` the camera X-position
|
|
31
|
+
- `y` the camera Y-position
|
|
32
|
+
- `shaking` it's `true` when the camera is shaking
|
|
33
|
+
- `scale` the camera zoom
|
|
34
|
+
- `rotation` the camera rotation
|
|
35
|
+
- `width` the camera width
|
|
36
|
+
- `height` the camera height
|
|
37
|
+
|
|
38
|
+
## Methods
|
|
39
|
+
|
|
40
|
+
### lookAt(x: number, y: number)
|
|
41
|
+
|
|
42
|
+
Sets the camera X and Y
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
camera.lookAt(100, 100)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### move(x: number, y: number)
|
|
49
|
+
|
|
50
|
+
Moves the camera X and Y
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
function update(dt) {
|
|
54
|
+
// moves the camera 100 px/s to right
|
|
55
|
+
camera.move(100 * dt, 0)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### zoomTo(factor: number)
|
|
60
|
+
|
|
61
|
+
Scales the camera view
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
// makes anything on camera 3 times bigger
|
|
65
|
+
camera.zoomTo(3)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### zoom(value: number)
|
|
69
|
+
|
|
70
|
+
Increases the camera scale
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
// increase the camera zoom over time
|
|
74
|
+
function update(dt) {
|
|
75
|
+
camera.zoom(2 * dt)
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### rotateTo(radians: number)
|
|
80
|
+
|
|
81
|
+
Sets the camera rotation
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
// makes anything on camera 3 times bigger
|
|
85
|
+
camera.rotateTo(Math.PI)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### rotate(value: number)
|
|
89
|
+
|
|
90
|
+
Increases the camera rotation
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
// increase the camera rotation over time
|
|
94
|
+
function update(dt) {
|
|
95
|
+
camera.zoom(dt)
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### shake(amplitude:number = 1, duration: number = 0.3)
|
|
100
|
+
|
|
101
|
+
Shakes the camera view
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
// shake when a touch/click happens
|
|
105
|
+
function tapped() {
|
|
106
|
+
camera.shake()
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### unshake()
|
|
111
|
+
|
|
112
|
+
Makes the camera stop shaking immediately
|
|
113
|
+
|
|
114
|
+
### getWorldPoint(x: number, y: number, output?: {x: number, y: number}): {x: number, y: number}
|
|
115
|
+
|
|
116
|
+
This method is used to convert a point (X, Y) if the camera is moved, rotated or zoomed.
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
function tapped(x, y) {
|
|
120
|
+
const fixedTap = camera.getWorldPoint(x, y)
|
|
121
|
+
}
|
|
122
|
+
```
|
package/src/camera/index.js
CHANGED
|
@@ -28,12 +28,12 @@ export default class Camera {
|
|
|
28
28
|
constructor(engine = null) {
|
|
29
29
|
engine = engine || globalThis
|
|
30
30
|
this._engine = engine
|
|
31
|
-
this.
|
|
31
|
+
this.resize(engine.WIDTH || 0, engine.HEIGHT || 0)
|
|
32
32
|
this.x = this.width / 2
|
|
33
33
|
this.y = this.height / 2
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
resize(width, height) {
|
|
37
37
|
this.width = width
|
|
38
38
|
this.height = height
|
|
39
39
|
}
|
|
@@ -105,8 +105,8 @@ export default class Camera {
|
|
|
105
105
|
return output
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
shake(
|
|
109
|
-
if (this.shaking
|
|
108
|
+
shake(amplitude = 1, duration = 0.3) {
|
|
109
|
+
if (this.shaking) return
|
|
110
110
|
|
|
111
111
|
this._shake.removeListener = this._engine.listen("update", (dt) => {
|
|
112
112
|
this._shake.x = this._engine.randi(-amplitude, amplitude)
|
|
@@ -119,14 +119,14 @@ export default class Camera {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
unshake() {
|
|
122
|
-
if (this.shaking
|
|
122
|
+
if (this.shaking) {
|
|
123
123
|
this._shake.removeListener()
|
|
124
124
|
this._shake.removeListener = null
|
|
125
125
|
this._shake.x = this._shake.y = 0
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
shaking() {
|
|
129
|
+
get shaking() {
|
|
130
130
|
return this._shake.removeListener !== null
|
|
131
131
|
}
|
|
132
132
|
}
|