litecanvas 0.76.0 → 0.77.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 +6 -5
- package/dist/dist.js +6 -5
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +10 -10
- package/types/index.d.ts +2 -4
- package/types/types.d.ts +2 -4
package/dist/dist.dev.js
CHANGED
|
@@ -1109,22 +1109,23 @@
|
|
|
1109
1109
|
});
|
|
1110
1110
|
}
|
|
1111
1111
|
if (settings.keyboardEvents) {
|
|
1112
|
-
const
|
|
1112
|
+
const _keyDownList = /* @__PURE__ */ new Set();
|
|
1113
1113
|
const iskeydown = (key) => {
|
|
1114
1114
|
DEV: assert(
|
|
1115
1115
|
"string" === typeof key,
|
|
1116
1116
|
"iskeydown: 1st param must be a string"
|
|
1117
1117
|
);
|
|
1118
|
-
|
|
1118
|
+
key = key.toLowerCase();
|
|
1119
|
+
return "any" === key ? _keyDownList.size > 0 : _keyDownList.has("space" === key ? " " : key);
|
|
1119
1120
|
};
|
|
1120
1121
|
instance.setvar("iskeydown", iskeydown);
|
|
1121
1122
|
on(root, "keydown", (event) => {
|
|
1122
|
-
|
|
1123
|
+
_keyDownList.add(event.key.toLowerCase());
|
|
1123
1124
|
});
|
|
1124
1125
|
on(root, "keyup", (event) => {
|
|
1125
|
-
|
|
1126
|
+
_keyDownList.delete(event.key.toLowerCase());
|
|
1126
1127
|
});
|
|
1127
|
-
on(root, "blur", () =>
|
|
1128
|
+
on(root, "blur", () => _keyDownList.clear());
|
|
1128
1129
|
}
|
|
1129
1130
|
if (settings.pauseOnBlur) {
|
|
1130
1131
|
on(root, "blur", () => {
|
package/dist/dist.js
CHANGED
|
@@ -827,18 +827,19 @@
|
|
|
827
827
|
});
|
|
828
828
|
}
|
|
829
829
|
if (settings.keyboardEvents) {
|
|
830
|
-
const
|
|
830
|
+
const _keyDownList = /* @__PURE__ */ new Set();
|
|
831
831
|
const iskeydown = (key) => {
|
|
832
|
-
|
|
832
|
+
key = key.toLowerCase();
|
|
833
|
+
return "any" === key ? _keyDownList.size > 0 : _keyDownList.has("space" === key ? " " : key);
|
|
833
834
|
};
|
|
834
835
|
instance.setvar("iskeydown", iskeydown);
|
|
835
836
|
on(root, "keydown", (event) => {
|
|
836
|
-
|
|
837
|
+
_keyDownList.add(event.key.toLowerCase());
|
|
837
838
|
});
|
|
838
839
|
on(root, "keyup", (event) => {
|
|
839
|
-
|
|
840
|
+
_keyDownList.delete(event.key.toLowerCase());
|
|
840
841
|
});
|
|
841
|
-
on(root, "blur", () =>
|
|
842
|
+
on(root, "blur", () => _keyDownList.clear());
|
|
842
843
|
}
|
|
843
844
|
if (settings.pauseOnBlur) {
|
|
844
845
|
on(root, "blur", () => {
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,o=.1,r=0,s=1,c=0,f=0,
|
|
1
|
+
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,o=.1,r=0,s=1,c=0,f=0,p=0,d=0,u=0,h=0,g=0,m=0,v=0,E=1,b=0,x=0,T=0)=>{let w=Math,y=2*w.PI,H=c*=500*y/44100/44100,I=l*=(1-a+2*a*w.random(a=[]))*y/44100,S=0,D=0,A=0,k=1,C=0,L=0,X=0,z=T<0?-1:1,P=y*z*T*2/44100,M=w.cos(P),O=w.sin,Y=O(P)/4,F=1+Y,W=-2*M/F,_=(1-Y)/F,R=(1+z*M)/2/F,G=-(z+M)/F,N=0,B=0,U=0,q=0;for(n=44100*n+9,b*=44100,i*=44100,o*=44100,v*=44100,f*=500*y/85766121e6,g*=y/44100,p*=y/44100,d*=44100,u=44100*u|0,t*=.3*(globalThis.zzfxV||1),z=n+b+i+o+v|0;A<z;a[A++]=X*t)++L%(100*m|0)||(X=r?1<r?2<r?3<r?O(S*S):w.max(w.min(w.tan(S),1),-1):1-(2*S/y%2+2)%2:1-4*w.abs(w.round(S/y)-S/y):O(S),X=(u?1-x+x*O(y*A/u):1)*(X<0?-1:1)*w.abs(X)**s*(A<n?A/n:A<n+b?1-(A-n)/b*(1-E):A<n+b+i?E:A<z-v?(z-A-v)/o*E:0),X=v?X/2+(v>A?0:(A<z-v?1:(z-A)/v)*a[A-v|0]/2/t):X,T&&(X=q=R*N+G*(N=B)+R*(B=X)-_*U-W*(U=q))),S+=(P=(l+=c+=f)*w.cos(g*D++))+P*h*O(A**5),k&&++k>d&&(l+=p,I+=p,k=0),!u||++C%u||(l=I,c=H,k=k||1);(t=e.createBuffer(1,z,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.PI,i=2*n,o=requestAnimationFrame,r=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),r.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 c=!1,f=[],p=e.canvas||document.createElement("canvas"),d=e.autoscale,u=e.animate,h=1,g,m=.5,v=1,E,b,x=0,T,w="sans-serif",y=32,H=Date.now(),I=e.global,S={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},D={settings:Object.assign({},e),colors:a},A={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],TWO_PI:i,HALF_PI:n/2,lerp:(e,t,a)=>a*(t-e)+e,deg2rad:e=>n/180*e,rad2deg:e=>180/n*e,clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*Math.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let o=(e-t)/(a-t)*(n-l)+l;return i?A.clamp(o,l,n):o},norm:(e,t,a)=>A.map(e,t,a,0,1),rand:(e=0,t=1)=>(H=(1664525*H+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>Math.floor(A.rand(e,t+1)),seed:e=>null==e?H:H=~~e,cls(e){null==e?g.clearRect(0,0,g.canvas.width,g.canvas.height):A.rectfill(0,0,g.canvas.width,g.canvas.height,e)},rect(e,t,a,l,n,i=null){g.beginPath(),g[i?"roundRect":"rect"](~~e-m,~~t-m,~~a+2*m,~~l+2*m,i),A.stroke(n)},rectfill(e,t,a,l,n,i=null){g.beginPath(),g[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),A.fill(n)},circ(e,t,a,l){g.beginPath(),g.arc(~~e,~~t,~~a,0,i),A.stroke(l)},circfill(e,t,a,l){g.beginPath(),g.arc(~~e,~~t,~~a,0,i),A.fill(l)},line(e,t,a,l,n){g.beginPath();let i=.5*(0!==m&&~~e==~~a),o=.5*(0!==m&&~~t==~~l);g.moveTo(~~e+i,~~t+o),g.lineTo(~~a+i,~~l+o),A.stroke(n)},linewidth(e){g.lineWidth=~~e,m=.5*(~~e%2!=0)},linedash(e,t=0){g.setLineDash(e),g.lineDashOffset=t},text(e,t,a,l=3,n="normal"){g.font=`${n} ${y}px ${w}`,g.fillStyle=A.getcolor(l),g.fillText(a,~~e,~~t)},textfont(e){w=e},textsize(e){y=e},textalign(e,t){e&&(g.textAlign=e),t&&(g.textBaseline=t)},image(e,t,a){g.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,o=g;if(n.width=e*i,n.height=t*i,(g=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(g.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&A.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(g);return g=o,n},ctx:e=>(e&&(g=e),g),push:()=>g.save(),pop:()=>g.restore(),translate:(e,t)=>g.translate(~~e,~~t),scale:(e,t)=>g.scale(e,t||e),rotate:e=>g.rotate(e),alpha(e){g.globalAlpha=A.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){g.fillStyle=A.getcolor(e),t?g.fill(t):g.fill()},stroke(e,t){g.strokeStyle=A.getcolor(e),t?g.stroke(t):g.stroke()},clip(e){g.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||A.DEFAULT_SFX,(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},colrect:(e,t,a,l,n,i,o,r)=>e<n+o&&e+a>n&&t<i+r&&t+l>i,colcirc:(e,t,a,l,n,i)=>(l-e)*(l-e)+(n-t)*(n-t)<=(a+i)*(a+i),use(e,t={}){c?z(e,t):f.push([e,t])},listen:(e,t)=>(S[e]=S[e]||new Set,S[e].add(t),()=>S[e].delete(t)),emit(e,t,a,l,n){c&&(X("before:"+e,t,a,l,n),X(e,t,a,l,n),X("after:"+e,t,a,l,n))},getcolor:e=>a[~~e%a.length],setvar(e,t){A[e]=t,I&&(l[e]=t)},resize(e,t){A.setvar("WIDTH",p.width=e),A.setvar("HEIGHT",p.height=t),A.setvar("CENTERX",A.WIDTH/2),A.setvar("CENTERY",A.HEIGHT/2),L()},timescale(e){v=e},setfps(e){b=1/~~e},quit(){for(let e of(A.emit("quit"),r))e();if(cancelAnimationFrame(T),S=!1,I){for(let e in A)delete l[e];delete l.__litecanvas}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,round,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))A[e]=Math[e];function k(){c=!0;let t=e.loop?e.loop:l;for(let e in S)t[e]&&A.listen(e,t[e]);for(let[e,t]of f)z(e,t);if(d&&s(l,"resize",L),e.tapEvents){let e=(e,t)=>[(e-p.offsetLeft)/h,(t-p.offsetTop)/h],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<=200,o=e=>e.preventDefault(),r=!1;s(p,"mousedown",t=>{if(0===t.button){o(t);let[l,n]=e(t.pageX,t.pageY);A.emit("tap",l,n,0),a(0,l,n),r=!0}}),s(p,"mouseup",a=>{if(0===a.button){o(a);let l=t.get(0),[n,s]=e(a.pageX,a.pageY);i(l)&&A.emit("tapped",l.startX,l.startY,0),A.emit("untap",n,s,0),t.delete(0),r=!1}}),s(p,"mousemove",t=>{o(t);let[a,l]=e(t.pageX,t.pageY);A.setvar("MOUSEX",a),A.setvar("MOUSEY",l),r&&(A.emit("tapping",a,l,0),n(0,a,l))}),s(p,"touchstart",t=>{for(let l of(o(t),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);A.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),s(p,"touchmove",t=>{for(let a of(o(t),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);A.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});let c=e=>{o(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)&&A.emit("tapped",l.startX,l.startY,e),A.emit("untap",l.x,l.y,e),t.delete(e))};s(p,"touchend",c),s(p,"touchcancel",c),s(l,"blur",()=>{for(let[e,a]of(r=!1,t))A.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=new Set;A.setvar("iskeydown",t=>"any"===(t=t.toLowerCase())?e.size>0:e.has("space"===t?" ":t)),s(l,"keydown",t=>{e.add(t.key.toLowerCase())}),s(l,"keyup",t=>{e.delete(t.key.toLowerCase())}),s(l,"blur",()=>e.clear())}e.pauseOnBlur&&(s(l,"blur",()=>{T=cancelAnimationFrame(T)}),s(l,"focus",()=>{T||(T=o(C))})),A.setfps(60),A.emit("init",A),E=performance.now(),T=o(C)}function C(e){u&&(T=o(C));let t=0,a=(e-E)/1e3;if(E=e,a>30*b);else for(x+=a,u||(x=b);x>=b;x-=b)A.emit("update",b*v),A.setvar("ELAPSED",A.ELAPSED+b*v),t++;(t||!u)&&(A.textalign("start","top"),A.emit("draw"))}function L(){let t=p.style;d&&(t.display||(t.display="block",t.margin="auto"),h=Math.min(l.innerWidth/A.WIDTH,l.innerHeight/A.HEIGHT),h=(e.pixelart?~~h:h)||1,t.width=A.WIDTH*h+"px",t.height=A.HEIGHT*h+"px"),(!e.antialias||e.pixelart)&&(g.imageSmoothingEnabled=!1,t.imageRendering="pixelated"),A.emit("resized",h),u||o(C)}function X(e,t,a,l,n){if(S[e])for(let i of S[e])i(t,a,l,n)}function z(e,t){let a=e(A,D,t);for(let e in a)A.setvar(e,a[e])}if(I){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,A),l.__litecanvas=A}return p="string"==typeof p?document.querySelector(p):p,A.setvar("CANVAS",p),g=p.getContext("2d"),s(p,"click",()=>l.focus()),p.style="",A.WIDTH||(A.WIDTH=l.innerWidth,A.HEIGHT=l.innerHeight),A.resize(A.WIDTH,A.HEIGHT,!1),p.parentNode||document.body.appendChild(p),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>o(k)):o(k),A}})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.77.0",
|
|
4
4
|
"description": "Lightweight HTML5 canvas 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
|
@@ -1331,13 +1331,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1331
1331
|
|
|
1332
1332
|
if (settings.keyboardEvents) {
|
|
1333
1333
|
/** @type {Set<string>} */
|
|
1334
|
-
const
|
|
1334
|
+
const _keyDownList = new Set()
|
|
1335
1335
|
|
|
1336
1336
|
/**
|
|
1337
|
-
* Checks if a key is
|
|
1338
|
-
*
|
|
1339
|
-
* - to check the space key use `iskeydown(" ")`.
|
|
1340
|
-
* - you can check if any key is pressed using `iskeydown("any")`.
|
|
1337
|
+
* Checks if a which key is pressed on the keyboard.
|
|
1338
|
+
* Note: use `iskeydown("any")` to check for any key pressed.
|
|
1341
1339
|
*
|
|
1342
1340
|
* @param {string} key
|
|
1343
1341
|
* @returns {boolean}
|
|
@@ -1348,22 +1346,24 @@ export default function litecanvas(settings = {}) {
|
|
|
1348
1346
|
'iskeydown: 1st param must be a string'
|
|
1349
1347
|
)
|
|
1350
1348
|
|
|
1349
|
+
key = key.toLowerCase()
|
|
1350
|
+
|
|
1351
1351
|
return 'any' === key
|
|
1352
|
-
?
|
|
1353
|
-
:
|
|
1352
|
+
? _keyDownList.size > 0
|
|
1353
|
+
: _keyDownList.has('space' === key ? ' ' : key)
|
|
1354
1354
|
}
|
|
1355
1355
|
|
|
1356
1356
|
instance.setvar('iskeydown', iskeydown)
|
|
1357
1357
|
|
|
1358
1358
|
on(root, 'keydown', (/** @type {KeyboardEvent} */ event) => {
|
|
1359
|
-
|
|
1359
|
+
_keyDownList.add(event.key.toLowerCase())
|
|
1360
1360
|
})
|
|
1361
1361
|
|
|
1362
1362
|
on(root, 'keyup', (/** @type {KeyboardEvent} */ event) => {
|
|
1363
|
-
|
|
1363
|
+
_keyDownList.delete(event.key.toLowerCase())
|
|
1364
1364
|
})
|
|
1365
1365
|
|
|
1366
|
-
on(root, 'blur', () =>
|
|
1366
|
+
on(root, 'blur', () => _keyDownList.clear())
|
|
1367
1367
|
}
|
|
1368
1368
|
|
|
1369
1369
|
// listen browser focus/blur events and pause the update/draw loop
|
package/types/index.d.ts
CHANGED
|
@@ -487,10 +487,8 @@ declare global {
|
|
|
487
487
|
|
|
488
488
|
/** UTILS API */
|
|
489
489
|
/**
|
|
490
|
-
* Checks if a key is
|
|
491
|
-
*
|
|
492
|
-
* - to check the space key use `iskeydown(" ")`.
|
|
493
|
-
* - you can check if any key is pressed using `iskeydown("any")`.
|
|
490
|
+
* Checks if a which key is pressed on the keyboard.
|
|
491
|
+
* Note: use `iskeydown("any")` to check for any key pressed.
|
|
494
492
|
*
|
|
495
493
|
* @param key
|
|
496
494
|
* @returns `true` if the which key is down
|
package/types/types.d.ts
CHANGED
|
@@ -465,10 +465,8 @@ type LitecanvasInstance = {
|
|
|
465
465
|
|
|
466
466
|
/** UTILS API */
|
|
467
467
|
/**
|
|
468
|
-
* Checks if a key is
|
|
469
|
-
*
|
|
470
|
-
* - to check the space key use `iskeydown(" ")`.
|
|
471
|
-
* - you can check if any key is pressed using `iskeydown("any")`.
|
|
468
|
+
* Checks if a which key is pressed on the keyboard.
|
|
469
|
+
* Note: use `iskeydown("any")` to check for any key pressed.
|
|
472
470
|
*
|
|
473
471
|
* @param key
|
|
474
472
|
* @returns `true` if the which key is down
|