litecanvas 0.206.0 → 0.206.2
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.dev.js +50 -54
- package/dist/dist.js +16 -20
- package/dist/dist.min.js +1 -1
- package/package.json +3 -3
- package/src/index.js +65 -63
- package/src/version.js +1 -1
package/dist/dist.dev.js
CHANGED
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
var assert = (condition, message = "Assertion failed") => {
|
|
116
116
|
if (!condition) throw new Error("[litecanvas] " + message);
|
|
117
117
|
};
|
|
118
|
-
var version = "0.206.
|
|
118
|
+
var version = "0.206.2";
|
|
119
119
|
function litecanvas(settings = {}) {
|
|
120
120
|
const root = window,
|
|
121
121
|
math = Math,
|
|
@@ -146,7 +146,8 @@
|
|
|
146
146
|
keyboardEvents: true,
|
|
147
147
|
};
|
|
148
148
|
settings = Object.assign(defaults, settings);
|
|
149
|
-
let
|
|
149
|
+
let _loop = settings.loop,
|
|
150
|
+
_initialized = false,
|
|
150
151
|
_paused,
|
|
151
152
|
_canvas,
|
|
152
153
|
_canvasScale = 1,
|
|
@@ -449,102 +450,98 @@
|
|
|
449
450
|
_ctx[radii ? "roundRect" : "rect"](~~x, ~~y, ~~width, ~~height, radii);
|
|
450
451
|
instance.fill(color);
|
|
451
452
|
},
|
|
452
|
-
|
|
453
|
+
oval(x, y, radiusX, radiusY, color) {
|
|
453
454
|
DEV: assert(
|
|
454
455
|
isNumber(x),
|
|
455
|
-
loggerPrefix + "
|
|
456
|
+
loggerPrefix + "oval() 1st param must be a number",
|
|
456
457
|
);
|
|
457
458
|
DEV: assert(
|
|
458
459
|
isNumber(y),
|
|
459
|
-
loggerPrefix + "
|
|
460
|
+
loggerPrefix + "oval() 2nd param must be a number",
|
|
460
461
|
);
|
|
461
462
|
DEV: assert(
|
|
462
|
-
isNumber(
|
|
463
|
-
loggerPrefix + "
|
|
463
|
+
isNumber(radiusX) && radiusX >= 0,
|
|
464
|
+
loggerPrefix + "oval() 3rd param must be a positive number or zero",
|
|
465
|
+
);
|
|
466
|
+
DEV: assert(
|
|
467
|
+
isNumber(radiusY) && radiusY >= 0,
|
|
468
|
+
loggerPrefix + "oval() 4th param must be a positive number or zero",
|
|
464
469
|
);
|
|
465
470
|
DEV: assert(
|
|
466
471
|
null == color || (isNumber(color) && color >= 0),
|
|
467
|
-
loggerPrefix + "
|
|
472
|
+
loggerPrefix + "oval() 5th param must be a positive number or zero",
|
|
468
473
|
);
|
|
469
474
|
beginPath(_ctx);
|
|
470
|
-
_ctx.
|
|
475
|
+
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
471
476
|
instance.stroke(color);
|
|
472
477
|
},
|
|
473
|
-
|
|
478
|
+
ovalfill(x, y, radiusX, radiusY, color) {
|
|
474
479
|
DEV: assert(
|
|
475
480
|
isNumber(x),
|
|
476
|
-
loggerPrefix + "
|
|
481
|
+
loggerPrefix + "ovalfill() 1st param must be a number",
|
|
477
482
|
);
|
|
478
483
|
DEV: assert(
|
|
479
484
|
isNumber(y),
|
|
480
|
-
loggerPrefix + "
|
|
485
|
+
loggerPrefix + "ovalfill() 2nd param must be a number",
|
|
481
486
|
);
|
|
482
487
|
DEV: assert(
|
|
483
|
-
isNumber(
|
|
488
|
+
isNumber(radiusX) && radiusX >= 0,
|
|
484
489
|
loggerPrefix +
|
|
485
|
-
"
|
|
490
|
+
"ovalfill() 3rd param must be a positive number or zero",
|
|
491
|
+
);
|
|
492
|
+
DEV: assert(
|
|
493
|
+
isNumber(radiusY) && radiusY >= 0,
|
|
494
|
+
loggerPrefix +
|
|
495
|
+
"ovalfill() 4th param must be a positive number or zero",
|
|
486
496
|
);
|
|
487
497
|
DEV: assert(
|
|
488
498
|
null == color || (isNumber(color) && color >= 0),
|
|
489
499
|
loggerPrefix +
|
|
490
|
-
"
|
|
500
|
+
"ovalfill() 5th param must be a positive number or zero",
|
|
491
501
|
);
|
|
492
502
|
beginPath(_ctx);
|
|
493
|
-
_ctx.
|
|
503
|
+
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
494
504
|
instance.fill(color);
|
|
495
505
|
},
|
|
496
|
-
|
|
506
|
+
circ(x, y, radius, color) {
|
|
497
507
|
DEV: assert(
|
|
498
508
|
isNumber(x),
|
|
499
|
-
loggerPrefix + "
|
|
509
|
+
loggerPrefix + "circ() 1st param must be a number",
|
|
500
510
|
);
|
|
501
511
|
DEV: assert(
|
|
502
512
|
isNumber(y),
|
|
503
|
-
loggerPrefix + "
|
|
504
|
-
);
|
|
505
|
-
DEV: assert(
|
|
506
|
-
isNumber(radiusX) && radiusX >= 0,
|
|
507
|
-
loggerPrefix + "oval() 3rd param must be a positive number or zero",
|
|
513
|
+
loggerPrefix + "circ() 2nd param must be a number",
|
|
508
514
|
);
|
|
509
515
|
DEV: assert(
|
|
510
|
-
isNumber(
|
|
511
|
-
loggerPrefix + "
|
|
516
|
+
isNumber(radius) && radius >= 0,
|
|
517
|
+
loggerPrefix + "circ() 3rd param must be a positive number or zero",
|
|
512
518
|
);
|
|
513
519
|
DEV: assert(
|
|
514
520
|
null == color || (isNumber(color) && color >= 0),
|
|
515
|
-
loggerPrefix + "
|
|
521
|
+
loggerPrefix + "circ() 4th param must be a positive number or zero",
|
|
516
522
|
);
|
|
517
|
-
|
|
518
|
-
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
519
|
-
instance.stroke(color);
|
|
523
|
+
instance.oval(x, y, radius, radius, color);
|
|
520
524
|
},
|
|
521
|
-
|
|
525
|
+
circfill(x, y, radius, color) {
|
|
522
526
|
DEV: assert(
|
|
523
527
|
isNumber(x),
|
|
524
|
-
loggerPrefix + "
|
|
528
|
+
loggerPrefix + "circfill() 1st param must be a number",
|
|
525
529
|
);
|
|
526
530
|
DEV: assert(
|
|
527
531
|
isNumber(y),
|
|
528
|
-
loggerPrefix + "
|
|
529
|
-
);
|
|
530
|
-
DEV: assert(
|
|
531
|
-
isNumber(radiusX) && radiusX >= 0,
|
|
532
|
-
loggerPrefix +
|
|
533
|
-
"ovalfill() 3rd param must be a positive number or zero",
|
|
532
|
+
loggerPrefix + "circfill() 2nd param must be a number",
|
|
534
533
|
);
|
|
535
534
|
DEV: assert(
|
|
536
|
-
isNumber(
|
|
535
|
+
isNumber(radius) && radius >= 0,
|
|
537
536
|
loggerPrefix +
|
|
538
|
-
"
|
|
537
|
+
"circfill() 3rd param must be a positive number or zero",
|
|
539
538
|
);
|
|
540
539
|
DEV: assert(
|
|
541
540
|
null == color || (isNumber(color) && color >= 0),
|
|
542
541
|
loggerPrefix +
|
|
543
|
-
"
|
|
542
|
+
"circfill() 4th param must be a positive number or zero",
|
|
544
543
|
);
|
|
545
|
-
|
|
546
|
-
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
547
|
-
instance.fill(color);
|
|
544
|
+
instance.ovalfill(x, y, radius, radius, color);
|
|
548
545
|
},
|
|
549
546
|
shape(points) {
|
|
550
547
|
DEV: assert(
|
|
@@ -844,17 +841,17 @@
|
|
|
844
841
|
callback(_ctx);
|
|
845
842
|
_ctx.clip();
|
|
846
843
|
},
|
|
847
|
-
sfx(zzfxParams, pitchSlide
|
|
844
|
+
sfx(zzfxParams, pitchSlide, volumeFactor) {
|
|
848
845
|
DEV: assert(
|
|
849
846
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
850
847
|
loggerPrefix + "sfx() 1st param must be an array",
|
|
851
848
|
);
|
|
852
849
|
DEV: assert(
|
|
853
|
-
isNumber(pitchSlide),
|
|
850
|
+
null == pitchSlide || isNumber(pitchSlide),
|
|
854
851
|
loggerPrefix + "sfx() 2nd param must be a number",
|
|
855
852
|
);
|
|
856
853
|
DEV: assert(
|
|
857
|
-
isNumber(volumeFactor),
|
|
854
|
+
null == volumeFactor || isNumber(volumeFactor),
|
|
858
855
|
loggerPrefix + "sfx() 3rd param must be a number",
|
|
859
856
|
);
|
|
860
857
|
if (
|
|
@@ -863,10 +860,10 @@
|
|
|
863
860
|
) {
|
|
864
861
|
return false;
|
|
865
862
|
}
|
|
866
|
-
zzfxParams
|
|
867
|
-
if (pitchSlide
|
|
863
|
+
zzfxParams ||= _defaultSound;
|
|
864
|
+
if (pitchSlide || volumeFactor) {
|
|
868
865
|
zzfxParams = zzfxParams.slice();
|
|
869
|
-
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
|
|
866
|
+
zzfxParams[0] = (volumeFactor || 1) * (zzfxParams[0] || 1);
|
|
870
867
|
zzfxParams[10] = ~~zzfxParams[10] + pitchSlide;
|
|
871
868
|
}
|
|
872
869
|
zzfx.apply(0, zzfxParams);
|
|
@@ -928,7 +925,7 @@
|
|
|
928
925
|
eventName = lowerCase(eventName);
|
|
929
926
|
triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
|
|
930
927
|
if (
|
|
931
|
-
!
|
|
928
|
+
!_loop &&
|
|
932
929
|
root[eventName] !== instance[eventName] &&
|
|
933
930
|
"function" === typeof root[eventName]
|
|
934
931
|
) {
|
|
@@ -1347,10 +1344,9 @@
|
|
|
1347
1344
|
DEV: console.info(loggerPrefix + `version ${version} started`);
|
|
1348
1345
|
DEV: console.debug(loggerPrefix + `litecanvas() options =`, settings);
|
|
1349
1346
|
setupCanvas();
|
|
1350
|
-
if (
|
|
1351
|
-
for (const eventName in
|
|
1352
|
-
if (
|
|
1353
|
-
instance.listen(eventName, settings.loop[eventName]);
|
|
1347
|
+
if (_loop) {
|
|
1348
|
+
for (const eventName in _loop) {
|
|
1349
|
+
if (_loop[eventName]) instance.listen(eventName, _loop[eventName]);
|
|
1354
1350
|
}
|
|
1355
1351
|
}
|
|
1356
1352
|
raf(init);
|
package/dist/dist.js
CHANGED
|
@@ -142,7 +142,8 @@
|
|
|
142
142
|
keyboardEvents: true,
|
|
143
143
|
};
|
|
144
144
|
settings = Object.assign(defaults, settings);
|
|
145
|
-
let
|
|
145
|
+
let _loop = settings.loop,
|
|
146
|
+
_initialized = false,
|
|
146
147
|
_paused,
|
|
147
148
|
_canvas,
|
|
148
149
|
_canvasScale = 1,
|
|
@@ -241,16 +242,6 @@
|
|
|
241
242
|
_ctx[radii ? "roundRect" : "rect"](~~x, ~~y, ~~width, ~~height, radii);
|
|
242
243
|
instance.fill(color);
|
|
243
244
|
},
|
|
244
|
-
circ(x, y, radius, color) {
|
|
245
|
-
beginPath(_ctx);
|
|
246
|
-
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
|
|
247
|
-
instance.stroke(color);
|
|
248
|
-
},
|
|
249
|
-
circfill(x, y, radius, color) {
|
|
250
|
-
beginPath(_ctx);
|
|
251
|
-
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
|
|
252
|
-
instance.fill(color);
|
|
253
|
-
},
|
|
254
245
|
oval(x, y, radiusX, radiusY, color) {
|
|
255
246
|
beginPath(_ctx);
|
|
256
247
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
@@ -261,6 +252,12 @@
|
|
|
261
252
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
262
253
|
instance.fill(color);
|
|
263
254
|
},
|
|
255
|
+
circ(x, y, radius, color) {
|
|
256
|
+
instance.oval(x, y, radius, radius, color);
|
|
257
|
+
},
|
|
258
|
+
circfill(x, y, radius, color) {
|
|
259
|
+
instance.ovalfill(x, y, radius, radius, color);
|
|
260
|
+
},
|
|
264
261
|
shape(points) {
|
|
265
262
|
beginPath(_ctx);
|
|
266
263
|
for (let i = 0; i < points.length; i += 2) {
|
|
@@ -383,17 +380,17 @@
|
|
|
383
380
|
callback(_ctx);
|
|
384
381
|
_ctx.clip();
|
|
385
382
|
},
|
|
386
|
-
sfx(zzfxParams, pitchSlide
|
|
383
|
+
sfx(zzfxParams, pitchSlide, volumeFactor) {
|
|
387
384
|
if (
|
|
388
385
|
!root.zzfxV ||
|
|
389
386
|
(navigator.userActivation && !navigator.userActivation.hasBeenActive)
|
|
390
387
|
) {
|
|
391
388
|
return false;
|
|
392
389
|
}
|
|
393
|
-
zzfxParams
|
|
394
|
-
if (pitchSlide
|
|
390
|
+
zzfxParams ||= _defaultSound;
|
|
391
|
+
if (pitchSlide || volumeFactor) {
|
|
395
392
|
zzfxParams = zzfxParams.slice();
|
|
396
|
-
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1);
|
|
393
|
+
zzfxParams[0] = (volumeFactor || 1) * (zzfxParams[0] || 1);
|
|
397
394
|
zzfxParams[10] = ~~zzfxParams[10] + pitchSlide;
|
|
398
395
|
}
|
|
399
396
|
zzfx.apply(0, zzfxParams);
|
|
@@ -422,7 +419,7 @@
|
|
|
422
419
|
eventName = lowerCase(eventName);
|
|
423
420
|
triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
|
|
424
421
|
if (
|
|
425
|
-
!
|
|
422
|
+
!_loop &&
|
|
426
423
|
root[eventName] !== instance[eventName] &&
|
|
427
424
|
"function" === typeof root[eventName]
|
|
428
425
|
) {
|
|
@@ -744,10 +741,9 @@
|
|
|
744
741
|
root.ENGINE = instance;
|
|
745
742
|
}
|
|
746
743
|
setupCanvas();
|
|
747
|
-
if (
|
|
748
|
-
for (const eventName in
|
|
749
|
-
if (
|
|
750
|
-
instance.listen(eventName, settings.loop[eventName]);
|
|
744
|
+
if (_loop) {
|
|
745
|
+
for (const eventName in _loop) {
|
|
746
|
+
if (_loop[eventName]) instance.listen(eventName, _loop[eventName]);
|
|
751
747
|
}
|
|
752
748
|
}
|
|
753
749
|
raf(init);
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let l,
|
|
1
|
+
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let a,l,n=window,i=Math,o=performance,r=2*i.PI,s=requestAnimationFrame,f=[],d=(e,t,a)=>{e.addEventListener(t,a,!1),f.push(()=>e.removeEventListener(t,a,!1))},c=(a=new AudioContext,n.zzfxV=1,(e=1,t=.05,l=220,i=0,o=0,r=.1,s=0,f=1,d=0,c=0,u=0,p=0,h=0,m=0,g=0,v=0,w=0,x=1,y=0,b=0,k=0)=>{let E=Math,T=2*E.PI,z=d*=500*T/44100/44100,P=l*=(1-t+2*t*E.random(t=[]))*T/44100,C=0,I=0,L=0,D=1,A=0,S=0,H=0,M=k<0?-1:1,N=T*M*k*2/44100,W=E.cos(N),q=E.sin,B=q(N)/4,V=1+B,O=-2*W/V,R=(1-B)/V,F=(1+M*W)/2/V,G=-(M+W)/V,X=0,Y=0,$=0,j=0;for(i=44100*i+9,y*=44100,o*=44100,r*=44100,w*=44100,c*=500*T/85766121e6,g*=T/44100,u*=T/44100,p*=44100,h=44100*h|0,e*=.3*n.zzfxV,M=i+y+o+r+w|0;L<M;t[L++]=H*e)++S%(100*v|0)||(H=s?1<s?2<s?3<s?q(C*C):E.max(E.min(E.tan(C),1),-1):1-(2*C/T%2+2)%2:1-4*E.abs(E.round(C/T)-C/T):q(C),H=(h?1-b+b*q(T*L/h):1)*(H<0?-1:1)*E.abs(H)**f*(L<i?L/i:L<i+y?1-(L-i)/y*(1-x):L<i+y+o?x:L<M-w?(M-L-w)/r*x:0),H=w?H/2+(w>L?0:(L<M-w?1:(M-L)/w)*t[L-w|0]/2/e):H,k&&(H=j=F*X+G*(X=Y)+F*(Y=H)-R*$-O*($=j))),C+=(N=(l+=d+=c)*E.cos(g*I++))+N*m*q(L**5),D&&++D>p&&(l+=u,P+=u,D=0),!h||++A%h||(l=P,d=z,D=D||1);(e=a.createBuffer(1,M,44100)).getChannelData(0).set(t),(l=a.createBufferSource()).buffer=e,l.connect(a.destination),l.start()}),u=(t=Object.assign({width:null,height:null,autoscale:!0,canvas:null,global:!0,loop:null,tapEvents:!0,keyboardEvents:!0},t)).loop,p=!1,h,m,g=1,v,w=.5,x=1,y,b=1e3/60,k,E=0,T=3,z="sans-serif",P=20,C=1.2,I=Date.now(),L=e,D=[],A=[.5,0,1750,,,.3,1,,,,600,.1],S={},H={W:0,H:0,T:0,MX:-1,MY:-1,TWO_PI:r,HALF_PI:r/4,lerp:(e,t,a)=>e+a*(t-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,dist:(e,t,a,l)=>i.hypot(a-e,l-t),wrap:(e,t,a)=>e-(a-t)*i.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let o=(e-t)/(a-t)*(n-l)+l;return i?H.clamp(o,l,n):o},norm:(e,t,a)=>H.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>~~H.rand(e,t+1),rseed(e){I=~~e},cls(e){null==e?v.clearRect(0,0,H.W,H.H):H.rectfill(0,0,H.W,H.H,e)},rect(e,t,a,l,n,i){v.beginPath(),v[i?"roundRect":"rect"](~~e-w,~~t-w,~~a+2*w,~~l+2*w,i),H.stroke(n)},rectfill(e,t,a,l,n,i){v.beginPath(),v[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),H.fill(n)},oval(e,t,a,l,n){v.beginPath(),v.ellipse(~~e,~~t,~~a,~~l,0,0,r),H.stroke(n)},ovalfill(e,t,a,l,n){v.beginPath(),v.ellipse(~~e,~~t,~~a,~~l,0,0,r),H.fill(n)},circ(e,t,a,l){H.oval(e,t,a,a,l)},circfill(e,t,a,l){H.ovalfill(e,t,a,a,l)},shape(e){v.beginPath();for(let t=0;t<e.length;t+=2)0===t?v.moveTo(~~e[t],~~e[t+1]):v.lineTo(~~e[t],~~e[t+1]);v.lineTo(~~e[0],~~e[1])},line(e,t,a,l,n){v.beginPath();let i=.5*(0!==w&&~~e==~~a),o=.5*(0!==w&&~~t==~~l);v.moveTo(~~e+i,~~t+o),v.lineTo(~~a+i,~~l+o),H.stroke(n)},linewidth(e){v.lineWidth=~~e,w=.5*(0!=~~e%2)},linedash(e,t=0){v.setLineDash(e),v.lineDashOffset=t},text(e,t,a,l=T,n="normal"){v.font=`${n} ${P}px ${z}`,v.fillStyle=B(l);let i=(""+a).split("\n");for(let a=0;a<i.length;a++)v.fillText(i[a],~~e,~~t+P*C*a)},textgap(e){C=e},textfont(e){z=e},textsize(e){P=e},textalign(e,t){e&&(v.textAlign=e),t&&(v.textBaseline=t)},image(e,t,a){v.drawImage(a,~~e,~~t)},spr(e,t,a){let l=a.trim().split("\n");for(let a=0;a<l.length;a++){let n=l[a].trim();for(let l=0;l<n.length;l++){let i=n[l];"."!==i&&" "!==i&&H.rectfill(e+l,t+a,1,1,parseInt(i,36)||0)}}},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,o=v;return n.width=e*i,n.height=t*i,(v=n.getContext("2d")).scale(i,i),a(v),v=o,n.transferToImageBitmap()},ctx:e=>(e&&(v=e),v),push(){v.save()},pop(){v.restore()},translate(e,t){v.translate(~~e,~~t)},scale(e,t=e){v.scale(e,t)},rotate(e){v.rotate(e)},alpha(e){v.globalAlpha=H.clamp(e,0,1)},fill(e){v.fillStyle=B(e),v.fill()},stroke(e){v.strokeStyle=B(e),v.stroke()},clip(e){v.beginPath(),e(v),v.clip()},sfx:(e,t,a)=>!!n.zzfxV&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e||=A,(t||a)&&((e=e.slice())[0]=(a||1)*(e[0]||1),e[10]=~~e[10]+t),c.apply(0,e),e),volume(e){n.zzfxV=e},canvas:()=>m,use(e,t={}){var a=e,l=t;let n=a(H,l);for(let e in n)H.def(e,n[e])},listen:(e,t)=>{S[e=e.toLowerCase()]=S[e]||new Set,S[e].add(t)},unlisten:(e,t)=>{S[e=e.toLowerCase()]&&S[e].delete(t)},emit:(e,t,a,l,i)=>(p&&(q("before:"+(e=e.toLowerCase()),t,a,l,i),u||n[e]===H[e]||"function"!=typeof n[e]||n[e](t,a,l,i),q(e,t,a,l,i),q("after:"+e,t,a,l,i)),t),pal(t,a=3){L=t||e,D=[],T=a,H.emit("pal",L,T)},palc(e,t){null==e?D=[]:D[e]=t},def(e,a){H[e]=a,t.global&&(n[e]=a)},timescale(e){x=e},framerate(e){b=1e3/~~e},stat:e=>[t,p,b/1e3,g,S,L,A,x,n.zzfxV,I,P,z,D,C][e],pause(){h||(h=!0,E=~~cancelAnimationFrame(E),H.emit("paused"))},resume(){p&&h&&(M(),h=!1,H.emit("resumed"))},ispaused:()=>h,quit(){for(let e of(H.emit("quit"),H.pause(),p=!1,S={},f))e();if(t.global){for(let e in H)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(","))H[e]=i[e];function M(){E||(k=0,y=o.now(),E=s(N))}function N(){E=s(N);let e=o.now(),t=0,a=e-y;for(y=e,k+=a<100?a:b;k>=b;){t++,k-=b;let e=b/1e3*x;H.emit("update",e,t),H.def("T",H.T+e)}t&&(H.emit("draw",v),t>1&&(k=0))}function W(){let e=t.width>0?t.width:innerWidth,a=t.width>0?t.height||t.width:innerHeight;if(H.def("W",e),H.def("H",a),m.width=e,m.height=a,t.autoscale){let l=+t.autoscale;m.style.display||(m.style.display="block",m.style.margin="auto"),g=i.min(innerWidth/e,innerHeight/a),g=l>1&&g>l?l:g,m.style.width=e*g+"px",m.style.height=a*g+"px"}v.imageSmoothingEnabled=!1,H.textalign("start","top"),H.emit("resized",g)}function q(e,t,a,l,n){if(S[e])for(let i of S[e])i(t,a,l,n)}function B(e){return L[~~(D[e]??e)%L.length]}if(t.global){if(n.ENGINE)throw Error("only one global litecanvas is allowed");Object.assign(n,H),n.ENGINE=H}if(l=document,v=(m=(m="string"==typeof t.canvas?l.querySelector(t.canvas):t.canvas)||l.createElement("canvas")).getContext("2d"),d(m,"click",()=>focus()),W(),m.parentNode||l.body.appendChild(m),m.style.imageRendering="pixelated",m.oncontextmenu=()=>!1,u)for(let e in u)u[e]&&H.listen(e,u[e]);return s(function(){if(t.autoscale&&d(n,"resize",W),t.tapEvents){let e=e=>[(e.pageX-m.offsetLeft)/g,(e.pageY-m.offsetTop)/g],t=new Map,a=(e,a,l)=>{let n={x:a,y:l,xi:a,yi:l,t:o.now()};return t.set(e,n),n},l=(e,l,n)=>{let i=t.get(e)||a(e);i.x=l,i.y=n},i=e=>e&&o.now()-e.t<=300,r=!1;d(m,"mousedown",t=>{if(0===t.button){t.preventDefault();let[l,n]=e(t);H.emit("tap",l,n,0),a(0,l,n),r=!0}}),d(m,"mouseup",a=>{if(0===a.button){a.preventDefault();let l=t.get(0),[n,o]=e(a);i(l)&&H.emit("tapped",l.xi,l.yi,0),H.emit("untap",n,o,0),t.delete(0),r=!1}}),d(n,"mousemove",t=>{t.preventDefault();let[a,n]=e(t);H.def("MX",a),H.def("MY",n),r&&(H.emit("tapping",a,n,0),l(0,a,n))}),d(m,"touchstart",t=>{for(let l of(t.preventDefault(),t.changedTouches)){let[t,n]=e(l);H.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),d(m,"touchmove",t=>{for(let a of(t.preventDefault(),t.changedTouches)){let[t,n]=e(a);H.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,l]of t)a.includes(e)||(i(l)&&H.emit("tapped",l.xi,l.yi,e),H.emit("untap",l.x,l.y,e),t.delete(e))};d(m,"touchend",s),d(m,"touchcancel",s),d(n,"blur",()=>{for(let[e,a]of(r=!1,t))H.emit("untap",a.x,a.y,e),t.delete(e)})}if(t.keyboardEvents){let e=new Set,t=new Set,a=(e,t="")=>(t=t.toLowerCase())?e.has("space"===t?" ":t):e.size>0,l="";d(n,"keydown",a=>{let n=a.key.toLowerCase();e.has(n)||(e.add(n),t.add(n),l=" "===n?"space":n)}),d(n,"keyup",t=>{e.delete(t.key.toLowerCase())}),d(n,"blur",()=>e.clear()),H.listen("after:update",()=>t.clear()),H.def("iskeydown",t=>a(e,t)),H.def("iskeypressed",e=>a(t,e)),H.def("lastkey",()=>l)}p=!0,H.emit("init",H),h||M()}),H}})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.206.
|
|
3
|
+
"version": "0.206.2",
|
|
4
4
|
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Luiz Bills <luizbills@pm.me>",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@happy-dom/global-registrator": "^20.9.0",
|
|
36
36
|
"@size-limit/preset-small-lib": "^12.1.0",
|
|
37
|
-
"@swc/core": "^1.15.
|
|
37
|
+
"@swc/core": "^1.15.33",
|
|
38
38
|
"ava": "^7.0.0",
|
|
39
|
-
"esbuild": "^0.
|
|
39
|
+
"esbuild": "^0.28.0",
|
|
40
40
|
"genversion": "^3.2.0",
|
|
41
41
|
"gzip-size": "^7.0.0",
|
|
42
42
|
"prettier": "^3.8.3",
|
package/src/index.js
CHANGED
|
@@ -11,8 +11,7 @@ import { version } from './version.js'
|
|
|
11
11
|
* @returns {LitecanvasInstance}
|
|
12
12
|
*/
|
|
13
13
|
export default function litecanvas(settings = {}) {
|
|
14
|
-
const
|
|
15
|
-
root = window,
|
|
14
|
+
const root = window,
|
|
16
15
|
math = Math,
|
|
17
16
|
perf = performance,
|
|
18
17
|
TWO_PI = math.PI * 2,
|
|
@@ -48,7 +47,8 @@ export default function litecanvas(settings = {}) {
|
|
|
48
47
|
// setup the settings default values
|
|
49
48
|
settings = Object.assign(defaults, settings)
|
|
50
49
|
|
|
51
|
-
let
|
|
50
|
+
let _loop = settings.loop,
|
|
51
|
+
/** @type {boolean} */
|
|
52
52
|
_initialized = false,
|
|
53
53
|
/** @type {boolean} */
|
|
54
54
|
_paused,
|
|
@@ -459,56 +459,6 @@ export default function litecanvas(settings = {}) {
|
|
|
459
459
|
instance.fill(color)
|
|
460
460
|
},
|
|
461
461
|
|
|
462
|
-
/**
|
|
463
|
-
* Draw a circle outline
|
|
464
|
-
*
|
|
465
|
-
* @param {number} x
|
|
466
|
-
* @param {number} y
|
|
467
|
-
* @param {number} radius
|
|
468
|
-
* @param {number} [color=0] the color index
|
|
469
|
-
*/
|
|
470
|
-
circ(x, y, radius, color) {
|
|
471
|
-
DEV: assert(isNumber(x), loggerPrefix + 'circ() 1st param must be a number')
|
|
472
|
-
DEV: assert(isNumber(y), loggerPrefix + 'circ() 2nd param must be a number')
|
|
473
|
-
DEV: assert(
|
|
474
|
-
isNumber(radius) && radius >= 0,
|
|
475
|
-
loggerPrefix + 'circ() 3rd param must be a positive number or zero'
|
|
476
|
-
)
|
|
477
|
-
DEV: assert(
|
|
478
|
-
null == color || (isNumber(color) && color >= 0),
|
|
479
|
-
loggerPrefix + 'circ() 4th param must be a positive number or zero'
|
|
480
|
-
)
|
|
481
|
-
|
|
482
|
-
beginPath(_ctx)
|
|
483
|
-
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
|
|
484
|
-
instance.stroke(color)
|
|
485
|
-
},
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* Draw a color-filled circle
|
|
489
|
-
*
|
|
490
|
-
* @param {number} x
|
|
491
|
-
* @param {number} y
|
|
492
|
-
* @param {number} radius
|
|
493
|
-
* @param {number} [color=0] the color index
|
|
494
|
-
*/
|
|
495
|
-
circfill(x, y, radius, color) {
|
|
496
|
-
DEV: assert(isNumber(x), loggerPrefix + 'circfill() 1st param must be a number')
|
|
497
|
-
DEV: assert(isNumber(y), loggerPrefix + 'circfill() 2nd param must be a number')
|
|
498
|
-
DEV: assert(
|
|
499
|
-
isNumber(radius) && radius >= 0,
|
|
500
|
-
loggerPrefix + 'circfill() 3rd param must be a positive number or zero'
|
|
501
|
-
)
|
|
502
|
-
DEV: assert(
|
|
503
|
-
null == color || (isNumber(color) && color >= 0),
|
|
504
|
-
loggerPrefix + 'circfill() 4th param must be a positive number or zero'
|
|
505
|
-
)
|
|
506
|
-
|
|
507
|
-
beginPath(_ctx)
|
|
508
|
-
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
|
|
509
|
-
instance.fill(color)
|
|
510
|
-
},
|
|
511
|
-
|
|
512
462
|
/**
|
|
513
463
|
* Draw a ellipse outline
|
|
514
464
|
*
|
|
@@ -569,6 +519,52 @@ export default function litecanvas(settings = {}) {
|
|
|
569
519
|
instance.fill(color)
|
|
570
520
|
},
|
|
571
521
|
|
|
522
|
+
/**
|
|
523
|
+
* Draw a circle outline
|
|
524
|
+
*
|
|
525
|
+
* @param {number} x
|
|
526
|
+
* @param {number} y
|
|
527
|
+
* @param {number} radius
|
|
528
|
+
* @param {number} [color=0] the color index
|
|
529
|
+
*/
|
|
530
|
+
circ(x, y, radius, color) {
|
|
531
|
+
DEV: assert(isNumber(x), loggerPrefix + 'circ() 1st param must be a number')
|
|
532
|
+
DEV: assert(isNumber(y), loggerPrefix + 'circ() 2nd param must be a number')
|
|
533
|
+
DEV: assert(
|
|
534
|
+
isNumber(radius) && radius >= 0,
|
|
535
|
+
loggerPrefix + 'circ() 3rd param must be a positive number or zero'
|
|
536
|
+
)
|
|
537
|
+
DEV: assert(
|
|
538
|
+
null == color || (isNumber(color) && color >= 0),
|
|
539
|
+
loggerPrefix + 'circ() 4th param must be a positive number or zero'
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
instance.oval(x, y, radius, radius, color)
|
|
543
|
+
},
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Draw a color-filled circle
|
|
547
|
+
*
|
|
548
|
+
* @param {number} x
|
|
549
|
+
* @param {number} y
|
|
550
|
+
* @param {number} radius
|
|
551
|
+
* @param {number} [color=0] the color index
|
|
552
|
+
*/
|
|
553
|
+
circfill(x, y, radius, color) {
|
|
554
|
+
DEV: assert(isNumber(x), loggerPrefix + 'circfill() 1st param must be a number')
|
|
555
|
+
DEV: assert(isNumber(y), loggerPrefix + 'circfill() 2nd param must be a number')
|
|
556
|
+
DEV: assert(
|
|
557
|
+
isNumber(radius) && radius >= 0,
|
|
558
|
+
loggerPrefix + 'circfill() 3rd param must be a positive number or zero'
|
|
559
|
+
)
|
|
560
|
+
DEV: assert(
|
|
561
|
+
null == color || (isNumber(color) && color >= 0),
|
|
562
|
+
loggerPrefix + 'circfill() 4th param must be a positive number or zero'
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
instance.ovalfill(x, y, radius, radius, color)
|
|
566
|
+
},
|
|
567
|
+
|
|
572
568
|
/**
|
|
573
569
|
* Make a custom shape in the canvas context.
|
|
574
570
|
* Then, just use `fill` or `stroke` to draw the shape.
|
|
@@ -1005,13 +1001,19 @@ export default function litecanvas(settings = {}) {
|
|
|
1005
1001
|
*
|
|
1006
1002
|
* @see https://github.com/KilledByAPixel/ZzFX
|
|
1007
1003
|
*/
|
|
1008
|
-
sfx(zzfxParams, pitchSlide
|
|
1004
|
+
sfx(zzfxParams, pitchSlide, volumeFactor) {
|
|
1009
1005
|
DEV: assert(
|
|
1010
1006
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
1011
1007
|
loggerPrefix + 'sfx() 1st param must be an array'
|
|
1012
1008
|
)
|
|
1013
|
-
DEV: assert(
|
|
1014
|
-
|
|
1009
|
+
DEV: assert(
|
|
1010
|
+
null == pitchSlide || isNumber(pitchSlide),
|
|
1011
|
+
loggerPrefix + 'sfx() 2nd param must be a number'
|
|
1012
|
+
)
|
|
1013
|
+
DEV: assert(
|
|
1014
|
+
null == volumeFactor || isNumber(volumeFactor),
|
|
1015
|
+
loggerPrefix + 'sfx() 3rd param must be a number'
|
|
1016
|
+
)
|
|
1015
1017
|
|
|
1016
1018
|
if (
|
|
1017
1019
|
!root.zzfxV ||
|
|
@@ -1020,12 +1022,12 @@ export default function litecanvas(settings = {}) {
|
|
|
1020
1022
|
return false
|
|
1021
1023
|
}
|
|
1022
1024
|
|
|
1023
|
-
zzfxParams
|
|
1025
|
+
zzfxParams ||= _defaultSound
|
|
1024
1026
|
|
|
1025
1027
|
// if has other arguments, copy the sound to not change the original
|
|
1026
|
-
if (pitchSlide
|
|
1028
|
+
if (pitchSlide || volumeFactor) {
|
|
1027
1029
|
zzfxParams = zzfxParams.slice()
|
|
1028
|
-
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1)
|
|
1030
|
+
zzfxParams[0] = (volumeFactor || 1) * (zzfxParams[0] || 1)
|
|
1029
1031
|
zzfxParams[10] = ~~zzfxParams[10] + pitchSlide
|
|
1030
1032
|
}
|
|
1031
1033
|
|
|
@@ -1150,7 +1152,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1150
1152
|
// calls a global function with the same name as the event,
|
|
1151
1153
|
// as long as it's not a global litecanvas function (to avoid infinite loops)
|
|
1152
1154
|
if (
|
|
1153
|
-
!
|
|
1155
|
+
!_loop &&
|
|
1154
1156
|
root[eventName] !== instance[eventName] &&
|
|
1155
1157
|
'function' === typeof root[eventName] /* if is a function */
|
|
1156
1158
|
) {
|
|
@@ -1865,9 +1867,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1865
1867
|
setupCanvas()
|
|
1866
1868
|
|
|
1867
1869
|
// setup default event listeners
|
|
1868
|
-
if (
|
|
1869
|
-
for (const eventName in
|
|
1870
|
-
if (
|
|
1870
|
+
if (_loop) {
|
|
1871
|
+
for (const eventName in _loop) {
|
|
1872
|
+
if (_loop[eventName]) instance.listen(eventName, _loop[eventName])
|
|
1871
1873
|
}
|
|
1872
1874
|
}
|
|
1873
1875
|
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '0.206.
|
|
2
|
+
export const version = '0.206.2'
|