@litecanvas/utils 0.21.0 → 0.23.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/all.js CHANGED
@@ -980,6 +980,8 @@
980
980
  _x;
981
981
  /** @type {number} */
982
982
  _d;
983
+ /** @type {number} */
984
+ _w;
983
985
  /** @type {(x: number) => number} */
984
986
  _e;
985
987
  /** @type {boolean} */
@@ -1009,48 +1011,43 @@
1009
1011
  this._x = toValue;
1010
1012
  this._d = duration;
1011
1013
  this._e = easing;
1014
+ this._w = 0;
1012
1015
  }
1013
1016
  /**
1014
1017
  * @param {LitecanvasInstance} [engine]
1015
- * @returns {TweenController} this instance
1018
+ * @returns {this}
1016
1019
  */
1017
1020
  start(engine) {
1018
- if (!this.running) {
1019
- this.stop();
1021
+ if (this.running) {
1022
+ return this;
1020
1023
  }
1021
1024
  this._cu.stop(false);
1022
1025
  this._ch = this._cu = this;
1026
+ this.running = true;
1023
1027
  const fromValue = this._o[this._p] || 0;
1024
1028
  const toValue = this._rel ? fromValue + this._x : this._x;
1025
1029
  this._lc = this._lc || engine || globalThis;
1026
1030
  this._u = this._lc.listen("update", (dt) => {
1027
- this._o[this._p] = this._lc.lerp(
1028
- fromValue,
1029
- toValue,
1030
- this._e(this._t / this._d)
1031
- );
1031
+ if (this._t <= this._w) {
1032
+ this._t += dt;
1033
+ return;
1034
+ }
1035
+ const t = this._t - this._w;
1036
+ this._o[this._p] = this._lc.lerp(fromValue, toValue, this._e(t / this._d));
1032
1037
  this._t += dt;
1033
- if (this._t >= this._d) {
1038
+ if (t >= this._d) {
1034
1039
  this._o[this._p] = toValue;
1035
1040
  this.stop();
1036
1041
  }
1037
1042
  });
1038
- this.running = true;
1039
- return this;
1040
- }
1041
- /**
1042
- * @param {Function} callback
1043
- */
1044
- onEnd(callback) {
1045
- this._cb.push(callback);
1046
1043
  return this;
1047
1044
  }
1048
1045
  /**
1049
1046
  * @param {boolean} completed if `false` don't call the `onEnd()` registered callbacks.
1050
- * @returns
1047
+ * @returns {this}
1051
1048
  */
1052
1049
  stop(completed = true) {
1053
- if (!this.running || !this._u) return;
1050
+ if (!this._u) return this;
1054
1051
  this.running = false;
1055
1052
  this._u();
1056
1053
  this._t = 0;
@@ -1061,9 +1058,24 @@
1061
1058
  }
1062
1059
  return this;
1063
1060
  }
1061
+ /**
1062
+ * @param {LitecanvasInstance} [engine]
1063
+ * @returns {this}
1064
+ */
1065
+ restart(engine = null, completed = false) {
1066
+ return this.stop(completed).restart(engine);
1067
+ }
1068
+ /**
1069
+ * @param {Function} callback
1070
+ * @returns {this}
1071
+ */
1072
+ onEnd(callback) {
1073
+ this._cb.push(callback);
1074
+ return this;
1075
+ }
1064
1076
  /**
1065
1077
  * @param {TweenController} another
1066
- * @returns {TweenController} this instance
1078
+ * @returns {this}
1067
1079
  */
1068
1080
  chain(another) {
1069
1081
  this._ch.onEnd(() => {
@@ -1072,24 +1084,46 @@
1072
1084
  this._ch = another;
1073
1085
  return this;
1074
1086
  }
1087
+ /**
1088
+ * @param {boolean} [flag=true]
1089
+ * @returns {this}
1090
+ */
1075
1091
  reset() {
1076
1092
  this._cb.length = 0;
1077
1093
  return this.stop();
1078
1094
  }
1095
+ /**
1096
+ * @param {boolean} [flag=true]
1097
+ * @returns {this}
1098
+ */
1079
1099
  relative(flag = true) {
1080
1100
  this._rel = flag;
1081
1101
  return this;
1082
1102
  }
1103
+ /**
1104
+ * @param {number} value
1105
+ * @returns {this}
1106
+ */
1107
+ delay(value) {
1108
+ this._w = value;
1109
+ return this;
1110
+ }
1083
1111
  /**
1084
1112
  * Returns the current tween of the chain.
1085
1113
  *
1086
- * @returns {TweenController}
1114
+ * @returns {this}
1087
1115
  */
1088
1116
  get current() {
1089
1117
  return this._cu;
1090
1118
  }
1119
+ /**
1120
+ * @returns {number} the current progress (0..1)
1121
+ */
1091
1122
  get progress() {
1092
- return this.running ? this._t / this._d : 0;
1123
+ if (this.running && this._t > this._w) {
1124
+ return (this._t - this._w) / this._d;
1125
+ }
1126
+ return 0;
1093
1127
  }
1094
1128
  };
1095
1129
 
package/dist/all.min.js CHANGED
@@ -1,3 +1,3 @@
1
- (()=>{var Q=Object.defineProperty;var v=(e,t)=>{for(var s in t)Q(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 k={};v(k,{ANCHOR_BOT_LEFT:()=>Ht,ANCHOR_BOT_RIGHT:()=>At,ANCHOR_CENTER:()=>It,ANCHOR_TOP_LEFT:()=>G,ANCHOR_TOP_RIGHT:()=>bt,Actor:()=>C,BACK_IN:()=>kt,BACK_IN_OUT:()=>Pt,BACK_OUT:()=>Lt,BOUNCE_IN:()=>J,BOUNCE_IN_OUT:()=>Bt,BOUNCE_OUT:()=>S,Camera:()=>f,DOWN:()=>Et,EASE_IN:()=>Ct,EASE_IN_OUT:()=>St,EASE_OUT:()=>Dt,ELASTIC_IN:()=>Xt,ELASTIC_IN_OUT:()=>Yt,ELASTIC_OUT:()=>Rt,Grid:()=>g,LEFT:()=>Mt,LINEAR:()=>Z,ONE:()=>mt,RIGHT:()=>wt,TypedGrid:()=>M,UP:()=>Tt,Vector:()=>p,ZERO:()=>F,advance:()=>q,diff:()=>V,fract:()=>$,intersection:()=>d,mod:()=>K,range:()=>z,resolve:()=>P,tween:()=>Ot,vec:()=>n,vecAbs:()=>ft,vecAdd:()=>H,vecAngle:()=>ht,vecAngleBetween:()=>at,vecCeil:()=>pt,vecClamp:()=>dt,vecCross:()=>ct,vecDist:()=>ot,vecDist2:()=>nt,vecDiv:()=>y,vecDot:()=>N,vecEq:()=>b,vecFloor:()=>_t,vecIsZero:()=>yt,vecLerp:()=>lt,vecLimit:()=>rt,vecMag:()=>U,vecMag2:()=>W,vecMove:()=>gt,vecMult:()=>_,vecNorm:()=>O,vecRand:()=>ut,vecReflect:()=>st,vecRotate:()=>et,vecRound:()=>xt,vecSet:()=>B,vecSetMag:()=>it,vecSub:()=>A,wave:()=>j});var d=(e,t,s,i,r,o,h,a)=>{let c=Math.max(e,r),L=Math.min(e+s,r+h)-c,x=Math.max(t,o),T=Math.min(t+i,o+a)-x;return[c,x,L,T]};var P=(e,t,s,i,r,o,h,a)=>{let[c,L,x,T]=d(e,t,s,i,r,o,h,a),u="",w=e,E=t;return x<T?e<r?(u="right",w=r-s):(u="left",w=r+h):t<o?(u="bottom",E=o-i):(u="top",E=o+a),{direction:u,x:w,y:E}};var f=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null,s=0,i=0,r=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=i,this.resize(r||this._engine.WIDTH-s,o||this._engine.HEIGHT-i),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){if(this._engine.push(),t){let r=path();r.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(r)}let s=this.ox+this.width/2,i=this.oy+this.height/2;this._engine.translate(s,i),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}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,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,s=(s-this.height/2-this.oy)/this.scale,i.x=r*t-o*s+this.x,i.y=o*t+r*s+this.y,i}getCameraPoint(t,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=r*t-o*s,s=o*t+r*s,i.x=t*this.scale+this.width/2+this.ox,i.y=s*this.scale+this.height/2+this.oy,i}getBounds(){return[this.ox,this.oy,this.width,this.height]}viewing(t,s,i,r){let o=this.width/2-this.x,h=this.height/2-this.y,a=this.width/this.scale,c=this.height/this.scale;return this._engine.colrect(t,s,i,r,o,h,a,c)}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",i=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=i,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,i=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=i}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new e(this._w,this._h,this._c)}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,i){this._c[this.pointToIndex(t,s)]=i}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}check(t,s){return t>=0&&t<this._w&&s>=0&&s<this._h}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 i=s?this.length-1:0,r=s?-1:this.length,o=s?-1:1;for(;i!==r;){let h=this.indexToPointX(i),a=this.indexToPointY(i),c=this._c[i];if(t(h,a,c,this)===!1)break;i+=o}}fill(t){this.forEach((s,i)=>{this.set(s,i,t)})}clampX(t){return X(t,0,this._w-1)}clampY(t){return X(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let i=[];return this.forEach((r,o,h)=>{i[o]=i[o]||"",i[o]+=h+t}),i.join(`
2
- `)}},M=class e extends g{constructor(t,s,i=Uint8Array){super(t,s,null),this._c=new i(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,i,r)=>{t.set(s,i,r)}),t}};function X(e,t,s){return e<t?t:e>s?s:e}var I=Math.sqrt,R=Math.cos,Y=Math.sin,tt=2*Math.PI,p=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},l=e=>e instanceof p,n=(e=0,t=e)=>(l(e)&&(t=e.y,e=e.x),new p(e,t)),b=(e,t,s=t)=>l(t)?b(e,t.x,t.y):e.x===t&&e.y===s,B=(e,t,s=t)=>(l(t)?B(e,t.x,t.y):(e.x=t,e.y=s),e),H=(e,t,s=t)=>l(t)?H(e,t.x,t.y):(e.x+=t,e.y+=s,e),A=(e,t,s=t)=>l(t)?A(e,t.x,t.y):(e.x-=t,e.y-=s,e),_=(e,t,s=t)=>l(t)?_(e,t.x,t.y):(e.x*=t,e.y*=s,e),y=(e,t,s=t)=>l(t)?y(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),et=(e,t)=>{let s=R(t),i=Y(t);return e.x=s*e.x-i*e.y,e.y=i*e.x+s*e.y,e},st=(e,t)=>{let s=O(n(t));return A(e,_(s,2*N(e,s)))},it=(e,t)=>(O(e),_(e,t),e),U=e=>I(e.x*e.x+e.y*e.y),W=e=>e.x*e.x+e.y*e.y,O=e=>{let t=U(e);return t>0&&y(e,t),e},rt=(e,t=1)=>{let s=W(e);return s>t*t&&(y(e,I(s)),_(e,t)),e},ot=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return I(s*s+i*i)},nt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},ht=e=>Math.atan2(e.y,e.x),at=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),N=(e,t)=>e.x*t.x+e.y*t.y,ct=(e,t)=>e.x*t.y-e.y*t.x,lt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),ut=(e=1,t=e,s=globalThis.rand||Math.random)=>{let i=s()*tt,r=s()*(t-e)+e;return n(R(i)*r,Y(i)*r)},ft=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),pt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),_t=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),xt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),dt=(e,t,s)=>(e.x<t.x&&(e.x=t.x),e.x>s.x&&(e.x=s.x),e.y<t.y&&(e.y=t.y),e.y>s.y&&(e.y=s.y),e),gt=(e,t,s=1)=>H(e,t.x*s,t.y*s),yt=e=>b(e,F),F=n(0,0),mt=n(1,1),Tt=n(0,-1),wt=n(1,0),Et=n(0,1),Mt=n(-1,0);var It=n(.5,.5),G=n(0,0),bt=n(1,0),Ht=n(0,1),At=n(1,1),C=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,i=G){this.sprite=t,this.pos=s||n(0),this._o=n(i),this._s=n(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.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),i=this.sprite.height*(t?this._s.y:1),r=this.pos.x-s*this.anchor.x,o=this.pos.y-i*this.anchor.y;return[r,o,s,i]}draw(t=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&t.push(),this.transform(t),this.drawImage(t),s&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,s=!0){let i=this.anchor,r=-this.sprite.width*(this.flipX?1-i.x:i.x),o=-this.sprite.height*(this.flipY?1-i.y:i.y);s&&t.alpha(this.opacity),t.image(r,o,this.sprite)}};var V=(e,t)=>Math.abs(t-e)||0;var j=(e,t,s,i=Math.sin)=>e+(i(s)+1)/2*(t-e);var $=e=>e%1||0;var z=e=>Array.from(Array(e).keys());var q=advance=(e,t,s,i=1)=>{s&&(t.x+=s.x*i,t.y+=s.y*i),e.x+=t.x*i,e.y+=t.y*i};var K=(e,t)=>(t+e%t)%t;var m=Math.PI/2,Ot=(e,t,s,i=1,r=Z)=>new D(e,t,s,i,r),Z=e=>e,Ct=e=>e*e,Dt=e=>-e*(e-2),St=e=>e<.5?2*e*e:-2*e*e+4*e-1,kt=e=>e*e*e-e*Math.sin(e*Math.PI),Lt=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},Pt=e=>{if(e<.5){let s=2*e;return .5*(s*s*s-s*Math.sin(s*Math.PI))}let t=1-(2*e-1);return .5*(1-(t*t*t-t*Math.sin(e*Math.PI)))+.5},Xt=e=>Math.sin(13*m*e)*Math.pow(2,10*(e-1)),Rt=e=>Math.sin(-13*m*(e+1))*Math.pow(2,-10*e)+1,Yt=e=>{if(e<.5){let i=Math.sin(13*m*(2*e)),r=Math.pow(2,10*(2*e-1));return .5*i*r}let t=Math.sin(-13*m*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},J=e=>1-S(1-e),S=e=>e<4/11?121*e*e/16:e<8/11?363/40*e*e-99/10*e+17/5:e<9/10?4356/361*e*e-35442/1805*e+16061/1805:54/5*e*e-513/25*e+268/25,Bt=e=>e<.5?.5*J(e*2):.5*S(e*2-1)+.5,D=class{running=!1;_o;_p;_x;_d;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,i,r,o){this._o=t,this._p=s,this._x=i,this._d=r,this._e=o}start(t){this.running||this.stop(),this._cu.stop(!1),this._ch=this._cu=this;let s=this._o[this._p]||0,i=this._rel?s+this._x:this._x;return this._lc=this._lc||t||globalThis,this._u=this._lc.listen("update",r=>{this._o[this._p]=this._lc.lerp(s,i,this._e(this._t/this._d)),this._t+=r,this._t>=this._d&&(this._o[this._p]=i,this.stop())}),this.running=!0,this}onEnd(t){return this._cb.push(t),this}stop(t=!0){if(!(!this.running||!this._u)){if(this.running=!1,this._u(),this._t=0,t)for(let s of this._cb)s(this._o);return this}}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}get current(){return this._cu}get progress(){return this.running?this._t/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},k);})();
1
+ (()=>{var Q=Object.defineProperty;var v=(e,t)=>{for(var s in t)Q(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 k={};v(k,{ANCHOR_BOT_LEFT:()=>Ht,ANCHOR_BOT_RIGHT:()=>At,ANCHOR_CENTER:()=>It,ANCHOR_TOP_LEFT:()=>G,ANCHOR_TOP_RIGHT:()=>bt,Actor:()=>S,BACK_IN:()=>kt,BACK_IN_OUT:()=>Pt,BACK_OUT:()=>Lt,BOUNCE_IN:()=>J,BOUNCE_IN_OUT:()=>Bt,BOUNCE_OUT:()=>D,Camera:()=>f,DOWN:()=>Et,EASE_IN:()=>St,EASE_IN_OUT:()=>Dt,EASE_OUT:()=>Ct,ELASTIC_IN:()=>Xt,ELASTIC_IN_OUT:()=>Yt,ELASTIC_OUT:()=>Rt,Grid:()=>g,LEFT:()=>Mt,LINEAR:()=>Z,ONE:()=>mt,RIGHT:()=>wt,TypedGrid:()=>M,UP:()=>Tt,Vector:()=>p,ZERO:()=>F,advance:()=>q,diff:()=>V,fract:()=>$,intersection:()=>x,mod:()=>K,range:()=>z,resolve:()=>P,tween:()=>Ot,vec:()=>n,vecAbs:()=>ft,vecAdd:()=>H,vecAngle:()=>ht,vecAngleBetween:()=>at,vecCeil:()=>pt,vecClamp:()=>xt,vecCross:()=>ct,vecDist:()=>ot,vecDist2:()=>nt,vecDiv:()=>y,vecDot:()=>W,vecEq:()=>b,vecFloor:()=>_t,vecIsZero:()=>yt,vecLerp:()=>lt,vecLimit:()=>rt,vecMag:()=>U,vecMag2:()=>N,vecMove:()=>gt,vecMult:()=>_,vecNorm:()=>O,vecRand:()=>ut,vecReflect:()=>st,vecRotate:()=>et,vecRound:()=>dt,vecSet:()=>B,vecSetMag:()=>it,vecSub:()=>A,wave:()=>j});var x=(e,t,s,i,r,o,h,a)=>{let c=Math.max(e,r),L=Math.min(e+s,r+h)-c,d=Math.max(t,o),T=Math.min(t+i,o+a)-d;return[c,d,L,T]};var P=(e,t,s,i,r,o,h,a)=>{let[c,L,d,T]=x(e,t,s,i,r,o,h,a),u="",w=e,E=t;return d<T?e<r?(u="right",w=r-s):(u="left",w=r+h):t<o?(u="bottom",E=o-i):(u="top",E=o+a),{direction:u,x:w,y:E}};var f=class{_engine=null;x=0;y=0;ox=0;oy=0;width=0;height=0;rotation=0;scale=1;_shake={x:0,y:0,removeListener:null};constructor(t=null,s=0,i=0,r=null,o=null){this._engine=t||globalThis,this.ox=s,this.oy=i,this.resize(r||this._engine.WIDTH-s,o||this._engine.HEIGHT-i),this.x=this.width/2,this.y=this.height/2}resize(t,s){this.width=t,this.height=s,this._engine.emit("camera-resized",this)}start(t=!1){if(this._engine.push(),t){let r=path();r.rect(this.ox,this.oy,this.width,this.height),this._engine.clip(r)}let s=this.ox+this.width/2,i=this.oy+this.height/2;this._engine.translate(s,i),this._engine.scale(this.scale),this._engine.rotate(this.rotation),this._engine.translate(-this.x+this._shake.x,-this.y+this._shake.y)}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,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=(t-this.width/2-this.ox)/this.scale,s=(s-this.height/2-this.oy)/this.scale,i.x=r*t-o*s+this.x,i.y=o*t+r*s+this.y,i}getCameraPoint(t,s,i={}){let r=Math.cos(-this.rotation),o=Math.sin(-this.rotation);return t=t-this.x,s=s-this.y,t=r*t-o*s,s=o*t+r*s,i.x=t*this.scale+this.width/2+this.ox,i.y=s*this.scale+this.height/2+this.oy,i}getBounds(){return[this.ox,this.oy,this.width,this.height]}viewing(t,s,i,r){let o=this.width/2-this.x,h=this.height/2-this.y,a=this.width/this.scale,c=this.height/this.scale;return this._engine.colrect(t,s,i,r,o,h,a,c)}shake(t=1,s=.3){this.shaking||(this._shake.removeListener=this._engine.listen("update",i=>{this._shake.x=this._engine.randi(-t,t),this._shake.y=this._engine.randi(-t,t),s-=i,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,i=[]){this._w=Math.max(1,~~t),this._h=Math.max(1,~~s),this._c=i}[Symbol.iterator](){let t=0;return{next:()=>({value:[this.indexToPointX(t),this.indexToPointY(t),this._c[t++]],done:t>this._c.length})}}clone(){return new e(this._w,this._h,this._c)}clear(){this.forEach((t,s)=>this.set(t,s,void 0))}get width(){return this._w}get height(){return this._h}set(t,s,i){this._c[this.pointToIndex(t,s)]=i}get(t,s){return this._c[this.pointToIndex(t,s)]}has(t,s){return this.get(t,s)!=null}check(t,s){return t>=0&&t<this._w&&s>=0&&s<this._h}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 i=s?this.length-1:0,r=s?-1:this.length,o=s?-1:1;for(;i!==r;){let h=this.indexToPointX(i),a=this.indexToPointY(i),c=this._c[i];if(t(h,a,c,this)===!1)break;i+=o}}fill(t){this.forEach((s,i)=>{this.set(s,i,t)})}clampX(t){return X(t,0,this._w-1)}clampY(t){return X(t,0,this._h-1)}toArray(){return this._c.slice()}toString(t=" ",s=!0){if(!s)return this._c.join(t);let i=[];return this.forEach((r,o,h)=>{i[o]=i[o]||"",i[o]+=h+t}),i.join(`
2
+ `)}},M=class e extends g{constructor(t,s,i=Uint8Array){super(t,s,null),this._c=new i(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,i,r)=>{t.set(s,i,r)}),t}};function X(e,t,s){return e<t?t:e>s?s:e}var I=Math.sqrt,R=Math.cos,Y=Math.sin,tt=2*Math.PI,p=class{x;y;constructor(t=0,s=t){this.x=t,this.y=s}toString(){return`Vector (${this.x}, ${this.y})`}},l=e=>e instanceof p,n=(e=0,t=e)=>(l(e)&&(t=e.y,e=e.x),new p(e,t)),b=(e,t,s=t)=>l(t)?b(e,t.x,t.y):e.x===t&&e.y===s,B=(e,t,s=t)=>(l(t)?B(e,t.x,t.y):(e.x=t,e.y=s),e),H=(e,t,s=t)=>l(t)?H(e,t.x,t.y):(e.x+=t,e.y+=s,e),A=(e,t,s=t)=>l(t)?A(e,t.x,t.y):(e.x-=t,e.y-=s,e),_=(e,t,s=t)=>l(t)?_(e,t.x,t.y):(e.x*=t,e.y*=s,e),y=(e,t,s=t)=>l(t)?y(e,t.x,t.y):(e.x/=t||1,e.y/=s||1,e),et=(e,t)=>{let s=R(t),i=Y(t);return e.x=s*e.x-i*e.y,e.y=i*e.x+s*e.y,e},st=(e,t)=>{let s=O(n(t));return A(e,_(s,2*W(e,s)))},it=(e,t)=>(O(e),_(e,t),e),U=e=>I(e.x*e.x+e.y*e.y),N=e=>e.x*e.x+e.y*e.y,O=e=>{let t=U(e);return t>0&&y(e,t),e},rt=(e,t=1)=>{let s=N(e);return s>t*t&&(y(e,I(s)),_(e,t)),e},ot=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return I(s*s+i*i)},nt=(e,t)=>{let s=e.x-t.x,i=e.y-t.y;return s*s+i*i},ht=e=>Math.atan2(e.y,e.x),at=(e,t)=>Math.atan2(t.y-e.y,t.x-e.x),W=(e,t)=>e.x*t.x+e.y*t.y,ct=(e,t)=>e.x*t.y-e.y*t.x,lt=(e,t,s)=>(e.x+=(t.x-e.x)*s||0,e.y+=(t.y-e.y)*s||0,e),ut=(e=1,t=e,s=globalThis.rand||Math.random)=>{let i=s()*tt,r=s()*(t-e)+e;return n(R(i)*r,Y(i)*r)},ft=e=>(e.x=Math.abs(e.x),e.y=Math.abs(e.y),e),pt=e=>(e.x=Math.ceil(e.x),e.y=Math.ceil(e.y),e),_t=e=>(e.x=Math.floor(e.x),e.y=Math.floor(e.y),e),dt=e=>(e.x=Math.round(e.x),e.y=Math.round(e.y),e),xt=(e,t,s)=>(e.x<t.x&&(e.x=t.x),e.x>s.x&&(e.x=s.x),e.y<t.y&&(e.y=t.y),e.y>s.y&&(e.y=s.y),e),gt=(e,t,s=1)=>H(e,t.x*s,t.y*s),yt=e=>b(e,F),F=n(0,0),mt=n(1,1),Tt=n(0,-1),wt=n(1,0),Et=n(0,1),Mt=n(-1,0);var It=n(.5,.5),G=n(0,0),bt=n(1,0),Ht=n(0,1),At=n(1,1),S=class{sprite;pos;_o;_s;flipX=!1;flipY=!1;angle=0;opacity=1;hidden=!1;constructor(t,s,i=G){this.sprite=t,this.pos=s||n(0),this._o=n(i),this._s=n(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.x}get height(){return this.sprite.height*this._s.y}get scale(){return this._s}scaleTo(t,s=t){this._s.x=t,this._s.y=s}scaleBy(t,s=t){this._s.x*=t,this._s.y*=s}getBounds(t=!0){let s=this.sprite.width*(t?this._s.x:1),i=this.sprite.height*(t?this._s.y:1),r=this.pos.x-s*this.anchor.x,o=this.pos.y-i*this.anchor.y;return[r,o,s,i]}draw(t=globalThis,s=!0){this.hidden||this.opacity<=0||(s&&t.push(),this.transform(t),this.drawImage(t),s&&t.pop())}transform(t){t.translate(this.pos.x,this.pos.y),t.rotate(t.deg2rad(this.angle)),t.scale((this.flipX?-1:1)*this._s.x,(this.flipY?-1:1)*this._s.y)}drawImage(t,s=!0){let i=this.anchor,r=-this.sprite.width*(this.flipX?1-i.x:i.x),o=-this.sprite.height*(this.flipY?1-i.y:i.y);s&&t.alpha(this.opacity),t.image(r,o,this.sprite)}};var V=(e,t)=>Math.abs(t-e)||0;var j=(e,t,s,i=Math.sin)=>e+(i(s)+1)/2*(t-e);var $=e=>e%1||0;var z=e=>Array.from(Array(e).keys());var q=advance=(e,t,s,i=1)=>{s&&(t.x+=s.x*i,t.y+=s.y*i),e.x+=t.x*i,e.y+=t.y*i};var K=(e,t)=>(t+e%t)%t;var m=Math.PI/2,Ot=(e,t,s,i=1,r=Z)=>new C(e,t,s,i,r),Z=e=>e,St=e=>e*e,Ct=e=>-e*(e-2),Dt=e=>e<.5?2*e*e:-2*e*e+4*e-1,kt=e=>e*e*e-e*Math.sin(e*Math.PI),Lt=e=>{let t=1-e;return 1-(t*t*t-t*Math.sin(t*Math.PI))},Pt=e=>{if(e<.5){let s=2*e;return .5*(s*s*s-s*Math.sin(s*Math.PI))}let t=1-(2*e-1);return .5*(1-(t*t*t-t*Math.sin(e*Math.PI)))+.5},Xt=e=>Math.sin(13*m*e)*Math.pow(2,10*(e-1)),Rt=e=>Math.sin(-13*m*(e+1))*Math.pow(2,-10*e)+1,Yt=e=>{if(e<.5){let i=Math.sin(13*m*(2*e)),r=Math.pow(2,10*(2*e-1));return .5*i*r}let t=Math.sin(-13*m*(2*e-1+1)),s=Math.pow(2,-10*(2*e-1));return .5*(t*s+2)},J=e=>1-D(1-e),D=e=>e<4/11?121*e*e/16:e<8/11?363/40*e*e-99/10*e+17/5:e<9/10?4356/361*e*e-35442/1805*e+16061/1805:54/5*e*e-513/25*e+268/25,Bt=e=>e<.5?.5*J(e*2):.5*D(e*2-1)+.5,C=class{running=!1;_o;_p;_x;_d;_w;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(t,s,i,r,o){this._o=t,this._p=s,this._x=i,this._d=r,this._e=o,this._w=0}start(t){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||t||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(t=!0){if(!this._u)return this;if(this.running=!1,this._u(),this._t=0,t)for(let s of this._cb)s(this._o);return this}restart(t=null,s=!1){return this.stop(s).restart(t)}onEnd(t){return this._cb.push(t),this}chain(t){return this._ch.onEnd(()=>{this._cu=t.start(this._lc)}),this._ch=t,this}reset(){return this._cb.length=0,this.stop()}relative(t=!0){return this._rel=t,this}delay(t){return this._w=t,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||{},k);})();
3
3
  /*! @litecanvas/utils by Luiz Bills | MIT Licensed */
package/dist/tween.js CHANGED
@@ -102,6 +102,8 @@
102
102
  _x;
103
103
  /** @type {number} */
104
104
  _d;
105
+ /** @type {number} */
106
+ _w;
105
107
  /** @type {(x: number) => number} */
106
108
  _e;
107
109
  /** @type {boolean} */
@@ -131,48 +133,43 @@
131
133
  this._x = toValue;
132
134
  this._d = duration;
133
135
  this._e = easing;
136
+ this._w = 0;
134
137
  }
135
138
  /**
136
139
  * @param {LitecanvasInstance} [engine]
137
- * @returns {TweenController} this instance
140
+ * @returns {this}
138
141
  */
139
142
  start(engine) {
140
- if (!this.running) {
141
- this.stop();
143
+ if (this.running) {
144
+ return this;
142
145
  }
143
146
  this._cu.stop(false);
144
147
  this._ch = this._cu = this;
148
+ this.running = true;
145
149
  const fromValue = this._o[this._p] || 0;
146
150
  const toValue = this._rel ? fromValue + this._x : this._x;
147
151
  this._lc = this._lc || engine || globalThis;
148
152
  this._u = this._lc.listen("update", (dt) => {
149
- this._o[this._p] = this._lc.lerp(
150
- fromValue,
151
- toValue,
152
- this._e(this._t / this._d)
153
- );
153
+ if (this._t <= this._w) {
154
+ this._t += dt;
155
+ return;
156
+ }
157
+ const t = this._t - this._w;
158
+ this._o[this._p] = this._lc.lerp(fromValue, toValue, this._e(t / this._d));
154
159
  this._t += dt;
155
- if (this._t >= this._d) {
160
+ if (t >= this._d) {
156
161
  this._o[this._p] = toValue;
157
162
  this.stop();
158
163
  }
159
164
  });
160
- this.running = true;
161
- return this;
162
- }
163
- /**
164
- * @param {Function} callback
165
- */
166
- onEnd(callback) {
167
- this._cb.push(callback);
168
165
  return this;
169
166
  }
170
167
  /**
171
168
  * @param {boolean} completed if `false` don't call the `onEnd()` registered callbacks.
172
- * @returns
169
+ * @returns {this}
173
170
  */
174
171
  stop(completed = true) {
175
- if (!this.running || !this._u) return;
172
+ if (!this._u) return this;
176
173
  this.running = false;
177
174
  this._u();
178
175
  this._t = 0;
@@ -183,9 +180,24 @@
183
180
  }
184
181
  return this;
185
182
  }
183
+ /**
184
+ * @param {LitecanvasInstance} [engine]
185
+ * @returns {this}
186
+ */
187
+ restart(engine = null, completed = false) {
188
+ return this.stop(completed).restart(engine);
189
+ }
190
+ /**
191
+ * @param {Function} callback
192
+ * @returns {this}
193
+ */
194
+ onEnd(callback) {
195
+ this._cb.push(callback);
196
+ return this;
197
+ }
186
198
  /**
187
199
  * @param {TweenController} another
188
- * @returns {TweenController} this instance
200
+ * @returns {this}
189
201
  */
190
202
  chain(another) {
191
203
  this._ch.onEnd(() => {
@@ -194,24 +206,46 @@
194
206
  this._ch = another;
195
207
  return this;
196
208
  }
209
+ /**
210
+ * @param {boolean} [flag=true]
211
+ * @returns {this}
212
+ */
197
213
  reset() {
198
214
  this._cb.length = 0;
199
215
  return this.stop();
200
216
  }
217
+ /**
218
+ * @param {boolean} [flag=true]
219
+ * @returns {this}
220
+ */
201
221
  relative(flag = true) {
202
222
  this._rel = flag;
203
223
  return this;
204
224
  }
225
+ /**
226
+ * @param {number} value
227
+ * @returns {this}
228
+ */
229
+ delay(value) {
230
+ this._w = value;
231
+ return this;
232
+ }
205
233
  /**
206
234
  * Returns the current tween of the chain.
207
235
  *
208
- * @returns {TweenController}
236
+ * @returns {this}
209
237
  */
210
238
  get current() {
211
239
  return this._cu;
212
240
  }
241
+ /**
242
+ * @returns {number} the current progress (0..1)
243
+ */
213
244
  get progress() {
214
- return this.running ? this._t / this._d : 0;
245
+ if (this.running && this._t > this._w) {
246
+ return (this._t - this._w) / this._d;
247
+ }
248
+ return 0;
215
249
  }
216
250
  };
217
251
 
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 l={};p(l,{BACK_IN:()=>E,BACK_IN_OUT:()=>m,BACK_OUT:()=>x,BOUNCE_IN:()=>f,BOUNCE_IN_OUT:()=>H,BOUNCE_OUT:()=>n,EASE_IN:()=>d,EASE_IN_OUT:()=>T,EASE_OUT:()=>g,ELASTIC_IN:()=>I,ELASTIC_IN_OUT:()=>y,ELASTIC_OUT:()=>b,LINEAR:()=>c,tween:()=>_});var r=Math.PI/2,_=(t,e,s,i=1,o=c)=>new a(t,e,s,i,o),c=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*r*t)*Math.pow(2,10*(t-1)),b=t=>Math.sin(-13*r*(t+1))*Math.pow(2,-10*t)+1,y=t=>{if(t<.5){let i=Math.sin(13*r*(2*t)),o=Math.pow(2,10*(2*t-1));return .5*i*o}let e=Math.sin(-13*r*(2*t-1+1)),s=Math.pow(2,-10*(2*t-1));return .5*(e*s+2)},f=t=>1-n(1-t),n=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,H=t=>t<.5?.5*f(t*2):.5*n(t*2-1)+.5,a=class{running=!1;_o;_p;_x;_d;_e;_rel;_cb=[];_t=0;_u=0;_ch=this;_cu=this;_lc;constructor(e,s,i,o,u){this._o=e,this._p=s,this._x=i,this._d=o,this._e=u}start(e){this.running||this.stop(),this._cu.stop(!1),this._ch=this._cu=this;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",o=>{this._o[this._p]=this._lc.lerp(s,i,this._e(this._t/this._d)),this._t+=o,this._t>=this._d&&(this._o[this._p]=i,this.stop())}),this.running=!0,this}onEnd(e){return this._cb.push(e),this}stop(e=!0){if(!(!this.running||!this._u)){if(this.running=!1,this._u(),this._t=0,e)for(let s of this._cb)s(this._o);return 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}get current(){return this._cu}get progress(){return this.running?this._t/this._d:0}};globalThis.utils=Object.assign(globalThis.utils||{},l);})();
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);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litecanvas/utils",
3
- "version": "0.21.0",
3
+ "version": "0.23.0",
4
4
  "description": "Utilities to help build litecanvas games",
5
5
  "author": "Luiz Bills <luizbills@pm.me>",
6
6
  "license": "MIT",
@@ -36,6 +36,7 @@
36
36
  "litecanvas": "*"
37
37
  },
38
38
  "devDependencies": {
39
- "esbuild": "^0.23.1"
39
+ "esbuild": "^0.23.1",
40
+ "gzip-size": "^7.0.0"
40
41
  }
41
42
  }
@@ -59,7 +59,7 @@ function draw() {
59
59
 
60
60
  Starts the animation.
61
61
 
62
- Syntax: `.start(engine?: LitecanvasInstance): TweenController`
62
+ Syntax: `.start(engine?: LitecanvasInstance): this`
63
63
 
64
64
  ```js
65
65
  // if your litecanvas has config.global = false
@@ -89,7 +89,7 @@ function init () {
89
89
 
90
90
  If enabled (`flag = true`) the tween we animate from a value to another relative value.
91
91
 
92
- Syntax: `.relative(flag?: boolean = true): void`
92
+ Syntax: `.relative(flag?: boolean = true): this`
93
93
 
94
94
  ```js
95
95
  const obj = { x: 0 }
@@ -113,7 +113,7 @@ tween(obj, "x", -100).relative()
113
113
 
114
114
  Make another tween start right after this tween ends.
115
115
 
116
- Syntax: `.relative(another: TweenController): TweenController`
116
+ Syntax: `.relative(another: TweenController): this`
117
117
 
118
118
  ```js
119
119
  const obj = { x: 0, angle: 0 }
@@ -131,7 +131,7 @@ moveRight.start()
131
131
 
132
132
  Enqueues a callback to be executed when the animation finishes.
133
133
 
134
- Syntax: `.onEnd(callback?: (object:any) => void): void`
134
+ Syntax: `.onEnd(callback?: (object:any) => void): this`
135
135
 
136
136
  ```js
137
137
  // lets imagine a animation
@@ -143,11 +143,17 @@ anim.onEnd(() => {
143
143
  })
144
144
  ```
145
145
 
146
+ ### TweenController#restart()
147
+
148
+ Stops and start again the animation.
149
+
150
+ Syntax: `.restart(engine?: LitecanvasInstance, completed: boolean = false): this`
151
+
146
152
  ### TweenController#stop()
147
153
 
148
154
  Stops the animation.
149
155
 
150
- Syntax: `.stop(complete?: boolean): void`
156
+ Syntax: `.stop(completed: boolean = true): this`
151
157
 
152
158
  ```js
153
159
  // lets imagine a animation with 5 seconds of duration
@@ -167,6 +173,8 @@ anim.stop(false)
167
173
 
168
174
  Stops the animation and remove all `.onEnd()` registered callbacks.
169
175
 
176
+ Syntax: `.reset(): this`
177
+
170
178
  ### TweenController#progress
171
179
 
172
180
  Returns the percentage of the animation's progress, a number between `0.0` and `1.0`. Where `0` represents 0% and `1` represents 100%.
@@ -8,7 +8,7 @@ const HALF_PI = Math.PI / 2
8
8
  * @param {number|number} toValue
9
9
  * @param {number} [duration]
10
10
  * @param {(n: number) => number} [easing]
11
- * @returns {TweenController}
11
+ * @returns {this}
12
12
  */
13
13
  export const tween = (object, prop, toValue, duration = 1, easing = LINEAR) => {
14
14
  return new TweenController(object, prop, toValue, duration, easing)
@@ -86,6 +86,8 @@ class TweenController {
86
86
  _x
87
87
  /** @type {number} */
88
88
  _d
89
+ /** @type {number} */
90
+ _w
89
91
  /** @type {(x: number) => number} */
90
92
  _e
91
93
  /** @type {boolean} */
@@ -117,19 +119,21 @@ class TweenController {
117
119
  this._x = toValue
118
120
  this._d = duration
119
121
  this._e = easing
122
+ this._w = 0
120
123
  }
121
124
 
122
125
  /**
123
126
  * @param {LitecanvasInstance} [engine]
124
- * @returns {TweenController} this instance
127
+ * @returns {this}
125
128
  */
126
129
  start(engine) {
127
- if (!this.running) {
128
- this.stop()
130
+ if (this.running) {
131
+ return this
129
132
  }
130
133
 
131
134
  this._cu.stop(false)
132
135
  this._ch = this._cu = this
136
+ this.running = true
133
137
 
134
138
  const fromValue = this._o[this._p] || 0
135
139
  const toValue = this._rel ? fromValue + this._x : this._x
@@ -137,37 +141,31 @@ class TweenController {
137
141
  this._lc = this._lc || engine || globalThis
138
142
 
139
143
  this._u = this._lc.listen("update", (dt) => {
140
- this._o[this._p] = this._lc.lerp(
141
- fromValue,
142
- toValue,
143
- this._e(this._t / this._d)
144
- )
144
+ if (this._t <= this._w) {
145
+ this._t += dt
146
+ return
147
+ }
148
+
149
+ const t = this._t - this._w
150
+ this._o[this._p] = this._lc.lerp(fromValue, toValue, this._e(t / this._d))
151
+
145
152
  this._t += dt
146
- if (this._t >= this._d) {
153
+
154
+ if (t >= this._d) {
147
155
  this._o[this._p] = toValue
148
156
  this.stop()
149
157
  }
150
158
  })
151
159
 
152
- this.running = true
153
-
154
- return this
155
- }
156
-
157
- /**
158
- * @param {Function} callback
159
- */
160
- onEnd(callback) {
161
- this._cb.push(callback)
162
160
  return this
163
161
  }
164
162
 
165
163
  /**
166
164
  * @param {boolean} completed if `false` don't call the `onEnd()` registered callbacks.
167
- * @returns
165
+ * @returns {this}
168
166
  */
169
167
  stop(completed = true) {
170
- if (!this.running || !this._u) return
168
+ if (!this._u) return this
171
169
 
172
170
  this.running = false
173
171
 
@@ -183,9 +181,26 @@ class TweenController {
183
181
  return this
184
182
  }
185
183
 
184
+ /**
185
+ * @param {LitecanvasInstance} [engine]
186
+ * @returns {this}
187
+ */
188
+ restart(engine = null, completed = false) {
189
+ return this.stop(completed).restart(engine)
190
+ }
191
+
192
+ /**
193
+ * @param {Function} callback
194
+ * @returns {this}
195
+ */
196
+ onEnd(callback) {
197
+ this._cb.push(callback)
198
+ return this
199
+ }
200
+
186
201
  /**
187
202
  * @param {TweenController} another
188
- * @returns {TweenController} this instance
203
+ * @returns {this}
189
204
  */
190
205
  chain(another) {
191
206
  this._ch.onEnd(() => {
@@ -195,26 +210,49 @@ class TweenController {
195
210
  return this
196
211
  }
197
212
 
213
+ /**
214
+ * @param {boolean} [flag=true]
215
+ * @returns {this}
216
+ */
198
217
  reset() {
199
218
  this._cb.length = 0
200
219
  return this.stop()
201
220
  }
202
221
 
222
+ /**
223
+ * @param {boolean} [flag=true]
224
+ * @returns {this}
225
+ */
203
226
  relative(flag = true) {
204
227
  this._rel = flag
205
228
  return this
206
229
  }
207
230
 
231
+ /**
232
+ * @param {number} value
233
+ * @returns {this}
234
+ */
235
+ delay(value) {
236
+ this._w = value
237
+ return this
238
+ }
239
+
208
240
  /**
209
241
  * Returns the current tween of the chain.
210
242
  *
211
- * @returns {TweenController}
243
+ * @returns {this}
212
244
  */
213
245
  get current() {
214
246
  return this._cu
215
247
  }
216
248
 
249
+ /**
250
+ * @returns {number} the current progress (0..1)
251
+ */
217
252
  get progress() {
218
- return this.running ? this._t / this._d : 0
253
+ if (this.running && this._t > this._w) {
254
+ return (this._t - this._w) / this._d
255
+ }
256
+ return 0
219
257
  }
220
258
  }