litecanvas 0.58.2 → 0.60.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.js CHANGED
@@ -27,18 +27,18 @@
27
27
 
28
28
  // src/palette.js
29
29
  var colors = [
30
- "#18161c",
30
+ "#000",
31
31
  "#6a7799",
32
32
  "#aec2c2",
33
- "#f3eade",
34
- "#f04f78",
35
- "#fcf660",
36
- "#2f328f",
37
- "#4b80ca",
33
+ "#FFF1E8",
34
+ "#d82800",
35
+ "#f8d878",
36
+ "#155fd9",
37
+ "#3cbcfc",
38
38
  "#327345",
39
39
  "#63c64d",
40
- "#703075",
41
- "#a56243"
40
+ "#6844fc",
41
+ "#ac7c00"
42
42
  ];
43
43
 
44
44
  // src/index.js
@@ -56,10 +56,11 @@
56
56
  loop: null,
57
57
  pauseOnBlur: true,
58
58
  tapEvents: true,
59
- keyboardEvents: true
59
+ keyboardEvents: true,
60
+ animate: true
60
61
  };
61
62
  settings = Object.assign(defaults, settings);
62
- let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _scale = 1, _mouseX, _mouseY, _ctx, _timeScale = 1, _lastFrame, _step = 1 / settings.fps, _stepMs = _step * 1e3, _accumulated = 0, _rafid, _drawCount = 0, _drawTime = 0, _fontFamily = "sans-serif", _fontStyle = "", _fontSize = 32, _rng_seed = Date.now(), _events = {
63
+ let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animate = settings.animate, _scale = 1, _ctx, _timeScale = 1, _lastFrame, _step = 1 / settings.fps, _stepMs = _step * 1e3, _accumulated = 0, _rafid, _drawCount = 0, _drawTime = 0, _fontFamily = "sans-serif", _fontStyle = "", _fontSize = 32, _rng_seed = Date.now(), _events = {
63
64
  init: false,
64
65
  update: false,
65
66
  draw: false,
@@ -84,9 +85,13 @@
84
85
  /** @type {number} */
85
86
  FPS: settings.fps,
86
87
  /** @type {number} */
87
- CENTERX: null,
88
+ CENTERX: 0,
88
89
  /** @type {number} */
89
- CENTERY: null,
90
+ CENTERY: 0,
91
+ /** @type {number} */
92
+ MOUSEX: -1,
93
+ /** @type {number} */
94
+ MOUSEY: -1,
90
95
  /** @type {number[]} */
91
96
  DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
92
97
  /** MATH API */
@@ -209,7 +214,7 @@
209
214
  * @param {number} [max=1]
210
215
  * @returns {number} the random number
211
216
  */
212
- randi: (min = 0, max = 1) => instance.floor(instance.rand() * (max - min + 1) + min),
217
+ randi: (min = 0, max = 1) => instance.floor(instance.rand(min, max + 1)),
213
218
  /**
214
219
  * If a value is passed, initializes the random number generator with an explicit seed value.
215
220
  * Otherwise, returns the current seed state.
@@ -337,7 +342,7 @@
337
342
  * @param {number} [color=3] the color index (generally from 0 to 7)
338
343
  */
339
344
  text(x, y, text, color = 3) {
340
- _ctx.font = `${_fontStyle || ""} ${_fontSize}px ${_fontFamily}`;
345
+ _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`;
341
346
  _ctx.fillStyle = instance.getcolor(color);
342
347
  _ctx.fillText(text, ~~x, ~~y);
343
348
  },
@@ -358,12 +363,12 @@
358
363
  _fontSize = size;
359
364
  },
360
365
  /**
361
- * Sets whether a font should be styled with a normal, italic, or bold.
366
+ * Sets whether a font should be styled with a "normal", "italic", or "bold".
362
367
  *
363
368
  * @param {string} style
364
369
  */
365
370
  textstyle(style) {
366
- _fontStyle = style;
371
+ _fontStyle = style || "";
367
372
  },
368
373
  /**
369
374
  * Sets the alignment used when drawing texts
@@ -385,8 +390,8 @@
385
390
  * @returns {TextMetrics}
386
391
  * @see https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics
387
392
  */
388
- textmetrics(text, size) {
389
- _ctx.font = `${_fontStyle || ""} ${size || _fontSize}px ${_fontFamily}`;
393
+ textmetrics(text, size = _fontSize) {
394
+ _ctx.font = `${_fontStyle} ${size}px ${_fontFamily}`;
390
395
  const metrics = _ctx.measureText(text);
391
396
  metrics.height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
392
397
  return metrics;
@@ -413,7 +418,7 @@
413
418
  * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
414
419
  */
415
420
  paint(width, height, draw, options = {}) {
416
- const oc = options.canvas || new OffscreenCanvas(0, 0), scale = options.scale || 1, contextBackup = _ctx;
421
+ const oc = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1, contextBackup = _ctx;
417
422
  oc.width = width * scale;
418
423
  oc.height = height * scale;
419
424
  _ctx = oc.getContext("2d");
@@ -453,16 +458,18 @@
453
458
  },
454
459
  /**
455
460
  * saves the current drawing style settings and transformations
461
+ *
456
462
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
457
463
  */
458
464
  push: () => _ctx.save(),
459
465
  /**
460
466
  * restores the drawing style settings and transformations
467
+ *
461
468
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
462
469
  */
463
470
  pop: () => _ctx.restore(),
464
471
  /**
465
- * Adds a translation transformation to the current matrix
472
+ * Adds a translation to the transformation matrix.
466
473
  *
467
474
  * @param {number} x
468
475
  * @param {number} y
@@ -476,14 +483,12 @@
476
483
  */
477
484
  scale: (x, y) => _ctx.scale(x, y || x),
478
485
  /**
479
- * Adds a rotation to the transformation matrix
486
+ * Adds a rotation to the transformation matrix.
480
487
  *
481
488
  * @param {number} radians
482
489
  */
483
490
  rotate: (radians) => _ctx.rotate(radians),
484
491
  /**
485
- * Adds a transformation that skews to the transformation matrix
486
- *
487
492
  * @param {number} a
488
493
  * @param {number} b
489
494
  * @param {number} c
@@ -491,7 +496,9 @@
491
496
  * @param {number} e
492
497
  * @param {number} f
493
498
  * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
499
+ *
494
500
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
501
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
495
502
  */
496
503
  transform: (a, b, c, d, e, f, resetFirst = true) => _ctx[resetFirst ? "setTransform" : "transform"](a, b, c, d, e, f),
497
504
  /**
@@ -521,7 +528,11 @@
521
528
  */
522
529
  fill(color, path) {
523
530
  _ctx.fillStyle = instance.getcolor(color);
524
- _ctx.fill(path);
531
+ if (path) {
532
+ _ctx.fill(path);
533
+ } else {
534
+ _ctx.fill();
535
+ }
525
536
  },
526
537
  /**
527
538
  * Outlines the current or given path with a given color.
@@ -531,7 +542,11 @@
531
542
  */
532
543
  stroke(color, path) {
533
544
  _ctx.strokeStyle = instance.getcolor(color);
534
- path ? _ctx.stroke(path) : _ctx.stroke();
545
+ if (path) {
546
+ _ctx.stroke(path);
547
+ } else {
548
+ _ctx.stroke();
549
+ }
535
550
  },
536
551
  /**
537
552
  * Create a retangular clipping region.
@@ -568,16 +583,6 @@
568
583
  _ctx.arc(x, y, radius, 0, TWO_PI);
569
584
  _ctx.clip();
570
585
  },
571
- /**
572
- * Sets the type of compositing operation to apply when drawing new shapes.
573
- * Default value = 'source-over'.
574
- *
575
- * @param {string} value
576
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
577
- */
578
- blendmode(value) {
579
- _ctx.globalCompositeOperation = value;
580
- },
581
586
  /** SOUND API */
582
587
  /**
583
588
  * Play a sound effects using ZzFX library.
@@ -639,11 +644,6 @@
639
644
  * @returns {boolean}
640
645
  */
641
646
  colcirc: (x1, y1, r1, x2, y2, r2) => (x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2,
642
- /**
643
- * Get the mouse position
644
- * @returns number[]
645
- */
646
- mousepos: () => [_mouseX, _mouseY],
647
647
  /**
648
648
  * The scale of the game's delta time (dt).
649
649
  * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
@@ -788,7 +788,9 @@
788
788
  });
789
789
  on(_canvas, "mousemove", (ev) => {
790
790
  ev.preventDefault();
791
- const [x, y] = [_mouseX, _mouseY] = _getXY(ev.pageX, ev.pageY);
791
+ const [x, y] = _getXY(ev.pageX, ev.pageY);
792
+ instance.setvar("MOUSEX", x);
793
+ instance.setvar("MOUSEY", y);
792
794
  if (!_pressingMouse) return;
793
795
  instance.emit("tapping", x, y, 0);
794
796
  _updateTap(0, x, y);
@@ -843,7 +845,6 @@
843
845
  on(_canvas, "touchcancel", _touchEndHandler);
844
846
  on(root, "blur", () => {
845
847
  _pressingMouse = false;
846
- if (_taps.size === 0) return;
847
848
  for (const [id, tap] of _taps) {
848
849
  instance.emit("untap", tap.x, tap.y, id);
849
850
  _taps.delete(id);
@@ -887,7 +888,7 @@
887
888
  _accumulated -= _stepMs;
888
889
  ticks++;
889
890
  }
890
- if (ticks) {
891
+ if (ticks || !_animate) {
891
892
  instance.textalign("start", "top");
892
893
  instance.emit("draw");
893
894
  _drawCount++;
@@ -898,7 +899,9 @@
898
899
  _drawTime -= 1e3;
899
900
  }
900
901
  }
901
- if (_rafid) _rafid = requestAnimationFrame(drawFrame);
902
+ if (_rafid && _animate) {
903
+ _rafid = requestAnimationFrame(drawFrame);
904
+ }
902
905
  }
903
906
  function setupCanvas() {
904
907
  _canvas = "string" === typeof _canvas ? document.querySelector(_canvas) : _canvas;
@@ -926,7 +929,7 @@
926
929
  pageWidth / instance.WIDTH,
927
930
  pageHeight / instance.HEIGHT
928
931
  );
929
- _scale = settings.pixelart ? Math.floor(_scale) : _scale;
932
+ _scale = (settings.pixelart ? ~~_scale : _scale) || 1;
930
933
  _canvas.style.width = instance.WIDTH * _scale + "px";
931
934
  _canvas.style.height = instance.HEIGHT * _scale + "px";
932
935
  }
@@ -937,6 +940,10 @@
937
940
  _canvas.style.imageRendering = "pixelated";
938
941
  }
939
942
  instance.emit("resized", _scale);
943
+ if (!_animate) {
944
+ _lastFrame = performance.now();
945
+ requestAnimationFrame(drawFrame);
946
+ }
940
947
  }
941
948
  function triggerEvent(eventName, arg1, arg2, arg3, arg4) {
942
949
  if (!_events[eventName]) return;
package/dist/dist.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var e=/* @__PURE__ */new AudioContext;globalThis.zzfxV=1;var t=(t=1,a=.05,l=220,n=0,r=0,i=.1,o=0,s=1,c=0,f=0,p=0,u=0,d=0,g=0,h=0,m=0,v=0,b=1,T=0,x=0,y=0)=>{let E=Math,w=2*E.PI,H=c*=500*w/44100/44100,D=l*=(1-a+2*a*E.random(a=[]))*w/44100,I=0,A=0,P=0,S=1,C=0,k=0,z=0,L=y<0?-1:1,W=w*L*y*2/44100,X=E.cos(W),O=E.sin,_=O(W)/4,B=1+_,F=-2*X/B,Y=(1-_)/B,G=(1+L*X)/2/B,M=-(L+X)/B,R=0,N=0,j=0,V=0;for(n=44100*n+9,T*=44100,r*=44100,i*=44100,v*=44100,f*=500*w/85766121e6,h*=w/44100,p*=w/44100,u*=44100,d=44100*d|0,t*=.3*globalThis.zzfxV,L=n+T+r+i+v|0;P<L;a[P++]=z*t)++k%(100*m|0)||(z=o?1<o?2<o?3<o?O(I*I):E.max(E.min(E.tan(I),1),-1):1-(2*I/w%2+2)%2:1-4*E.abs(E.round(I/w)-I/w):O(I),z=(d?1-x+x*O(w*P/d):1)*(z<0?-1:1)*E.abs(z)**s*(P<n?P/n:P<n+T?1-(P-n)/T*(1-b):P<n+T+r?b:P<L-v?(L-P-v)/i*b:0),z=v?z/2+(v>P?0:(P<L-v?1:(L-P)/v)*a[P-v|0]/2/t):z,y&&(z=V=G*R+M*(R=N)+G*(N=z)-Y*j-F*(j=V))),I+=(W=(l+=c+=f)*E.cos(h*A++))+W*g*O(P**5),S&&++S>u&&(l+=p,D+=p,S=0),!d||++C%d||(l=D,c=H,S=S||1);(t=e.createBuffer(1,L,44100)).getChannelData(0).set(a),(l=e.createBufferSource()).buffer=t,l.connect(e.destination),l.start()},a=["#18161c","#6a7799","#aec2c2","#f3eade","#f04f78","#fcf660","#2f328f","#4b80ca","#327345","#63c64d","#703075","#a56243"];globalThis.litecanvas=function(e={}){let l=globalThis,n=Math.PI,r=2*n,i=(e,t,a)=>e.addEventListener(t,a);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},e);let o=!1,s=[],c=e.canvas||document.createElement("canvas"),f=e.fullscreen,p=e.autoscale,u=1,d,g,h,m=1,v,b=1/e.fps,T=1e3*b,x=0,y,E=0,w=0,H="sans-serif",D="",I=32,A=Date.now(),P={init:!1,update:!1,draw:!1,resized:!1,tap:!1,untap:!1,tapping:!1,tapped:!1},S={settings:Object.assign({},e),colors:a},C={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,FPS:e.fps,CENTERX:null,CENTERY:null,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],PI:n,TWO_PI:r,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,r=!1){let i=(e-t)/(a-t)*(n-l)+l;return r?C.clamp(i,l,n):i},norm:(e,t,a)=>C.map(e,t,a,0,1),rand:(e=0,t=1)=>(A=(1664525*A+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>C.floor(C.rand()*(t-e+1)+e),seed:e=>null==e?A:A=~~e,cls(e){null==e?h.clearRect(0,0,C.WIDTH,C.HEIGHT):C.rectfill(0,0,C.WIDTH,C.HEIGHT,e)},rect(e,t,a,l,n=0,r=null){h.beginPath(),h[r?"roundRect":"rect"](~~e,~~t,a,l,r),C.stroke(n)},rectfill(e,t,a,l,n=0,r=null){h.beginPath(),h[r?"roundRect":"rect"](~~e,~~t,a,l,r),C.fill(n)},circ(e,t,a,l){h.beginPath(),h.arc(~~e,~~t,a,0,r),h.closePath(),C.stroke(l)},circfill(e,t,a,l){h.beginPath(),h.arc(~~e,~~t,a,0,r),h.closePath(),C.fill(l)},line(e,t,a,l,n){h.beginPath(),h.moveTo(~~e,~~t),h.lineTo(~~a,~~l),C.stroke(n)},linewidth(e){h.lineWidth=e},linedash(e,t=0){h.setLineDash(Array.isArray(e)?e:[e]),h.lineDashOffset=t},text(e,t,a,l=3){h.font=`${D||""} ${I}px ${H}`,h.fillStyle=C.getcolor(l),h.fillText(a,~~e,~~t)},textfont(e){H=e},textsize(e){I=e},textstyle(e){D=e},textalign(e,t){e&&(h.textAlign=e),t&&(h.textBaseline=t)},textmetrics(e,t){h.font=`${D||""} ${t||I}px ${H}`;let a=h.measureText(e);return a.height=a.actualBoundingBoxAscent+a.actualBoundingBoxDescent,a},image(e,t,a){h.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(0,0),r=l.scale||1,i=h;if(n.width=e*r,n.height=t*r,(h=n.getContext("2d")).scale(r,r),Array.isArray(a)){let e=0,t=0;for(let l of(h.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(n,h);return h=i,n},ctx:e=>(e&&(h=e),h),push:()=>h.save(),pop:()=>h.restore(),translate:(e,t)=>h.translate(~~e,~~t),scale:(e,t)=>h.scale(e,t||e),rotate:e=>h.rotate(e),transform:(e,t,a,l,n,r,i=!0)=>h[i?"setTransform":"transform"](e,t,a,l,n,r),alpha(e){h.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){h.fillStyle=C.getcolor(e),h.fill(t)},stroke(e,t){h.strokeStyle=C.getcolor(e),t?h.stroke(t):h.stroke()},cliprect(e,t,a,l){h.beginPath(),h.rect(e,t,a,l),h.clip()},clipcirc(e,t,a){h.beginPath(),h.arc(e,t,a,0,r),h.clip()},blendmode(e){h.globalCompositeOperation=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,r,i,o)=>e<n+i&&e+a>n&&t<r+o&&t+l>r,colcirc:(e,t,a,l,n,r)=>(l-e)**2+(n-t)**2<=(a+r)**2,mousepos:()=>[d,g],timescale(e){m=e},use(e,t={}){e.__conf=t,o?X(e):s.push(e)},listen:(e,t)=>(P[e]=P[e]||[],P[e].push(t),()=>{P[e]=P[e].filter(e=>t!==e)}),emit(e,t,a,l,n){W("before:"+e,t,a,l,n),W(e,t,a,l,n),W("after:"+e,t,a,l,n)},getcolor:e=>a[~~e%a.length],setvar(t,a){C[t]=a,e.global&&(l[t]=a)},resize(e,t){C.setvar("WIDTH",c.width=e),C.setvar("HEIGHT",c.height=t||e),L()}};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 k(){o=!0,c="string"==typeof c?document.querySelector(c):c,C.setvar("CANVAS",c),h=c.getContext("2d"),C.WIDTH>0&&(f=!1),c.width=C.WIDTH,c.height=C.HEIGHT||C.WIDTH,c.parentNode||document.body.appendChild(c),c.style.display="block",f?(c.style.position="absolute",c.style.inset=0):p&&(c.style.margin="auto");let t=e.loop?e.loop:l;for(let e of Object.keys(P))t[e]&&C.listen(e,t[e]);for(let e of s)X(e);if(i(l,"resize",L),L(),e.tapEvents){let e=(e,t)=>[(e-c.offsetLeft)/u,(t-c.offsetTop)/u],t=/* @__PURE__ */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 r=t.get(e)||a(e);r.x=l,r.y=n},r=e=>e&&performance.now()-e.ts<=200,o=!1;i(c,"mousedown",t=>{t.preventDefault();let[l,n]=e(t.pageX,t.pageY);C.emit("tap",l,n,0),a(0,l,n),o=!0}),i(c,"mousemove",t=>{t.preventDefault();let[a,l]=[d,g]=e(t.pageX,t.pageY);o&&(C.emit("tapping",a,l,0),n(0,a,l))}),i(c,"mouseup",a=>{a.preventDefault();let l=t.get(0),[n,i]=e(a.pageX,a.pageY);r(l)&&C.emit("tapped",l.startX,l.startY,0),C.emit("untap",n,i,0),t.delete(0),o=!1}),i(c,"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)}}),i(c,"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 s=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)||(r(l)&&C.emit("tapped",l.startX,l.startY,e),C.emit("untap",l.x,l.y,e),t.delete(e))};i(c,"touchend",s),i(c,"touchcancel",s),i(l,"blur",()=>{if(o=!1,0!==t.size)for(let[e,a]of t)C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=/* @__PURE__ */new Set;C.setvar("iskeydown",t=>"any"===t?e.size>0:e.has(t.toLowerCase())),i(l,"keydown",t=>{e.add(t.key.toLowerCase())}),i(l,"keyup",t=>{e.delete(t.key.toLowerCase())}),i(l,"blur",()=>e.clear())}e.pauseOnBlur&&(i(l,"blur",()=>{y=null}),i(l,"focus",()=>{y||(v=performance.now(),y=requestAnimationFrame(z))})),C.emit("init",C),v=performance.now(),y=requestAnimationFrame(z)}function z(e){let t=0,a=e-v;for(v=e,x+=a;x>=T;)C.emit("update",b*m),C.setvar("ELAPSED",C.ELAPSED+b*m),x-=T,t++;t&&(C.textalign("start","top"),C.emit("draw"),E++,(w+=T*t)+x>=1e3&&(C.setvar("FPS",E),E=0,w-=1e3)),y&&(y=requestAnimationFrame(z))}function L(){let t=l.innerWidth,a=l.innerHeight;f?(C.setvar("WIDTH",c.width=t),C.setvar("HEIGHT",c.height=a)):p&&(u=Math.min(t/C.WIDTH,a/C.HEIGHT),u=e.pixelart?Math.floor(u):u,c.style.width=C.WIDTH*u+"px",c.style.height=C.HEIGHT*u+"px"),C.setvar("CENTERX",C.WIDTH/2),C.setvar("CENTERY",C.HEIGHT/2),(!e.antialias||e.pixelart)&&(h.imageSmoothingEnabled=!1,c.style.imageRendering="pixelated"),C.emit("resized",u)}function W(e,t,a,l,n){if(P[e])for(let r of P[e])r(t,a,l,n)}function X(e){let t=e(C,S,e.__conf);if("object"==typeof t)for(let[e,a]of Object.entries(t))C.setvar(e,a)}if(e.global){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,C),l.__litecanvas=C}return"loading"===document.readyState?i(l,"DOMContentLoaded",k):k(),C}})();
1
+ (()=>{var e=/* @__PURE__ */new AudioContext;globalThis.zzfxV=1;var t=(t=1,a=.05,n=220,l=0,r=0,i=.1,o=0,s=1,c=0,f=0,p=0,u=0,d=0,g=0,h=0,m=0,v=0,T=1,x=0,b=0,E=0)=>{let y=Math,w=2*y.PI,H=c*=500*w/44100/44100,D=n*=(1-a+2*a*y.random(a=[]))*w/44100,A=0,I=0,S=0,P=1,k=0,C=0,z=0,F=E<0?-1:1,O=w*F*E*2/44100,X=y.cos(O),L=y.sin,W=L(O)/4,Y=1+W,M=-2*X/Y,_=(1-W)/Y,B=(1+F*X)/2/Y,G=-(F+X)/Y,R=0,N=0,j=0,q=0;for(l=44100*l+9,x*=44100,r*=44100,i*=44100,v*=44100,f*=500*w/85766121e6,h*=w/44100,p*=w/44100,u*=44100,d=44100*d|0,t*=.3*globalThis.zzfxV,F=l+x+r+i+v|0;S<F;a[S++]=z*t)++C%(100*m|0)||(z=o?1<o?2<o?3<o?L(A*A):y.max(y.min(y.tan(A),1),-1):1-(2*A/w%2+2)%2:1-4*y.abs(y.round(A/w)-A/w):L(A),z=(d?1-b+b*L(w*S/d):1)*(z<0?-1:1)*y.abs(z)**s*(S<l?S/l:S<l+x?1-(S-l)/x*(1-T):S<l+x+r?T:S<F-v?(F-S-v)/i*T:0),z=v?z/2+(v>S?0:(S<F-v?1:(F-S)/v)*a[S-v|0]/2/t):z,E&&(z=q=B*R+G*(R=N)+B*(N=z)-_*j-M*(j=q))),A+=(O=(n+=c+=f)*y.cos(h*I++))+O*g*L(S**5),P&&++P>u&&(n+=p,D+=p,P=0),!d||++k%d||(n=D,c=H,P=P||1);(t=e.createBuffer(1,F,44100)).getChannelData(0).set(a),(n=e.createBufferSource()).buffer=t,n.connect(e.destination),n.start()},a=["#000","#6a7799","#aec2c2","#FFF1E8","#d82800","#f8d878","#155fd9","#3cbcfc","#327345","#63c64d","#6844fc","#ac7c00"];globalThis.litecanvas=function(e={}){let n=globalThis,l=Math.PI,r=2*l,i=(e,t,a)=>e.addEventListener(t,a);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 o=!1,s=[],c=e.canvas||document.createElement("canvas"),f=e.fullscreen,p=e.autoscale,u=e.animate,d=1,g,h=1,m,v=1/e.fps,T=1e3*v,x=0,b,E=0,y=0,w="sans-serif",H="",D=32,A=Date.now(),I={init:!1,update:!1,draw:!1,resized:!1,tap:!1,untap:!1,tapping:!1,tapped:!1},S={settings:Object.assign({},e),colors:a},P={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,FPS:e.fps,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],PI:l,TWO_PI:r,HALF_PI:.5*l,lerp:(e,t,a)=>e+a*(t-e),deg2rad:e=>l/180*e,rad2deg:e=>180/l*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,n,l,r=!1){let i=(e-t)/(a-t)*(l-n)+n;return r?P.clamp(i,n,l):i},norm:(e,t,a)=>P.map(e,t,a,0,1),rand:(e=0,t=1)=>(A=(1664525*A+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>P.floor(P.rand(e,t+1)),seed:e=>null==e?A:A=~~e,cls(e){null==e?g.clearRect(0,0,P.WIDTH,P.HEIGHT):P.rectfill(0,0,P.WIDTH,P.HEIGHT,e)},rect(e,t,a,n,l=0,r=null){g.beginPath(),g[r?"roundRect":"rect"](~~e,~~t,a,n,r),P.stroke(l)},rectfill(e,t,a,n,l=0,r=null){g.beginPath(),g[r?"roundRect":"rect"](~~e,~~t,a,n,r),P.fill(l)},circ(e,t,a,n){g.beginPath(),g.arc(~~e,~~t,a,0,r),g.closePath(),P.stroke(n)},circfill(e,t,a,n){g.beginPath(),g.arc(~~e,~~t,a,0,r),g.closePath(),P.fill(n)},line(e,t,a,n,l){g.beginPath(),g.moveTo(~~e,~~t),g.lineTo(~~a,~~n),P.stroke(l)},linewidth(e){g.lineWidth=e},linedash(e,t=0){g.setLineDash(Array.isArray(e)?e:[e]),g.lineDashOffset=t},text(e,t,a,n=3){g.font=`${H} ${D}px ${w}`,g.fillStyle=P.getcolor(n),g.fillText(a,~~e,~~t)},textfont(e){w=e},textsize(e){D=e},textstyle(e){H=e||""},textalign(e,t){e&&(g.textAlign=e),t&&(g.textBaseline=t)},textmetrics(e,t=D){g.font=`${H} ${t}px ${w}`;let a=g.measureText(e);return a.height=a.actualBoundingBoxAscent+a.actualBoundingBoxDescent,a},image(e,t,a){g.drawImage(a,~~e,~~t)},paint(e,t,a,n={}){let l=n.canvas||new OffscreenCanvas(1,1),r=n.scale||1,i=g;if(l.width=e*r,l.height=t*r,(g=l.getContext("2d")).scale(r,r),Array.isArray(a)){let e=0,t=0;for(let n of(g.imageSmoothingEnabled=!1,a)){for(let a of n)" "!==a&&"."!==a&&P.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(l,g);return g=i,l},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),transform:(e,t,a,n,l,r,i=!0)=>g[i?"setTransform":"transform"](e,t,a,n,l,r),alpha(e){g.globalAlpha=P.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){g.fillStyle=P.getcolor(e),t?g.fill(t):g.fill()},stroke(e,t){g.strokeStyle=P.getcolor(e),t?g.stroke(t):g.stroke()},cliprect(e,t,a,n){g.beginPath(),g.rect(e,t,a,n),g.clip()},clipcirc(e,t,a){g.beginPath(),g.arc(e,t,a,0,r),g.clip()},sfx:(e,a=0,l=1)=>!(n.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||P.DEFAULT_SFX,(a>0||1!==l)&&((e=e.slice())[0]=l*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){n.zzfxV=+e},colrect:(e,t,a,n,l,r,i,o)=>e<l+i&&e+a>l&&t<r+o&&t+n>r,colcirc:(e,t,a,n,l,r)=>(n-e)**2+(l-t)**2<=(a+r)**2,timescale(e){h=e},use(e,t={}){e.__conf=t,o?O(e):s.push(e)},listen:(e,t)=>(I[e]=I[e]||[],I[e].push(t),()=>{I[e]=I[e].filter(e=>t!==e)}),emit(e,t,a,n,l){F("before:"+e,t,a,n,l),F(e,t,a,n,l),F("after:"+e,t,a,n,l)},getcolor:e=>a[~~e%a.length],setvar(t,a){P[t]=a,e.global&&(n[t]=a)},resize(e,t){P.setvar("WIDTH",c.width=e),P.setvar("HEIGHT",c.height=t||e),z()}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])P[e]=Math[e];function k(){o=!0,c="string"==typeof c?document.querySelector(c):c,P.setvar("CANVAS",c),g=c.getContext("2d"),P.WIDTH>0&&(f=!1),c.width=P.WIDTH,c.height=P.HEIGHT||P.WIDTH,c.parentNode||document.body.appendChild(c),c.style.display="block",f?(c.style.position="absolute",c.style.inset=0):p&&(c.style.margin="auto");let t=e.loop?e.loop:n;for(let e of Object.keys(I))t[e]&&P.listen(e,t[e]);for(let e of s)O(e);if(i(n,"resize",z),z(),e.tapEvents){let e=(e,t)=>[(e-c.offsetLeft)/d,(t-c.offsetTop)/d],t=/* @__PURE__ */new Map,a=(e,a,n)=>{let l={x:a,y:n,startX:a,startY:n,ts:performance.now()};return t.set(e,l),l},l=(e,n,l)=>{let r=t.get(e)||a(e);r.x=n,r.y=l},r=e=>e&&performance.now()-e.ts<=200,o=!1;i(c,"mousedown",t=>{t.preventDefault();let[n,l]=e(t.pageX,t.pageY);P.emit("tap",n,l,0),a(0,n,l),o=!0}),i(c,"mousemove",t=>{t.preventDefault();let[a,n]=e(t.pageX,t.pageY);P.setvar("MOUSEX",a),P.setvar("MOUSEY",n),o&&(P.emit("tapping",a,n,0),l(0,a,n))}),i(c,"mouseup",a=>{a.preventDefault();let n=t.get(0),[l,i]=e(a.pageX,a.pageY);r(n)&&P.emit("tapped",n.startX,n.startY,0),P.emit("untap",l,i,0),t.delete(0),o=!1}),i(c,"touchstart",t=>{for(let n of(t.preventDefault(),t.changedTouches)){let[t,l]=e(n.pageX,n.pageY);P.emit("tap",t,l,n.identifier+1),a(n.identifier+1,t,l)}}),i(c,"touchmove",t=>{for(let a of(t.preventDefault(),t.changedTouches)){let[t,n]=e(a.pageX,a.pageY);P.emit("tapping",t,n,a.identifier+1),l(a.identifier+1,t,n)}});let s=e=>{e.preventDefault();let a=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)a.push(t.identifier+1);for(let[e,n]of t)a.includes(e)||(r(n)&&P.emit("tapped",n.startX,n.startY,e),P.emit("untap",n.x,n.y,e),t.delete(e))};i(c,"touchend",s),i(c,"touchcancel",s),i(n,"blur",()=>{for(let[e,a]of(o=!1,t))P.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=/* @__PURE__ */new Set;P.setvar("iskeydown",t=>"any"===t?e.size>0:e.has(t.toLowerCase())),i(n,"keydown",t=>{e.add(t.key.toLowerCase())}),i(n,"keyup",t=>{e.delete(t.key.toLowerCase())}),i(n,"blur",()=>e.clear())}e.pauseOnBlur&&(i(n,"blur",()=>{b=null}),i(n,"focus",()=>{b||(m=performance.now(),b=requestAnimationFrame(C))})),P.emit("init",P),m=performance.now(),b=requestAnimationFrame(C)}function C(e){let t=0,a=e-m;for(m=e,x+=a;x>=T;)P.emit("update",v*h),P.setvar("ELAPSED",P.ELAPSED+v*h),x-=T,t++;(t||!u)&&(P.textalign("start","top"),P.emit("draw"),E++,(y+=T*t)+x>=1e3&&(P.setvar("FPS",E),E=0,y-=1e3)),b&&u&&(b=requestAnimationFrame(C))}function z(){let t=n.innerWidth,a=n.innerHeight;f?(P.setvar("WIDTH",c.width=t),P.setvar("HEIGHT",c.height=a)):p&&(d=Math.min(t/P.WIDTH,a/P.HEIGHT),d=(e.pixelart?~~d:d)||1,c.style.width=P.WIDTH*d+"px",c.style.height=P.HEIGHT*d+"px"),P.setvar("CENTERX",P.WIDTH/2),P.setvar("CENTERY",P.HEIGHT/2),(!e.antialias||e.pixelart)&&(g.imageSmoothingEnabled=!1,c.style.imageRendering="pixelated"),P.emit("resized",d),u||(m=performance.now(),requestAnimationFrame(C))}function F(e,t,a,n,l){if(I[e])for(let r of I[e])r(t,a,n,l)}function O(e){let t=e(P,S,e.__conf);if("object"==typeof t)for(let[e,a]of Object.entries(t))P.setvar(e,a)}if(e.global){if(n.__litecanvas)throw"global litecanvas already instantiated";Object.assign(n,P),n.__litecanvas=P}return"loading"===document.readyState?i(n,"DOMContentLoaded",k):k(),P}})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litecanvas",
3
- "version": "0.58.2",
3
+ "version": "0.60.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
@@ -29,6 +29,7 @@ export default function litecanvas(settings = {}) {
29
29
  pauseOnBlur: true,
30
30
  tapEvents: true,
31
31
  keyboardEvents: true,
32
+ animate: true,
32
33
  }
33
34
 
34
35
  // setup the settings default values
@@ -44,12 +45,10 @@ export default function litecanvas(settings = {}) {
44
45
  _fullscreen = settings.fullscreen,
45
46
  /** @type {boolean} */
46
47
  _autoscale = settings.autoscale,
48
+ /** @type {boolean} */
49
+ _animate = settings.animate,
47
50
  /** @type {number} */
48
51
  _scale = 1,
49
- /** @type {number?} */
50
- _mouseX,
51
- /** @type {number?} */
52
- _mouseY,
53
52
  /** @type {CanvasRenderingContext2D} */
54
53
  _ctx,
55
54
  /** @type {number} */
@@ -117,10 +116,16 @@ export default function litecanvas(settings = {}) {
117
116
  FPS: settings.fps,
118
117
 
119
118
  /** @type {number} */
120
- CENTERX: null,
119
+ CENTERX: 0,
120
+
121
+ /** @type {number} */
122
+ CENTERY: 0,
123
+
124
+ /** @type {number} */
125
+ MOUSEX: -1,
121
126
 
122
127
  /** @type {number} */
123
- CENTERY: null,
128
+ MOUSEY: -1,
124
129
 
125
130
  /** @type {number[]} */
126
131
  DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
@@ -261,7 +266,7 @@ export default function litecanvas(settings = {}) {
261
266
  * @returns {number} the random number
262
267
  */
263
268
  randi: (min = 0, max = 1) =>
264
- instance.floor(instance.rand() * (max - min + 1) + min),
269
+ instance.floor(instance.rand(min, max + 1)),
265
270
 
266
271
  /**
267
272
  * If a value is passed, initializes the random number generator with an explicit seed value.
@@ -399,7 +404,7 @@ export default function litecanvas(settings = {}) {
399
404
  * @param {number} [color=3] the color index (generally from 0 to 7)
400
405
  */
401
406
  text(x, y, text, color = 3) {
402
- _ctx.font = `${_fontStyle || ''} ${_fontSize}px ${_fontFamily}`
407
+ _ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`
403
408
  _ctx.fillStyle = instance.getcolor(color)
404
409
  _ctx.fillText(text, ~~x, ~~y)
405
410
  },
@@ -423,12 +428,12 @@ export default function litecanvas(settings = {}) {
423
428
  },
424
429
 
425
430
  /**
426
- * Sets whether a font should be styled with a normal, italic, or bold.
431
+ * Sets whether a font should be styled with a "normal", "italic", or "bold".
427
432
  *
428
433
  * @param {string} style
429
434
  */
430
435
  textstyle(style) {
431
- _fontStyle = style
436
+ _fontStyle = style || ''
432
437
  },
433
438
 
434
439
  /**
@@ -452,9 +457,9 @@ export default function litecanvas(settings = {}) {
452
457
  * @returns {TextMetrics}
453
458
  * @see https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics
454
459
  */
455
- textmetrics(text, size) {
460
+ textmetrics(text, size = _fontSize) {
456
461
  // prettier-ignore
457
- _ctx.font = `${_fontStyle || ''} ${(size || _fontSize)}px ${_fontFamily}`
462
+ _ctx.font = `${_fontStyle} ${size}px ${_fontFamily}`
458
463
  const metrics = _ctx.measureText(text)
459
464
  metrics.height =
460
465
  metrics.actualBoundingBoxAscent +
@@ -485,7 +490,7 @@ export default function litecanvas(settings = {}) {
485
490
  * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
486
491
  */
487
492
  paint(width, height, draw, options = {}) {
488
- const oc = options.canvas || new OffscreenCanvas(0, 0),
493
+ const oc = options.canvas || new OffscreenCanvas(1, 1),
489
494
  scale = options.scale || 1,
490
495
  contextBackup = _ctx
491
496
 
@@ -539,18 +544,20 @@ export default function litecanvas(settings = {}) {
539
544
 
540
545
  /**
541
546
  * saves the current drawing style settings and transformations
547
+ *
542
548
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
543
549
  */
544
550
  push: () => _ctx.save(),
545
551
 
546
552
  /**
547
553
  * restores the drawing style settings and transformations
554
+ *
548
555
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
549
556
  */
550
557
  pop: () => _ctx.restore(),
551
558
 
552
559
  /**
553
- * Adds a translation transformation to the current matrix
560
+ * Adds a translation to the transformation matrix.
554
561
  *
555
562
  * @param {number} x
556
563
  * @param {number} y
@@ -566,15 +573,13 @@ export default function litecanvas(settings = {}) {
566
573
  scale: (x, y) => _ctx.scale(x, y || x),
567
574
 
568
575
  /**
569
- * Adds a rotation to the transformation matrix
576
+ * Adds a rotation to the transformation matrix.
570
577
  *
571
578
  * @param {number} radians
572
579
  */
573
580
  rotate: (radians) => _ctx.rotate(radians),
574
581
 
575
582
  /**
576
- * Adds a transformation that skews to the transformation matrix
577
- *
578
583
  * @param {number} a
579
584
  * @param {number} b
580
585
  * @param {number} c
@@ -582,7 +587,9 @@ export default function litecanvas(settings = {}) {
582
587
  * @param {number} e
583
588
  * @param {number} f
584
589
  * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
590
+ *
585
591
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
592
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
586
593
  */
587
594
  transform: (a, b, c, d, e, f, resetFirst = true) =>
588
595
  _ctx[resetFirst ? 'setTransform' : 'transform'](a, b, c, d, e, f),
@@ -616,7 +623,11 @@ export default function litecanvas(settings = {}) {
616
623
  */
617
624
  fill(color, path) {
618
625
  _ctx.fillStyle = instance.getcolor(color)
619
- _ctx.fill(path)
626
+ if (path) {
627
+ _ctx.fill(path)
628
+ } else {
629
+ _ctx.fill()
630
+ }
620
631
  },
621
632
 
622
633
  /**
@@ -627,7 +638,11 @@ export default function litecanvas(settings = {}) {
627
638
  */
628
639
  stroke(color, path) {
629
640
  _ctx.strokeStyle = instance.getcolor(color)
630
- path ? _ctx.stroke(path) : _ctx.stroke()
641
+ if (path) {
642
+ _ctx.stroke(path)
643
+ } else {
644
+ _ctx.stroke()
645
+ }
631
646
  },
632
647
 
633
648
  /**
@@ -667,17 +682,6 @@ export default function litecanvas(settings = {}) {
667
682
  _ctx.clip()
668
683
  },
669
684
 
670
- /**
671
- * Sets the type of compositing operation to apply when drawing new shapes.
672
- * Default value = 'source-over'.
673
- *
674
- * @param {string} value
675
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
676
- */
677
- blendmode(value) {
678
- _ctx.globalCompositeOperation = value
679
- },
680
-
681
685
  /** SOUND API */
682
686
  /**
683
687
  * Play a sound effects using ZzFX library.
@@ -754,12 +758,6 @@ export default function litecanvas(settings = {}) {
754
758
  colcirc: (x1, y1, r1, x2, y2, r2) =>
755
759
  (x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2,
756
760
 
757
- /**
758
- * Get the mouse position
759
- * @returns number[]
760
- */
761
- mousepos: () => [_mouseX, _mouseY],
762
-
763
761
  /**
764
762
  * The scale of the game's delta time (dt).
765
763
  * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
@@ -931,8 +929,13 @@ export default function litecanvas(settings = {}) {
931
929
 
932
930
  on(_canvas, 'mousemove', (ev) => {
933
931
  ev.preventDefault()
934
- const [x, y] = ([_mouseX, _mouseY] = _getXY(ev.pageX, ev.pageY))
932
+
933
+ const [x, y] = _getXY(ev.pageX, ev.pageY)
934
+ instance.setvar('MOUSEX', x)
935
+ instance.setvar('MOUSEY', y)
936
+
935
937
  if (!_pressingMouse) return
938
+
936
939
  instance.emit('tapping', x, y, 0)
937
940
  _updateTap(0, x, y)
938
941
  })
@@ -996,9 +999,6 @@ export default function litecanvas(settings = {}) {
996
999
 
997
1000
  on(root, 'blur', () => {
998
1001
  _pressingMouse = false
999
-
1000
- if (_taps.size === 0) return
1001
-
1002
1002
  for (const [id, tap] of _taps) {
1003
1003
  instance.emit('untap', tap.x, tap.y, id)
1004
1004
  _taps.delete(id)
@@ -1071,7 +1071,7 @@ export default function litecanvas(settings = {}) {
1071
1071
  ticks++
1072
1072
  }
1073
1073
 
1074
- if (ticks) {
1074
+ if (ticks || !_animate) {
1075
1075
  // default custom values for textAlign & textBaseline
1076
1076
  instance.textalign('start', 'top')
1077
1077
 
@@ -1086,7 +1086,9 @@ export default function litecanvas(settings = {}) {
1086
1086
  }
1087
1087
  }
1088
1088
 
1089
- if (_rafid) _rafid = requestAnimationFrame(drawFrame)
1089
+ if (_rafid && _animate) {
1090
+ _rafid = requestAnimationFrame(drawFrame)
1091
+ }
1090
1092
  }
1091
1093
 
1092
1094
  function setupCanvas() {
@@ -1128,7 +1130,7 @@ export default function litecanvas(settings = {}) {
1128
1130
  pageWidth / instance.WIDTH,
1129
1131
  pageHeight / instance.HEIGHT
1130
1132
  )
1131
- _scale = settings.pixelart ? Math.floor(_scale) : _scale
1133
+ _scale = (settings.pixelart ? ~~_scale : _scale) || 1
1132
1134
  _canvas.style.width = instance.WIDTH * _scale + 'px'
1133
1135
  _canvas.style.height = instance.HEIGHT * _scale + 'px'
1134
1136
  }
@@ -1143,6 +1145,11 @@ export default function litecanvas(settings = {}) {
1143
1145
  }
1144
1146
 
1145
1147
  instance.emit('resized', _scale)
1148
+
1149
+ if (!_animate) {
1150
+ _lastFrame = performance.now()
1151
+ requestAnimationFrame(drawFrame)
1152
+ }
1146
1153
  }
1147
1154
 
1148
1155
  function triggerEvent(eventName, arg1, arg2, arg3, arg4) {
package/src/palette.js CHANGED
@@ -1,17 +1,17 @@
1
1
  // Default colors inspired by https://lospec.com/palette-list/trirampo
2
2
  export const colors = [
3
- '#18161c',
3
+ '#000',
4
4
  '#6a7799',
5
5
  '#aec2c2',
6
- '#f3eade',
6
+ '#FFF1E8',
7
7
 
8
- '#f04f78',
9
- '#fcf660',
10
- '#2f328f',
11
- '#4b80ca',
8
+ '#d82800',
9
+ '#f8d878',
10
+ '#155fd9',
11
+ '#3cbcfc',
12
12
 
13
13
  '#327345',
14
14
  '#63c64d',
15
- '#703075',
16
- '#a56243',
15
+ '#6844fc',
16
+ '#ac7c00',
17
17
  ]
package/src/types.js CHANGED
@@ -13,6 +13,7 @@
13
13
  * @property {boolean} [global=true]
14
14
  * @property {boolean} [tapEvents=true]
15
15
  * @property {boolean} [keyboardEvents=true]
16
+ * @property {boolean} [animate=true]
16
17
  * @property {LitecanvasGameLoop} [loop]
17
18
  */
18
19
 
package/types/index.d.ts CHANGED
@@ -24,6 +24,10 @@ declare global {
24
24
  var CENTERX: number
25
25
  /** the center Y of the game screen */
26
26
  var CENTERY: number
27
+ /** The current mouse's horizontal (X) position or -1 (if the mouse was not used or detected) */
28
+ var MOUSEX: number | null
29
+ /** The current mouse's vertical (Y) position or -1 (if the mouse was not used or detected) */
30
+ var MOUSEY: number | null
27
31
  /** the default `sfx()` sound */
28
32
  var DEFAULT_SFX: number[]
29
33
 
@@ -418,8 +422,6 @@ declare global {
418
422
  */
419
423
  function rotate(radians: number): void
420
424
  /**
421
- * Adds a transformation that skews to the transformation matrix
422
- *
423
425
  * @param {number} a
424
426
  * @param {number} b
425
427
  * @param {number} c
@@ -427,7 +429,9 @@ declare global {
427
429
  * @param {number} e
428
430
  * @param {number} f
429
431
  * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
432
+ *
430
433
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
434
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
431
435
  */
432
436
  function transform(
433
437
  a: number,
@@ -496,14 +500,6 @@ declare global {
496
500
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
497
501
  */
498
502
  function clipcirc(x: number, y: number, radius: number): void
499
- /**
500
- * Sets the type of compositing operation to apply when drawing new shapes.
501
- * Default value = 'source-over'.
502
- *
503
- * @param {string} value
504
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
505
- */
506
- function blendmode(value: string): void
507
503
 
508
504
  /** SOUND API */
509
505
  /**
@@ -583,12 +579,6 @@ declare global {
583
579
  y2: number,
584
580
  r2: number
585
581
  ): boolean
586
- /**
587
- * Get the mouse position
588
- *
589
- * @returns number[]
590
- */
591
- function mousepos(): number[]
592
582
  /**
593
583
  * The scale of the game's delta time (dt).
594
584
  * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
package/types/types.d.ts CHANGED
@@ -13,6 +13,10 @@ type LitecanvasInstance = {
13
13
  CENTERX: number
14
14
  /** the center Y of the game screen */
15
15
  CENTERY: number
16
+ /** The current mouse's horizontal (X) position or -1 (if the mouse was not used or detected) */
17
+ MOUSEX: number
18
+ /** The current mouse's vertical (Y) position or -1 (if the mouse was not used or detected) */
19
+ MOUSEY: number
16
20
  /** the default `sfx()` sound */
17
21
  DEFAULT_SFX: number[]
18
22
 
@@ -401,8 +405,6 @@ type LitecanvasInstance = {
401
405
  */
402
406
  rotate(radians: number): void
403
407
  /**
404
- * Adds a transformation that skews to the transformation matrix
405
- *
406
408
  * @param {number} a
407
409
  * @param {number} b
408
410
  * @param {number} c
@@ -410,7 +412,9 @@ type LitecanvasInstance = {
410
412
  * @param {number} e
411
413
  * @param {number} f
412
414
  * @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
415
+ *
413
416
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
417
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
414
418
  */
415
419
  transform(
416
420
  a: number,
@@ -479,14 +483,6 @@ type LitecanvasInstance = {
479
483
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
480
484
  */
481
485
  clipcirc(x: number, y: number, radius: number): void
482
- /**
483
- * Sets the type of compositing operation to apply when drawing new shapes.
484
- * Default value = 'source-over'.
485
- *
486
- * @param {string} value
487
- * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
488
- */
489
- blendmode(value: string): void
490
486
 
491
487
  /** SOUND API */
492
488
  /**
@@ -567,12 +563,6 @@ type LitecanvasInstance = {
567
563
  y2: number,
568
564
  r2: number
569
565
  ): boolean
570
- /**
571
- * Get the mouse position
572
- *
573
- * @returns number[]
574
- */
575
- mousepos(): number[]
576
566
  /**
577
567
  * The scale of the game's delta time (dt).
578
568
  * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
@@ -699,6 +689,12 @@ type LitecanvasOptions = {
699
689
  * Useful when you want to implement your keyboard handler.
700
690
  */
701
691
  keyboardEvents?: boolean
692
+ /**
693
+ * default: `true`
694
+ *
695
+ * if `false` stops the code in `update()` and `draw()` from running repeatedly. By default, tries to run these functions 60 times per second.
696
+ */
697
+ animate?: boolean
702
698
  }
703
699
 
704
700
  type LitecanvasGameLoop = {