litecanvas 0.69.4 → 0.70.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/README.md CHANGED
@@ -5,13 +5,13 @@
5
5
  ![](https://badgen.net/bundlephobia/min/litecanvas)
6
6
  ![](https://badgen.net/bundlephobia/minzip/litecanvas)
7
7
 
8
- [![Discord Server](https://badgen.net/static/CHAT/ON%20DISCORD/5865f2)](https://discord.com/invite/r2c3rGsvH3)
9
- [![Discord Server](https://badgen.net/static/FOLLOW/ON%20ITCH.IO/fa5c5c)](https://bills.itch.io/litecanvas)
10
-
11
8
  Litecanvas is a lightweight HTML5 canvas engine suitable for small web games, prototypes, game jams, animations, creative coding, learning game programming and game design, etc.
12
9
 
13
10
  :warning: **This project is still under development. All feedback is appreciated!** :warning:
14
11
 
12
+ [![Discord Server](https://flat.badgen.net/static/CHAT/ON%20DISCORD/5865f2?scale=1.5)](https://discord.com/invite/r2c3rGsvH3)
13
+ [![Discord Server](https://flat.badgen.net/static/FOLLOW/ON%20ITCH.IO/fa5c5c?scale=1.5)](https://bills.itch.io/litecanvas)
14
+
15
15
  ### Features
16
16
 
17
17
  - **Tiny**: Only `~4KB` (minified + gzipped).
package/dist/dist.js CHANGED
@@ -231,7 +231,7 @@
231
231
  /**
232
232
  * Clear the game screen
233
233
  *
234
- * @param {number|null} color The background color (from 0 to 7) or null (for transparent)
234
+ * @param {number|null} color The background color (index) or null (for transparent)
235
235
  */
236
236
  cls(color) {
237
237
  let width = _ctx.canvas.width, height = _ctx.canvas.height;
@@ -248,12 +248,18 @@
248
248
  * @param {number} y
249
249
  * @param {number} width
250
250
  * @param {number} height
251
- * @param {number} [color=0] the color index (generally from 0 to 7)
251
+ * @param {number} [color=0] the color index
252
252
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
253
253
  */
254
254
  rect(x, y, width, height, color = 0, radii = null) {
255
255
  _ctx.beginPath();
256
- _ctx[radii ? "roundRect" : "rect"](~~x, ~~y, width, height, radii);
256
+ _ctx[radii ? "roundRect" : "rect"](
257
+ ~~x,
258
+ ~~y,
259
+ ~~width,
260
+ ~~height,
261
+ radii
262
+ );
257
263
  instance.stroke(color);
258
264
  },
259
265
  /**
@@ -263,12 +269,18 @@
263
269
  * @param {number} y
264
270
  * @param {number} width
265
271
  * @param {number} height
266
- * @param {number} [color=0] the color index (generally from 0 to 7)
272
+ * @param {number} [color=0] the color index
267
273
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
268
274
  */
269
275
  rectfill(x, y, width, height, color = 0, radii = null) {
270
276
  _ctx.beginPath();
271
- _ctx[radii ? "roundRect" : "rect"](~~x, ~~y, width, height, radii);
277
+ _ctx[radii ? "roundRect" : "rect"](
278
+ ~~x,
279
+ ~~y,
280
+ ~~width,
281
+ ~~height,
282
+ radii
283
+ );
272
284
  instance.fill(color);
273
285
  },
274
286
  /**
@@ -277,11 +289,11 @@
277
289
  * @param {number} x
278
290
  * @param {number} y
279
291
  * @param {number} radius
280
- * @param {number} [color=0] the color index (generally from 0 to 7)
292
+ * @param {number} [color=0] the color index
281
293
  */
282
294
  circ(x, y, radius, color) {
283
295
  _ctx.beginPath();
284
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI);
296
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
285
297
  instance.stroke(color);
286
298
  },
287
299
  /**
@@ -290,11 +302,11 @@
290
302
  * @param {number} x
291
303
  * @param {number} y
292
304
  * @param {number} radius
293
- * @param {number} [color=0] the color index (generally from 0 to 7)
305
+ * @param {number} [color=0] the color index
294
306
  */
295
307
  circfill(x, y, radius, color) {
296
308
  _ctx.beginPath();
297
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI);
309
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
298
310
  instance.fill(color);
299
311
  },
300
312
  /**
@@ -304,7 +316,7 @@
304
316
  * @param {number} y1
305
317
  * @param {number} x2
306
318
  * @param {number} y2
307
- * @param {number} [color=0] the color index (generally from 0 to 7)
319
+ * @param {number} [color=0] the color index
308
320
  */
309
321
  line(x1, y1, x2, y2, color) {
310
322
  _ctx.beginPath();
@@ -319,7 +331,7 @@
319
331
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
320
332
  */
321
333
  linewidth(value) {
322
- _ctx.lineWidth = value;
334
+ _ctx.lineWidth = ~~value;
323
335
  },
324
336
  /**
325
337
  * Sets the line dash pattern used when drawing lines
@@ -340,7 +352,7 @@
340
352
  * @param {number} x
341
353
  * @param {number} y
342
354
  * @param {string} text the text message
343
- * @param {number} [color=3] the color index (generally from 0 to 7)
355
+ * @param {number} [color=3] the color index
344
356
  */
345
357
  text(x, y, text, color = 3) {
346
358
  _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`;
@@ -491,19 +503,6 @@
491
503
  * @param {number} radians
492
504
  */
493
505
  rotate: (radians) => _ctx.rotate(radians),
494
- /**
495
- * @param {number} a
496
- * @param {number} b
497
- * @param {number} c
498
- * @param {number} d
499
- * @param {number} e
500
- * @param {number} f
501
- * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
502
- *
503
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
504
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
505
- */
506
- transform: (a, b, c, d, e, f, resetFirst = true) => _ctx[resetFirst ? "setTransform" : "transform"](a, b, c, d, e, f),
507
506
  /**
508
507
  * Sets the alpha (opacity) value to apply when drawing new shapes and images
509
508
  *
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,x=1,E=0,b=0,T=0)=>{let w=Math,y=2*w.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*w.random(a=[]))*y/44100,I=0,S=0,A=0,k=1,C=0,L=0,X=0,O=T<0?-1:1,z=y*O*T*2/44100,M=w.cos(z),P=w.sin,Y=P(z)/4,W=1+Y,B=-2*M/W,F=(1-Y)/W,_=(1+O*M)/2/W,R=-(O+M)/W,G=0,N=0,U=0,$=0;for(n=44100*n+9,E*=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+E+i+r+v|0;A<O;a[A++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):w.max(w.min(w.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*w.abs(w.round(I/y)-I/y):P(I),X=(d?1-b+b*P(y*A/d):1)*(X<0?-1:1)*w.abs(X)**s*(A<n?A/n:A<n+E?1-(A-n)/E*(1-x):A<n+E+i?x:A<O-v?(O-A-v)/r*x:0),X=v?X/2+(v>A?0:(A<O-v?1:(O-A)/v)*a[A-v|0]/2/t):X,T&&(X=$=_*G+R*(G=N)+_*(N=X)-F*U-B*(U=$))),I+=(z=(l+=c+=f)*w.cos(g*S++))+z*h*P(A**5),k&&++k>p&&(l+=u,H+=u,k=0),!d||++C%d||(l=H,c=D,k=k||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({fps:60,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=1,x,E,b,T=0,w=!0,y="sans-serif",D="",H=32,I=Date.now(),S=e.global,A={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},k={settings:Object.assign({},e),colors:a},C={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?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>Math.floor(C.rand(e,t+1)),seed:e=>null==e?I:I=~~e,cls(e){let t=m.canvas.width,a=m.canvas.height;null==e?m.clearRect(0,0,t,a):C.rectfill(0,0,t,a,e)},rect(e,t,a,l,n=0,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,a,l,i),C.stroke(n)},rectfill(e,t,a,l,n=0,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,a,l,i),C.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,a,0,i),C.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,a,0,i),C.fill(l)},line(e,t,a,l,n){m.beginPath(),m.moveTo(~~e,~~t),m.lineTo(~~a,~~l),C.stroke(n)},linewidth(e){m.lineWidth=e},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3){m.font=`${D} ${H}px ${y}`,m.fillStyle=C.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){H=e},textstyle(e){D=e||""},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},textmetrics(e,t=H){m.font=`${D} ${t}px ${y}`;let a=m.measureText(e);return a.height=a.actualBoundingBoxAscent+a.actualBoundingBoxDescent,a},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&&C.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),transform:(e,t,a,l,n,i,r=!0)=>m[r?"setTransform":"transform"](e,t,a,l,n,i),alpha(e){m.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=C.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=C.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||C.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?M(e,t):f.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){z("before:"+e,t,a,l,n),z(e,t,a,l,n),z("after:"+e,t,a,l,n)},getcolor:e=>a[~~e%a.length],setvar(e,t){C[e]=t,S&&(l[e]=t)},resize(e,t){C.setvar("WIDTH",u.width=e),C.setvar("HEIGHT",u.height=t||e),O()},timescale(e){v=e},setfps(e){b=1e3*(E=1/e),T=0},quit(){for(let e of(C.emit("quit"),o))e();if(w=A=!1,S){for(let e in C)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"])C[e]=Math[e];function L(){c=!0;let t=e.loop?e.loop:l;for(let e in A)t[e]&&C.listen(e,t[e]);for(let[e,t]of f)M(e,t);if((p||d)&&s(l,"resize",O),O(),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);C.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);C.setvar("MOUSEX",a),C.setvar("MOUSEY",l),r&&(C.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)&&C.emit("tapped",l.startX,l.startY,0),C.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);C.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);C.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)&&C.emit("tapped",l.startX,l.startY,e),C.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))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=new Set;C.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",()=>{w=!1}),s(l,"focus",()=>{w=!0,r(X)})),C.setfps(e.fps),C.emit("init",C),x=performance.now(),r(X)}function X(e){let t=!h,a=e-x;for(T+=a>100?b:a,x=e;T>=b;)C.emit("update",E*v),C.setvar("ELAPSED",C.ELAPSED+E*v),T-=b,t=!0;t&&(C.textalign("start","top"),C.emit("draw")),w&&h&&r(X)}function O(){let t=l.innerWidth,a=l.innerHeight,n=u.style;n.display="block",p?(n.position="absolute",n.inset=0,C.setvar("WIDTH",u.width=t),C.setvar("HEIGHT",u.height=a)):d&&(n.margin="auto",g=Math.min(t/C.WIDTH,a/C.HEIGHT),g=(e.pixelart?~~g:g)||1,n.width=C.WIDTH*g+"px",n.height=C.HEIGHT*g+"px"),C.setvar("CENTERX",C.WIDTH/2),C.setvar("CENTERY",C.HEIGHT/2),(!e.antialias||e.pixelart)&&(m.imageSmoothingEnabled=!1,u.style.imageRendering="pixelated"),C.emit("resized",g),h||r(X)}function z(e,t,a,l,n){if(A[e])for(let i of A[e])i(t,a,l,n)}function M(e,t){let a=e(C,k,t);if("object"==typeof a)for(let e of Object.keys(a))C.setvar(e,a[e])}if(S){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,C),l.__litecanvas=C}return u="string"==typeof u?document.querySelector(u):u,C.setvar("CANVAS",u),m=u.getContext("2d"),s(u,"click",()=>l.focus()),C.WIDTH>0&&(p=!1),u.style="",u.width=C.WIDTH,u.height=C.HEIGHT||C.WIDTH,u.parentNode||document.body.appendChild(u),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(L)):r(L),C}})();
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,x=1,E=0,b=0,T=0)=>{let w=Math,y=2*w.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*w.random(a=[]))*y/44100,I=0,S=0,A=0,k=1,C=0,L=0,X=0,O=T<0?-1:1,z=y*O*T*2/44100,M=w.cos(z),P=w.sin,Y=P(z)/4,W=1+Y,B=-2*M/W,F=(1-Y)/W,_=(1+O*M)/2/W,R=-(O+M)/W,G=0,N=0,U=0,$=0;for(n=44100*n+9,E*=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+E+i+r+v|0;A<O;a[A++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):w.max(w.min(w.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*w.abs(w.round(I/y)-I/y):P(I),X=(d?1-b+b*P(y*A/d):1)*(X<0?-1:1)*w.abs(X)**s*(A<n?A/n:A<n+E?1-(A-n)/E*(1-x):A<n+E+i?x:A<O-v?(O-A-v)/r*x:0),X=v?X/2+(v>A?0:(A<O-v?1:(O-A)/v)*a[A-v|0]/2/t):X,T&&(X=$=_*G+R*(G=N)+_*(N=X)-F*U-B*(U=$))),I+=(z=(l+=c+=f)*w.cos(g*S++))+z*h*P(A**5),k&&++k>p&&(l+=u,H+=u,k=0),!d||++C%d||(l=H,c=D,k=k||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({fps:60,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=1,x,E,b,T=0,w=!0,y="sans-serif",D="",H=32,I=Date.now(),S=e.global,A={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},k={settings:Object.assign({},e),colors:a},C={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?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>Math.floor(C.rand(e,t+1)),seed:e=>null==e?I:I=~~e,cls(e){let t=m.canvas.width,a=m.canvas.height;null==e?m.clearRect(0,0,t,a):C.rectfill(0,0,t,a,e)},rect(e,t,a,l,n=0,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.stroke(n)},rectfill(e,t,a,l,n=0,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.fill(l)},line(e,t,a,l,n){m.beginPath(),m.moveTo(~~e,~~t),m.lineTo(~~a,~~l),C.stroke(n)},linewidth(e){m.lineWidth=~~e},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3){m.font=`${D} ${H}px ${y}`,m.fillStyle=C.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){H=e},textstyle(e){D=e||""},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},textmetrics(e,t=H){m.font=`${D} ${t}px ${y}`;let a=m.measureText(e);return a.height=a.actualBoundingBoxAscent+a.actualBoundingBoxDescent,a},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&&C.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=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=C.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=C.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||C.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?M(e,t):f.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){z("before:"+e,t,a,l,n),z(e,t,a,l,n),z("after:"+e,t,a,l,n)},getcolor:e=>a[~~e%a.length],setvar(e,t){C[e]=t,S&&(l[e]=t)},resize(e,t){C.setvar("WIDTH",u.width=e),C.setvar("HEIGHT",u.height=t||e),O()},timescale(e){v=e},setfps(e){b=1e3*(E=1/e),T=0},quit(){for(let e of(C.emit("quit"),o))e();if(w=A=!1,S){for(let e in C)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"])C[e]=Math[e];function L(){c=!0;let t=e.loop?e.loop:l;for(let e in A)t[e]&&C.listen(e,t[e]);for(let[e,t]of f)M(e,t);if((p||d)&&s(l,"resize",O),O(),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);C.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);C.setvar("MOUSEX",a),C.setvar("MOUSEY",l),r&&(C.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)&&C.emit("tapped",l.startX,l.startY,0),C.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);C.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);C.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)&&C.emit("tapped",l.startX,l.startY,e),C.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))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=new Set;C.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",()=>{w=!1}),s(l,"focus",()=>{w=!0,r(X)})),C.setfps(e.fps),C.emit("init",C),x=performance.now(),r(X)}function X(e){let t=!h,a=e-x;for(T+=a>100?b:a,x=e;T>=b;)C.emit("update",E*v),C.setvar("ELAPSED",C.ELAPSED+E*v),T-=b,t=!0;t&&(C.textalign("start","top"),C.emit("draw")),w&&h&&r(X)}function O(){let t=l.innerWidth,a=l.innerHeight,n=u.style;n.display="block",p?(n.position="absolute",n.inset=0,C.setvar("WIDTH",u.width=t),C.setvar("HEIGHT",u.height=a)):d&&(n.margin="auto",g=Math.min(t/C.WIDTH,a/C.HEIGHT),g=(e.pixelart?~~g:g)||1,n.width=C.WIDTH*g+"px",n.height=C.HEIGHT*g+"px"),C.setvar("CENTERX",C.WIDTH/2),C.setvar("CENTERY",C.HEIGHT/2),(!e.antialias||e.pixelart)&&(m.imageSmoothingEnabled=!1,u.style.imageRendering="pixelated"),C.emit("resized",g),h||r(X)}function z(e,t,a,l,n){if(A[e])for(let i of A[e])i(t,a,l,n)}function M(e,t){let a=e(C,k,t);if("object"==typeof a)for(let e of Object.keys(a))C.setvar(e,a[e])}if(S){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,C),l.__litecanvas=C}return u="string"==typeof u?document.querySelector(u):u,C.setvar("CANVAS",u),m=u.getContext("2d"),s(u,"click",()=>l.focus()),C.WIDTH>0&&(p=!1),u.style="",u.width=C.WIDTH,u.height=C.HEIGHT||C.WIDTH,u.parentNode||document.body.appendChild(u),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(L)):r(L),C}})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litecanvas",
3
- "version": "0.69.4",
3
+ "version": "0.70.0",
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
@@ -286,7 +286,7 @@ export default function litecanvas(settings = {}) {
286
286
  /**
287
287
  * Clear the game screen
288
288
  *
289
- * @param {number|null} color The background color (from 0 to 7) or null (for transparent)
289
+ * @param {number|null} color The background color (index) or null (for transparent)
290
290
  */
291
291
  cls(color) {
292
292
  let width = _ctx.canvas.width,
@@ -305,12 +305,18 @@ export default function litecanvas(settings = {}) {
305
305
  * @param {number} y
306
306
  * @param {number} width
307
307
  * @param {number} height
308
- * @param {number} [color=0] the color index (generally from 0 to 7)
308
+ * @param {number} [color=0] the color index
309
309
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
310
310
  */
311
311
  rect(x, y, width, height, color = 0, radii = null) {
312
312
  _ctx.beginPath()
313
- _ctx[radii ? 'roundRect' : 'rect'](~~x, ~~y, width, height, radii)
313
+ _ctx[radii ? 'roundRect' : 'rect'](
314
+ ~~x,
315
+ ~~y,
316
+ ~~width,
317
+ ~~height,
318
+ radii
319
+ )
314
320
  instance.stroke(color)
315
321
  },
316
322
 
@@ -321,12 +327,18 @@ export default function litecanvas(settings = {}) {
321
327
  * @param {number} y
322
328
  * @param {number} width
323
329
  * @param {number} height
324
- * @param {number} [color=0] the color index (generally from 0 to 7)
330
+ * @param {number} [color=0] the color index
325
331
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
326
332
  */
327
333
  rectfill(x, y, width, height, color = 0, radii = null) {
328
334
  _ctx.beginPath()
329
- _ctx[radii ? 'roundRect' : 'rect'](~~x, ~~y, width, height, radii)
335
+ _ctx[radii ? 'roundRect' : 'rect'](
336
+ ~~x,
337
+ ~~y,
338
+ ~~width,
339
+ ~~height,
340
+ radii
341
+ )
330
342
  instance.fill(color)
331
343
  },
332
344
 
@@ -336,11 +348,11 @@ export default function litecanvas(settings = {}) {
336
348
  * @param {number} x
337
349
  * @param {number} y
338
350
  * @param {number} radius
339
- * @param {number} [color=0] the color index (generally from 0 to 7)
351
+ * @param {number} [color=0] the color index
340
352
  */
341
353
  circ(x, y, radius, color) {
342
354
  _ctx.beginPath()
343
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI)
355
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
344
356
  instance.stroke(color)
345
357
  },
346
358
 
@@ -350,11 +362,11 @@ export default function litecanvas(settings = {}) {
350
362
  * @param {number} x
351
363
  * @param {number} y
352
364
  * @param {number} radius
353
- * @param {number} [color=0] the color index (generally from 0 to 7)
365
+ * @param {number} [color=0] the color index
354
366
  */
355
367
  circfill(x, y, radius, color) {
356
368
  _ctx.beginPath()
357
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI)
369
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
358
370
  instance.fill(color)
359
371
  },
360
372
 
@@ -365,7 +377,7 @@ export default function litecanvas(settings = {}) {
365
377
  * @param {number} y1
366
378
  * @param {number} x2
367
379
  * @param {number} y2
368
- * @param {number} [color=0] the color index (generally from 0 to 7)
380
+ * @param {number} [color=0] the color index
369
381
  */
370
382
  line(x1, y1, x2, y2, color) {
371
383
  _ctx.beginPath()
@@ -381,7 +393,7 @@ export default function litecanvas(settings = {}) {
381
393
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
382
394
  */
383
395
  linewidth(value) {
384
- _ctx.lineWidth = value
396
+ _ctx.lineWidth = ~~value
385
397
  },
386
398
 
387
399
  /**
@@ -404,7 +416,7 @@ export default function litecanvas(settings = {}) {
404
416
  * @param {number} x
405
417
  * @param {number} y
406
418
  * @param {string} text the text message
407
- * @param {number} [color=3] the color index (generally from 0 to 7)
419
+ * @param {number} [color=3] the color index
408
420
  */
409
421
  text(x, y, text, color = 3) {
410
422
  _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`
@@ -584,21 +596,6 @@ export default function litecanvas(settings = {}) {
584
596
  */
585
597
  rotate: (radians) => _ctx.rotate(radians),
586
598
 
587
- /**
588
- * @param {number} a
589
- * @param {number} b
590
- * @param {number} c
591
- * @param {number} d
592
- * @param {number} e
593
- * @param {number} f
594
- * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
595
- *
596
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
597
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
598
- */
599
- transform: (a, b, c, d, e, f, resetFirst = true) =>
600
- _ctx[resetFirst ? 'setTransform' : 'transform'](a, b, c, d, e, f),
601
-
602
599
  /**
603
600
  * Sets the alpha (opacity) value to apply when drawing new shapes and images
604
601
  *
package/types/index.d.ts CHANGED
@@ -429,27 +429,6 @@ declare global {
429
429
  * @param radians
430
430
  */
431
431
  function rotate(radians: number): void
432
- /**
433
- * @param a
434
- * @param b
435
- * @param c
436
- * @param d
437
- * @param e
438
- * @param f
439
- * @param [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
440
- *
441
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
442
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
443
- */
444
- function transform(
445
- a: number,
446
- b: number,
447
- c: number,
448
- d: number,
449
- e: number,
450
- f: number,
451
- resetFirst?: boolean
452
- ): void
453
432
  /**
454
433
  * Sets the alpha (transparency) value to apply when drawing new shapes and images
455
434
  *
package/types/types.d.ts CHANGED
@@ -407,27 +407,6 @@ type LitecanvasInstance = {
407
407
  * @param radians
408
408
  */
409
409
  rotate(radians: number): void
410
- /**
411
- * @param a
412
- * @param b
413
- * @param c
414
- * @param d
415
- * @param e
416
- * @param f
417
- * @param [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
418
- *
419
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
420
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
421
- */
422
- transform(
423
- a: number,
424
- b: number,
425
- c: number,
426
- d: number,
427
- e: number,
428
- f: number,
429
- resetFirst?: boolean
430
- ): void
431
410
  /**
432
411
  * Sets the alpha (transparency) value to apply when drawing new shapes and images
433
412
  *