litecanvas 0.69.3 → 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
@@ -41,7 +41,6 @@
41
41
  ];
42
42
 
43
43
  // src/index.js
44
- globalThis.litecanvas = litecanvas;
45
44
  function litecanvas(settings = {}) {
46
45
  const root = globalThis, PI = Math.PI, TWO_PI = PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
47
46
  elem.addEventListener(evt, callback, false);
@@ -232,7 +231,7 @@
232
231
  /**
233
232
  * Clear the game screen
234
233
  *
235
- * @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)
236
235
  */
237
236
  cls(color) {
238
237
  let width = _ctx.canvas.width, height = _ctx.canvas.height;
@@ -249,12 +248,18 @@
249
248
  * @param {number} y
250
249
  * @param {number} width
251
250
  * @param {number} height
252
- * @param {number} [color=0] the color index (generally from 0 to 7)
251
+ * @param {number} [color=0] the color index
253
252
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
254
253
  */
255
254
  rect(x, y, width, height, color = 0, radii = null) {
256
255
  _ctx.beginPath();
257
- _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
+ );
258
263
  instance.stroke(color);
259
264
  },
260
265
  /**
@@ -264,12 +269,18 @@
264
269
  * @param {number} y
265
270
  * @param {number} width
266
271
  * @param {number} height
267
- * @param {number} [color=0] the color index (generally from 0 to 7)
272
+ * @param {number} [color=0] the color index
268
273
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
269
274
  */
270
275
  rectfill(x, y, width, height, color = 0, radii = null) {
271
276
  _ctx.beginPath();
272
- _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
+ );
273
284
  instance.fill(color);
274
285
  },
275
286
  /**
@@ -278,11 +289,11 @@
278
289
  * @param {number} x
279
290
  * @param {number} y
280
291
  * @param {number} radius
281
- * @param {number} [color=0] the color index (generally from 0 to 7)
292
+ * @param {number} [color=0] the color index
282
293
  */
283
294
  circ(x, y, radius, color) {
284
295
  _ctx.beginPath();
285
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI);
296
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
286
297
  instance.stroke(color);
287
298
  },
288
299
  /**
@@ -291,11 +302,11 @@
291
302
  * @param {number} x
292
303
  * @param {number} y
293
304
  * @param {number} radius
294
- * @param {number} [color=0] the color index (generally from 0 to 7)
305
+ * @param {number} [color=0] the color index
295
306
  */
296
307
  circfill(x, y, radius, color) {
297
308
  _ctx.beginPath();
298
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI);
309
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
299
310
  instance.fill(color);
300
311
  },
301
312
  /**
@@ -305,7 +316,7 @@
305
316
  * @param {number} y1
306
317
  * @param {number} x2
307
318
  * @param {number} y2
308
- * @param {number} [color=0] the color index (generally from 0 to 7)
319
+ * @param {number} [color=0] the color index
309
320
  */
310
321
  line(x1, y1, x2, y2, color) {
311
322
  _ctx.beginPath();
@@ -320,7 +331,7 @@
320
331
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
321
332
  */
322
333
  linewidth(value) {
323
- _ctx.lineWidth = value;
334
+ _ctx.lineWidth = ~~value;
324
335
  },
325
336
  /**
326
337
  * Sets the line dash pattern used when drawing lines
@@ -341,7 +352,7 @@
341
352
  * @param {number} x
342
353
  * @param {number} y
343
354
  * @param {string} text the text message
344
- * @param {number} [color=3] the color index (generally from 0 to 7)
355
+ * @param {number} [color=3] the color index
345
356
  */
346
357
  text(x, y, text, color = 3) {
347
358
  _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`;
@@ -492,19 +503,6 @@
492
503
  * @param {number} radians
493
504
  */
494
505
  rotate: (radians) => _ctx.rotate(radians),
495
- /**
496
- * @param {number} a
497
- * @param {number} b
498
- * @param {number} c
499
- * @param {number} d
500
- * @param {number} e
501
- * @param {number} f
502
- * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
503
- *
504
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
505
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
506
- */
507
- transform: (a, b, c, d, e, f, resetFirst = true) => _ctx[resetFirst ? "setTransform" : "transform"](a, b, c, d, e, f),
508
506
  /**
509
507
  * Sets the alpha (opacity) value to apply when drawing new shapes and images
510
508
  *
@@ -965,4 +963,7 @@
965
963
  }
966
964
  return instance;
967
965
  }
966
+
967
+ // src/web.js
968
+ globalThis.litecanvas = litecanvas;
968
969
  })();
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.3",
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>",
@@ -25,7 +25,7 @@
25
25
  "scripts": {
26
26
  "test": "ava",
27
27
  "dev:test": "ava --watch",
28
- "dev": "esbuild src/index.js --bundle --watch --outfile=dist/dist.js --servedir=.",
28
+ "dev": "esbuild src/web.js --bundle --watch --outfile=dist/dist.js --servedir=.",
29
29
  "build": "node script/build.js",
30
30
  "gzip-size": "gzip -c dist/dist.min.js | wc -c | xargs printf \" Gzip size: %s bytes\n\"",
31
31
  "format": "prettier -w src/* samples/* types/* script/* types/*",
package/src/index.js CHANGED
@@ -2,8 +2,6 @@ import { zzfx } from './zzfx.js'
2
2
  import { colors } from './palette.js'
3
3
  import './types.js'
4
4
 
5
- globalThis.litecanvas = litecanvas
6
-
7
5
  /**
8
6
  * The litecanvas constructor
9
7
  *
@@ -288,7 +286,7 @@ export default function litecanvas(settings = {}) {
288
286
  /**
289
287
  * Clear the game screen
290
288
  *
291
- * @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)
292
290
  */
293
291
  cls(color) {
294
292
  let width = _ctx.canvas.width,
@@ -307,12 +305,18 @@ export default function litecanvas(settings = {}) {
307
305
  * @param {number} y
308
306
  * @param {number} width
309
307
  * @param {number} height
310
- * @param {number} [color=0] the color index (generally from 0 to 7)
308
+ * @param {number} [color=0] the color index
311
309
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
312
310
  */
313
311
  rect(x, y, width, height, color = 0, radii = null) {
314
312
  _ctx.beginPath()
315
- _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
+ )
316
320
  instance.stroke(color)
317
321
  },
318
322
 
@@ -323,12 +327,18 @@ export default function litecanvas(settings = {}) {
323
327
  * @param {number} y
324
328
  * @param {number} width
325
329
  * @param {number} height
326
- * @param {number} [color=0] the color index (generally from 0 to 7)
330
+ * @param {number} [color=0] the color index
327
331
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
328
332
  */
329
333
  rectfill(x, y, width, height, color = 0, radii = null) {
330
334
  _ctx.beginPath()
331
- _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
+ )
332
342
  instance.fill(color)
333
343
  },
334
344
 
@@ -338,11 +348,11 @@ export default function litecanvas(settings = {}) {
338
348
  * @param {number} x
339
349
  * @param {number} y
340
350
  * @param {number} radius
341
- * @param {number} [color=0] the color index (generally from 0 to 7)
351
+ * @param {number} [color=0] the color index
342
352
  */
343
353
  circ(x, y, radius, color) {
344
354
  _ctx.beginPath()
345
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI)
355
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
346
356
  instance.stroke(color)
347
357
  },
348
358
 
@@ -352,11 +362,11 @@ export default function litecanvas(settings = {}) {
352
362
  * @param {number} x
353
363
  * @param {number} y
354
364
  * @param {number} radius
355
- * @param {number} [color=0] the color index (generally from 0 to 7)
365
+ * @param {number} [color=0] the color index
356
366
  */
357
367
  circfill(x, y, radius, color) {
358
368
  _ctx.beginPath()
359
- _ctx.arc(~~x, ~~y, radius, 0, TWO_PI)
369
+ _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
360
370
  instance.fill(color)
361
371
  },
362
372
 
@@ -367,7 +377,7 @@ export default function litecanvas(settings = {}) {
367
377
  * @param {number} y1
368
378
  * @param {number} x2
369
379
  * @param {number} y2
370
- * @param {number} [color=0] the color index (generally from 0 to 7)
380
+ * @param {number} [color=0] the color index
371
381
  */
372
382
  line(x1, y1, x2, y2, color) {
373
383
  _ctx.beginPath()
@@ -383,7 +393,7 @@ export default function litecanvas(settings = {}) {
383
393
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
384
394
  */
385
395
  linewidth(value) {
386
- _ctx.lineWidth = value
396
+ _ctx.lineWidth = ~~value
387
397
  },
388
398
 
389
399
  /**
@@ -406,7 +416,7 @@ export default function litecanvas(settings = {}) {
406
416
  * @param {number} x
407
417
  * @param {number} y
408
418
  * @param {string} text the text message
409
- * @param {number} [color=3] the color index (generally from 0 to 7)
419
+ * @param {number} [color=3] the color index
410
420
  */
411
421
  text(x, y, text, color = 3) {
412
422
  _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`
@@ -586,21 +596,6 @@ export default function litecanvas(settings = {}) {
586
596
  */
587
597
  rotate: (radians) => _ctx.rotate(radians),
588
598
 
589
- /**
590
- * @param {number} a
591
- * @param {number} b
592
- * @param {number} c
593
- * @param {number} d
594
- * @param {number} e
595
- * @param {number} f
596
- * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
597
- *
598
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
599
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
600
- */
601
- transform: (a, b, c, d, e, f, resetFirst = true) =>
602
- _ctx[resetFirst ? 'setTransform' : 'transform'](a, b, c, d, e, f),
603
-
604
599
  /**
605
600
  * Sets the alpha (opacity) value to apply when drawing new shapes and images
606
601
  *
package/src/web.js ADDED
@@ -0,0 +1,3 @@
1
+ import litecanvas from './index.js'
2
+
3
+ globalThis.litecanvas = litecanvas
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
  *