litecanvas 0.74.0 → 0.74.1

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 CHANGED
@@ -312,7 +312,7 @@
312
312
  },
313
313
  /** BASIC GRAPHICS API */
314
314
  /**
315
- * Clear the game screen
315
+ * Clear the game screen with an optional color
316
316
  *
317
317
  * @param {number?} color The background color (index) or null (for transparent)
318
318
  */
@@ -320,14 +320,19 @@
320
320
  if (true) {
321
321
  assert(
322
322
  null == color || isFinite(color) && color >= 0,
323
- "cls: 1st param must be a number"
323
+ "cls: 1st param must be a positive number or zero or null"
324
324
  );
325
325
  }
326
- let width = _ctx.canvas.width, height = _ctx.canvas.height;
327
326
  if (null == color) {
328
- _ctx.clearRect(0, 0, width, height);
327
+ _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height);
329
328
  } else {
330
- instance.rectfill(0, 0, width, height, color);
329
+ instance.rectfill(
330
+ 0,
331
+ 0,
332
+ _ctx.canvas.width,
333
+ _ctx.canvas.height,
334
+ color
335
+ );
331
336
  }
332
337
  },
333
338
  /**
@@ -672,8 +677,8 @@
672
677
  /**
673
678
  * Get or set the canvas context 2D
674
679
  *
675
- * @param {CanvasRenderingContext2D} [context]
676
- * @returns {CanvasRenderingContext2D}
680
+ * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
681
+ * @returns {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D}
677
682
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
678
683
  */
679
684
  ctx(context) {
@@ -857,7 +862,7 @@
857
862
  return false;
858
863
  }
859
864
  zzfxParams = zzfxParams || instance.DEFAULT_SFX;
860
- if (pitchSlide > 0 || volumeFactor !== 1) {
865
+ if (pitchSlide !== 0 || volumeFactor !== 1) {
861
866
  zzfxParams = zzfxParams.slice();
862
867
  zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
863
868
  zzfxParams[10] = ~~zzfxParams[10] + pitchSlide;
@@ -924,7 +929,7 @@
924
929
  assert(isFinite(y2), "colcirc: 5th param must be a number");
925
930
  assert(isFinite(r2), "colcirc: 6th param must be a number");
926
931
  }
927
- return (x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2;
932
+ return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
928
933
  },
929
934
  /** PLUGINS API */
930
935
  /**
package/dist/dist.js CHANGED
@@ -307,7 +307,7 @@
307
307
  },
308
308
  /** BASIC GRAPHICS API */
309
309
  /**
310
- * Clear the game screen
310
+ * Clear the game screen with an optional color
311
311
  *
312
312
  * @param {number?} color The background color (index) or null (for transparent)
313
313
  */
@@ -315,14 +315,19 @@
315
315
  if (false) {
316
316
  assert(
317
317
  null == color || isFinite(color) && color >= 0,
318
- "cls: 1st param must be a number"
318
+ "cls: 1st param must be a positive number or zero or null"
319
319
  );
320
320
  }
321
- let width = _ctx.canvas.width, height = _ctx.canvas.height;
322
321
  if (null == color) {
323
- _ctx.clearRect(0, 0, width, height);
322
+ _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height);
324
323
  } else {
325
- instance.rectfill(0, 0, width, height, color);
324
+ instance.rectfill(
325
+ 0,
326
+ 0,
327
+ _ctx.canvas.width,
328
+ _ctx.canvas.height,
329
+ color
330
+ );
326
331
  }
327
332
  },
328
333
  /**
@@ -667,8 +672,8 @@
667
672
  /**
668
673
  * Get or set the canvas context 2D
669
674
  *
670
- * @param {CanvasRenderingContext2D} [context]
671
- * @returns {CanvasRenderingContext2D}
675
+ * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
676
+ * @returns {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D}
672
677
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
673
678
  */
674
679
  ctx(context) {
@@ -852,7 +857,7 @@
852
857
  return false;
853
858
  }
854
859
  zzfxParams = zzfxParams || instance.DEFAULT_SFX;
855
- if (pitchSlide > 0 || volumeFactor !== 1) {
860
+ if (pitchSlide !== 0 || volumeFactor !== 1) {
856
861
  zzfxParams = zzfxParams.slice();
857
862
  zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
858
863
  zzfxParams[10] = ~~zzfxParams[10] + pitchSlide;
@@ -919,7 +924,7 @@
919
924
  assert(isFinite(y2), "colcirc: 5th param must be a number");
920
925
  assert(isFinite(r2), "colcirc: 6th param must be a number");
921
926
  }
922
- return (x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2;
927
+ return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
923
928
  },
924
929
  /** PLUGINS API */
925
930
  /**
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,c=0,f=0,u=0,p=0,d=0,h=0,g=0,m=0,v=0,E=1,b=0,T=0,w=0)=>{let x=Math,y=2*x.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*x.random(a=[]))*y/44100,I=0,S=0,k=0,A=1,C=0,L=0,X=0,O=w<0?-1:1,z=y*O*w*2/44100,M=x.cos(z),P=x.sin,Y=P(z)/4,W=1+Y,F=-2*M/W,_=(1-Y)/W,R=(1+O*M)/2/W,G=-(O+M)/W,N=0,B=0,U=0,j=0;for(n=44100*n+9,b*=44100,i*=44100,r*=44100,v*=44100,f*=500*y/85766121e6,g*=y/44100,u*=y/44100,p*=44100,d=44100*d|0,t*=.3*(globalThis.zzfxV||1),O=n+b+i+r+v|0;k<O;a[k++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):x.max(x.min(x.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*x.abs(x.round(I/y)-I/y):P(I),X=(d?1-T+T*P(y*k/d):1)*(X<0?-1:1)*x.abs(X)**s*(k<n?k/n:k<n+b?1-(k-n)/b*(1-E):k<n+b+i?E:k<O-v?(O-k-v)/r*E:0),X=v?X/2+(v>k?0:(k<O-v?1:(O-k)/v)*a[k-v|0]/2/t):X,w&&(X=j=R*N+G*(N=B)+R*(B=X)-_*U-F*(U=j))),I+=(z=(l+=c+=f)*x.cos(g*S++))+z*h*P(k**5),A&&++A>p&&(l+=u,H+=u,A=0),!d||++C%d||(l=H,c=D,A=A||1);(t=e.createBuffer(1,O,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,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({fullscreen:!0,width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let c=!1,f=[],u=e.canvas||document.createElement("canvas"),p=e.fullscreen,d=e.autoscale,h=e.animate,g=1,m,v=.5,E=1,b,T,w,x=!0,y="sans-serif",D=32,H=Date.now(),I=e.global,S={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},k={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],PI:n,TWO_PI:i,HALF_PI:.5*n,lerp:(e,t,a)=>e+a*(t-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 r=(e-t)/(a-t)*(n-l)+l;return i?A.clamp(r,l,n):r},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){let t=m.canvas.width,a=m.canvas.height;null==e?m.clearRect(0,0,t,a):A.rectfill(0,0,t,a,e)},rect(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e-v,~~t-v,~~a+2*v,~~l+2*v,i),A.stroke(n)},rectfill(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),A.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),A.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),A.fill(l)},line(e,t,a,l,n){m.beginPath();let i=.5*(0!==v&&~~e==~~a),r=.5*(0!==v&&~~t==~~l);m.moveTo(~~e+i,~~t+r),m.lineTo(~~a+i,~~l+r),A.stroke(n)},linewidth(e){m.lineWidth=~~e,v=.5*(~~e%2!=0)},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3,n="normal"){m.font=`${n} ${D}px ${y}`,m.fillStyle=A.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){D=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,a){m.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=m;if(n.width=e*i,n.height=t*i,(m=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(m.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(m);return m=r,n},ctx:e=>(e&&(m=e),m),push:()=>m.save(),pop:()=>m.restore(),translate:(e,t)=>m.translate(~~e,~~t),scale:(e,t)=>m.scale(e,t||e),rotate:e=>m.rotate(e),alpha(e){m.globalAlpha=A.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=A.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=A.getcolor(e),t?m.stroke(t):m.stroke()},clip(e){m.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||A.DEFAULT_SFX,(a>0||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,r,o)=>e<n+r&&e+a>n&&t<i+o&&t+l>i,colcirc:(e,t,a,l,n,i)=>(l-e)**2+(n-t)**2<=(a+i)**2,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){O("before:"+e,t,a,l,n),O(e,t,a,l,n),O("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",u.width=e),A.setvar("HEIGHT",u.height=t),X()},timescale(e){E=e},setfps(e){T=1/~~e,w=0},quit(){for(let e of(A.emit("quit"),o))e();if(x=S=!1,I){for(let e in A)delete l[e];delete l.__litecanvas}}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])A[e]=Math[e];function C(){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((p||d)&&s(l,"resize",X),X(),e.tapEvents){let e=(e,t)=>[(e-u.offsetLeft)/g,(t-u.offsetTop)/g],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,r=!1;s(u,"mousedown",t=>{t.preventDefault();let[l,n]=e(t.pageX,t.pageY);A.emit("tap",l,n,0),a(0,l,n),r=!0}),s(u,"mousemove",t=>{t.preventDefault();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(u,"mouseup",a=>{a.preventDefault();let l=t.get(0),[n,o]=e(a.pageX,a.pageY);i(l)&&A.emit("tapped",l.startX,l.startY,0),A.emit("untap",n,o,0),t.delete(0),r=!1}),s(u,"touchstart",t=>{for(let l of(t.preventDefault(),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(u,"touchmove",t=>{for(let a of(t.preventDefault(),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 o=e=>{e.preventDefault();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(u,"touchend",o),s(u,"touchcancel",o),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?e.size>0:e.has(t.toLowerCase())),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",()=>{x=!1}),s(l,"focus",()=>{x=!0,r(L)})),A.setfps(60),A.emit("init",A),b=performance.now(),r(L)}function L(e){let t=!h,a=(e-b)/1e3,l=5*T;for(w+=a>l?l:a,b=e;w>=T;)A.emit("update",T*E),A.setvar("ELAPSED",A.ELAPSED+T*E),w-=T,t=!0;t&&(A.textalign("start","top"),A.emit("draw")),x&&h&&r(L)}function X(){let t=l.innerWidth,a=l.innerHeight,n=u.style;n.display="block",p?(n.position="absolute",n.inset=0,A.setvar("WIDTH",u.width=t),A.setvar("HEIGHT",u.height=a)):d&&(n.margin="auto",g=Math.min(t/A.WIDTH,a/A.HEIGHT),g=(e.pixelart?~~g:g)||1,n.width=A.WIDTH*g+"px",n.height=A.HEIGHT*g+"px"),A.setvar("CENTERX",A.WIDTH/2),A.setvar("CENTERY",A.HEIGHT/2),(!e.antialias||e.pixelart)&&(m.imageSmoothingEnabled=!1,u.style.imageRendering="pixelated"),A.emit("resized",g),h||r(L)}function O(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,k,t);if("object"==typeof a)for(let e of Object.keys(a))A.setvar(e,a[e])}if(I){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,A),l.__litecanvas=A}return u="string"==typeof u?document.querySelector(u):u,A.setvar("CANVAS",u),m=u.getContext("2d"),s(u,"click",()=>l.focus()),A.WIDTH>0&&(p=!1),u.style="",u.width=A.WIDTH,u.height=A.HEIGHT||A.WIDTH,u.parentNode||document.body.appendChild(u),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(C)):r(C),A}})();
1
+ (()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,r=.1,o=0,s=1,c=0,f=0,u=0,p=0,d=0,h=0,g=0,m=0,v=0,E=1,b=0,w=0,T=0)=>{let x=Math,y=2*x.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*x.random(a=[]))*y/44100,I=0,S=0,k=0,A=1,C=0,L=0,X=0,O=T<0?-1:1,z=y*O*T*2/44100,M=x.cos(z),P=x.sin,Y=P(z)/4,W=1+Y,F=-2*M/W,_=(1-Y)/W,R=(1+O*M)/2/W,G=-(O+M)/W,N=0,B=0,U=0,j=0;for(n=44100*n+9,b*=44100,i*=44100,r*=44100,v*=44100,f*=500*y/85766121e6,g*=y/44100,u*=y/44100,p*=44100,d=44100*d|0,t*=.3*(globalThis.zzfxV||1),O=n+b+i+r+v|0;k<O;a[k++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):x.max(x.min(x.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*x.abs(x.round(I/y)-I/y):P(I),X=(d?1-w+w*P(y*k/d):1)*(X<0?-1:1)*x.abs(X)**s*(k<n?k/n:k<n+b?1-(k-n)/b*(1-E):k<n+b+i?E:k<O-v?(O-k-v)/r*E:0),X=v?X/2+(v>k?0:(k<O-v?1:(O-k)/v)*a[k-v|0]/2/t):X,T&&(X=j=R*N+G*(N=B)+R*(B=X)-_*U-F*(U=j))),I+=(z=(l+=c+=f)*x.cos(g*S++))+z*h*P(k**5),A&&++A>p&&(l+=u,H+=u,A=0),!d||++C%d||(l=H,c=D,A=A||1);(t=e.createBuffer(1,O,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,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({fullscreen:!0,width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let c=!1,f=[],u=e.canvas||document.createElement("canvas"),p=e.fullscreen,d=e.autoscale,h=e.animate,g=1,m,v=.5,E=1,b,w,T,x=!0,y="sans-serif",D=32,H=Date.now(),I=e.global,S={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},k={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],PI:n,TWO_PI:i,HALF_PI:.5*n,lerp:(e,t,a)=>e+a*(t-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 r=(e-t)/(a-t)*(n-l)+l;return i?A.clamp(r,l,n):r},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?m.clearRect(0,0,m.canvas.width,m.canvas.height):A.rectfill(0,0,m.canvas.width,m.canvas.height,e)},rect(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e-v,~~t-v,~~a+2*v,~~l+2*v,i),A.stroke(n)},rectfill(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),A.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),A.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),A.fill(l)},line(e,t,a,l,n){m.beginPath();let i=.5*(0!==v&&~~e==~~a),r=.5*(0!==v&&~~t==~~l);m.moveTo(~~e+i,~~t+r),m.lineTo(~~a+i,~~l+r),A.stroke(n)},linewidth(e){m.lineWidth=~~e,v=.5*(~~e%2!=0)},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3,n="normal"){m.font=`${n} ${D}px ${y}`,m.fillStyle=A.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){D=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,a){m.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=m;if(n.width=e*i,n.height=t*i,(m=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(m.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(m);return m=r,n},ctx:e=>(e&&(m=e),m),push:()=>m.save(),pop:()=>m.restore(),translate:(e,t)=>m.translate(~~e,~~t),scale:(e,t)=>m.scale(e,t||e),rotate:e=>m.rotate(e),alpha(e){m.globalAlpha=A.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=A.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=A.getcolor(e),t?m.stroke(t):m.stroke()},clip(e){m.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,r,o)=>e<n+r&&e+a>n&&t<i+o&&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){O("before:"+e,t,a,l,n),O(e,t,a,l,n),O("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",u.width=e),A.setvar("HEIGHT",u.height=t),X()},timescale(e){E=e},setfps(e){w=1/~~e,T=0},quit(){for(let e of(A.emit("quit"),o))e();if(x=S=!1,I){for(let e in A)delete l[e];delete l.__litecanvas}}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])A[e]=Math[e];function C(){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((p||d)&&s(l,"resize",X),X(),e.tapEvents){let e=(e,t)=>[(e-u.offsetLeft)/g,(t-u.offsetTop)/g],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,r=!1;s(u,"mousedown",t=>{t.preventDefault();let[l,n]=e(t.pageX,t.pageY);A.emit("tap",l,n,0),a(0,l,n),r=!0}),s(u,"mousemove",t=>{t.preventDefault();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(u,"mouseup",a=>{a.preventDefault();let l=t.get(0),[n,o]=e(a.pageX,a.pageY);i(l)&&A.emit("tapped",l.startX,l.startY,0),A.emit("untap",n,o,0),t.delete(0),r=!1}),s(u,"touchstart",t=>{for(let l of(t.preventDefault(),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(u,"touchmove",t=>{for(let a of(t.preventDefault(),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 o=e=>{e.preventDefault();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(u,"touchend",o),s(u,"touchcancel",o),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?e.size>0:e.has(t.toLowerCase())),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",()=>{x=!1}),s(l,"focus",()=>{x=!0,r(L)})),A.setfps(60),A.emit("init",A),b=performance.now(),r(L)}function L(e){let t=!h,a=(e-b)/1e3,l=5*w;for(T+=a>l?l:a,b=e;T>=w;)A.emit("update",w*E),A.setvar("ELAPSED",A.ELAPSED+w*E),T-=w,t=!0;t&&(A.textalign("start","top"),A.emit("draw")),x&&h&&r(L)}function X(){let t=l.innerWidth,a=l.innerHeight,n=u.style;n.display="block",p?(n.position="absolute",n.inset=0,A.setvar("WIDTH",u.width=t),A.setvar("HEIGHT",u.height=a)):d&&(n.margin="auto",g=Math.min(t/A.WIDTH,a/A.HEIGHT),g=(e.pixelart?~~g:g)||1,n.width=A.WIDTH*g+"px",n.height=A.HEIGHT*g+"px"),A.setvar("CENTERX",A.WIDTH/2),A.setvar("CENTERY",A.HEIGHT/2),(!e.antialias||e.pixelart)&&(m.imageSmoothingEnabled=!1,u.style.imageRendering="pixelated"),A.emit("resized",g),h||r(L)}function O(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,k,t);if("object"==typeof a)for(let e of Object.keys(a))A.setvar(e,a[e])}if(I){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,A),l.__litecanvas=A}return u="string"==typeof u?document.querySelector(u):u,A.setvar("CANVAS",u),m=u.getContext("2d"),s(u,"click",()=>l.focus()),A.WIDTH>0&&(p=!1),u.style="",u.width=A.WIDTH,u.height=A.HEIGHT||A.WIDTH,u.parentNode||document.body.appendChild(u),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(C)):r(C),A}})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litecanvas",
3
- "version": "0.74.0",
3
+ "version": "0.74.1",
4
4
  "description": "Lightweight HTML5 canvas engine suitable for small games and animations.",
5
5
  "license": "MIT",
6
6
  "author": "Luiz Bills <luizbills@pm.me>",
package/src/index.js CHANGED
@@ -361,7 +361,7 @@ export default function litecanvas(settings = {}) {
361
361
 
362
362
  /** BASIC GRAPHICS API */
363
363
  /**
364
- * Clear the game screen
364
+ * Clear the game screen with an optional color
365
365
  *
366
366
  * @param {number?} color The background color (index) or null (for transparent)
367
367
  */
@@ -369,15 +369,19 @@ export default function litecanvas(settings = {}) {
369
369
  if (DEV_BUILD) {
370
370
  assert(
371
371
  null == color || (isFinite(color) && color >= 0),
372
- 'cls: 1st param must be a number'
372
+ 'cls: 1st param must be a positive number or zero or null'
373
373
  )
374
374
  }
375
- let width = _ctx.canvas.width,
376
- height = _ctx.canvas.height
377
375
  if (null == color) {
378
- _ctx.clearRect(0, 0, width, height)
376
+ _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height)
379
377
  } else {
380
- instance.rectfill(0, 0, width, height, color)
378
+ instance.rectfill(
379
+ 0,
380
+ 0,
381
+ _ctx.canvas.width,
382
+ _ctx.canvas.height,
383
+ color
384
+ )
381
385
  }
382
386
  },
383
387
 
@@ -759,8 +763,8 @@ export default function litecanvas(settings = {}) {
759
763
  /**
760
764
  * Get or set the canvas context 2D
761
765
  *
762
- * @param {CanvasRenderingContext2D} [context]
763
- * @returns {CanvasRenderingContext2D}
766
+ * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context]
767
+ * @returns {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D}
764
768
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
765
769
  */
766
770
  ctx(context) {
@@ -965,7 +969,7 @@ export default function litecanvas(settings = {}) {
965
969
  zzfxParams = zzfxParams || instance.DEFAULT_SFX
966
970
 
967
971
  // if has other arguments, copy the sound to not change the original
968
- if (pitchSlide > 0 || volumeFactor !== 1) {
972
+ if (pitchSlide !== 0 || volumeFactor !== 1) {
969
973
  zzfxParams = zzfxParams.slice()
970
974
  zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1)
971
975
  zzfxParams[10] = ~~zzfxParams[10] + pitchSlide
@@ -1037,7 +1041,10 @@ export default function litecanvas(settings = {}) {
1037
1041
  assert(isFinite(y2), 'colcirc: 5th param must be a number')
1038
1042
  assert(isFinite(r2), 'colcirc: 6th param must be a number')
1039
1043
  }
1040
- return (x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2
1044
+ return (
1045
+ (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <=
1046
+ (r1 + r2) * (r1 + r2)
1047
+ )
1041
1048
  },
1042
1049
 
1043
1050
  /** PLUGINS API */
package/types/index.d.ts CHANGED
@@ -214,7 +214,7 @@ declare global {
214
214
 
215
215
  /** BASIC GRAPHICS API */
216
216
  /**
217
- * Clear the game screen
217
+ * Clear the game screen with an optional color
218
218
  *
219
219
  * @param color The background color index or `null`
220
220
  */
package/types/types.d.ts CHANGED
@@ -203,7 +203,7 @@ type LitecanvasInstance = {
203
203
 
204
204
  /** BASIC GRAPHICS API */
205
205
  /**
206
- * Clear the game screen
206
+ * Clear the game screen with an optional color
207
207
  *
208
208
  * @param color The background color index or `null`
209
209
  */