litecanvas 0.78.2 → 0.79.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dist.js CHANGED
@@ -25,12 +25,12 @@
25
25
 
26
26
  // src/index.js
27
27
  function litecanvas(settings = {}) {
28
- const root = globalThis, math = Math, PI = math.PI, TWO_PI = PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
28
+ const root = globalThis, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
29
29
  elem.addEventListener(evt, callback, false);
30
30
  _browserEventListeners.push(
31
31
  () => elem.removeEventListener(evt, callback, false)
32
32
  );
33
- }, isFinite = Number.isFinite, defaults = {
33
+ }, isNumber = Number.isFinite, defaults = {
34
34
  width: null,
35
35
  height: null,
36
36
  autoscale: true,
@@ -45,26 +45,26 @@
45
45
  animate: true
46
46
  };
47
47
  settings = Object.assign(defaults, settings);
48
- let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 18, _rng_seed = Date.now(), _global = settings.global, _events = {
49
- init: null,
50
- update: null,
51
- draw: null,
52
- resized: null,
53
- tap: null,
54
- untap: null,
55
- tapping: null,
56
- tapped: null
48
+ let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rng_seed = Date.now(), _events = {
49
+ init: false,
50
+ update: false,
51
+ draw: false,
52
+ resized: false,
53
+ tap: false,
54
+ untap: false,
55
+ tapping: false,
56
+ tapped: false
57
57
  }, _helpers = {
58
58
  settings: Object.assign({}, settings),
59
59
  colors
60
60
  };
61
61
  const instance = {
62
62
  /** @type {number} */
63
- WIDTH: settings.width,
63
+ WIDTH: 0,
64
64
  /** @type {number} */
65
- HEIGHT: settings.height || settings.width,
65
+ HEIGHT: 0,
66
66
  /** @type {HTMLCanvasElement} */
67
- CANVAS: null,
67
+ CANVAS: false,
68
68
  /** @type {number} */
69
69
  ELAPSED: 0,
70
70
  /** @type {number} */
@@ -76,7 +76,7 @@
76
76
  /** @type {number} */
77
77
  MOUSEY: -1,
78
78
  /** @type {number[]} */
79
- DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
79
+ DEFAULT_SFX: [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
80
80
  /** MATH API */
81
81
  /**
82
82
  * Twice the value of the mathematical constant PI (π).
@@ -94,7 +94,7 @@
94
94
  *
95
95
  * @type {number}
96
96
  */
97
- HALF_PI: PI / 2,
97
+ HALF_PI: math.PI / 2,
98
98
  /**
99
99
  * Calculates a linear (interpolation) value over t%.
100
100
  *
@@ -114,7 +114,7 @@
114
114
  * @returns {number} the value in radians
115
115
  */
116
116
  deg2rad: (degs) => {
117
- return PI / 180 * degs;
117
+ return math.PI / 180 * degs;
118
118
  },
119
119
  /**
120
120
  * Convert radians to degrees
@@ -123,7 +123,7 @@
123
123
  * @returns {number} the value in degrees
124
124
  */
125
125
  rad2deg: (rads) => {
126
- return 180 / PI * rads;
126
+ return 180 / math.PI * rads;
127
127
  },
128
128
  /**
129
129
  * Returns the rounded value of an number to optional precision (number of digits after the decimal point).
@@ -233,7 +233,7 @@
233
233
  /**
234
234
  * Clear the game screen with an optional color
235
235
  *
236
- * @param {number?} color The background color (index) or null/undefined (for transparent)
236
+ * @param {number} [color] The background color (index) or null/undefined (for transparent)
237
237
  */
238
238
  cls(color) {
239
239
  if (null == color) {
@@ -258,7 +258,7 @@
258
258
  * @param {number} [color=0] the color index
259
259
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
260
260
  */
261
- rect(x, y, width, height, color, radii = null) {
261
+ rect(x, y, width, height, color, radii) {
262
262
  _ctx.beginPath();
263
263
  _ctx[radii ? "roundRect" : "rect"](
264
264
  ~~x - _outline_fix,
@@ -279,7 +279,7 @@
279
279
  * @param {number} [color=0] the color index
280
280
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
281
281
  */
282
- rectfill(x, y, width, height, color, radii = null) {
282
+ rectfill(x, y, width, height, color, radii) {
283
283
  _ctx.beginPath();
284
284
  _ctx[radii ? "roundRect" : "rect"](
285
285
  ~~x,
@@ -341,7 +341,7 @@
341
341
  */
342
342
  linewidth(value) {
343
343
  _ctx.lineWidth = ~~value;
344
- _outline_fix = ~~value % 2 === 0 ? 0 : 0.5;
344
+ _outline_fix = 0 === ~~value % 2 ? 0 : 0.5;
345
345
  },
346
346
  /**
347
347
  * Sets the line dash pattern used when drawing lines
@@ -643,26 +643,13 @@
643
643
  * @param {*} value
644
644
  */
645
645
  setvar(key, value) {
646
- if (value == null) {
646
+ if (null == value) {
647
647
  }
648
648
  instance[key] = value;
649
- if (_global) {
649
+ if (settings.global) {
650
650
  root[key] = value;
651
651
  }
652
652
  },
653
- /**
654
- * Resizes the game canvas and emit the "resized" event
655
- *
656
- * @param {number} width
657
- * @param {number} height
658
- */
659
- resize(width, height) {
660
- instance.setvar("WIDTH", _canvas.width = width);
661
- instance.setvar("HEIGHT", _canvas.height = height);
662
- instance.setvar("CENTERX", instance.WIDTH / 2);
663
- instance.setvar("CENTERY", instance.HEIGHT / 2);
664
- onResize();
665
- },
666
653
  /**
667
654
  * The scale of the game's delta time (dt).
668
655
  * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
@@ -691,7 +678,7 @@
691
678
  for (const removeListener of _browserEventListeners) {
692
679
  removeListener();
693
680
  }
694
- if (_global) {
681
+ if (settings.global) {
695
682
  for (const key in instance) {
696
683
  delete root[key];
697
684
  }
@@ -712,7 +699,7 @@
712
699
  loadPlugin(callback, config);
713
700
  }
714
701
  if (settings.autoscale) {
715
- on(root, "resize", onResize);
702
+ on(root, "resize", resizeCanvas);
716
703
  }
717
704
  if (settings.tapEvents) {
718
705
  const _getXY = (pageX, pageY) => [
@@ -837,7 +824,7 @@
837
824
  * Checks if a which key is pressed (down) on the keyboard.
838
825
  * Note: use `iskeydown()` to check for any key.
839
826
  *
840
- * @param {string?} key
827
+ * @param {string} [key]
841
828
  * @returns {boolean}
842
829
  */
843
830
  (key) => {
@@ -850,7 +837,7 @@
850
837
  * Checks if a which key just got pressed on the keyboard.
851
838
  * Note: use `iskeypressed()` to check for any key.
852
839
  *
853
- * @param {string?} key
840
+ * @param {string} [key]
854
841
  * @returns {boolean}
855
842
  */
856
843
  (key) => {
@@ -875,7 +862,7 @@
875
862
  function drawFrame(now) {
876
863
  let updated = 0, frameTime = (now - _lastFrameTime) / 1e3;
877
864
  _lastFrameTime = now;
878
- if (_animated) {
865
+ if (settings.animate) {
879
866
  _rafid = raf(drawFrame);
880
867
  if (frameTime > 0.3) {
881
868
  return void 0;
@@ -899,39 +886,40 @@
899
886
  }
900
887
  }
901
888
  function setupCanvas() {
889
+ _canvas = settings.canvas || document.createElement("canvas");
902
890
  _canvas = "string" === typeof _canvas ? document.querySelector(_canvas) : _canvas;
903
891
  instance.setvar("CANVAS", _canvas);
904
892
  _ctx = _canvas.getContext("2d");
905
893
  on(_canvas, "click", () => root.focus());
906
894
  _canvas.style = "";
907
- if (!instance.WIDTH) {
908
- instance.WIDTH = root.innerWidth;
909
- instance.HEIGHT = root.innerHeight;
910
- }
911
- instance.resize(instance.WIDTH, instance.HEIGHT, false);
895
+ resizeCanvas();
912
896
  if (!_canvas.parentNode) document.body.appendChild(_canvas);
913
897
  }
914
- function onResize() {
915
- const styles = _canvas.style;
898
+ function resizeCanvas() {
899
+ const width = settings.width || root.innerWidth, height = settings.height || settings.width || root.innerHeight;
900
+ instance.setvar("WIDTH", _canvas.width = width);
901
+ instance.setvar("HEIGHT", _canvas.height = height);
902
+ instance.setvar("CENTERX", instance.WIDTH / 2);
903
+ instance.setvar("CENTERY", instance.HEIGHT / 2);
916
904
  if (settings.autoscale) {
917
- if (!styles.display) {
918
- styles.display = "block";
919
- styles.margin = "auto";
905
+ if (!_canvas.style.display) {
906
+ _canvas.style.display = "block";
907
+ _canvas.style.margin = "auto";
920
908
  }
921
909
  _scale = math.min(
922
910
  root.innerWidth / instance.WIDTH,
923
911
  root.innerHeight / instance.HEIGHT
924
912
  );
925
913
  _scale = (settings.pixelart ? ~~_scale : _scale) || 1;
926
- styles.width = instance.WIDTH * _scale + "px";
927
- styles.height = instance.HEIGHT * _scale + "px";
914
+ _canvas.style.width = instance.WIDTH * _scale + "px";
915
+ _canvas.style.height = instance.HEIGHT * _scale + "px";
928
916
  }
929
917
  if (!settings.antialias || settings.pixelart) {
930
918
  _ctx.imageSmoothingEnabled = false;
931
- styles.imageRendering = "pixelated";
919
+ _canvas.style.imageRendering = "pixelated";
932
920
  }
933
921
  instance.emit("resized", _scale);
934
- if (!_animated) {
922
+ if (!settings.animate) {
935
923
  raf(drawFrame);
936
924
  }
937
925
  }
@@ -947,9 +935,9 @@
947
935
  instance.setvar(key, pluginData[key]);
948
936
  }
949
937
  }
950
- if (_global) {
938
+ if (settings.global) {
951
939
  if (root.ENGINE) {
952
- throw "two global litecanvas detected";
940
+ throw new Error("two global litecanvas detected");
953
941
  }
954
942
  Object.assign(root, instance);
955
943
  root.ENGINE = instance;
package/dist/dist.min.js CHANGED
@@ -1 +1 @@
1
- (()=>{var e=new AudioContext,t=(t=1,a=.05,n=220,l=0,i=0,r=.1,o=0,s=1,c=0,f=0,d=0,u=0,p=0,g=0,h=0,m=0,E=0,v=1,b=0,w=0,x=0)=>{let T=Math,y=2*T.PI,H=c*=500*y/44100/44100,I=n*=(1-a+2*a*T.random(a=[]))*y/44100,S=0,D=0,k=0,A=1,C=0,X=0,z=0,N=x<0?-1:1,P=y*N*x*2/44100,L=T.cos(P),O=T.sin,Y=O(P)/4,F=1+Y,W=-2*L/F,G=(1-Y)/F,M=(1+N*L)/2/F,R=-(N+L)/F,B=0,U=0,q=0,V=0;for(l=44100*l+9,b*=44100,i*=44100,r*=44100,E*=44100,f*=500*y/85766121e6,h*=y/44100,d*=y/44100,u*=44100,p=44100*p|0,t*=.3*(globalThis.zzfxV||1),N=l+b+i+r+E|0;k<N;a[k++]=z*t)++X%(100*m|0)||(z=o?1<o?2<o?3<o?O(S*S):T.max(T.min(T.tan(S),1),-1):1-(2*S/y%2+2)%2:1-4*T.abs(T.round(S/y)-S/y):O(S),z=(p?1-w+w*O(y*k/p):1)*(z<0?-1:1)*T.abs(z)**s*(k<l?k/l:k<l+b?1-(k-l)/b*(1-v):k<l+b+i?v:k<N-E?(N-k-E)/r*v:0),z=E?z/2+(E>k?0:(k<N-E?1:(N-k)/E)*a[k-E|0]/2/t):z,x&&(z=V=M*B+R*(B=U)+M*(U=z)-G*q-W*(q=V))),S+=(P=(n+=c+=f)*T.cos(h*D++))+P*g*O(k**5),A&&++A>u&&(n+=d,I+=d,A=0),!p||++C%p||(n=I,c=H,A=A||1);(t=e.createBuffer(1,N,44100)).getChannelData(0).set(a),(n=e.createBufferSource()).buffer=t,n.connect(e.destination),n.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let n=globalThis,l=Math,i=l.PI,r=2*i,o=requestAnimationFrame,s=[],c=(e,t,a)=>{e.addEventListener(t,a,!1),s.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let f=!1,d=[],u=e.canvas||document.createElement("canvas"),p=e.animate,g=1,h,m=.5,E=1,v,b=1/60,w=0,x,T="sans-serif",y=18,H=Date.now(),I=e.global,S={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},D={settings:Object.assign({},e),colors:a},k={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],TWO_PI:r,HALF_PI:i/2,lerp:(e,t,a)=>a*(t-e)+e,deg2rad:e=>i/180*e,rad2deg:e=>180/i*e,round:(e,t=0)=>{if(!t)return l.round(e);let a=10**t;return l.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*l.floor((e-t)/(a-t)),map(e,t,a,n,l,i){let r=(e-t)/(a-t)*(l-n)+n;return i?k.clamp(r,n,l):r},norm:(e,t,a)=>k.map(e,t,a,0,1),rand:(e=0,t=1)=>(H=(1664525*H+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>l.floor(k.rand(e,t+1)),seed:e=>null==e?H:H=~~e,cls(e){null==e?h.clearRect(0,0,h.canvas.width,h.canvas.height):k.rectfill(0,0,h.canvas.width,h.canvas.height,e)},rect(e,t,a,n,l,i=null){h.beginPath(),h[i?"roundRect":"rect"](~~e-m,~~t-m,~~a+2*m,~~n+2*m,i),k.stroke(l)},rectfill(e,t,a,n,l,i=null){h.beginPath(),h[i?"roundRect":"rect"](~~e,~~t,~~a,~~n,i),k.fill(l)},circ(e,t,a,n){h.beginPath(),h.arc(~~e,~~t,~~a,0,r),k.stroke(n)},circfill(e,t,a,n){h.beginPath(),h.arc(~~e,~~t,~~a,0,r),k.fill(n)},line(e,t,a,n,l){h.beginPath();let i=.5*(0!==m&&~~e==~~a),r=.5*(0!==m&&~~t==~~n);h.moveTo(~~e+i,~~t+r),h.lineTo(~~a+i,~~n+r),k.stroke(l)},linewidth(e){h.lineWidth=~~e,m=.5*(~~e%2!=0)},linedash(e,t=0){h.setLineDash(e),h.lineDashOffset=t},text(e,t,a,n=3,l="normal"){h.font=`${l} ${y}px ${T}`,h.fillStyle=k.getcolor(n),h.fillText(a,~~e,~~t)},textfont(e){T=e},textsize(e){y=e},textalign(e,t){e&&(h.textAlign=e),t&&(h.textBaseline=t)},image(e,t,a){h.drawImage(a,~~e,~~t)},paint(e,t,a,n={}){let l=n.canvas||new OffscreenCanvas(1,1),i=n.scale||1,r=h;if(l.width=e*i,l.height=t*i,(h=l.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let n of(h.imageSmoothingEnabled=!1,a)){for(let a of n)" "!==a&&"."!==a&&k.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(h);return h=r,l},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),alpha(e){h.globalAlpha=k.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){h.fillStyle=k.getcolor(e),t?h.fill(t):h.fill()},stroke(e,t){h.strokeStyle=k.getcolor(e),t?h.stroke(t):h.stroke()},clip(e){h.clip(e)},sfx:(e,a=0,l=1)=>!(n.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||k.DEFAULT_SFX,(0!==a||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},use(e,t={}){f?N(e,t):d.push([e,t])},listen:(e,t)=>(S[e]=S[e]||new Set,S[e].add(t),()=>S[e].delete(t)),emit(e,t,a,n,l){f&&(z("before:"+e,t,a,n,l),z(e,t,a,n,l),z("after:"+e,t,a,n,l))},getcolor:e=>a[~~e%a.length],setvar(e,t){k[e]=t,I&&(n[e]=t)},resize(e,t){k.setvar("WIDTH",u.width=e),k.setvar("HEIGHT",u.height=t),k.setvar("CENTERX",k.WIDTH/2),k.setvar("CENTERY",k.HEIGHT/2),X()},timescale(e){E=e},setfps(e){b=1/~~e},quit(){for(let e of(cancelAnimationFrame(x),k.emit("quit"),S=[],s))e();if(I){for(let e in k)delete n[e];delete n.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))k[e]=l[e];function A(){f=!0;let t=e.loop?e.loop:n;for(let e in S)t[e]&&k.listen(e,t[e]);for(let[e,t]of d)N(e,t);if(e.autoscale&&c(n,"resize",X),e.tapEvents){let e=(e,t)=>[(e-u.offsetLeft)/g,(t-u.offsetTop)/g],t=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 i=t.get(e)||a(e);i.x=n,i.y=l},i=e=>e&&performance.now()-e.ts<=300,r=e=>e.preventDefault(),o=!1;c(u,"mousedown",t=>{if(0===t.button){r(t);let[n,l]=e(t.pageX,t.pageY);k.emit("tap",n,l,0),a(0,n,l),o=!0}}),c(u,"mouseup",a=>{if(0===a.button){r(a);let n=t.get(0),[l,s]=e(a.pageX,a.pageY);i(n)&&k.emit("tapped",n.startX,n.startY,0),k.emit("untap",l,s,0),t.delete(0),o=!1}}),c(u,"mousemove",t=>{r(t);let[a,n]=e(t.pageX,t.pageY);k.setvar("MOUSEX",a),k.setvar("MOUSEY",n),o&&(k.emit("tapping",a,n,0),l(0,a,n))}),c(u,"touchstart",t=>{for(let n of(r(t),t.changedTouches)){let[t,l]=e(n.pageX,n.pageY);k.emit("tap",t,l,n.identifier+1),a(n.identifier+1,t,l)}}),c(u,"touchmove",t=>{for(let a of(r(t),t.changedTouches)){let[t,n]=e(a.pageX,a.pageY);k.emit("tapping",t,n,a.identifier+1),l(a.identifier+1,t,n)}});let s=e=>{r(e);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)||(i(n)&&k.emit("tapped",n.startX,n.startY,e),k.emit("untap",n.x,n.y,e),t.delete(e))};c(u,"touchend",s),c(u,"touchcancel",s),c(n,"blur",()=>{for(let[e,a]of(o=!1,t))k.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=e=>e.toLowerCase(),t=new Set,a=new Set,l=(t,a)=>a?t.has("space"===e(a)?" ":e(a)):t.size>0;c(n,"keydown",n=>{t.has(e(n.key))||(t.add(e(n.key)),a.add(e(n.key)))}),c(n,"keyup",a=>{t.delete(e(a.key))}),c(n,"blur",()=>t.clear()),k.listen("after:draw",()=>a.clear()),k.setvar("iskeydown",e=>l(t,e)),k.setvar("iskeypressed",e=>l(a,e))}e.pauseOnBlur&&(c(n,"blur",()=>{x=cancelAnimationFrame(x)}),c(n,"focus",()=>{x||(x=o(C))})),k.emit("init",k),v=performance.now(),x=o(C)}function C(e){let t=0,a=(e-v)/1e3;if(v=e,p){if(x=o(C),a>.3)return;for(w+=a;w>=b;)k.emit("update",b*E),k.setvar("ELAPSED",k.ELAPSED+b*E),t++,w-=b}else t=1;t&&(k.textalign("start","top"),k.emit("draw"))}function X(){let t=u.style;e.autoscale&&(t.display||(t.display="block",t.margin="auto"),g=l.min(n.innerWidth/k.WIDTH,n.innerHeight/k.HEIGHT),g=(e.pixelart?~~g:g)||1,t.width=k.WIDTH*g+"px",t.height=k.HEIGHT*g+"px"),(!e.antialias||e.pixelart)&&(h.imageSmoothingEnabled=!1,t.imageRendering="pixelated"),k.emit("resized",g),p||o(C)}function z(e,t,a,n,l){if(S[e])for(let i of S[e])i(t,a,n,l)}function N(e,t){let a=e(k,D,t);for(let e in a)k.setvar(e,a[e])}if(I){if(n.ENGINE)throw"two global litecanvas detected";Object.assign(n,k),n.ENGINE=k}return u="string"==typeof u?document.querySelector(u):u,k.setvar("CANVAS",u),h=u.getContext("2d"),c(u,"click",()=>n.focus()),u.style="",k.WIDTH||(k.WIDTH=n.innerWidth,k.HEIGHT=n.innerHeight),k.resize(k.WIDTH,k.HEIGHT,!1),u.parentNode||document.body.appendChild(u),"loading"===document.readyState?c(n,"DOMContentLoaded",()=>o(A)):o(A),k}})();
1
+ (()=>{var e=new AudioContext,t=(t=1,a=.05,n=220,i=0,l=0,r=.1,o=0,s=1,c=0,f=0,d=0,p=0,u=0,g=0,h=0,m=0,v=0,E=1,b=0,w=0,x=0)=>{let y=Math,T=2*y.PI,I=c*=500*T/44100/44100,S=n*=(1-a+2*a*y.random(a=[]))*T/44100,k=0,A=0,H=0,D=1,P=0,C=0,X=0,N=x<0?-1:1,L=T*N*x*2/44100,O=y.cos(L),Y=y.sin,z=Y(L)/4,F=1+z,G=-2*O/F,M=(1-z)/F,R=(1+N*O)/2/F,W=-(N+O)/F,B=0,U=0,q=0,V=0;for(i=44100*i+9,b*=44100,l*=44100,r*=44100,v*=44100,f*=500*T/85766121e6,h*=T/44100,d*=T/44100,p*=44100,u=44100*u|0,t*=.3*(globalThis.zzfxV||1),N=i+b+l+r+v|0;H<N;a[H++]=X*t)++C%(100*m|0)||(X=o?1<o?2<o?3<o?Y(k*k):y.max(y.min(y.tan(k),1),-1):1-(2*k/T%2+2)%2:1-4*y.abs(y.round(k/T)-k/T):Y(k),X=(u?1-w+w*Y(T*H/u):1)*(X<0?-1:1)*y.abs(X)**s*(H<i?H/i:H<i+b?1-(H-i)/b*(1-E):H<i+b+l?E:H<N-v?(N-H-v)/r*E:0),X=v?X/2+(v>H?0:(H<N-v?1:(N-H)/v)*a[H-v|0]/2/t):X,x&&(X=V=R*B+W*(B=U)+R*(U=X)-M*q-G*(q=V))),k+=(L=(n+=c+=f)*y.cos(h*A++))+L*g*Y(H**5),D&&++D>p&&(n+=d,S+=d,D=0),!u||++P%u||(n=S,c=I,D=D||1);(t=e.createBuffer(1,N,44100)).getChannelData(0).set(a),(n=e.createBufferSource()).buffer=t,n.connect(e.destination),n.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let n=globalThis,i=Math,l=2*i.PI,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let c=!1,f=[],d,p=1,u,g=.5,h=1,m,v=1/60,E=0,b,w="sans-serif",x=20,y=Date.now(),T={init:!1,update:!1,draw:!1,resized:!1,tap:!1,untap:!1,tapping:!1,tapped:!1},I={settings:Object.assign({},e),colors:a},S={WIDTH:0,HEIGHT:0,CANVAS:!1,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,0,1750,,,.3,1,,,,600,.1],TWO_PI:l,HALF_PI:i.PI/2,lerp:(e,t,a)=>a*(t-e)+e,deg2rad:e=>i.PI/180*e,rad2deg:e=>180/i.PI*e,round:(e,t=0)=>{if(!t)return i.round(e);let a=10**t;return i.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*i.floor((e-t)/(a-t)),map(e,t,a,n,i,l){let r=(e-t)/(a-t)*(i-n)+n;return l?S.clamp(r,n,i):r},norm:(e,t,a)=>S.map(e,t,a,0,1),rand:(e=0,t=1)=>(y=(1664525*y+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>i.floor(S.rand(e,t+1)),seed:e=>null==e?y:y=~~e,cls(e){null==e?u.clearRect(0,0,u.canvas.width,u.canvas.height):S.rectfill(0,0,u.canvas.width,u.canvas.height,e)},rect(e,t,a,n,i,l){u.beginPath(),u[l?"roundRect":"rect"](~~e-g,~~t-g,~~a+2*g,~~n+2*g,l),S.stroke(i)},rectfill(e,t,a,n,i,l){u.beginPath(),u[l?"roundRect":"rect"](~~e,~~t,~~a,~~n,l),S.fill(i)},circ(e,t,a,n){u.beginPath(),u.arc(~~e,~~t,~~a,0,l),S.stroke(n)},circfill(e,t,a,n){u.beginPath(),u.arc(~~e,~~t,~~a,0,l),S.fill(n)},line(e,t,a,n,i){u.beginPath();let l=.5*(0!==g&&~~e==~~a),r=.5*(0!==g&&~~t==~~n);u.moveTo(~~e+l,~~t+r),u.lineTo(~~a+l,~~n+r),S.stroke(i)},linewidth(e){u.lineWidth=~~e,g=.5*(0!=~~e%2)},linedash(e,t=0){u.setLineDash(e),u.lineDashOffset=t},text(e,t,a,n=3,i="normal"){u.font=`${i} ${x}px ${w}`,u.fillStyle=S.getcolor(n),u.fillText(a,~~e,~~t)},textfont(e){w=e},textsize(e){x=e},textalign(e,t){e&&(u.textAlign=e),t&&(u.textBaseline=t)},image(e,t,a){u.drawImage(a,~~e,~~t)},paint(e,t,a,n={}){let i=n.canvas||new OffscreenCanvas(1,1),l=n.scale||1,r=u;if(i.width=e*l,i.height=t*l,(u=i.getContext("2d")).scale(l,l),a.push){let e=0,t=0;for(let n of(u.imageSmoothingEnabled=!1,a)){for(let a of n)" "!==a&&"."!==a&&S.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(u);return u=r,i},ctx:e=>(e&&(u=e),u),push:()=>u.save(),pop:()=>u.restore(),translate:(e,t)=>u.translate(~~e,~~t),scale:(e,t)=>u.scale(e,t||e),rotate:e=>u.rotate(e),alpha(e){u.globalAlpha=S.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){u.fillStyle=S.getcolor(e),t?u.fill(t):u.fill()},stroke(e,t){u.strokeStyle=S.getcolor(e),t?u.stroke(t):u.stroke()},clip(e){u.clip(e)},sfx:(e,a=0,i=1)=>!(n.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||S.DEFAULT_SFX,(0!==a||1!==i)&&((e=e.slice())[0]=i*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){n.zzfxV=e},use(e,t={}){c?P(e,t):f.push([e,t])},listen:(e,t)=>(T[e]=T[e]||new Set,T[e].add(t),()=>T[e].delete(t)),emit(e,t,a,n,i){c&&(D("before:"+e,t,a,n,i),D(e,t,a,n,i),D("after:"+e,t,a,n,i))},getcolor:e=>a[~~e%a.length],setvar(t,a){S[t]=a,e.global&&(n[t]=a)},timescale(e){h=e},setfps(e){v=1/~~e},quit(){for(let e of(cancelAnimationFrame(b),S.emit("quit"),T=[],o))e();if(e.global){for(let e in S)delete n[e];delete n.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))S[e]=i[e];function k(){c=!0;let t=e.loop?e.loop:n;for(let e in T)t[e]&&S.listen(e,t[e]);for(let[e,t]of f)P(e,t);if(e.autoscale&&s(n,"resize",H),e.tapEvents){let e=(e,t)=>[(e-d.offsetLeft)/p,(t-d.offsetTop)/p],t=new Map,a=(e,a,n)=>{let i={x:a,y:n,startX:a,startY:n,ts:performance.now()};return t.set(e,i),i},i=(e,n,i)=>{let l=t.get(e)||a(e);l.x=n,l.y=i},l=e=>e&&performance.now()-e.ts<=300,r=e=>e.preventDefault(),o=!1;s(d,"mousedown",t=>{if(0===t.button){r(t);let[n,i]=e(t.pageX,t.pageY);S.emit("tap",n,i,0),a(0,n,i),o=!0}}),s(d,"mouseup",a=>{if(0===a.button){r(a);let n=t.get(0),[i,s]=e(a.pageX,a.pageY);l(n)&&S.emit("tapped",n.startX,n.startY,0),S.emit("untap",i,s,0),t.delete(0),o=!1}}),s(d,"mousemove",t=>{r(t);let[a,n]=e(t.pageX,t.pageY);S.setvar("MOUSEX",a),S.setvar("MOUSEY",n),o&&(S.emit("tapping",a,n,0),i(0,a,n))}),s(d,"touchstart",t=>{for(let n of(r(t),t.changedTouches)){let[t,i]=e(n.pageX,n.pageY);S.emit("tap",t,i,n.identifier+1),a(n.identifier+1,t,i)}}),s(d,"touchmove",t=>{for(let a of(r(t),t.changedTouches)){let[t,n]=e(a.pageX,a.pageY);S.emit("tapping",t,n,a.identifier+1),i(a.identifier+1,t,n)}});let c=e=>{r(e);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)||(l(n)&&S.emit("tapped",n.startX,n.startY,e),S.emit("untap",n.x,n.y,e),t.delete(e))};s(d,"touchend",c),s(d,"touchcancel",c),s(n,"blur",()=>{for(let[e,a]of(o=!1,t))S.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=e=>e.toLowerCase(),t=new Set,a=new Set,i=(t,a)=>a?t.has("space"===e(a)?" ":e(a)):t.size>0;s(n,"keydown",n=>{t.has(e(n.key))||(t.add(e(n.key)),a.add(e(n.key)))}),s(n,"keyup",a=>{t.delete(e(a.key))}),s(n,"blur",()=>t.clear()),S.listen("after:draw",()=>a.clear()),S.setvar("iskeydown",e=>i(t,e)),S.setvar("iskeypressed",e=>i(a,e))}e.pauseOnBlur&&(s(n,"blur",()=>{b=cancelAnimationFrame(b)}),s(n,"focus",()=>{b||(b=r(A))})),S.emit("init",S),m=performance.now(),b=r(A)}function A(t){let a=0,n=(t-m)/1e3;if(m=t,e.animate){if(b=r(A),n>.3)return;for(E+=n;E>=v;)S.emit("update",v*h),S.setvar("ELAPSED",S.ELAPSED+v*h),a++,E-=v}else a=1;a&&(S.textalign("start","top"),S.emit("draw"))}function H(){let t=e.width||n.innerWidth,a=e.height||e.width||n.innerHeight;S.setvar("WIDTH",d.width=t),S.setvar("HEIGHT",d.height=a),S.setvar("CENTERX",S.WIDTH/2),S.setvar("CENTERY",S.HEIGHT/2),e.autoscale&&(d.style.display||(d.style.display="block",d.style.margin="auto"),p=i.min(n.innerWidth/S.WIDTH,n.innerHeight/S.HEIGHT),p=(e.pixelart?~~p:p)||1,d.style.width=S.WIDTH*p+"px",d.style.height=S.HEIGHT*p+"px"),(!e.antialias||e.pixelart)&&(u.imageSmoothingEnabled=!1,d.style.imageRendering="pixelated"),S.emit("resized",p),e.animate||r(A)}function D(e,t,a,n,i){if(T[e])for(let l of T[e])l(t,a,n,i)}function P(e,t){let a=e(S,I,t);for(let e in a)S.setvar(e,a[e])}if(e.global){if(n.ENGINE)throw Error("two global litecanvas detected");Object.assign(n,S),n.ENGINE=S}return d="string"==typeof(d=e.canvas||document.createElement("canvas"))?document.querySelector(d):d,S.setvar("CANVAS",d),u=d.getContext("2d"),s(d,"click",()=>n.focus()),d.style="",H(),d.parentNode||document.body.appendChild(d),"loading"===document.readyState?s(n,"DOMContentLoaded",()=>r(k)):r(k),S}})();
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "litecanvas",
3
- "version": "0.78.2",
3
+ "version": "0.79.1",
4
4
  "description": "Lightweight HTML5 canvas game engine suitable for small projects and creative coding. Inspired by PICO-8 and P5/Processing.",
5
5
  "license": "MIT",
6
6
  "author": "Luiz Bills <luizbills@pm.me>",
7
7
  "contributors": [],
8
8
  "devDependencies": {
9
- "@swc/core": "^1.11.11",
9
+ "@swc/core": "^1.11.13",
10
10
  "ava": "^6.2.0",
11
11
  "esbuild": "^0.25.1",
12
12
  "gzip-size": "^7.0.0",