litecanvas 0.202.0 → 0.204.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/dist.dev.js +34 -19
- package/dist/dist.js +12 -11
- package/dist/dist.min.js +1 -1
- package/package.json +8 -8
- package/src/index.js +49 -26
- package/src/version.js +1 -1
- package/types/global.d.ts +7 -5
- package/types/types.d.ts +8 -6
package/dist/dist.dev.js
CHANGED
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
var assert = (condition, message = "Assertion failed") => {
|
|
116
116
|
if (!condition) throw new Error("[litecanvas] " + message);
|
|
117
117
|
};
|
|
118
|
-
var version = "0.
|
|
118
|
+
var version = "0.204.0";
|
|
119
119
|
function litecanvas(settings = {}) {
|
|
120
120
|
const root = window,
|
|
121
121
|
math = Math,
|
|
@@ -192,11 +192,11 @@
|
|
|
192
192
|
return start + t * (end - start);
|
|
193
193
|
},
|
|
194
194
|
deg2rad: (degs) => {
|
|
195
|
-
DEV: assert(isNumber(degs), "deg2rad
|
|
195
|
+
DEV: assert(isNumber(degs), "deg2rad() 1st param must be a number");
|
|
196
196
|
return (math.PI / 180) * degs;
|
|
197
197
|
},
|
|
198
198
|
rad2deg: (rads) => {
|
|
199
|
-
DEV: assert(isNumber(rads), "rad2deg
|
|
199
|
+
DEV: assert(isNumber(rads), "rad2deg() 1st param must be a number");
|
|
200
200
|
return (180 / math.PI) * rads;
|
|
201
201
|
},
|
|
202
202
|
round: (n, precision = 0) => {
|
|
@@ -646,6 +646,10 @@
|
|
|
646
646
|
}
|
|
647
647
|
},
|
|
648
648
|
textgap(value) {
|
|
649
|
+
DEV: assert(
|
|
650
|
+
isNumber(value),
|
|
651
|
+
loggerPrefix + "textgap() 1st param must be a number",
|
|
652
|
+
);
|
|
649
653
|
_fontLineHeight = value;
|
|
650
654
|
},
|
|
651
655
|
textfont(family) {
|
|
@@ -740,8 +744,10 @@
|
|
|
740
744
|
loggerPrefix + "paint() 3rd param must be a function",
|
|
741
745
|
);
|
|
742
746
|
DEV: assert(
|
|
743
|
-
(options && null == options.scale) ||
|
|
744
|
-
|
|
747
|
+
(options && null == options.scale) ||
|
|
748
|
+
(isNumber(options.scale) && options.scale > 0),
|
|
749
|
+
loggerPrefix +
|
|
750
|
+
"paint() 4th param (options.scale) must be a positive number",
|
|
745
751
|
);
|
|
746
752
|
DEV: assert(
|
|
747
753
|
(options && null == options.canvas) ||
|
|
@@ -761,6 +767,13 @@
|
|
|
761
767
|
return canvas.transferToImageBitmap();
|
|
762
768
|
},
|
|
763
769
|
ctx(context) {
|
|
770
|
+
DEV: assert(
|
|
771
|
+
null == context ||
|
|
772
|
+
context instanceof CanvasRenderingContext2D ||
|
|
773
|
+
context instanceof OffscreenCanvasRenderingContext2D,
|
|
774
|
+
loggerPrefix +
|
|
775
|
+
"ctx() 1st param must be an [Offscreen]CanvasRenderingContext2D",
|
|
776
|
+
);
|
|
764
777
|
if (context) {
|
|
765
778
|
_ctx = context;
|
|
766
779
|
}
|
|
@@ -783,16 +796,16 @@
|
|
|
783
796
|
);
|
|
784
797
|
_ctx.translate(~~x, ~~y);
|
|
785
798
|
},
|
|
786
|
-
scale(x, y) {
|
|
799
|
+
scale(x, y = x) {
|
|
787
800
|
DEV: assert(
|
|
788
801
|
isNumber(x),
|
|
789
802
|
loggerPrefix + "scale() 1st param must be a number",
|
|
790
803
|
);
|
|
791
804
|
DEV: assert(
|
|
792
|
-
|
|
805
|
+
isNumber(y),
|
|
793
806
|
loggerPrefix + "scale() 2nd param must be a number",
|
|
794
807
|
);
|
|
795
|
-
_ctx.scale(x, y
|
|
808
|
+
_ctx.scale(x, y);
|
|
796
809
|
},
|
|
797
810
|
rotate(radians) {
|
|
798
811
|
DEV: assert(
|
|
@@ -827,7 +840,7 @@
|
|
|
827
840
|
clip(callback) {
|
|
828
841
|
DEV: assert(
|
|
829
842
|
"function" === typeof callback,
|
|
830
|
-
loggerPrefix + "clip() 1st param must be a function",
|
|
843
|
+
loggerPrefix + "clip() 1st param must be a function (ctx) => void",
|
|
831
844
|
);
|
|
832
845
|
beginPath(_ctx);
|
|
833
846
|
callback(_ctx);
|
|
@@ -872,7 +885,8 @@
|
|
|
872
885
|
use(callback, config = {}) {
|
|
873
886
|
DEV: assert(
|
|
874
887
|
"function" === typeof callback,
|
|
875
|
-
loggerPrefix +
|
|
888
|
+
loggerPrefix +
|
|
889
|
+
"use() 1st param must be a function (instance, config) => any",
|
|
876
890
|
);
|
|
877
891
|
DEV: assert(
|
|
878
892
|
"object" === typeof config,
|
|
@@ -921,6 +935,7 @@
|
|
|
921
935
|
triggerEvent(eventName, arg1, arg2, arg3, arg4);
|
|
922
936
|
triggerEvent("after:" + eventName, arg1, arg2, arg3, arg4);
|
|
923
937
|
}
|
|
938
|
+
return arg1;
|
|
924
939
|
},
|
|
925
940
|
pal(colors, textColor = 3) {
|
|
926
941
|
DEV: assert(
|
|
@@ -1011,8 +1026,11 @@
|
|
|
1011
1026
|
return internals[index];
|
|
1012
1027
|
},
|
|
1013
1028
|
pause() {
|
|
1014
|
-
_paused
|
|
1015
|
-
|
|
1029
|
+
if (!_paused) {
|
|
1030
|
+
_paused = true;
|
|
1031
|
+
cancelAnimationFrame(_rafid);
|
|
1032
|
+
instance.emit("paused");
|
|
1033
|
+
}
|
|
1016
1034
|
},
|
|
1017
1035
|
resume() {
|
|
1018
1036
|
DEV: assert(
|
|
@@ -1025,13 +1043,14 @@
|
|
|
1025
1043
|
_accumulated = _fpsInterval;
|
|
1026
1044
|
_lastFrameTime = perf.now();
|
|
1027
1045
|
_rafid = raf(drawFrame);
|
|
1046
|
+
instance.emit("resumed");
|
|
1028
1047
|
}
|
|
1029
1048
|
},
|
|
1030
|
-
|
|
1049
|
+
ispaused() {
|
|
1031
1050
|
return _paused;
|
|
1032
1051
|
},
|
|
1033
1052
|
quit() {
|
|
1034
|
-
instance.emit("
|
|
1053
|
+
instance.emit("shutdown");
|
|
1035
1054
|
instance.pause();
|
|
1036
1055
|
_initialized = false;
|
|
1037
1056
|
_eventListeners = {};
|
|
@@ -1327,11 +1346,7 @@
|
|
|
1327
1346
|
instance.listen(eventName, settings.loop[eventName]);
|
|
1328
1347
|
}
|
|
1329
1348
|
}
|
|
1330
|
-
|
|
1331
|
-
on(root, "DOMContentLoaded", () => raf(init));
|
|
1332
|
-
} else {
|
|
1333
|
-
_rafid = raf(init);
|
|
1334
|
-
}
|
|
1349
|
+
_rafid = raf(init);
|
|
1335
1350
|
return instance;
|
|
1336
1351
|
}
|
|
1337
1352
|
window.litecanvas = litecanvas;
|
package/dist/dist.js
CHANGED
|
@@ -363,8 +363,8 @@
|
|
|
363
363
|
translate(x, y) {
|
|
364
364
|
_ctx.translate(~~x, ~~y);
|
|
365
365
|
},
|
|
366
|
-
scale(x, y) {
|
|
367
|
-
_ctx.scale(x, y
|
|
366
|
+
scale(x, y = x) {
|
|
367
|
+
_ctx.scale(x, y);
|
|
368
368
|
},
|
|
369
369
|
rotate(radians) {
|
|
370
370
|
_ctx.rotate(radians);
|
|
@@ -429,6 +429,7 @@
|
|
|
429
429
|
triggerEvent(eventName, arg1, arg2, arg3, arg4);
|
|
430
430
|
triggerEvent("after:" + eventName, arg1, arg2, arg3, arg4);
|
|
431
431
|
}
|
|
432
|
+
return arg1;
|
|
432
433
|
},
|
|
433
434
|
pal(colors, textColor = 3) {
|
|
434
435
|
_colorPalette = colors || defaultPalette;
|
|
@@ -474,8 +475,11 @@
|
|
|
474
475
|
return internals[index];
|
|
475
476
|
},
|
|
476
477
|
pause() {
|
|
477
|
-
_paused
|
|
478
|
-
|
|
478
|
+
if (!_paused) {
|
|
479
|
+
_paused = true;
|
|
480
|
+
cancelAnimationFrame(_rafid);
|
|
481
|
+
instance.emit("paused");
|
|
482
|
+
}
|
|
479
483
|
},
|
|
480
484
|
resume() {
|
|
481
485
|
if (_initialized && _paused) {
|
|
@@ -483,13 +487,14 @@
|
|
|
483
487
|
_accumulated = _fpsInterval;
|
|
484
488
|
_lastFrameTime = perf.now();
|
|
485
489
|
_rafid = raf(drawFrame);
|
|
490
|
+
instance.emit("resumed");
|
|
486
491
|
}
|
|
487
492
|
},
|
|
488
|
-
|
|
493
|
+
ispaused() {
|
|
489
494
|
return _paused;
|
|
490
495
|
},
|
|
491
496
|
quit() {
|
|
492
|
-
instance.emit("
|
|
497
|
+
instance.emit("shutdown");
|
|
493
498
|
instance.pause();
|
|
494
499
|
_initialized = false;
|
|
495
500
|
_eventListeners = {};
|
|
@@ -732,11 +737,7 @@
|
|
|
732
737
|
instance.listen(eventName, settings.loop[eventName]);
|
|
733
738
|
}
|
|
734
739
|
}
|
|
735
|
-
|
|
736
|
-
on(root, "DOMContentLoaded", () => raf(init));
|
|
737
|
-
} else {
|
|
738
|
-
_rafid = raf(init);
|
|
739
|
-
}
|
|
740
|
+
_rafid = raf(init);
|
|
740
741
|
return instance;
|
|
741
742
|
}
|
|
742
743
|
window.litecanvas = litecanvas;
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let a
|
|
1
|
+
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let l,a=window,n=Math,i=performance,o=2*n.PI,r=requestAnimationFrame,s=[],f=(e,t,l)=>{e.addEventListener(t,l,!1),s.push(()=>e.removeEventListener(t,l,!1))},d=(l=new AudioContext,a.zzfxV=1,(e=1,t=.05,n=220,i=0,o=0,r=.1,s=0,f=1,d=0,c=0,p=0,u=0,h=0,m=0,g=0,w=0,v=0,x=1,y=0,b=0,k=0)=>{let E=Math,P=2*E.PI,T=d*=500*P/44100/44100,z=n*=(1-t+2*t*E.random(t=[]))*P/44100,C=0,I=0,L=0,D=1,A=0,S=0,H=0,M=k<0?-1:1,N=P*M*k*2/44100,W=E.cos(N),B=E.sin,V=B(N)/4,q=1+V,O=-2*W/q,R=(1-V)/q,F=(1+M*W)/2/q,G=-(M+W)/q,X=0,Y=0,$=0,j=0;for(i=44100*i+9,y*=44100,o*=44100,r*=44100,v*=44100,c*=500*P/85766121e6,g*=P/44100,p*=P/44100,u*=44100,h=44100*h|0,e*=.3*a.zzfxV,M=i+y+o+r+v|0;L<M;t[L++]=H*e)++S%(100*w|0)||(H=s?1<s?2<s?3<s?B(C*C):E.max(E.min(E.tan(C),1),-1):1-(2*C/P%2+2)%2:1-4*E.abs(E.round(C/P)-C/P):B(C),H=(h?1-b+b*B(P*L/h):1)*(H<0?-1:1)*E.abs(H)**f*(L<i?L/i:L<i+y?1-(L-i)/y*(1-x):L<i+y+o?x:L<M-v?(M-L-v)/r*x:0),H=v?H/2+(v>L?0:(L<M-v?1:(M-L)/v)*t[L-v|0]/2/e):H,k&&(H=j=F*X+G*(X=Y)+F*(Y=H)-R*$-O*($=j))),C+=(N=(n+=d+=c)*E.cos(g*I++))+N*m*B(L**5),D&&++D>u&&(n+=p,z+=p,D=0),!h||++A%h||(n=z,d=T,D=D||1);(e=l.createBuffer(1,M,44100)).getChannelData(0).set(t),(n=l.createBufferSource()).buffer=e,n.connect(l.destination),n.start()});t=Object.assign({width:null,height:null,autoscale:!0,canvas:null,global:!0,loop:null,tapEvents:!0,keyboardEvents:!0},t);let c=!1,p=!0,u,h=1,m,g=.5,w=1,v,x=1e3/60,y,b,k=3,E="sans-serif",P=20,T=1.2,z=Date.now(),C=e,I=[],L=[.5,0,1750,,,.3,1,,,,600,.1],D={},A={W:0,H:0,T:0,MX:-1,MY:-1,TWO_PI:o,HALF_PI:o/4,lerp:(e,t,l)=>e+l*(t-e),deg2rad:e=>n.PI/180*e,rad2deg:e=>180/n.PI*e,round:(e,t=0)=>{if(!t)return n.round(e);let l=10**t;return n.round(e*l)/l},clamp:(e,t,l)=>e<t?t:e>l?l:e,dist:(e,t,l,a)=>n.hypot(l-e,a-t),wrap:(e,t,l)=>e-(l-t)*n.floor((e-t)/(l-t)),map(e,t,l,a,n,i){let o=(e-t)/(l-t)*(n-a)+a;return i?A.clamp(o,a,n):o},norm:(e,t,l)=>A.map(e,t,l,0,1),rand:(e=0,t=1)=>(z=(1664525*z+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>~~A.rand(e,t+1),rseed(e){z=~~e},cls(e){null==e?m.clearRect(0,0,A.W,A.H):A.rectfill(0,0,A.W,A.H,e)},rect(e,t,l,a,n,i){m.beginPath(),m[i?"roundRect":"rect"](~~e-g,~~t-g,~~l+2*g,~~a+2*g,i),A.stroke(n)},rectfill(e,t,l,a,n,i){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~l,~~a,i),A.fill(n)},circ(e,t,l,a){m.beginPath(),m.arc(~~e,~~t,~~l,0,o),A.stroke(a)},circfill(e,t,l,a){m.beginPath(),m.arc(~~e,~~t,~~l,0,o),A.fill(a)},oval(e,t,l,a,n){m.beginPath(),m.ellipse(~~e,~~t,~~l,~~a,0,0,o),A.stroke(n)},ovalfill(e,t,l,a,n){m.beginPath(),m.ellipse(~~e,~~t,~~l,~~a,0,0,o),A.fill(n)},shape(e){m.beginPath();for(let t=0;t<e.length;t+=2)0===t?m.moveTo(~~e[t],~~e[t+1]):m.lineTo(~~e[t],~~e[t+1]);m.lineTo(~~e[0],~~e[1])},line(e,t,l,a,n){m.beginPath();let i=.5*(0!==g&&~~e==~~l),o=.5*(0!==g&&~~t==~~a);m.moveTo(~~e+i,~~t+o),m.lineTo(~~l+i,~~a+o),A.stroke(n)},linewidth(e){m.lineWidth=~~e,g=.5*(0!=~~e%2)},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,l,a=k,n="normal"){m.font=`${n} ${P}px ${E}`,m.fillStyle=N(a);let i=(""+l).split("\n");for(let l=0;l<i.length;l++)m.fillText(i[l],~~e,~~t+P*T*l)},textgap(e){T=e},textfont(e){E=e},textsize(e){P=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,l){m.drawImage(l,~~e,~~t)},spr(e,t,l){let a=l.trim().split("\n");for(let l=0;l<a.length;l++){let n=a[l].trim();for(let a=0;a<n.length;a++){let i=n[a];"."!==i&&" "!==i&&A.rectfill(e+a,t+l,1,1,parseInt(i,36)||0)}}},paint(e,t,l,a={}){let n=a.canvas||new OffscreenCanvas(1,1),i=a.scale||1,o=m;return n.width=e*i,n.height=t*i,(m=n.getContext("2d")).scale(i,i),l(m),m=o,n.transferToImageBitmap()},ctx:e=>(e&&(m=e),m),push(){m.save()},pop(){m.restore()},translate(e,t){m.translate(~~e,~~t)},scale(e,t=e){m.scale(e,t)},rotate(e){m.rotate(e)},alpha(e){m.globalAlpha=A.clamp(e,0,1)},fill(e){m.fillStyle=N(e),m.fill()},stroke(e){m.strokeStyle=N(e),m.stroke()},clip(e){m.beginPath(),e(m),m.clip()},sfx:(e,t=0,l=1)=>!!a.zzfxV&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||L,(0!==t||1!==l)&&((e=e.slice())[0]=l*(e[0]||1),e[10]=~~e[10]+t),d.apply(0,e),e),volume(e){a.zzfxV=e},canvas:()=>u,use(e,t={}){var l=e,a=t;let n=l(A,a);for(let e in n)A.def(e,n[e])},listen:(e,t)=>{D[e=e.toLowerCase()]=D[e]||new Set,D[e].add(t)},unlisten:(e,t)=>{D[e=e.toLowerCase()]&&D[e].delete(t)},emit:(e,l,n,i,o)=>(c&&(M("before:"+(e=e.toLowerCase()),l,n,i,o),t.loop||"function"!=typeof a[e]||a[e](l,n,i,o),M(e,l,n,i,o),M("after:"+e,l,n,i,o)),l),pal(t,l=3){C=t||e,I=[],k=l},palc(e,t){null==e?I=[]:I[e]=t},def(e,l){A[e]=l,t.global&&(a[e]=l)},timescale(e){w=e},framerate(e){x=1e3/~~e},stat:e=>[t,c,x/1e3,h,D,C,L,w,a.zzfxV,z,P,E,I,T][e],pause(){p||(p=!0,cancelAnimationFrame(b),A.emit("paused"))},resume(){c&&p&&(p=!1,y=x,v=i.now(),b=r(S),A.emit("resumed"))},ispaused:()=>p,quit(){for(let e of(A.emit("shutdown"),A.pause(),c=!1,D={},s))e();if(t.global){for(let e in A)delete a[e];delete a.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))A[e]=n[e];function S(){b=r(S);let e=i.now(),t=0,l=e-v;for(v=e,y+=l<100?l:x;y>=x;){t++,y-=x;let e=x/1e3*w;A.emit("update",e,t),A.def("T",A.T+e)}t&&(A.emit("draw",m),t>1&&(y=0))}function H(){let e=t.width>0?t.width:innerWidth,l=t.width>0?t.height||t.width:innerHeight;if(A.def("W",e),A.def("H",l),u.width=e,u.height=l,t.autoscale){let a=+t.autoscale;u.style.display||(u.style.display="block",u.style.margin="auto"),h=n.min(innerWidth/e,innerHeight/l),h=a>1&&h>a?a:h,u.style.width=e*h+"px",u.style.height=l*h+"px"}m.imageSmoothingEnabled=!1,A.textalign("start","top"),A.emit("resized",h)}function M(e,t,l,a,n){if(D[e])for(let i of D[e])i(t,l,a,n)}function N(e){return C[~~(I[e]??e)%C.length]}if(t.global){if(a.ENGINE)throw Error("only one global litecanvas is allowed");Object.assign(a,A),a.ENGINE=A}if(m=(u=(u="string"==typeof t.canvas?document.querySelector(t.canvas):t.canvas)||document.createElement("canvas")).getContext("2d"),f(u,"click",()=>focus()),H(),u.parentNode||document.body.appendChild(u),u.style.imageRendering="pixelated",u.oncontextmenu=()=>!1,t.loop)for(let e in t.loop)t.loop[e]&&A.listen(e,t.loop[e]);return b=r(function(){if(t.autoscale&&f(a,"resize",H),t.tapEvents){let e=e=>[(e.pageX-u.offsetLeft)/h,(e.pageY-u.offsetTop)/h],t=new Map,l=(e,l,a)=>{let n={x:l,y:a,xi:l,yi:a,t:i.now()};return t.set(e,n),n},n=(e,a,n)=>{let i=t.get(e)||l(e);i.x=a,i.y=n},o=e=>e&&i.now()-e.t<=300,r=!1;f(u,"mousedown",t=>{if(0===t.button){t.preventDefault();let[a,n]=e(t);A.emit("tap",a,n,0),l(0,a,n),r=!0}}),f(u,"mouseup",l=>{if(0===l.button){l.preventDefault();let a=t.get(0),[n,i]=e(l);o(a)&&A.emit("tapped",a.xi,a.yi,0),A.emit("untap",n,i,0),t.delete(0),r=!1}}),f(a,"mousemove",t=>{t.preventDefault();let[l,a]=e(t);A.def("MX",l),A.def("MY",a),r&&(A.emit("tapping",l,a,0),n(0,l,a))}),f(u,"touchstart",t=>{for(let a of(t.preventDefault(),t.changedTouches)){let[t,n]=e(a);A.emit("tap",t,n,a.identifier+1),l(a.identifier+1,t,n)}}),f(u,"touchmove",t=>{for(let l of(t.preventDefault(),t.changedTouches)){let[t,a]=e(l);A.emit("tapping",t,a,l.identifier+1),n(l.identifier+1,t,a)}});let s=e=>{e.preventDefault();let l=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)l.push(t.identifier+1);for(let[e,a]of t)l.includes(e)||(o(a)&&A.emit("tapped",a.xi,a.yi,e),A.emit("untap",a.x,a.y,e),t.delete(e))};f(u,"touchend",s),f(u,"touchcancel",s),f(a,"blur",()=>{for(let[e,l]of(r=!1,t))A.emit("untap",l.x,l.y,e),t.delete(e)})}if(t.keyboardEvents){let e=new Set,t=new Set,l=(e,t="")=>(t=t.toLowerCase())?e.has("space"===t?" ":t):e.size>0,n="";f(a,"keydown",l=>{let a=l.key.toLowerCase();e.has(a)||(e.add(a),t.add(a),n=" "===a?"space":a)}),f(a,"keyup",t=>{e.delete(t.key.toLowerCase())}),f(a,"blur",()=>e.clear()),A.listen("after:update",()=>t.clear()),A.def("iskeydown",t=>l(e,t)),A.def("iskeypressed",e=>l(t,e)),A.def("lastkey",()=>n)}c=!0,A.resume(),A.emit("init",A)}),A}})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.204.0",
|
|
4
4
|
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
@@ -32,16 +32,16 @@
|
|
|
32
32
|
"creative coding"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@happy-dom/global-registrator": "^20.
|
|
36
|
-
"@size-limit/preset-small-lib": "^12.0
|
|
37
|
-
"@swc/core": "^1.15.
|
|
35
|
+
"@happy-dom/global-registrator": "^20.9.0",
|
|
36
|
+
"@size-limit/preset-small-lib": "^12.1.0",
|
|
37
|
+
"@swc/core": "^1.15.26",
|
|
38
38
|
"ava": "^7.0.0",
|
|
39
|
-
"esbuild": "^0.27.
|
|
39
|
+
"esbuild": "^0.27.7",
|
|
40
40
|
"genversion": "^3.2.0",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
|
-
"prettier": "^3.8.
|
|
43
|
-
"sinon": "^21.
|
|
44
|
-
"size-limit": "^12.0
|
|
42
|
+
"prettier": "^3.8.3",
|
|
43
|
+
"sinon": "^21.1.2",
|
|
44
|
+
"size-limit": "^12.1.0",
|
|
45
45
|
"tap-min": "^3.0.0"
|
|
46
46
|
},
|
|
47
47
|
"trustedDependencies": [
|
package/src/index.js
CHANGED
|
@@ -86,7 +86,7 @@ export default function litecanvas(settings = {}) {
|
|
|
86
86
|
_colorPaletteState = [],
|
|
87
87
|
/** @type {number[]} */
|
|
88
88
|
_defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
|
|
89
|
-
/** @type {string} list of functions copied from `Math` module*/
|
|
89
|
+
/** @type {string} list of functions copied from `Math` module */
|
|
90
90
|
_mathFunctions =
|
|
91
91
|
'PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp',
|
|
92
92
|
/**
|
|
@@ -155,7 +155,7 @@ export default function litecanvas(settings = {}) {
|
|
|
155
155
|
* @returns {number} the value in radians
|
|
156
156
|
*/
|
|
157
157
|
deg2rad: (degs) => {
|
|
158
|
-
DEV: assert(isNumber(degs), 'deg2rad
|
|
158
|
+
DEV: assert(isNumber(degs), 'deg2rad() 1st param must be a number')
|
|
159
159
|
|
|
160
160
|
return (math.PI / 180) * degs
|
|
161
161
|
},
|
|
@@ -167,7 +167,7 @@ export default function litecanvas(settings = {}) {
|
|
|
167
167
|
* @returns {number} the value in degrees
|
|
168
168
|
*/
|
|
169
169
|
rad2deg: (rads) => {
|
|
170
|
-
DEV: assert(isNumber(rads), 'rad2deg
|
|
170
|
+
DEV: assert(isNumber(rads), 'rad2deg() 1st param must be a number')
|
|
171
171
|
|
|
172
172
|
return (180 / math.PI) * rads
|
|
173
173
|
},
|
|
@@ -706,9 +706,10 @@ export default function litecanvas(settings = {}) {
|
|
|
706
706
|
*
|
|
707
707
|
* Default = `1.2`
|
|
708
708
|
*
|
|
709
|
-
* @param value
|
|
709
|
+
* @param {number} value
|
|
710
710
|
*/
|
|
711
711
|
textgap(value) {
|
|
712
|
+
DEV: assert(isNumber(value), loggerPrefix + 'textgap() 1st param must be a number')
|
|
712
713
|
_fontLineHeight = value
|
|
713
714
|
},
|
|
714
715
|
|
|
@@ -833,8 +834,9 @@ export default function litecanvas(settings = {}) {
|
|
|
833
834
|
loggerPrefix + 'paint() 3rd param must be a function'
|
|
834
835
|
)
|
|
835
836
|
DEV: assert(
|
|
836
|
-
(options && null == options.scale) ||
|
|
837
|
-
|
|
837
|
+
(options && null == options.scale) ||
|
|
838
|
+
(isNumber(options.scale) && options.scale > 0),
|
|
839
|
+
loggerPrefix + 'paint() 4th param (options.scale) must be a positive number'
|
|
838
840
|
)
|
|
839
841
|
DEV: assert(
|
|
840
842
|
(options && null == options.canvas) || options.canvas instanceof OffscreenCanvas,
|
|
@@ -867,6 +869,13 @@ export default function litecanvas(settings = {}) {
|
|
|
867
869
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
|
|
868
870
|
*/
|
|
869
871
|
ctx(context) {
|
|
872
|
+
DEV: assert(
|
|
873
|
+
null == context ||
|
|
874
|
+
context instanceof CanvasRenderingContext2D ||
|
|
875
|
+
context instanceof OffscreenCanvasRenderingContext2D,
|
|
876
|
+
loggerPrefix + 'ctx() 1st param must be an [Offscreen]CanvasRenderingContext2D'
|
|
877
|
+
)
|
|
878
|
+
|
|
870
879
|
if (context) {
|
|
871
880
|
_ctx = context
|
|
872
881
|
}
|
|
@@ -910,14 +919,11 @@ export default function litecanvas(settings = {}) {
|
|
|
910
919
|
* @param {number} x
|
|
911
920
|
* @param {number} [y]
|
|
912
921
|
*/
|
|
913
|
-
scale(x, y) {
|
|
922
|
+
scale(x, y = x) {
|
|
914
923
|
DEV: assert(isNumber(x), loggerPrefix + 'scale() 1st param must be a number')
|
|
915
|
-
DEV: assert(
|
|
916
|
-
null == y || isNumber(y),
|
|
917
|
-
loggerPrefix + 'scale() 2nd param must be a number'
|
|
918
|
-
)
|
|
924
|
+
DEV: assert(isNumber(y), loggerPrefix + 'scale() 2nd param must be a number')
|
|
919
925
|
|
|
920
|
-
_ctx.scale(x, y
|
|
926
|
+
_ctx.scale(x, y)
|
|
921
927
|
},
|
|
922
928
|
|
|
923
929
|
/**
|
|
@@ -982,7 +988,7 @@ export default function litecanvas(settings = {}) {
|
|
|
982
988
|
clip(callback) {
|
|
983
989
|
DEV: assert(
|
|
984
990
|
'function' === typeof callback,
|
|
985
|
-
loggerPrefix + 'clip() 1st param must be a function'
|
|
991
|
+
loggerPrefix + 'clip() 1st param must be a function (ctx) => void'
|
|
986
992
|
)
|
|
987
993
|
|
|
988
994
|
beginPath(_ctx)
|
|
@@ -1055,14 +1061,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1055
1061
|
canvas: () => _canvas,
|
|
1056
1062
|
|
|
1057
1063
|
/**
|
|
1058
|
-
*
|
|
1064
|
+
* Loads a Litecanvas plugin
|
|
1059
1065
|
*
|
|
1060
1066
|
* @param {pluginCallback} callback
|
|
1067
|
+
* @param {object} [config]
|
|
1061
1068
|
*/
|
|
1062
1069
|
use(callback, config = {}) {
|
|
1063
1070
|
DEV: assert(
|
|
1064
1071
|
'function' === typeof callback,
|
|
1065
|
-
loggerPrefix + 'use() 1st param must be a function'
|
|
1072
|
+
loggerPrefix + 'use() 1st param must be a function (instance, config) => any'
|
|
1066
1073
|
)
|
|
1067
1074
|
DEV: assert(
|
|
1068
1075
|
'object' === typeof config,
|
|
@@ -1129,6 +1136,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1129
1136
|
* @param {any} [arg2] any data to be passed over the listeners
|
|
1130
1137
|
* @param {any} [arg3] any data to be passed over the listeners
|
|
1131
1138
|
* @param {any} [arg4] any data to be passed over the listeners
|
|
1139
|
+
* @returns {any} always returns the second argument
|
|
1132
1140
|
*/
|
|
1133
1141
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
1134
1142
|
DEV: assert(
|
|
@@ -1148,6 +1156,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1148
1156
|
|
|
1149
1157
|
triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
|
|
1150
1158
|
}
|
|
1159
|
+
|
|
1160
|
+
return arg1
|
|
1151
1161
|
},
|
|
1152
1162
|
|
|
1153
1163
|
/**
|
|
@@ -1202,7 +1212,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1202
1212
|
* Define or update a instance property.
|
|
1203
1213
|
*
|
|
1204
1214
|
* Note: when the `litecanvas()` option "global" is `true` (default),
|
|
1205
|
-
* `def()` with set/update a
|
|
1215
|
+
* `def()` with set/update a window property.
|
|
1206
1216
|
*
|
|
1207
1217
|
* E.g: `def('ONE', 1)` do `window.ONE = 1`.
|
|
1208
1218
|
*
|
|
@@ -1266,30 +1276,43 @@ export default function litecanvas(settings = {}) {
|
|
|
1266
1276
|
const internals = [
|
|
1267
1277
|
// 0
|
|
1268
1278
|
settings,
|
|
1279
|
+
|
|
1269
1280
|
// 1
|
|
1270
1281
|
_initialized,
|
|
1282
|
+
|
|
1271
1283
|
// 2
|
|
1272
1284
|
_fpsInterval / 1000,
|
|
1285
|
+
|
|
1273
1286
|
// 3
|
|
1274
1287
|
_canvasScale,
|
|
1288
|
+
|
|
1275
1289
|
// 4
|
|
1276
1290
|
_eventListeners,
|
|
1291
|
+
|
|
1277
1292
|
// 5
|
|
1278
1293
|
_colorPalette,
|
|
1294
|
+
|
|
1279
1295
|
// 6
|
|
1280
1296
|
_defaultSound,
|
|
1297
|
+
|
|
1281
1298
|
// 7
|
|
1282
1299
|
_timeScale,
|
|
1300
|
+
|
|
1283
1301
|
// 8
|
|
1284
1302
|
root.zzfxV,
|
|
1303
|
+
|
|
1285
1304
|
// 9
|
|
1286
1305
|
_rngSeed,
|
|
1306
|
+
|
|
1287
1307
|
// 10
|
|
1288
1308
|
_fontSize,
|
|
1309
|
+
|
|
1289
1310
|
// 11
|
|
1290
1311
|
_fontFamily,
|
|
1312
|
+
|
|
1291
1313
|
// 12
|
|
1292
1314
|
_colorPaletteState,
|
|
1315
|
+
|
|
1293
1316
|
// 13
|
|
1294
1317
|
_fontLineHeight,
|
|
1295
1318
|
]
|
|
@@ -1308,8 +1331,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1308
1331
|
* Pauses the engine loop (update & draw).
|
|
1309
1332
|
*/
|
|
1310
1333
|
pause() {
|
|
1311
|
-
_paused
|
|
1312
|
-
|
|
1334
|
+
if (!_paused) {
|
|
1335
|
+
_paused = true
|
|
1336
|
+
cancelAnimationFrame(_rafid)
|
|
1337
|
+
instance.emit('paused')
|
|
1338
|
+
}
|
|
1313
1339
|
},
|
|
1314
1340
|
|
|
1315
1341
|
/**
|
|
@@ -1326,6 +1352,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1326
1352
|
_accumulated = _fpsInterval
|
|
1327
1353
|
_lastFrameTime = perf.now()
|
|
1328
1354
|
_rafid = raf(drawFrame)
|
|
1355
|
+
instance.emit('resumed')
|
|
1329
1356
|
}
|
|
1330
1357
|
},
|
|
1331
1358
|
|
|
@@ -1334,7 +1361,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1334
1361
|
*
|
|
1335
1362
|
* @returns {boolean}
|
|
1336
1363
|
*/
|
|
1337
|
-
|
|
1364
|
+
ispaused() {
|
|
1338
1365
|
return _paused
|
|
1339
1366
|
},
|
|
1340
1367
|
|
|
@@ -1342,8 +1369,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1342
1369
|
* Shutdown the litecanvas instance and remove all event listeners.
|
|
1343
1370
|
*/
|
|
1344
1371
|
quit() {
|
|
1345
|
-
// emit "
|
|
1346
|
-
instance.emit('
|
|
1372
|
+
// emit "shutdown" event to manual clean ups
|
|
1373
|
+
instance.emit('shutdown')
|
|
1347
1374
|
|
|
1348
1375
|
// stop the game loop (update & draw)
|
|
1349
1376
|
instance.pause()
|
|
@@ -1826,11 +1853,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1826
1853
|
}
|
|
1827
1854
|
|
|
1828
1855
|
// init the engine (async)
|
|
1829
|
-
|
|
1830
|
-
on(root, 'DOMContentLoaded', () => raf(init))
|
|
1831
|
-
} else {
|
|
1832
|
-
_rafid = raf(init)
|
|
1833
|
-
}
|
|
1856
|
+
_rafid = raf(init)
|
|
1834
1857
|
|
|
1835
1858
|
return instance
|
|
1836
1859
|
}
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '0.
|
|
2
|
+
export const version = '0.204.0'
|
package/types/global.d.ts
CHANGED
|
@@ -541,11 +541,12 @@ declare global {
|
|
|
541
541
|
*/
|
|
542
542
|
function canvas(): HTMLCanvasElement
|
|
543
543
|
/**
|
|
544
|
-
*
|
|
544
|
+
* Loads a plugin
|
|
545
545
|
*
|
|
546
546
|
* @param callback
|
|
547
|
+
* @param config
|
|
547
548
|
*/
|
|
548
|
-
function use(callback: pluginCallback): void
|
|
549
|
+
function use(callback: pluginCallback, config: object): void
|
|
549
550
|
/**
|
|
550
551
|
* Add a game loop event listener.
|
|
551
552
|
*
|
|
@@ -572,8 +573,9 @@ declare global {
|
|
|
572
573
|
* @param [arg2] any data to be passed over the listeners
|
|
573
574
|
* @param [arg3] any data to be passed over the listeners
|
|
574
575
|
* @param [arg4] any data to be passed over the listeners
|
|
576
|
+
* @returns always returns the second argument
|
|
575
577
|
*/
|
|
576
|
-
function emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any):
|
|
578
|
+
function emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any): any
|
|
577
579
|
/**
|
|
578
580
|
* Set new palette colors or restore the default palette.
|
|
579
581
|
*
|
|
@@ -596,7 +598,7 @@ declare global {
|
|
|
596
598
|
* Define or update a instance property.
|
|
597
599
|
*
|
|
598
600
|
* Note: when the `litecanvas()` option "global" is `true` (default),
|
|
599
|
-
* `def()` with set/update a
|
|
601
|
+
* `def()` with set/update a window property.
|
|
600
602
|
* E.g: `def('ONE', 1)` also do `window.ONE = 1`.
|
|
601
603
|
*
|
|
602
604
|
* @param key the property name
|
|
@@ -649,7 +651,7 @@ declare global {
|
|
|
649
651
|
/**
|
|
650
652
|
* Returns `true` if the engine loop is paused.
|
|
651
653
|
*/
|
|
652
|
-
function
|
|
654
|
+
function ispaused(): boolean
|
|
653
655
|
/**
|
|
654
656
|
* Shutdown the litecanvas instance and remove all event listeners.
|
|
655
657
|
*/
|
package/types/types.d.ts
CHANGED
|
@@ -525,11 +525,12 @@ type LitecanvasInstance = {
|
|
|
525
525
|
*/
|
|
526
526
|
canvas(): HTMLCanvasElement
|
|
527
527
|
/**
|
|
528
|
-
*
|
|
528
|
+
* Loads a plugin
|
|
529
529
|
*
|
|
530
530
|
* @param callback
|
|
531
|
+
* @param config
|
|
531
532
|
*/
|
|
532
|
-
use(callback: pluginCallback): void
|
|
533
|
+
use(callback: pluginCallback, config: object): void
|
|
533
534
|
/**
|
|
534
535
|
* Add a game loop event listener.
|
|
535
536
|
*
|
|
@@ -556,13 +557,14 @@ type LitecanvasInstance = {
|
|
|
556
557
|
* @param [arg2] any data to be passed over the listeners
|
|
557
558
|
* @param [arg3] any data to be passed over the listeners
|
|
558
559
|
* @param [arg4] any data to be passed over the listeners
|
|
560
|
+
* @returns always returns the second argument
|
|
559
561
|
*/
|
|
560
|
-
emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any):
|
|
562
|
+
emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any): any
|
|
561
563
|
/**
|
|
562
564
|
* Define or update a instance property.
|
|
563
565
|
*
|
|
564
566
|
* Note: when the `litecanvas()` option "global" is `true` (default),
|
|
565
|
-
* `def()` with set/update a
|
|
567
|
+
* `def()` with set/update a window property.
|
|
566
568
|
* E.g: `def('ONE', 1)` also do `window.ONE = 1`.
|
|
567
569
|
*
|
|
568
570
|
* @param key the property name
|
|
@@ -633,7 +635,7 @@ type LitecanvasInstance = {
|
|
|
633
635
|
/**
|
|
634
636
|
* Returns `true` if the engine loop is paused.
|
|
635
637
|
*/
|
|
636
|
-
|
|
638
|
+
ispaused(): boolean
|
|
637
639
|
/**
|
|
638
640
|
* Shutdown the litecanvas instance and remove all event listeners.
|
|
639
641
|
*/
|
|
@@ -703,7 +705,7 @@ type LitecanvasGameLoop = {
|
|
|
703
705
|
|
|
704
706
|
type drawCallback = (context: OffscreenCanvasRenderingContext2D) => void
|
|
705
707
|
|
|
706
|
-
type pluginCallback = (instance: LitecanvasInstance, config?:
|
|
708
|
+
type pluginCallback = (instance: LitecanvasInstance, config?: object) => any
|
|
707
709
|
|
|
708
710
|
type clipCallback = (ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => void
|
|
709
711
|
|