@litecanvas/utils 0.24.0 → 0.24.2

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/tween.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var h=Object.defineProperty;var p=(t,e)=>{for(var s in e)h(t,s,{get:e[s],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let t in globalThis.utils)t!=="global"&&(globalThis[t]=globalThis.utils[t])};var c={};p(c,{BACK_IN:()=>E,BACK_IN_OUT:()=>m,BACK_OUT:()=>x,BOUNCE_IN:()=>f,BOUNCE_IN_OUT:()=>y,BOUNCE_OUT:()=>l,EASE_IN:()=>d,EASE_IN_OUT:()=>T,EASE_OUT:()=>g,ELASTIC_IN:()=>I,ELASTIC_IN_OUT:()=>w,ELASTIC_OUT:()=>b,LINEAR:()=>u,tween:()=>_});var n=Math.PI/2,_=(t,e,s,i=1,r=u)=>new a(t,e,s,i,r),u=t=>t,d=t=>t*t,g=t=>-t*(t-2),T=t=>t<.5?2*t*t:-2*t*t+4*t-1,E=t=>t*t*t-t*Math.sin(t*Math.PI),x=t=>{let e=1-t;return 1-(e*e*e-e*Math.sin(e*Math.PI))},m=t=>{if(t<.5){let s=2*t;return .5*(s*s*s-s*Math.sin(s*Math.PI))}let e=1-(2*t-1);return .5*(1-(e*e*e-e*Math.sin(t*Math.PI)))+.5},I=t=>Math.sin(13*n*t)*Math.pow(2,10*(t-1)),b=t=>Math.sin(-13*n*(t+1))*Math.pow(2,-10*t)+1,w=t=>{if(t<.5){let i=Math.sin(13*n*(2*t)),r=Math.pow(2,10*(2*t-1));return .5*i*r}let e=Math.sin(-13*n*(2*t-1+1)),s=Math.pow(2,-10*(2*t-1));return .5*(e*s+2)},f=t=>1-l(1-t),l=t=>t<4/11?121*t*t/16:t<8/11?363/40*t*t-99/10*t+17/5:t<9/10?4356/361*t*t-35442/1805*t+16061/1805:54/5*t*t-513/25*t+268/25,y=t=>t<.5?.5*f(t*2):.5*l(t*2-1)+.5,a=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(e,s,i,r,o){this._o=e,this._p=s,this._x=i,this._d=r,this._e=o,this._w=0}start(e){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let s=this._o[this._p]||0,i=this._rel?s+this._x:this._x;return this._lc=this._lc||e||globalThis,this._u=this._lc.listen("update",r=>{if(this._t<=this._w){this._t+=r;return}let o=this._t-this._w;this._o[this._p]=this._lc.lerp(s,i,this._e(o/this._d)),this._t+=r,o>=this._d&&(this._o[this._p]=i,this.stop())}),this}stop(e=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,e)for(let s of this._cb)s(this._o);return this}restart(e=null,s=!1){return this.stop(s).restart(e)}onEnd(e){return this._cb.push(e),this}chain(e){return this._ch.onEnd(()=>{this._cu=e.start(this._lc)}),this._ch=e,this}reset(){return this._cb.length=0,this.stop()}relative(e=!0){return this._rel=e,this}delay(e){return this._w=e,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},c);})();
1
+ (()=>{var ot=Object.defineProperty;var nt=(s,o)=>{for(var u in o)ot(s,u,{get:o[u],enumerable:!0})};globalThis.utils=globalThis.utils||{};globalThis.utils.global=()=>{for(let s in globalThis.utils)s!=="global"&&(globalThis[s]=globalThis.utils[s])};var Z={};nt(Z,{BACK_IN:()=>ft,BACK_IN_OUT:()=>pt,BACK_OUT:()=>ht,BOUNCE_IN:()=>rt,BOUNCE_IN_OUT:()=>Tt,BOUNCE_OUT:()=>Q,EASE_IN:()=>lt,EASE_IN_OUT:()=>ut,EASE_OUT:()=>ct,ELASTIC_IN:()=>_t,ELASTIC_IN_OUT:()=>gt,ELASTIC_OUT:()=>dt,LINEAR:()=>it,tween:()=>at});var z=new AudioContext,et=(s=1,o=.05,u=220,g=0,E=0,S=.1,T=0,tt=1,Y=0,N=0,p=0,P=0,L=0,G=0,M=0,n=0,H=0,W=1,A=0,B=0,k=0)=>{let b=Math,w=2*b.PI,x=44100,V=Y*=500*w/x/x,X=u*=(1-o+2*o*b.random(o=[]))*w/x,v=0,O=0,m=0,r=1,$=0,F=0,D=0,y=k<0?-1:1,U=w*y*k*2/x,R=b.cos(U),t=b.sin,e=t(U)/4,i=1+e,a=-2*R/i,f=(1-e)/i,_=(1+y*R)/2/i,I=-(y+R)/i,C=_,l=0,h=0,c=0,d=0;for(g=x*g+9,A*=x,E*=x,S*=x,H*=x,N*=500*w/x**3,M*=w/x,p*=w/x,P*=x,L=x*L|0,s*=.3*(globalThis.zzfxV||1),y=g+A+E+S+H|0;m<y;o[m++]=D*s)++F%(100*n|0)||(D=T?1<T?2<T?3<T?t(v*v):b.max(b.min(b.tan(v),1),-1):1-(2*v/w%2+2)%2:1-4*b.abs(b.round(v/w)-v/w):t(v),D=(L?1-B+B*t(w*m/L):1)*(D<0?-1:1)*b.abs(D)**tt*(m<g?m/g:m<g+A?1-(m-g)/A*(1-W):m<g+A+E?W:m<y-H?(y-m-H)/S*W:0),D=H?D/2+(H>m?0:(m<y-H?1:(y-m)/H)*o[m-H|0]/2/s):D,k&&(D=d=C*l+I*(l=h)+_*(h=D)-f*c-a*(c=d))),U=(u+=Y+=N)*b.cos(M*O++),v+=U+U*G*t(m**5),r&&++r>P&&(u+=p,X+=p,r=0),!L||++$%L||(u=X,Y=V,r=r||1);s=z.createBuffer(1,y,x),s.getChannelData(0).set(o),u=z.createBufferSource(),u.buffer=s,u.connect(z.destination),u.start()};var K=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=st;function st(s={}){let o=globalThis,u=Math.PI,g=u*2,E=requestAnimationFrame,S=[],T=(t,e,i)=>{t.addEventListener(e,i,!1),S.push(()=>t.removeEventListener(e,i,!1))};s=Object.assign({fps:60,fullscreen:!0,width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},s);let Y=!1,N=[],p=s.canvas||document.createElement("canvas"),P=s.fullscreen,L=s.autoscale,G=s.animate,M=1,n,H=1,W,A,B,k=0,b=!0,w="sans-serif",x="",V=32,X=Date.now(),v=s.global,O={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},m={settings:Object.assign({},s),colors:K},r={WIDTH:s.width,HEIGHT:s.height||s.width,CANVAS:null,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],PI:u,TWO_PI:g,HALF_PI:u*.5,lerp:(t,e,i)=>t+i*(e-t),deg2rad:t=>u/180*t,rad2deg:t=>180/u*t,clamp:(t,e,i)=>t<e?e:t>i?i:t,wrap:(t,e,i)=>t-(i-e)*Math.floor((t-e)/(i-e)),map(t,e,i,a,f,_){let I=(t-e)/(i-e)*(f-a)+a;return _?r.clamp(I,a,f):I},norm:(t,e,i)=>r.map(t,e,i,0,1),rand:(t=0,e=1)=>(X=(1664525*X+1013904223)%4294967296,X/4294967296*(e-t)+t),randi:(t=0,e=1)=>Math.floor(r.rand(t,e+1)),seed:t=>t==null?X:X=~~t,cls(t){t==null?n.clearRect(0,0,r.WIDTH,r.HEIGHT):r.rectfill(0,0,r.WIDTH,r.HEIGHT,t)},rect(t,e,i,a,f=0,_=null){n.beginPath(),n[_?"roundRect":"rect"](~~t,~~e,i,a,_),r.stroke(f)},rectfill(t,e,i,a,f=0,_=null){n.beginPath(),n[_?"roundRect":"rect"](~~t,~~e,i,a,_),r.fill(f)},circ(t,e,i,a){n.beginPath(),n.arc(~~t,~~e,i,0,g),r.stroke(a)},circfill(t,e,i,a){n.beginPath(),n.arc(~~t,~~e,i,0,g),r.fill(a)},line(t,e,i,a,f){n.beginPath(),n.moveTo(~~t,~~e),n.lineTo(~~i,~~a),r.stroke(f)},linewidth(t){n.lineWidth=t},linedash(t,e=0){n.setLineDash(t),n.lineDashOffset=e},text(t,e,i,a=3){n.font=`${x} ${V}px ${w}`,n.fillStyle=r.getcolor(a),n.fillText(i,~~t,~~e)},textfont(t){w=t},textsize(t){V=t},textstyle(t){x=t||""},textalign(t,e){t&&(n.textAlign=t),e&&(n.textBaseline=e)},textmetrics(t,e=V){n.font=`${x} ${e}px ${w}`;let i=n.measureText(t);return i.height=i.actualBoundingBoxAscent+i.actualBoundingBoxDescent,i},image(t,e,i){n.drawImage(i,~~t,~~e)},paint(t,e,i,a={}){let f=a.canvas||new OffscreenCanvas(1,1),_=a.scale||1,I=n;if(f.width=t*_,f.height=e*_,n=f.getContext("2d"),n.scale(_,_),i.push){let C=0,l=0;n.imageSmoothingEnabled=!1;for(let h of i){for(let c of h)c!==" "&&c!=="."&&r.rectfill(C,l,1,1,parseInt(c,16)),C++;l++,C=0}}else i(n);return n=I,f},ctx(t){return t&&(n=t),n},push:()=>n.save(),pop:()=>n.restore(),translate:(t,e)=>n.translate(~~t,~~e),scale:(t,e)=>n.scale(t,e||t),rotate:t=>n.rotate(t),transform:(t,e,i,a,f,_,I=!0)=>n[I?"setTransform":"transform"](t,e,i,a,f,_),alpha(t){n.globalAlpha=r.clamp(t,0,1)},path:t=>new Path2D(t),fill(t,e){n.fillStyle=r.getcolor(t),e?n.fill(e):n.fill()},stroke(t,e){n.strokeStyle=r.getcolor(t),e?n.stroke(e):n.stroke()},clip(t){n.clip(t)},sfx(t,e=0,i=1){return o.zzfxV<=0||navigator.userActivation&&!navigator.userActivation.hasBeenActive?!1:(t=t||r.DEFAULT_SFX,(e>0||i!==1)&&(t=t.slice(),t[0]=i*(t[0]||1),t[10]=~~t[10]+e),et.apply(0,t),t)},volume(t){o.zzfxV=+t},colrect:(t,e,i,a,f,_,I,C)=>t<f+I&&t+i>f&&e<_+C&&e+a>_,colcirc:(t,e,i,a,f,_)=>(a-t)**2+(f-e)**2<=(i+_)**2,use(t,e={}){Y?R(t,e):N.push([t,e])},listen(t,e){return O[t]=O[t]||new Set,O[t].add(e),()=>O[t].delete(e)},emit(t,e,i,a,f){U("before:"+t,e,i,a,f),U(t,e,i,a,f),U("after:"+t,e,i,a,f)},getcolor:t=>K[~~t%K.length],setvar(t,e){r[t]=e,v&&(o[t]=e)},resize(t,e){r.setvar("WIDTH",p.width=t),r.setvar("HEIGHT",p.height=e||t),y()},timescale(t){H=t},setfps(t){A=1/t,B=A*1e3,k=0},quit(){r.emit("quit");for(let t of S)t();if(b=O=!1,v){for(let t in r)delete o[t];delete o.__litecanvas}}};for(let t of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])r[t]=Math[t];function $(){Y=!0;let t=s.loop?s.loop:o;for(let e in O)t[e]&&r.listen(e,t[e]);for(let[e,i]of N)R(e,i);if((P||L)&&T(o,"resize",y),y(),s.tapEvents){let e=(l,h)=>[(l-p.offsetLeft)/M,(h-p.offsetTop)/M],i=new Map,a=(l,h,c)=>{let d={x:h,y:c,startX:h,startY:c,ts:performance.now()};return i.set(l,d),d},f=(l,h,c)=>{let d=i.get(l)||a(l);d.x=h,d.y=c},_=l=>l&&performance.now()-l.ts<=200,I=!1;T(p,"mousedown",l=>{l.preventDefault();let[h,c]=e(l.pageX,l.pageY);r.emit("tap",h,c,0),a(0,h,c),I=!0}),T(p,"mousemove",l=>{l.preventDefault();let[h,c]=e(l.pageX,l.pageY);r.setvar("MOUSEX",h),r.setvar("MOUSEY",c),I&&(r.emit("tapping",h,c,0),f(0,h,c))}),T(p,"mouseup",l=>{l.preventDefault();let h=i.get(0),[c,d]=e(l.pageX,l.pageY);_(h)&&r.emit("tapped",h.startX,h.startY,0),r.emit("untap",c,d,0),i.delete(0),I=!1}),T(p,"touchstart",l=>{l.preventDefault();let h=l.changedTouches;for(let c of h){let[d,j]=e(c.pageX,c.pageY);r.emit("tap",d,j,c.identifier+1),a(c.identifier+1,d,j)}}),T(p,"touchmove",l=>{l.preventDefault();let h=l.changedTouches;for(let c of h){let[d,j]=e(c.pageX,c.pageY);r.emit("tapping",d,j,c.identifier+1),f(c.identifier+1,d,j)}});let C=l=>{l.preventDefault();let h=[];if(l.targetTouches.length>0)for(let c of l.targetTouches)h.push(c.identifier+1);for(let[c,d]of i)h.includes(c)||(_(d)&&r.emit("tapped",d.startX,d.startY,c),r.emit("untap",d.x,d.y,c),i.delete(c))};T(p,"touchend",C),T(p,"touchcancel",C),T(o,"blur",()=>{I=!1;for(let[l,h]of i)r.emit("untap",h.x,h.y,l),i.delete(l)})}if(s.keyboardEvents){let e=new Set,i=a=>a==="any"?e.size>0:e.has(a.toLowerCase());r.setvar("iskeydown",i),T(o,"keydown",a=>{e.add(a.key.toLowerCase())}),T(o,"keyup",a=>{e.delete(a.key.toLowerCase())}),T(o,"blur",()=>e.clear())}s.pauseOnBlur&&(T(o,"blur",()=>{b=!1}),T(o,"focus",()=>{b=!0,E(F)})),r.setfps(s.fps),r.emit("init",r),W=performance.now(),E(F)}function F(t){let e=!G,i=t-W;for(k+=i>100?B:i,W=t;k>=B;)r.emit("update",A*H),r.setvar("ELAPSED",r.ELAPSED+A*H),k-=B,e=!0;e&&(r.textalign("start","top"),r.emit("draw")),b&&G&&E(F)}function D(){p=typeof p=="string"?document.querySelector(p):p,r.setvar("CANVAS",p),n=p.getContext("2d"),T(p,"click",()=>o.focus()),r.WIDTH>0&&(P=!1),p.style="",p.width=r.WIDTH,p.height=r.HEIGHT||r.WIDTH,p.parentNode||document.body.appendChild(p)}function y(){let t=o.innerWidth,e=o.innerHeight,i=p.style;i.display="block",P?(i.position="absolute",i.inset=0,r.setvar("WIDTH",p.width=t),r.setvar("HEIGHT",p.height=e)):L&&(i.margin="auto",M=Math.min(t/r.WIDTH,e/r.HEIGHT),M=(s.pixelart?~~M:M)||1,i.width=r.WIDTH*M+"px",i.height=r.HEIGHT*M+"px"),r.setvar("CENTERX",r.WIDTH/2),r.setvar("CENTERY",r.HEIGHT/2),(!s.antialias||s.pixelart)&&(n.imageSmoothingEnabled=!1,p.style.imageRendering="pixelated"),r.emit("resized",M),G||E(F)}function U(t,e,i,a,f){if(O[t])for(let _ of O[t])_(e,i,a,f)}function R(t,e){let i=t(r,m,e);if(typeof i=="object")for(let a of Object.keys(i))r.setvar(a,i[a])}if(v){if(o.__litecanvas)throw"global litecanvas already instantiated";Object.assign(o,r),o.__litecanvas=r}return D(),document.readyState==="loading"?T(o,"DOMContentLoaded",()=>E($)):E($),r}var q=Math.PI/2,at=(s,o,u,g=1,E=it)=>new J(s,o,u,g,E),it=s=>s,lt=s=>s*s,ct=s=>-s*(s-2),ut=s=>s<.5?2*s*s:-2*s*s+4*s-1,ft=s=>s*s*s-s*Math.sin(s*Math.PI),ht=s=>{let o=1-s;return 1-(o*o*o-o*Math.sin(o*Math.PI))},pt=s=>{if(s<.5){let u=2*s;return .5*(u*u*u-u*Math.sin(u*Math.PI))}let o=1-(2*s-1);return .5*(1-(o*o*o-o*Math.sin(s*Math.PI)))+.5},_t=s=>Math.sin(13*q*s)*Math.pow(2,10*(s-1)),dt=s=>Math.sin(-13*q*(s+1))*Math.pow(2,-10*s)+1,gt=s=>{if(s<.5){let g=Math.sin(13*q*(2*s)),E=Math.pow(2,10*(2*s-1));return .5*g*E}let o=Math.sin(-13*q*(2*s-1+1)),u=Math.pow(2,-10*(2*s-1));return .5*(o*u+2)},rt=s=>1-Q(1-s),Q=s=>s<4/11?121*s*s/16:s<8/11?363/40*s*s-99/10*s+17/5:s<9/10?4356/361*s*s-35442/1805*s+16061/1805:54/5*s*s-513/25*s+268/25,Tt=s=>s<.5?.5*rt(s*2):.5*Q(s*2-1)+.5,J=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(o,u,g,E,S){this._o=o,this._p=u,this._x=g,this._d=E,this._e=S,this._w=0}start(o){if(this.running)return this;this._cu.stop(!1),this._ch=this._cu=this,this.running=!0;let u=this._o[this._p]||0,g=this._rel?u+this._x:this._x;return this._lc=this._lc||o||globalThis,this._u=this._lc.listen("update",E=>{if(this._t<=this._w){this._t+=E;return}let S=this._t-this._w;this._o[this._p]=this._lc.lerp(u,g,this._e(S/this._d)),this._t+=E,S>=this._d&&(this._o[this._p]=g,this.stop())}),this}stop(o=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,o)for(let u of this._cb)u(this._o);return this}restart(o=null,u=!1){return this.stop(u).restart(o)}onEnd(o){return this._cb.push(o),this}chain(o){return this._ch.onEnd(()=>{this._cu=o.start(this._lc)}),this._ch=o,this}reset(){return this._cb.length=0,this.stop()}relative(o=!0){return this._rel=o,this}delay(o){return this._w=o,this}get current(){return this._cu}get progress(){return this.running&&this._t>this._w?(this._t-this._w)/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},Z);})();
package/dist/vector.js CHANGED
@@ -55,8 +55,8 @@
55
55
  /** @type {number} */
56
56
  y;
57
57
  /**
58
- * @param {number} x
59
- * @param {number} y
58
+ * @param {number} [x=0]
59
+ * @param {number} [y]
60
60
  */
61
61
  constructor(x = 0, y = x) {
62
62
  this.x = x;
@@ -207,7 +207,7 @@
207
207
  if (v.y > max.y) v.y = max.y;
208
208
  return v;
209
209
  };
210
- var vecMove = (v, to, delta = 1) => vecAdd(v, to.x * delta, to.y * delta);
210
+ var vecMove = (from, to, delta = 1) => vecAdd(from, to.x * delta, to.y * delta);
211
211
  var vecIsZero = (v) => vecEq(v, ZERO);
212
212
  var ZERO = /* @__PURE__ */ vec(0, 0);
213
213
  var ONE = /* @__PURE__ */ vec(1, 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.24.0",
3
+ "version": "0.24.2",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
@@ -6,8 +6,8 @@ import { Vector } from "../vector"
6
6
  *
7
7
  * @param {Vector} position
8
8
  * @param {Vector} velocity
9
- * @param {Vector?} acceleration
10
- * @param {number?} deltaTime
9
+ * @param {Vector?} [acceleration]
10
+ * @param {number?} [deltaTime]
11
11
  */
12
12
  export default advance = (position, velocity, acceleration, deltaTime = 1) => {
13
13
  if (acceleration) {
@@ -126,6 +126,18 @@ moveRight.chain(moveLeft)
126
126
  moveRight.start()
127
127
  ```
128
128
 
129
+ ### TweenController#delay()
130
+
131
+ Wait a amount of seconds before starts the animation.
132
+
133
+ Syntax: `.delay(seconds: number): this`
134
+
135
+ ```js
136
+ tween(...)
137
+ .delay(3)
138
+ .start() // wait 3 seconds before starts
139
+ ```
140
+
129
141
  ### TweenController#onEnd()
130
142
 
131
143
  Enqueues a callback to be executed when the animation finishes.
@@ -10,8 +10,8 @@ export class Vector {
10
10
  y
11
11
 
12
12
  /**
13
- * @param {number} x
14
- * @param {number} y
13
+ * @param {number} [x=0]
14
+ * @param {number} [y]
15
15
  */
16
16
  constructor(x = 0, y = x) {
17
17
  this.x = x
@@ -417,13 +417,13 @@ export const vecClamp = (v, min, max) => {
417
417
  }
418
418
 
419
419
  /**
420
- * @param {Vector} v
420
+ * @param {Vector} from
421
421
  * @param {Vector} to
422
- * @param {number} delta
422
+ * @param {number} [delta=1]
423
423
  * @returns {Vector}
424
424
  */
425
- export const vecMove = (v, to, delta = 1) =>
426
- vecAdd(v, to.x * delta, to.y * delta)
425
+ export const vecMove = (from, to, delta = 1) =>
426
+ vecAdd(from, to.x * delta, to.y * delta)
427
427
 
428
428
  export const vecIsZero = (v) => vecEq(v, ZERO)
429
429