litecanvas 0.58.2 → 0.59.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 +34 -34
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +38 -41
- package/types/index.d.ts +6 -16
- package/types/types.d.ts +6 -16
package/dist/dist.js
CHANGED
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
keyboardEvents: true
|
|
60
60
|
};
|
|
61
61
|
settings = Object.assign(defaults, settings);
|
|
62
|
-
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _scale = 1,
|
|
62
|
+
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _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
63
|
init: false,
|
|
64
64
|
update: false,
|
|
65
65
|
draw: false,
|
|
@@ -84,9 +84,13 @@
|
|
|
84
84
|
/** @type {number} */
|
|
85
85
|
FPS: settings.fps,
|
|
86
86
|
/** @type {number} */
|
|
87
|
-
CENTERX:
|
|
87
|
+
CENTERX: 0,
|
|
88
88
|
/** @type {number} */
|
|
89
|
-
CENTERY:
|
|
89
|
+
CENTERY: 0,
|
|
90
|
+
/** @type {number} */
|
|
91
|
+
MOUSEX: -1,
|
|
92
|
+
/** @type {number} */
|
|
93
|
+
MOUSEY: -1,
|
|
90
94
|
/** @type {number[]} */
|
|
91
95
|
DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
|
|
92
96
|
/** MATH API */
|
|
@@ -209,7 +213,7 @@
|
|
|
209
213
|
* @param {number} [max=1]
|
|
210
214
|
* @returns {number} the random number
|
|
211
215
|
*/
|
|
212
|
-
randi: (min = 0, max = 1) => instance.floor(instance.rand(
|
|
216
|
+
randi: (min = 0, max = 1) => instance.floor(instance.rand(min, max + 1)),
|
|
213
217
|
/**
|
|
214
218
|
* If a value is passed, initializes the random number generator with an explicit seed value.
|
|
215
219
|
* Otherwise, returns the current seed state.
|
|
@@ -337,7 +341,7 @@
|
|
|
337
341
|
* @param {number} [color=3] the color index (generally from 0 to 7)
|
|
338
342
|
*/
|
|
339
343
|
text(x, y, text, color = 3) {
|
|
340
|
-
_ctx.font = `${_fontStyle
|
|
344
|
+
_ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`;
|
|
341
345
|
_ctx.fillStyle = instance.getcolor(color);
|
|
342
346
|
_ctx.fillText(text, ~~x, ~~y);
|
|
343
347
|
},
|
|
@@ -358,12 +362,12 @@
|
|
|
358
362
|
_fontSize = size;
|
|
359
363
|
},
|
|
360
364
|
/**
|
|
361
|
-
* Sets whether a font should be styled with a normal, italic, or bold.
|
|
365
|
+
* Sets whether a font should be styled with a "normal", "italic", or "bold".
|
|
362
366
|
*
|
|
363
367
|
* @param {string} style
|
|
364
368
|
*/
|
|
365
369
|
textstyle(style) {
|
|
366
|
-
_fontStyle = style;
|
|
370
|
+
_fontStyle = style || "";
|
|
367
371
|
},
|
|
368
372
|
/**
|
|
369
373
|
* Sets the alignment used when drawing texts
|
|
@@ -385,8 +389,8 @@
|
|
|
385
389
|
* @returns {TextMetrics}
|
|
386
390
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics
|
|
387
391
|
*/
|
|
388
|
-
textmetrics(text, size) {
|
|
389
|
-
_ctx.font = `${_fontStyle
|
|
392
|
+
textmetrics(text, size = _fontSize) {
|
|
393
|
+
_ctx.font = `${_fontStyle} ${size}px ${_fontFamily}`;
|
|
390
394
|
const metrics = _ctx.measureText(text);
|
|
391
395
|
metrics.height = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
|
|
392
396
|
return metrics;
|
|
@@ -413,7 +417,7 @@
|
|
|
413
417
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
414
418
|
*/
|
|
415
419
|
paint(width, height, draw, options = {}) {
|
|
416
|
-
const oc = options.canvas || new OffscreenCanvas(
|
|
420
|
+
const oc = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1, contextBackup = _ctx;
|
|
417
421
|
oc.width = width * scale;
|
|
418
422
|
oc.height = height * scale;
|
|
419
423
|
_ctx = oc.getContext("2d");
|
|
@@ -453,16 +457,18 @@
|
|
|
453
457
|
},
|
|
454
458
|
/**
|
|
455
459
|
* saves the current drawing style settings and transformations
|
|
460
|
+
*
|
|
456
461
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
|
|
457
462
|
*/
|
|
458
463
|
push: () => _ctx.save(),
|
|
459
464
|
/**
|
|
460
465
|
* restores the drawing style settings and transformations
|
|
466
|
+
*
|
|
461
467
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
|
|
462
468
|
*/
|
|
463
469
|
pop: () => _ctx.restore(),
|
|
464
470
|
/**
|
|
465
|
-
* Adds a translation
|
|
471
|
+
* Adds a translation to the transformation matrix.
|
|
466
472
|
*
|
|
467
473
|
* @param {number} x
|
|
468
474
|
* @param {number} y
|
|
@@ -476,14 +482,12 @@
|
|
|
476
482
|
*/
|
|
477
483
|
scale: (x, y) => _ctx.scale(x, y || x),
|
|
478
484
|
/**
|
|
479
|
-
* Adds a rotation to the transformation matrix
|
|
485
|
+
* Adds a rotation to the transformation matrix.
|
|
480
486
|
*
|
|
481
487
|
* @param {number} radians
|
|
482
488
|
*/
|
|
483
489
|
rotate: (radians) => _ctx.rotate(radians),
|
|
484
490
|
/**
|
|
485
|
-
* Adds a transformation that skews to the transformation matrix
|
|
486
|
-
*
|
|
487
491
|
* @param {number} a
|
|
488
492
|
* @param {number} b
|
|
489
493
|
* @param {number} c
|
|
@@ -491,7 +495,9 @@
|
|
|
491
495
|
* @param {number} e
|
|
492
496
|
* @param {number} f
|
|
493
497
|
* @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
|
|
498
|
+
*
|
|
494
499
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
|
|
500
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
|
|
495
501
|
*/
|
|
496
502
|
transform: (a, b, c, d, e, f, resetFirst = true) => _ctx[resetFirst ? "setTransform" : "transform"](a, b, c, d, e, f),
|
|
497
503
|
/**
|
|
@@ -521,7 +527,11 @@
|
|
|
521
527
|
*/
|
|
522
528
|
fill(color, path) {
|
|
523
529
|
_ctx.fillStyle = instance.getcolor(color);
|
|
524
|
-
|
|
530
|
+
if (path) {
|
|
531
|
+
_ctx.fill(path);
|
|
532
|
+
} else {
|
|
533
|
+
_ctx.fill();
|
|
534
|
+
}
|
|
525
535
|
},
|
|
526
536
|
/**
|
|
527
537
|
* Outlines the current or given path with a given color.
|
|
@@ -531,7 +541,11 @@
|
|
|
531
541
|
*/
|
|
532
542
|
stroke(color, path) {
|
|
533
543
|
_ctx.strokeStyle = instance.getcolor(color);
|
|
534
|
-
|
|
544
|
+
if (path) {
|
|
545
|
+
_ctx.stroke(path);
|
|
546
|
+
} else {
|
|
547
|
+
_ctx.stroke();
|
|
548
|
+
}
|
|
535
549
|
},
|
|
536
550
|
/**
|
|
537
551
|
* Create a retangular clipping region.
|
|
@@ -568,16 +582,6 @@
|
|
|
568
582
|
_ctx.arc(x, y, radius, 0, TWO_PI);
|
|
569
583
|
_ctx.clip();
|
|
570
584
|
},
|
|
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
585
|
/** SOUND API */
|
|
582
586
|
/**
|
|
583
587
|
* Play a sound effects using ZzFX library.
|
|
@@ -639,11 +643,6 @@
|
|
|
639
643
|
* @returns {boolean}
|
|
640
644
|
*/
|
|
641
645
|
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
646
|
/**
|
|
648
647
|
* The scale of the game's delta time (dt).
|
|
649
648
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
@@ -788,7 +787,9 @@
|
|
|
788
787
|
});
|
|
789
788
|
on(_canvas, "mousemove", (ev) => {
|
|
790
789
|
ev.preventDefault();
|
|
791
|
-
const [x, y] =
|
|
790
|
+
const [x, y] = _getXY(ev.pageX, ev.pageY);
|
|
791
|
+
instance.setvar("MOUSEX", x);
|
|
792
|
+
instance.setvar("MOUSEY", y);
|
|
792
793
|
if (!_pressingMouse) return;
|
|
793
794
|
instance.emit("tapping", x, y, 0);
|
|
794
795
|
_updateTap(0, x, y);
|
|
@@ -843,7 +844,6 @@
|
|
|
843
844
|
on(_canvas, "touchcancel", _touchEndHandler);
|
|
844
845
|
on(root, "blur", () => {
|
|
845
846
|
_pressingMouse = false;
|
|
846
|
-
if (_taps.size === 0) return;
|
|
847
847
|
for (const [id, tap] of _taps) {
|
|
848
848
|
instance.emit("untap", tap.x, tap.y, id);
|
|
849
849
|
_taps.delete(id);
|
|
@@ -926,7 +926,7 @@
|
|
|
926
926
|
pageWidth / instance.WIDTH,
|
|
927
927
|
pageHeight / instance.HEIGHT
|
|
928
928
|
);
|
|
929
|
-
_scale = settings.pixelart ?
|
|
929
|
+
_scale = (settings.pixelart ? ~~_scale : _scale) || 1;
|
|
930
930
|
_canvas.style.width = instance.WIDTH * _scale + "px";
|
|
931
931
|
_canvas.style.height = instance.HEIGHT * _scale + "px";
|
|
932
932
|
}
|
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,
|
|
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,T=1,x=0,b=0,E=0)=>{let y=Math,w=2*y.PI,H=c*=500*w/44100/44100,D=l*=(1-a+2*a*y.random(a=[]))*w/44100,I=0,A=0,S=0,P=1,k=0,C=0,z=0,O=E<0?-1:1,X=w*O*E*2/44100,L=y.cos(X),W=y.sin,Y=W(X)/4,M=1+Y,_=-2*L/M,B=(1-Y)/M,F=(1+O*L)/2/M,G=-(O+L)/M,R=0,N=0,j=0,U=0;for(n=44100*n+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,O=n+x+r+i+v|0;S<O;a[S++]=z*t)++C%(100*m|0)||(z=o?1<o?2<o?3<o?W(I*I):y.max(y.min(y.tan(I),1),-1):1-(2*I/w%2+2)%2:1-4*y.abs(y.round(I/w)-I/w):W(I),z=(d?1-b+b*W(w*S/d):1)*(z<0?-1:1)*y.abs(z)**s*(S<n?S/n:S<n+x?1-(S-n)/x*(1-T):S<n+x+r?T:S<O-v?(O-S-v)/i*T:0),z=v?z/2+(v>S?0:(S<O-v?1:(O-S)/v)*a[S-v|0]/2/t):z,E&&(z=U=F*R+G*(R=N)+F*(N=z)-B*j-_*(j=U))),I+=(X=(l+=c+=f)*y.cos(h*A++))+X*g*W(S**5),P&&++P>u&&(l+=p,D+=p,P=0),!d||++k%d||(l=D,c=H,P=P||1);(t=e.createBuffer(1,O,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=1,h,m=1/e.fps,v=1e3*m,T=0,x,b=0,E=0,y="sans-serif",w="",H=32,D=Date.now(),I={init:!1,update:!1,draw:!1,resized:!1,tap:!1,untap:!1,tapping:!1,tapped:!1},A={settings:Object.assign({},e),colors:a},S={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: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?S.clamp(i,l,n):i},norm:(e,t,a)=>S.map(e,t,a,0,1),rand:(e=0,t=1)=>(D=(1664525*D+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>S.floor(S.rand(e,t+1)),seed:e=>null==e?D:D=~~e,cls(e){null==e?d.clearRect(0,0,S.WIDTH,S.HEIGHT):S.rectfill(0,0,S.WIDTH,S.HEIGHT,e)},rect(e,t,a,l,n=0,r=null){d.beginPath(),d[r?"roundRect":"rect"](~~e,~~t,a,l,r),S.stroke(n)},rectfill(e,t,a,l,n=0,r=null){d.beginPath(),d[r?"roundRect":"rect"](~~e,~~t,a,l,r),S.fill(n)},circ(e,t,a,l){d.beginPath(),d.arc(~~e,~~t,a,0,r),d.closePath(),S.stroke(l)},circfill(e,t,a,l){d.beginPath(),d.arc(~~e,~~t,a,0,r),d.closePath(),S.fill(l)},line(e,t,a,l,n){d.beginPath(),d.moveTo(~~e,~~t),d.lineTo(~~a,~~l),S.stroke(n)},linewidth(e){d.lineWidth=e},linedash(e,t=0){d.setLineDash(Array.isArray(e)?e:[e]),d.lineDashOffset=t},text(e,t,a,l=3){d.font=`${w} ${H}px ${y}`,d.fillStyle=S.getcolor(l),d.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){H=e},textstyle(e){w=e||""},textalign(e,t){e&&(d.textAlign=e),t&&(d.textBaseline=t)},textmetrics(e,t=H){d.font=`${w} ${t}px ${y}`;let a=d.measureText(e);return a.height=a.actualBoundingBoxAscent+a.actualBoundingBoxDescent,a},image(e,t,a){d.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),r=l.scale||1,i=d;if(n.width=e*r,n.height=t*r,(d=n.getContext("2d")).scale(r,r),Array.isArray(a)){let e=0,t=0;for(let l of(d.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&S.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(n,d);return d=i,n},ctx:e=>(e&&(d=e),d),push:()=>d.save(),pop:()=>d.restore(),translate:(e,t)=>d.translate(~~e,~~t),scale:(e,t)=>d.scale(e,t||e),rotate:e=>d.rotate(e),transform:(e,t,a,l,n,r,i=!0)=>d[i?"setTransform":"transform"](e,t,a,l,n,r),alpha(e){d.globalAlpha=S.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){d.fillStyle=S.getcolor(e),t?d.fill(t):d.fill()},stroke(e,t){d.strokeStyle=S.getcolor(e),t?d.stroke(t):d.stroke()},cliprect(e,t,a,l){d.beginPath(),d.rect(e,t,a,l),d.clip()},clipcirc(e,t,a){d.beginPath(),d.arc(e,t,a,0,r),d.clip()},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||S.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,timescale(e){g=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,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(t,a){S[t]=a,e.global&&(l[t]=a)},resize(e,t){S.setvar("WIDTH",c.width=e),S.setvar("HEIGHT",c.height=t||e),C()}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])S[e]=Math[e];function P(){o=!0,c="string"==typeof c?document.querySelector(c):c,S.setvar("CANVAS",c),d=c.getContext("2d"),S.WIDTH>0&&(f=!1),c.width=S.WIDTH,c.height=S.HEIGHT||S.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(I))t[e]&&S.listen(e,t[e]);for(let e of s)O(e);if(i(l,"resize",C),C(),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);S.emit("tap",l,n,0),a(0,l,n),o=!0}),i(c,"mousemove",t=>{t.preventDefault();let[a,l]=e(t.pageX,t.pageY);S.setvar("MOUSEX",a),S.setvar("MOUSEY",l),o&&(S.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)&&S.emit("tapped",l.startX,l.startY,0),S.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);S.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);S.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)&&S.emit("tapped",l.startX,l.startY,e),S.emit("untap",l.x,l.y,e),t.delete(e))};i(c,"touchend",s),i(c,"touchcancel",s),i(l,"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=/* @__PURE__ */new Set;S.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",()=>{x=null}),i(l,"focus",()=>{x||(h=performance.now(),x=requestAnimationFrame(k))})),S.emit("init",S),h=performance.now(),x=requestAnimationFrame(k)}function k(e){let t=0,a=e-h;for(h=e,T+=a;T>=v;)S.emit("update",m*g),S.setvar("ELAPSED",S.ELAPSED+m*g),T-=v,t++;t&&(S.textalign("start","top"),S.emit("draw"),b++,(E+=v*t)+T>=1e3&&(S.setvar("FPS",b),b=0,E-=1e3)),x&&(x=requestAnimationFrame(k))}function C(){let t=l.innerWidth,a=l.innerHeight;f?(S.setvar("WIDTH",c.width=t),S.setvar("HEIGHT",c.height=a)):p&&(u=Math.min(t/S.WIDTH,a/S.HEIGHT),u=(e.pixelart?~~u:u)||1,c.style.width=S.WIDTH*u+"px",c.style.height=S.HEIGHT*u+"px"),S.setvar("CENTERX",S.WIDTH/2),S.setvar("CENTERY",S.HEIGHT/2),(!e.antialias||e.pixelart)&&(d.imageSmoothingEnabled=!1,c.style.imageRendering="pixelated"),S.emit("resized",u)}function z(e,t,a,l,n){if(I[e])for(let r of I[e])r(t,a,l,n)}function O(e){let t=e(S,A,e.__conf);if("object"==typeof t)for(let[e,a]of Object.entries(t))S.setvar(e,a)}if(e.global){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,S),l.__litecanvas=S}return"loading"===document.readyState?i(l,"DOMContentLoaded",P):P(),S}})();
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -46,10 +46,6 @@ export default function litecanvas(settings = {}) {
|
|
|
46
46
|
_autoscale = settings.autoscale,
|
|
47
47
|
/** @type {number} */
|
|
48
48
|
_scale = 1,
|
|
49
|
-
/** @type {number?} */
|
|
50
|
-
_mouseX,
|
|
51
|
-
/** @type {number?} */
|
|
52
|
-
_mouseY,
|
|
53
49
|
/** @type {CanvasRenderingContext2D} */
|
|
54
50
|
_ctx,
|
|
55
51
|
/** @type {number} */
|
|
@@ -117,10 +113,16 @@ export default function litecanvas(settings = {}) {
|
|
|
117
113
|
FPS: settings.fps,
|
|
118
114
|
|
|
119
115
|
/** @type {number} */
|
|
120
|
-
CENTERX:
|
|
116
|
+
CENTERX: 0,
|
|
121
117
|
|
|
122
118
|
/** @type {number} */
|
|
123
|
-
CENTERY:
|
|
119
|
+
CENTERY: 0,
|
|
120
|
+
|
|
121
|
+
/** @type {number} */
|
|
122
|
+
MOUSEX: -1,
|
|
123
|
+
|
|
124
|
+
/** @type {number} */
|
|
125
|
+
MOUSEY: -1,
|
|
124
126
|
|
|
125
127
|
/** @type {number[]} */
|
|
126
128
|
DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
|
|
@@ -261,7 +263,7 @@ export default function litecanvas(settings = {}) {
|
|
|
261
263
|
* @returns {number} the random number
|
|
262
264
|
*/
|
|
263
265
|
randi: (min = 0, max = 1) =>
|
|
264
|
-
instance.floor(instance.rand(
|
|
266
|
+
instance.floor(instance.rand(min, max + 1)),
|
|
265
267
|
|
|
266
268
|
/**
|
|
267
269
|
* If a value is passed, initializes the random number generator with an explicit seed value.
|
|
@@ -399,7 +401,7 @@ export default function litecanvas(settings = {}) {
|
|
|
399
401
|
* @param {number} [color=3] the color index (generally from 0 to 7)
|
|
400
402
|
*/
|
|
401
403
|
text(x, y, text, color = 3) {
|
|
402
|
-
_ctx.font = `${_fontStyle
|
|
404
|
+
_ctx.font = `${_fontStyle} ${_fontSize}px ${_fontFamily}`
|
|
403
405
|
_ctx.fillStyle = instance.getcolor(color)
|
|
404
406
|
_ctx.fillText(text, ~~x, ~~y)
|
|
405
407
|
},
|
|
@@ -423,12 +425,12 @@ export default function litecanvas(settings = {}) {
|
|
|
423
425
|
},
|
|
424
426
|
|
|
425
427
|
/**
|
|
426
|
-
* Sets whether a font should be styled with a normal, italic, or bold.
|
|
428
|
+
* Sets whether a font should be styled with a "normal", "italic", or "bold".
|
|
427
429
|
*
|
|
428
430
|
* @param {string} style
|
|
429
431
|
*/
|
|
430
432
|
textstyle(style) {
|
|
431
|
-
_fontStyle = style
|
|
433
|
+
_fontStyle = style || ''
|
|
432
434
|
},
|
|
433
435
|
|
|
434
436
|
/**
|
|
@@ -452,9 +454,9 @@ export default function litecanvas(settings = {}) {
|
|
|
452
454
|
* @returns {TextMetrics}
|
|
453
455
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextMetrics
|
|
454
456
|
*/
|
|
455
|
-
textmetrics(text, size) {
|
|
457
|
+
textmetrics(text, size = _fontSize) {
|
|
456
458
|
// prettier-ignore
|
|
457
|
-
_ctx.font = `${_fontStyle
|
|
459
|
+
_ctx.font = `${_fontStyle} ${size}px ${_fontFamily}`
|
|
458
460
|
const metrics = _ctx.measureText(text)
|
|
459
461
|
metrics.height =
|
|
460
462
|
metrics.actualBoundingBoxAscent +
|
|
@@ -485,7 +487,7 @@ export default function litecanvas(settings = {}) {
|
|
|
485
487
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
486
488
|
*/
|
|
487
489
|
paint(width, height, draw, options = {}) {
|
|
488
|
-
const oc = options.canvas || new OffscreenCanvas(
|
|
490
|
+
const oc = options.canvas || new OffscreenCanvas(1, 1),
|
|
489
491
|
scale = options.scale || 1,
|
|
490
492
|
contextBackup = _ctx
|
|
491
493
|
|
|
@@ -539,18 +541,20 @@ export default function litecanvas(settings = {}) {
|
|
|
539
541
|
|
|
540
542
|
/**
|
|
541
543
|
* saves the current drawing style settings and transformations
|
|
544
|
+
*
|
|
542
545
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
|
|
543
546
|
*/
|
|
544
547
|
push: () => _ctx.save(),
|
|
545
548
|
|
|
546
549
|
/**
|
|
547
550
|
* restores the drawing style settings and transformations
|
|
551
|
+
*
|
|
548
552
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
|
|
549
553
|
*/
|
|
550
554
|
pop: () => _ctx.restore(),
|
|
551
555
|
|
|
552
556
|
/**
|
|
553
|
-
* Adds a translation
|
|
557
|
+
* Adds a translation to the transformation matrix.
|
|
554
558
|
*
|
|
555
559
|
* @param {number} x
|
|
556
560
|
* @param {number} y
|
|
@@ -566,15 +570,13 @@ export default function litecanvas(settings = {}) {
|
|
|
566
570
|
scale: (x, y) => _ctx.scale(x, y || x),
|
|
567
571
|
|
|
568
572
|
/**
|
|
569
|
-
* Adds a rotation to the transformation matrix
|
|
573
|
+
* Adds a rotation to the transformation matrix.
|
|
570
574
|
*
|
|
571
575
|
* @param {number} radians
|
|
572
576
|
*/
|
|
573
577
|
rotate: (radians) => _ctx.rotate(radians),
|
|
574
578
|
|
|
575
579
|
/**
|
|
576
|
-
* Adds a transformation that skews to the transformation matrix
|
|
577
|
-
*
|
|
578
580
|
* @param {number} a
|
|
579
581
|
* @param {number} b
|
|
580
582
|
* @param {number} c
|
|
@@ -582,7 +584,9 @@ export default function litecanvas(settings = {}) {
|
|
|
582
584
|
* @param {number} e
|
|
583
585
|
* @param {number} f
|
|
584
586
|
* @param {boolean} [resetFirst=true] `false` to use _ctx.transform(); by default use _ctx.setTransform()
|
|
587
|
+
*
|
|
585
588
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform
|
|
589
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform
|
|
586
590
|
*/
|
|
587
591
|
transform: (a, b, c, d, e, f, resetFirst = true) =>
|
|
588
592
|
_ctx[resetFirst ? 'setTransform' : 'transform'](a, b, c, d, e, f),
|
|
@@ -616,7 +620,11 @@ export default function litecanvas(settings = {}) {
|
|
|
616
620
|
*/
|
|
617
621
|
fill(color, path) {
|
|
618
622
|
_ctx.fillStyle = instance.getcolor(color)
|
|
619
|
-
|
|
623
|
+
if (path) {
|
|
624
|
+
_ctx.fill(path)
|
|
625
|
+
} else {
|
|
626
|
+
_ctx.fill()
|
|
627
|
+
}
|
|
620
628
|
},
|
|
621
629
|
|
|
622
630
|
/**
|
|
@@ -627,7 +635,11 @@ export default function litecanvas(settings = {}) {
|
|
|
627
635
|
*/
|
|
628
636
|
stroke(color, path) {
|
|
629
637
|
_ctx.strokeStyle = instance.getcolor(color)
|
|
630
|
-
|
|
638
|
+
if (path) {
|
|
639
|
+
_ctx.stroke(path)
|
|
640
|
+
} else {
|
|
641
|
+
_ctx.stroke()
|
|
642
|
+
}
|
|
631
643
|
},
|
|
632
644
|
|
|
633
645
|
/**
|
|
@@ -667,17 +679,6 @@ export default function litecanvas(settings = {}) {
|
|
|
667
679
|
_ctx.clip()
|
|
668
680
|
},
|
|
669
681
|
|
|
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
682
|
/** SOUND API */
|
|
682
683
|
/**
|
|
683
684
|
* Play a sound effects using ZzFX library.
|
|
@@ -754,12 +755,6 @@ export default function litecanvas(settings = {}) {
|
|
|
754
755
|
colcirc: (x1, y1, r1, x2, y2, r2) =>
|
|
755
756
|
(x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2,
|
|
756
757
|
|
|
757
|
-
/**
|
|
758
|
-
* Get the mouse position
|
|
759
|
-
* @returns number[]
|
|
760
|
-
*/
|
|
761
|
-
mousepos: () => [_mouseX, _mouseY],
|
|
762
|
-
|
|
763
758
|
/**
|
|
764
759
|
* The scale of the game's delta time (dt).
|
|
765
760
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
@@ -931,8 +926,13 @@ export default function litecanvas(settings = {}) {
|
|
|
931
926
|
|
|
932
927
|
on(_canvas, 'mousemove', (ev) => {
|
|
933
928
|
ev.preventDefault()
|
|
934
|
-
|
|
929
|
+
|
|
930
|
+
const [x, y] = _getXY(ev.pageX, ev.pageY)
|
|
931
|
+
instance.setvar('MOUSEX', x)
|
|
932
|
+
instance.setvar('MOUSEY', y)
|
|
933
|
+
|
|
935
934
|
if (!_pressingMouse) return
|
|
935
|
+
|
|
936
936
|
instance.emit('tapping', x, y, 0)
|
|
937
937
|
_updateTap(0, x, y)
|
|
938
938
|
})
|
|
@@ -996,9 +996,6 @@ export default function litecanvas(settings = {}) {
|
|
|
996
996
|
|
|
997
997
|
on(root, 'blur', () => {
|
|
998
998
|
_pressingMouse = false
|
|
999
|
-
|
|
1000
|
-
if (_taps.size === 0) return
|
|
1001
|
-
|
|
1002
999
|
for (const [id, tap] of _taps) {
|
|
1003
1000
|
instance.emit('untap', tap.x, tap.y, id)
|
|
1004
1001
|
_taps.delete(id)
|
|
@@ -1128,7 +1125,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1128
1125
|
pageWidth / instance.WIDTH,
|
|
1129
1126
|
pageHeight / instance.HEIGHT
|
|
1130
1127
|
)
|
|
1131
|
-
_scale = settings.pixelart ?
|
|
1128
|
+
_scale = (settings.pixelart ? ~~_scale : _scale) || 1
|
|
1132
1129
|
_canvas.style.width = instance.WIDTH * _scale + 'px'
|
|
1133
1130
|
_canvas.style.height = instance.HEIGHT * _scale + 'px'
|
|
1134
1131
|
}
|
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.
|