litecanvas 0.81.0 → 0.81.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/dist.dev.js +11 -20
- package/dist/dist.js +11 -20
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +12 -21
- package/src/web.js +3 -0
- package/types/index.d.ts +13 -12
- package/types/types.d.ts +13 -12
package/dist/dist.dev.js
CHANGED
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
animate: true
|
|
51
51
|
};
|
|
52
52
|
settings = Object.assign(defaults, settings);
|
|
53
|
-
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20,
|
|
53
|
+
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _events = {
|
|
54
54
|
init: null,
|
|
55
55
|
update: null,
|
|
56
56
|
draw: null,
|
|
@@ -257,8 +257,8 @@
|
|
|
257
257
|
const a = 1664525;
|
|
258
258
|
const c = 1013904223;
|
|
259
259
|
const m = 4294967296;
|
|
260
|
-
|
|
261
|
-
return
|
|
260
|
+
_rngSeed = (a * _rngSeed + c) % m;
|
|
261
|
+
return _rngSeed / m * (max - min) + min;
|
|
262
262
|
},
|
|
263
263
|
/**
|
|
264
264
|
* Generates a pseudorandom integer between min (inclusive) and max (inclusive)
|
|
@@ -288,7 +288,7 @@
|
|
|
288
288
|
null == value || isNumber(value) && value >= 0,
|
|
289
289
|
"rseed: 1st param must be a positive number or zero"
|
|
290
290
|
);
|
|
291
|
-
|
|
291
|
+
_rngSeed = ~~value;
|
|
292
292
|
},
|
|
293
293
|
/** BASIC GRAPHICS API */
|
|
294
294
|
/**
|
|
@@ -803,7 +803,7 @@
|
|
|
803
803
|
if (root.zzfxV <= 0 || navigator.userActivation && !navigator.userActivation.hasBeenActive) {
|
|
804
804
|
return false;
|
|
805
805
|
}
|
|
806
|
-
zzfxParams = zzfxParams ||
|
|
806
|
+
zzfxParams = zzfxParams || _defaultSound;
|
|
807
807
|
if (pitchSlide !== 0 || volumeFactor !== 1) {
|
|
808
808
|
zzfxParams = zzfxParams.slice();
|
|
809
809
|
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
|
|
@@ -941,19 +941,6 @@
|
|
|
941
941
|
/**
|
|
942
942
|
* Returns information about that engine instance.
|
|
943
943
|
*
|
|
944
|
-
* n = 0: the settings passed to that instance
|
|
945
|
-
* n = 1: returns true if the "init" event has already been emitted
|
|
946
|
-
* n = 2: the current ID returned by last requestAnimationFrame
|
|
947
|
-
* n = 3: the current canvas element scale (not the context 2D scale)
|
|
948
|
-
* n = 4: the attached event callbacks
|
|
949
|
-
* n = 5: the current color palette
|
|
950
|
-
* n = 6: the default sound used by `sfx()`
|
|
951
|
-
* n = 7: the current time scale
|
|
952
|
-
* n = 8: the current volume used by ZzFX
|
|
953
|
-
* n = 9: the current RNG state
|
|
954
|
-
*
|
|
955
|
-
* n = any other value: returns undefined
|
|
956
|
-
*
|
|
957
944
|
* @param {number} n
|
|
958
945
|
* @returns {any}
|
|
959
946
|
*/
|
|
@@ -976,13 +963,17 @@
|
|
|
976
963
|
// 5
|
|
977
964
|
_colors,
|
|
978
965
|
// 6
|
|
979
|
-
|
|
966
|
+
_defaultSound,
|
|
980
967
|
// 7
|
|
981
968
|
_timeScale,
|
|
982
969
|
// 8
|
|
983
970
|
root.zzfxV || 1,
|
|
984
971
|
// 9
|
|
985
|
-
|
|
972
|
+
_rngSeed,
|
|
973
|
+
// 10
|
|
974
|
+
_fontSize,
|
|
975
|
+
// 11
|
|
976
|
+
_fontFamily
|
|
986
977
|
];
|
|
987
978
|
return list[n];
|
|
988
979
|
},
|
package/dist/dist.js
CHANGED
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
animate: true
|
|
46
46
|
};
|
|
47
47
|
settings = Object.assign(defaults, settings);
|
|
48
|
-
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20,
|
|
48
|
+
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _events = {
|
|
49
49
|
init: null,
|
|
50
50
|
update: null,
|
|
51
51
|
draw: null,
|
|
@@ -210,8 +210,8 @@
|
|
|
210
210
|
const a = 1664525;
|
|
211
211
|
const c = 1013904223;
|
|
212
212
|
const m = 4294967296;
|
|
213
|
-
|
|
214
|
-
return
|
|
213
|
+
_rngSeed = (a * _rngSeed + c) % m;
|
|
214
|
+
return _rngSeed / m * (max - min) + min;
|
|
215
215
|
},
|
|
216
216
|
/**
|
|
217
217
|
* Generates a pseudorandom integer between min (inclusive) and max (inclusive)
|
|
@@ -231,7 +231,7 @@
|
|
|
231
231
|
* @param {number} value
|
|
232
232
|
*/
|
|
233
233
|
rseed(value) {
|
|
234
|
-
|
|
234
|
+
_rngSeed = ~~value;
|
|
235
235
|
},
|
|
236
236
|
/** BASIC GRAPHICS API */
|
|
237
237
|
/**
|
|
@@ -576,7 +576,7 @@
|
|
|
576
576
|
if (root.zzfxV <= 0 || navigator.userActivation && !navigator.userActivation.hasBeenActive) {
|
|
577
577
|
return false;
|
|
578
578
|
}
|
|
579
|
-
zzfxParams = zzfxParams ||
|
|
579
|
+
zzfxParams = zzfxParams || _defaultSound;
|
|
580
580
|
if (pitchSlide !== 0 || volumeFactor !== 1) {
|
|
581
581
|
zzfxParams = zzfxParams.slice();
|
|
582
582
|
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
|
|
@@ -672,19 +672,6 @@
|
|
|
672
672
|
/**
|
|
673
673
|
* Returns information about that engine instance.
|
|
674
674
|
*
|
|
675
|
-
* n = 0: the settings passed to that instance
|
|
676
|
-
* n = 1: returns true if the "init" event has already been emitted
|
|
677
|
-
* n = 2: the current ID returned by last requestAnimationFrame
|
|
678
|
-
* n = 3: the current canvas element scale (not the context 2D scale)
|
|
679
|
-
* n = 4: the attached event callbacks
|
|
680
|
-
* n = 5: the current color palette
|
|
681
|
-
* n = 6: the default sound used by `sfx()`
|
|
682
|
-
* n = 7: the current time scale
|
|
683
|
-
* n = 8: the current volume used by ZzFX
|
|
684
|
-
* n = 9: the current RNG state
|
|
685
|
-
*
|
|
686
|
-
* n = any other value: returns undefined
|
|
687
|
-
*
|
|
688
675
|
* @param {number} n
|
|
689
676
|
* @returns {any}
|
|
690
677
|
*/
|
|
@@ -703,13 +690,17 @@
|
|
|
703
690
|
// 5
|
|
704
691
|
_colors,
|
|
705
692
|
// 6
|
|
706
|
-
|
|
693
|
+
_defaultSound,
|
|
707
694
|
// 7
|
|
708
695
|
_timeScale,
|
|
709
696
|
// 8
|
|
710
697
|
root.zzfxV || 1,
|
|
711
698
|
// 9
|
|
712
|
-
|
|
699
|
+
_rngSeed,
|
|
700
|
+
// 10
|
|
701
|
+
_fontSize,
|
|
702
|
+
// 11
|
|
703
|
+
_fontFamily
|
|
713
704
|
];
|
|
714
705
|
return list[n];
|
|
715
706
|
},
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,r=.1,o=0,s=1,f=0,c=0,d=0,u=0,p=0,h=0,g=0,m=0,b=0,v=1,w=0,x=0,y=0)=>{let k=Math,E=2*k.PI,T=f*=500*E/44100/44100,A=l*=(1-a+2*a*k.random(a=[]))*E/44100,C=0,z=0,I=0,P=1,S=0,X=0,Y=0,M=y<0?-1:1,N=E*M*y*2/44100,H=k.cos(N),W=k.sin,B=W(N)/4,D=1+B,F=-2*H/D,L=(1-B)/D,O=(1+M*H)/2/D,V=-(M+H)/D,q=0,R=0,G=0,$=0;for(n=44100*n+9,w*=44100,i*=44100,r*=44100,b*=44100,c*=500*E/85766121e6,g*=E/44100,d*=E/44100,u*=44100,p=44100*p|0,t*=.3*(globalThis.zzfxV||1),M=n+w+i+r+b|0;I<M;a[I++]=Y*t)++X%(100*m|0)||(Y=o?1<o?2<o?3<o?W(C*C):k.max(k.min(k.tan(C),1),-1):1-(2*C/E%2+2)%2:1-4*k.abs(k.round(C/E)-C/E):W(C),Y=(p?1-x+x*W(E*I/p):1)*(Y<0?-1:1)*k.abs(Y)**s*(I<n?I/n:I<n+w?1-(I-n)/w*(1-v):I<n+w+i?v:I<M-b?(M-I-b)/r*v:0),Y=b?Y/2+(b>I?0:(I<M-b?1:(M-I)/b)*a[I-b|0]/2/t):Y,y&&(Y=$=O*q+V*(q=R)+O*(R=Y)-L*G-F*(G=$))),C+=(N=(l+=f+=c)*k.cos(g*z++))+N*h*W(I**5),P&&++P>u&&(l+=d,A+=d,P=0),!p||++S%p||(l=A,f=T,P=P||1);(t=e.createBuffer(1,M,44100)).getChannelData(0).set(a),(l=e.createBufferSource()).buffer=t,l.connect(e.destination),l.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let l=globalThis,n=Math,i=2*n.PI,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let f=!1,c=[],d,u=1,p,h=.5,g=1,m,b=1/60,v=0,w,x="sans-serif",y=20,k=Date.now(),E=a,T=[.5,0,1750,,,.3,1,,,,600,.1],A={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},C={CANVAS:null,W:0,H:0,T:0,CX:0,CY:0,MX:-1,MY:-1,TWO_PI:i,HALF_PI:n.PI/2,lerp:(e,t,a)=>a*(t-e)+e,deg2rad:e=>n.PI/180*e,rad2deg:e=>180/n.PI*e,round:(e,t=0)=>{if(!t)return n.round(e);let a=10**t;return n.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*n.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let r=(e-t)/(a-t)*(n-l)+l;return i?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),wave:(e,t,a,l=Math.sin)=>e+(l(a)+1)/2*(t-e),rand:(e=0,t=1)=>(k=(1664525*k+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>n.floor(C.rand(e,t+1)),rseed(e){k=~~e},cls(e){null==e?p.clearRect(0,0,p.canvas.width,p.canvas.height):C.rectfill(0,0,p.canvas.width,p.canvas.height,e)},rect(e,t,a,l,n,i){p.beginPath(),p[i?"roundRect":"rect"](~~e-h,~~t-h,~~a+2*h,~~l+2*h,i),C.stroke(n)},rectfill(e,t,a,l,n,i){p.beginPath(),p[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.fill(n)},circ(e,t,a,l){p.beginPath(),p.arc(~~e,~~t,~~a,0,i),C.stroke(l)},circfill(e,t,a,l){p.beginPath(),p.arc(~~e,~~t,~~a,0,i),C.fill(l)},line(e,t,a,l,n){p.beginPath();let i=.5*(0!==h&&~~e==~~a),r=.5*(0!==h&&~~t==~~l);p.moveTo(~~e+i,~~t+r),p.lineTo(~~a+i,~~l+r),C.stroke(n)},linewidth(e){p.lineWidth=~~e,h=.5*(0!=~~e%2)},linedash(e,t=0){p.setLineDash(e),p.lineDashOffset=t},text(e,t,a,l=3,n="normal"){p.font=`${n} ${y}px ${x}`,p.fillStyle=E[~~l%E.length],p.fillText(a,~~e,~~t)},textfont(e){x=e},textsize(e){y=e},textalign(e,t){e&&(p.textAlign=e),t&&(p.textBaseline=t)},image(e,t,a){p.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=p;if(n.width=e*i,n.height=t*i,(p=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(p.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&C.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(p);return p=r,n.transferToImageBitmap()},ctx:e=>(e&&(p=e),p),push:()=>p.save(),pop:()=>p.restore(),translate:(e,t)=>p.translate(~~e,~~t),scale:(e,t)=>p.scale(e,t||e),rotate:e=>p.rotate(e),alpha(e){p.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){p.fillStyle=E[~~e%E.length],t?p.fill(t):p.fill()},stroke(e,t){p.strokeStyle=E[~~e%E.length],t?p.stroke(t):p.stroke()},clip(e){p.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||T,(0!==a||1!==n)&&((e=e.slice())[0]=n*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){l.zzfxV=e},use(e,t={}){f?X(e,t):c.push([e,t])},listen:(e,t)=>(A[e]=A[e]||new Set,A[e].add(t),()=>A[e].delete(t)),emit(e,t,a,l,n){f&&(S("before:"+e,t,a,l,n),S(e,t,a,l,n),S("after:"+e,t,a,l,n))},pal(e=a){E=e},def(t,a){C[t]=a,e.global&&(l[t]=a)},timescale(e){g=e},framerate(e){b=1/~~e},stat:t=>[e,f,w,u,A,E,T,g,l.zzfxV||1,k][t],quit(){for(let e of(cancelAnimationFrame(w),C.emit("quit"),o))e();if(A={},e.global){for(let e in C)delete l[e];delete l.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))C[e]=n[e];function z(){let t=e.loop?e.loop:l;for(let e in A)t[e]&&C.listen(e,t[e]);for(let[e,t]of c)X(e,t);if(e.autoscale&&s(l,"resize",P),e.tapEvents){let e=(e,t)=>[(e-d.offsetLeft)/u,(t-d.offsetTop)/u],t=new Map,a=(e,a,l)=>{let n={x:a,y:l,startX:a,startY:l,ts:performance.now()};return t.set(e,n),n},n=(e,l,n)=>{let i=t.get(e)||a(e);i.x=l,i.y=n},i=e=>e&&performance.now()-e.ts<=300,r=e=>e.preventDefault(),o=!1;s(d,"mousedown",t=>{if(0===t.button){r(t);let[l,n]=e(t.pageX,t.pageY);C.emit("tap",l,n,0),a(0,l,n),o=!0}}),s(d,"mouseup",a=>{if(0===a.button){r(a);let l=t.get(0),[n,s]=e(a.pageX,a.pageY);i(l)&&C.emit("tapped",l.startX,l.startY,0),C.emit("untap",n,s,0),t.delete(0),o=!1}}),s(d,"mousemove",t=>{r(t);let[a,l]=e(t.pageX,t.pageY);C.def("MX",a),C.def("MY",l),o&&(C.emit("tapping",a,l,0),n(0,a,l))}),s(d,"touchstart",t=>{for(let l of(r(t),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);C.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),s(d,"touchmove",t=>{for(let a of(r(t),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);C.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});let f=e=>{r(e);let a=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)a.push(t.identifier+1);for(let[e,l]of t)a.includes(e)||(i(l)&&C.emit("tapped",l.startX,l.startY,e),C.emit("untap",l.x,l.y,e),t.delete(e))};s(d,"touchend",f),s(d,"touchcancel",f),s(l,"blur",()=>{for(let[e,a]of(o=!1,t))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=e=>e.toLowerCase(),t=new Set,a=new Set,n=(t,a)=>a?t.has("space"===e(a)?" ":e(a)):t.size>0;s(l,"keydown",l=>{t.has(e(l.key))||(t.add(e(l.key)),a.add(e(l.key)))}),s(l,"keyup",a=>{t.delete(e(a.key))}),s(l,"blur",()=>t.clear()),C.listen("after:draw",()=>a.clear()),C.def("iskeydown",e=>n(t,e)),C.def("iskeypressed",e=>n(a,e))}e.pauseOnBlur&&(s(l,"blur",()=>{w=cancelAnimationFrame(w)}),s(l,"focus",()=>{w||(v=0,w=r(I))})),f=!0,C.emit("init",C),m=performance.now(),w=r(I)}function I(t){let a=0,l=(t-m)/1e3;if(m=t,e.animate){if(w=r(I),l>.3)return;for(v+=l;v>=b;)C.emit("update",b*g),C.def("T",C.T+b*g),a++,v-=b}else a=1;a&&(C.textalign("start","top"),C.emit("draw"))}function P(){let t=e.width||l.innerWidth,a=e.height||e.width||l.innerHeight;C.def("W",d.width=t),C.def("H",d.height=a),C.def("CX",C.W/2),C.def("CY",C.H/2),e.autoscale&&(d.style.display||(d.style.display="block",d.style.margin="auto"),u=n.min(l.innerWidth/C.W,l.innerHeight/C.H),u=(e.pixelart?~~u:u)||1,d.style.width=C.W*u+"px",d.style.height=C.H*u+"px"),(!e.antialias||e.pixelart)&&(p.imageSmoothingEnabled=!1,d.style.imageRendering="pixelated"),C.emit("resized",u),C.cls(0),e.animate||r(I)}function S(e,t,a,l,n){if(A[e])for(let i of A[e])i(t,a,l,n)}function X(e,t){let a=e(C,t);for(let e in a)C.def(e,a[e])}if(e.global){if(l.ENGINE)throw Error("two global litecanvas detected");Object.assign(l,C),l.ENGINE=C}return d="string"==typeof e.canvas?document.querySelector(e.canvas):e.canvas||document.createElement("canvas"),C.def("CANVAS",d),p=d.getContext("2d"),s(d,"click",()=>l.focus()),d.style="",P(),d.parentNode||document.body.appendChild(d),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(z)):r(z),C}})();
|
|
1
|
+
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,r=.1,o=0,s=1,f=0,c=0,d=0,u=0,p=0,h=0,g=0,m=0,b=0,v=1,w=0,x=0,y=0)=>{let k=Math,E=2*k.PI,T=f*=500*E/44100/44100,A=l*=(1-a+2*a*k.random(a=[]))*E/44100,C=0,z=0,I=0,P=1,S=0,X=0,Y=0,M=y<0?-1:1,N=E*M*y*2/44100,H=k.cos(N),W=k.sin,B=W(N)/4,D=1+B,F=-2*H/D,L=(1-B)/D,O=(1+M*H)/2/D,V=-(M+H)/D,q=0,R=0,G=0,$=0;for(n=44100*n+9,w*=44100,i*=44100,r*=44100,b*=44100,c*=500*E/85766121e6,g*=E/44100,d*=E/44100,u*=44100,p=44100*p|0,t*=.3*(globalThis.zzfxV||1),M=n+w+i+r+b|0;I<M;a[I++]=Y*t)++X%(100*m|0)||(Y=o?1<o?2<o?3<o?W(C*C):k.max(k.min(k.tan(C),1),-1):1-(2*C/E%2+2)%2:1-4*k.abs(k.round(C/E)-C/E):W(C),Y=(p?1-x+x*W(E*I/p):1)*(Y<0?-1:1)*k.abs(Y)**s*(I<n?I/n:I<n+w?1-(I-n)/w*(1-v):I<n+w+i?v:I<M-b?(M-I-b)/r*v:0),Y=b?Y/2+(b>I?0:(I<M-b?1:(M-I)/b)*a[I-b|0]/2/t):Y,y&&(Y=$=O*q+V*(q=R)+O*(R=Y)-L*G-F*(G=$))),C+=(N=(l+=f+=c)*k.cos(g*z++))+N*h*W(I**5),P&&++P>u&&(l+=d,A+=d,P=0),!p||++S%p||(l=A,f=T,P=P||1);(t=e.createBuffer(1,M,44100)).getChannelData(0).set(a),(l=e.createBufferSource()).buffer=t,l.connect(e.destination),l.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let l=globalThis,n=Math,i=2*n.PI,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let f=!1,c=[],d,u=1,p,h=.5,g=1,m,b=1/60,v=0,w,x="sans-serif",y=20,k=Date.now(),E=a,T=[.5,0,1750,,,.3,1,,,,600,.1],A={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},C={CANVAS:null,W:0,H:0,T:0,CX:0,CY:0,MX:-1,MY:-1,TWO_PI:i,HALF_PI:n.PI/2,lerp:(e,t,a)=>a*(t-e)+e,deg2rad:e=>n.PI/180*e,rad2deg:e=>180/n.PI*e,round:(e,t=0)=>{if(!t)return n.round(e);let a=10**t;return n.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*n.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let r=(e-t)/(a-t)*(n-l)+l;return i?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),wave:(e,t,a,l=Math.sin)=>e+(l(a)+1)/2*(t-e),rand:(e=0,t=1)=>(k=(1664525*k+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>n.floor(C.rand(e,t+1)),rseed(e){k=~~e},cls(e){null==e?p.clearRect(0,0,p.canvas.width,p.canvas.height):C.rectfill(0,0,p.canvas.width,p.canvas.height,e)},rect(e,t,a,l,n,i){p.beginPath(),p[i?"roundRect":"rect"](~~e-h,~~t-h,~~a+2*h,~~l+2*h,i),C.stroke(n)},rectfill(e,t,a,l,n,i){p.beginPath(),p[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.fill(n)},circ(e,t,a,l){p.beginPath(),p.arc(~~e,~~t,~~a,0,i),C.stroke(l)},circfill(e,t,a,l){p.beginPath(),p.arc(~~e,~~t,~~a,0,i),C.fill(l)},line(e,t,a,l,n){p.beginPath();let i=.5*(0!==h&&~~e==~~a),r=.5*(0!==h&&~~t==~~l);p.moveTo(~~e+i,~~t+r),p.lineTo(~~a+i,~~l+r),C.stroke(n)},linewidth(e){p.lineWidth=~~e,h=.5*(0!=~~e%2)},linedash(e,t=0){p.setLineDash(e),p.lineDashOffset=t},text(e,t,a,l=3,n="normal"){p.font=`${n} ${y}px ${x}`,p.fillStyle=E[~~l%E.length],p.fillText(a,~~e,~~t)},textfont(e){x=e},textsize(e){y=e},textalign(e,t){e&&(p.textAlign=e),t&&(p.textBaseline=t)},image(e,t,a){p.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=p;if(n.width=e*i,n.height=t*i,(p=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(p.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&C.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(p);return p=r,n.transferToImageBitmap()},ctx:e=>(e&&(p=e),p),push:()=>p.save(),pop:()=>p.restore(),translate:(e,t)=>p.translate(~~e,~~t),scale:(e,t)=>p.scale(e,t||e),rotate:e=>p.rotate(e),alpha(e){p.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){p.fillStyle=E[~~e%E.length],t?p.fill(t):p.fill()},stroke(e,t){p.strokeStyle=E[~~e%E.length],t?p.stroke(t):p.stroke()},clip(e){p.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||T,(0!==a||1!==n)&&((e=e.slice())[0]=n*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){l.zzfxV=e},use(e,t={}){f?X(e,t):c.push([e,t])},listen:(e,t)=>(A[e]=A[e]||new Set,A[e].add(t),()=>A[e].delete(t)),emit(e,t,a,l,n){f&&(S("before:"+e,t,a,l,n),S(e,t,a,l,n),S("after:"+e,t,a,l,n))},pal(e=a){E=e},def(t,a){C[t]=a,e.global&&(l[t]=a)},timescale(e){g=e},framerate(e){b=1/~~e},stat:t=>[e,f,w,u,A,E,T,g,l.zzfxV||1,k,y,x][t],quit(){for(let e of(cancelAnimationFrame(w),C.emit("quit"),o))e();if(A={},e.global){for(let e in C)delete l[e];delete l.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))C[e]=n[e];function z(){let t=e.loop?e.loop:l;for(let e in A)t[e]&&C.listen(e,t[e]);for(let[e,t]of c)X(e,t);if(e.autoscale&&s(l,"resize",P),e.tapEvents){let e=(e,t)=>[(e-d.offsetLeft)/u,(t-d.offsetTop)/u],t=new Map,a=(e,a,l)=>{let n={x:a,y:l,startX:a,startY:l,ts:performance.now()};return t.set(e,n),n},n=(e,l,n)=>{let i=t.get(e)||a(e);i.x=l,i.y=n},i=e=>e&&performance.now()-e.ts<=300,r=e=>e.preventDefault(),o=!1;s(d,"mousedown",t=>{if(0===t.button){r(t);let[l,n]=e(t.pageX,t.pageY);C.emit("tap",l,n,0),a(0,l,n),o=!0}}),s(d,"mouseup",a=>{if(0===a.button){r(a);let l=t.get(0),[n,s]=e(a.pageX,a.pageY);i(l)&&C.emit("tapped",l.startX,l.startY,0),C.emit("untap",n,s,0),t.delete(0),o=!1}}),s(d,"mousemove",t=>{r(t);let[a,l]=e(t.pageX,t.pageY);C.def("MX",a),C.def("MY",l),o&&(C.emit("tapping",a,l,0),n(0,a,l))}),s(d,"touchstart",t=>{for(let l of(r(t),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);C.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),s(d,"touchmove",t=>{for(let a of(r(t),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);C.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});let f=e=>{r(e);let a=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)a.push(t.identifier+1);for(let[e,l]of t)a.includes(e)||(i(l)&&C.emit("tapped",l.startX,l.startY,e),C.emit("untap",l.x,l.y,e),t.delete(e))};s(d,"touchend",f),s(d,"touchcancel",f),s(l,"blur",()=>{for(let[e,a]of(o=!1,t))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=e=>e.toLowerCase(),t=new Set,a=new Set,n=(t,a)=>a?t.has("space"===e(a)?" ":e(a)):t.size>0;s(l,"keydown",l=>{t.has(e(l.key))||(t.add(e(l.key)),a.add(e(l.key)))}),s(l,"keyup",a=>{t.delete(e(a.key))}),s(l,"blur",()=>t.clear()),C.listen("after:draw",()=>a.clear()),C.def("iskeydown",e=>n(t,e)),C.def("iskeypressed",e=>n(a,e))}e.pauseOnBlur&&(s(l,"blur",()=>{w=cancelAnimationFrame(w)}),s(l,"focus",()=>{w||(v=0,w=r(I))})),f=!0,C.emit("init",C),m=performance.now(),w=r(I)}function I(t){let a=0,l=(t-m)/1e3;if(m=t,e.animate){if(w=r(I),l>.3)return;for(v+=l;v>=b;)C.emit("update",b*g),C.def("T",C.T+b*g),a++,v-=b}else a=1;a&&(C.textalign("start","top"),C.emit("draw"))}function P(){let t=e.width||l.innerWidth,a=e.height||e.width||l.innerHeight;C.def("W",d.width=t),C.def("H",d.height=a),C.def("CX",C.W/2),C.def("CY",C.H/2),e.autoscale&&(d.style.display||(d.style.display="block",d.style.margin="auto"),u=n.min(l.innerWidth/C.W,l.innerHeight/C.H),u=(e.pixelart?~~u:u)||1,d.style.width=C.W*u+"px",d.style.height=C.H*u+"px"),(!e.antialias||e.pixelart)&&(p.imageSmoothingEnabled=!1,d.style.imageRendering="pixelated"),C.emit("resized",u),C.cls(0),e.animate||r(I)}function S(e,t,a,l,n){if(A[e])for(let i of A[e])i(t,a,l,n)}function X(e,t){let a=e(C,t);for(let e in a)C.def(e,a[e])}if(e.global){if(l.ENGINE)throw Error("two global litecanvas detected");Object.assign(l,C),l.ENGINE=C}return d="string"==typeof e.canvas?document.querySelector(e.canvas):e.canvas||document.createElement("canvas"),C.def("CANVAS",d),p=d.getContext("2d"),s(d,"click",()=>l.focus()),d.style="",P(),d.parentNode||document.body.appendChild(d),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(z)):r(z),C}})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.81.
|
|
3
|
+
"version": "0.81.2",
|
|
4
4
|
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and P5/Processing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Luiz Bills <luizbills@pm.me>",
|
package/src/index.js
CHANGED
|
@@ -71,11 +71,11 @@ export default function litecanvas(settings = {}) {
|
|
|
71
71
|
/** @type {number} */
|
|
72
72
|
_fontSize = 20,
|
|
73
73
|
/** @type {number} */
|
|
74
|
-
|
|
74
|
+
_rngSeed = Date.now(),
|
|
75
75
|
/** @type {string[]} */
|
|
76
76
|
_colors = defaultPalette,
|
|
77
77
|
/** @type {number[]} */
|
|
78
|
-
|
|
78
|
+
_defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
|
|
79
79
|
/**
|
|
80
80
|
* default game events
|
|
81
81
|
*
|
|
@@ -320,9 +320,9 @@ export default function litecanvas(settings = {}) {
|
|
|
320
320
|
const c = 1013904223
|
|
321
321
|
const m = 4294967296
|
|
322
322
|
|
|
323
|
-
|
|
323
|
+
_rngSeed = (a * _rngSeed + c) % m
|
|
324
324
|
|
|
325
|
-
return (
|
|
325
|
+
return (_rngSeed / m) * (max - min) + min
|
|
326
326
|
},
|
|
327
327
|
|
|
328
328
|
/**
|
|
@@ -356,7 +356,7 @@ export default function litecanvas(settings = {}) {
|
|
|
356
356
|
'rseed: 1st param must be a positive number or zero'
|
|
357
357
|
)
|
|
358
358
|
|
|
359
|
-
|
|
359
|
+
_rngSeed = ~~value
|
|
360
360
|
},
|
|
361
361
|
|
|
362
362
|
/** BASIC GRAPHICS API */
|
|
@@ -950,7 +950,7 @@ export default function litecanvas(settings = {}) {
|
|
|
950
950
|
return false
|
|
951
951
|
}
|
|
952
952
|
|
|
953
|
-
zzfxParams = zzfxParams ||
|
|
953
|
+
zzfxParams = zzfxParams || _defaultSound
|
|
954
954
|
|
|
955
955
|
// if has other arguments, copy the sound to not change the original
|
|
956
956
|
if (pitchSlide !== 0 || volumeFactor !== 1) {
|
|
@@ -1111,19 +1111,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1111
1111
|
/**
|
|
1112
1112
|
* Returns information about that engine instance.
|
|
1113
1113
|
*
|
|
1114
|
-
* n = 0: the settings passed to that instance
|
|
1115
|
-
* n = 1: returns true if the "init" event has already been emitted
|
|
1116
|
-
* n = 2: the current ID returned by last requestAnimationFrame
|
|
1117
|
-
* n = 3: the current canvas element scale (not the context 2D scale)
|
|
1118
|
-
* n = 4: the attached event callbacks
|
|
1119
|
-
* n = 5: the current color palette
|
|
1120
|
-
* n = 6: the default sound used by `sfx()`
|
|
1121
|
-
* n = 7: the current time scale
|
|
1122
|
-
* n = 8: the current volume used by ZzFX
|
|
1123
|
-
* n = 9: the current RNG state
|
|
1124
|
-
*
|
|
1125
|
-
* n = any other value: returns undefined
|
|
1126
|
-
*
|
|
1127
1114
|
* @param {number} n
|
|
1128
1115
|
* @returns {any}
|
|
1129
1116
|
*/
|
|
@@ -1147,13 +1134,17 @@ export default function litecanvas(settings = {}) {
|
|
|
1147
1134
|
// 5
|
|
1148
1135
|
_colors,
|
|
1149
1136
|
// 6
|
|
1150
|
-
|
|
1137
|
+
_defaultSound,
|
|
1151
1138
|
// 7
|
|
1152
1139
|
_timeScale,
|
|
1153
1140
|
// 8
|
|
1154
1141
|
root.zzfxV || 1,
|
|
1155
1142
|
// 9
|
|
1156
|
-
|
|
1143
|
+
_rngSeed,
|
|
1144
|
+
// 10
|
|
1145
|
+
_fontSize,
|
|
1146
|
+
// 11
|
|
1147
|
+
_fontFamily,
|
|
1157
1148
|
]
|
|
1158
1149
|
return list[n]
|
|
1159
1150
|
},
|
package/src/web.js
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -589,18 +589,19 @@ declare global {
|
|
|
589
589
|
/**
|
|
590
590
|
* Returns information about that engine instance.
|
|
591
591
|
*
|
|
592
|
-
* n = 0: the settings passed to that instance
|
|
593
|
-
* n = 1: returns true if the "init" event has already been emitted
|
|
594
|
-
* n = 2: the current ID returned by last requestAnimationFrame
|
|
595
|
-
* n = 3: the current canvas element scale (not the context 2D scale)
|
|
596
|
-
* n = 4: the attached event callbacks
|
|
597
|
-
* n = 5: the current color palette
|
|
598
|
-
* n = 6: the default sound used by `sfx()`
|
|
599
|
-
* n = 7: the current time scale
|
|
600
|
-
* n = 8: the current volume used by ZzFX
|
|
601
|
-
* n = 9: the current RNG state
|
|
602
|
-
*
|
|
603
|
-
* n =
|
|
592
|
+
* - n = 0: the settings passed to that instance
|
|
593
|
+
* - n = 1: returns true if the "init" event has already been emitted
|
|
594
|
+
* - n = 2: the current ID returned by last requestAnimationFrame
|
|
595
|
+
* - n = 3: the current canvas element scale (not the context 2D scale)
|
|
596
|
+
* - n = 4: the attached event callbacks
|
|
597
|
+
* - n = 5: the current color palette
|
|
598
|
+
* - n = 6: the default sound used by `sfx()`
|
|
599
|
+
* - n = 7: the current time scale
|
|
600
|
+
* - n = 8: the current volume used by ZzFX
|
|
601
|
+
* - n = 9: the current RNG state
|
|
602
|
+
* - n = 10: the current font size
|
|
603
|
+
* - n = 11: the current font family
|
|
604
|
+
* - n = *any other value*: returns undefined
|
|
604
605
|
*
|
|
605
606
|
* @param n
|
|
606
607
|
*/
|
package/types/types.d.ts
CHANGED
|
@@ -558,18 +558,19 @@ type LitecanvasInstance = {
|
|
|
558
558
|
/**
|
|
559
559
|
* Returns information about that engine instance.
|
|
560
560
|
*
|
|
561
|
-
* n = 0: the settings passed to that instance
|
|
562
|
-
* n = 1: returns true if the "init" event has already been emitted
|
|
563
|
-
* n = 2: the current ID returned by last requestAnimationFrame
|
|
564
|
-
* n = 3: the current canvas element scale (not the context 2D scale)
|
|
565
|
-
* n = 4: the attached event callbacks
|
|
566
|
-
* n = 5: the current color palette
|
|
567
|
-
* n = 6: the default sound used by `sfx()`
|
|
568
|
-
* n = 7: the current time scale
|
|
569
|
-
* n = 8: the current volume used by ZzFX
|
|
570
|
-
* n = 9: the current RNG state
|
|
571
|
-
*
|
|
572
|
-
* n =
|
|
561
|
+
* - n = 0: the settings passed to that instance
|
|
562
|
+
* - n = 1: returns true if the "init" event has already been emitted
|
|
563
|
+
* - n = 2: the current ID returned by last requestAnimationFrame
|
|
564
|
+
* - n = 3: the current canvas element scale (not the context 2D scale)
|
|
565
|
+
* - n = 4: the attached event callbacks
|
|
566
|
+
* - n = 5: the current color palette
|
|
567
|
+
* - n = 6: the default sound used by `sfx()`
|
|
568
|
+
* - n = 7: the current time scale
|
|
569
|
+
* - n = 8: the current volume used by ZzFX
|
|
570
|
+
* - n = 9: the current RNG state
|
|
571
|
+
* - n = 10: the current font size
|
|
572
|
+
* - n = 11: the current font family
|
|
573
|
+
* - n = *any other value*: returns undefined
|
|
573
574
|
*
|
|
574
575
|
* @param n
|
|
575
576
|
*/
|