litecanvas 0.73.2 → 0.73.3
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 +8 -8
- package/dist/dist.js +8 -8
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +8 -8
- package/src/types.js +3 -2
- package/types/index.d.ts +10 -10
- package/types/types.d.ts +16 -13
package/dist/dist.dev.js
CHANGED
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
animate: true
|
|
70
70
|
};
|
|
71
71
|
settings = Object.assign(defaults, settings);
|
|
72
|
-
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1,
|
|
72
|
+
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _step, _stepMs, _accumulated = 0, _focused = true, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = {
|
|
73
73
|
init: null,
|
|
74
74
|
update: null,
|
|
75
75
|
draw: null,
|
|
@@ -591,14 +591,14 @@
|
|
|
591
591
|
*
|
|
592
592
|
* @param {number} x
|
|
593
593
|
* @param {number} y
|
|
594
|
-
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement}
|
|
594
|
+
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
595
595
|
*/
|
|
596
|
-
image(x, y,
|
|
596
|
+
image(x, y, source) {
|
|
597
597
|
if (true) {
|
|
598
598
|
assert(isFinite(x), "image: 1st param must be a number");
|
|
599
599
|
assert(isFinite(y), "image: 2nd param must be a number");
|
|
600
600
|
}
|
|
601
|
-
_ctx.drawImage(
|
|
601
|
+
_ctx.drawImage(source, ~~x, ~~y);
|
|
602
602
|
},
|
|
603
603
|
/**
|
|
604
604
|
* Creates a offscreen canvas to draw on it
|
|
@@ -1222,13 +1222,13 @@
|
|
|
1222
1222
|
}
|
|
1223
1223
|
instance.setfps(settings.fps);
|
|
1224
1224
|
instance.emit("init", instance);
|
|
1225
|
-
|
|
1225
|
+
_lastFrameTime = performance.now();
|
|
1226
1226
|
raf(drawFrame);
|
|
1227
1227
|
}
|
|
1228
1228
|
function drawFrame(now) {
|
|
1229
|
-
let shouldRender = !_animated, delta = now -
|
|
1230
|
-
_accumulated += delta > 100 ?
|
|
1231
|
-
|
|
1229
|
+
let shouldRender = !_animated, delta = now - _lastFrameTime;
|
|
1230
|
+
_accumulated += delta > 100 ? 100 : delta;
|
|
1231
|
+
_lastFrameTime = now;
|
|
1232
1232
|
while (_accumulated >= _stepMs) {
|
|
1233
1233
|
instance.emit("update", _step * _timeScale);
|
|
1234
1234
|
instance.setvar("ELAPSED", instance.ELAPSED + _step * _timeScale);
|
package/dist/dist.js
CHANGED
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
animate: true
|
|
65
65
|
};
|
|
66
66
|
settings = Object.assign(defaults, settings);
|
|
67
|
-
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1,
|
|
67
|
+
let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _step, _stepMs, _accumulated = 0, _focused = true, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = {
|
|
68
68
|
init: null,
|
|
69
69
|
update: null,
|
|
70
70
|
draw: null,
|
|
@@ -586,14 +586,14 @@
|
|
|
586
586
|
*
|
|
587
587
|
* @param {number} x
|
|
588
588
|
* @param {number} y
|
|
589
|
-
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement}
|
|
589
|
+
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
590
590
|
*/
|
|
591
|
-
image(x, y,
|
|
591
|
+
image(x, y, source) {
|
|
592
592
|
if (false) {
|
|
593
593
|
assert(isFinite(x), "image: 1st param must be a number");
|
|
594
594
|
assert(isFinite(y), "image: 2nd param must be a number");
|
|
595
595
|
}
|
|
596
|
-
_ctx.drawImage(
|
|
596
|
+
_ctx.drawImage(source, ~~x, ~~y);
|
|
597
597
|
},
|
|
598
598
|
/**
|
|
599
599
|
* Creates a offscreen canvas to draw on it
|
|
@@ -1217,13 +1217,13 @@
|
|
|
1217
1217
|
}
|
|
1218
1218
|
instance.setfps(settings.fps);
|
|
1219
1219
|
instance.emit("init", instance);
|
|
1220
|
-
|
|
1220
|
+
_lastFrameTime = performance.now();
|
|
1221
1221
|
raf(drawFrame);
|
|
1222
1222
|
}
|
|
1223
1223
|
function drawFrame(now) {
|
|
1224
|
-
let shouldRender = !_animated, delta = now -
|
|
1225
|
-
_accumulated += delta > 100 ?
|
|
1226
|
-
|
|
1224
|
+
let shouldRender = !_animated, delta = now - _lastFrameTime;
|
|
1225
|
+
_accumulated += delta > 100 ? 100 : delta;
|
|
1226
|
+
_lastFrameTime = now;
|
|
1227
1227
|
while (_accumulated >= _stepMs) {
|
|
1228
1228
|
instance.emit("update", _step * _timeScale);
|
|
1229
1229
|
instance.setvar("ELAPSED", instance.ELAPSED + _step * _timeScale);
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,r=.1,o=0,s=1,c=0,f=0,p=0,u=0,d=0,h=0,g=0,m=0,v=0,E=1,b=0,T=0,w=0)=>{let x=Math,y=2*x.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*x.random(a=[]))*y/44100,I=0,S=0,k=0,A=1,C=0,L=0,X=0,O=w<0?-1:1,z=y*O*w*2/44100,M=x.cos(z),P=x.sin,Y=P(z)/4,W=1+Y,F=-2*M/W,_=(1-Y)/W,R=(1+O*M)/2/W,G=-(O+M)/W,N=0,B=0,U=0,j=0;for(n=44100*n+9,b*=44100,i*=44100,r*=44100,v*=44100,f*=500*y/85766121e6,g*=y/44100,p*=y/44100,u*=44100,d=44100*d|0,t*=.3*(globalThis.zzfxV||1),O=n+b+i+r+v|0;k<O;a[k++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):x.max(x.min(x.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*x.abs(x.round(I/y)-I/y):P(I),X=(d?1-T+T*P(y*k/d):1)*(X<0?-1:1)*x.abs(X)**s*(k<n?k/n:k<n+b?1-(k-n)/b*(1-E):k<n+b+i?E:k<O-v?(O-k-v)/r*E:0),X=v?X/2+(v>k?0:(k<O-v?1:(O-k)/v)*a[k-v|0]/2/t):X,w&&(X=j=R*N+G*(N=B)+R*(B=X)-_*U-F*(U=j))),I+=(z=(l+=c+=f)*x.cos(g*S++))+z*h*P(k**5),A&&++A>u&&(l+=p,H+=p,A=0),!d||++C%d||(l=H,c=D,A=A||1);(t=e.createBuffer(1,O,44100)).getChannelData(0).set(a),(l=e.createBufferSource()).buffer=t,l.connect(e.destination),l.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let l=globalThis,n=Math.PI,i=2*n,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({fps:60,fullscreen:!0,width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let c=!1,f=[],p=e.canvas||document.createElement("canvas"),u=e.fullscreen,d=e.autoscale,h=e.animate,g=1,m,v=.5,E=1,b,T,w,x=0,y=!0,D="sans-serif",H=32,I=Date.now(),S=e.global,k={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},A={settings:Object.assign({},e),colors:a},C={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],PI:n,TWO_PI:i,HALF_PI:.5*n,lerp:(e,t,a)=>e+a*(t-e),deg2rad:e=>n/180*e,rad2deg:e=>180/n*e,clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*Math.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let r=(e-t)/(a-t)*(n-l)+l;return i?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>Math.floor(C.rand(e,t+1)),seed:e=>null==e?I:I=~~e,cls(e){let t=m.canvas.width,a=m.canvas.height;null==e?m.clearRect(0,0,t,a):C.rectfill(0,0,t,a,e)},rect(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e-v,~~t-v,~~a+2*v,~~l+2*v,i),C.stroke(n)},rectfill(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.fill(l)},line(e,t,a,l,n){m.beginPath();let i=.5*(0!==v&&~~e==~~a),r=.5*(0!==v&&~~t==~~l);m.moveTo(~~e+i,~~t+r),m.lineTo(~~a+i,~~l+r),C.stroke(n)},linewidth(e){m.lineWidth=~~e,v=.5*(~~e%2!=0)},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3,n="normal"){m.font=`${n} ${H}px ${D}`,m.fillStyle=C.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){D=e},textsize(e){H=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,a){m.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=m;if(n.width=e*i,n.height=t*i,(m=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(m.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&C.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(m);return m=r,n},ctx:e=>(e&&(m=e),m),push:()=>m.save(),pop:()=>m.restore(),translate:(e,t)=>m.translate(~~e,~~t),scale:(e,t)=>m.scale(e,t||e),rotate:e=>m.rotate(e),alpha(e){m.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=C.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=C.getcolor(e),t?m.stroke(t):m.stroke()},clip(e){m.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||C.DEFAULT_SFX,(a>0||1!==n)&&((e=e.slice())[0]=n*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){l.zzfxV=e},colrect:(e,t,a,l,n,i,r,o)=>e<n+r&&e+a>n&&t<i+o&&t+l>i,colcirc:(e,t,a,l,n,i)=>(l-e)**2+(n-t)**2<=(a+i)**2,use(e,t={}){c?M(e,t):f.push([e,t])},listen:(e,t)=>(k[e]=k[e]||new Set,k[e].add(t),()=>k[e].delete(t)),emit(e,t,a,l,n){z("before:"+e,t,a,l,n),z(e,t,a,l,n),z("after:"+e,t,a,l,n)},getcolor:e=>a[~~e%a.length],setvar(e,t){C[e]=t,S&&(l[e]=t)},resize(e,t){C.setvar("WIDTH",p.width=e),C.setvar("HEIGHT",p.height=t),O()},timescale(e){E=e},setfps(e){w=1e3*(T=1/e),x=0},quit(){for(let e of(C.emit("quit"),o))e();if(y=k=!1,S){for(let e in C)delete l[e];delete l.__litecanvas}}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])C[e]=Math[e];function L(){c=!0;let t=e.loop?e.loop:l;for(let e in k)t[e]&&C.listen(e,t[e]);for(let[e,t]of f)M(e,t);if((u||d)&&s(l,"resize",O),O(),e.tapEvents){let e=(e,t)=>[(e-p.offsetLeft)/g,(t-p.offsetTop)/g],t=new Map,a=(e,a,l)=>{let n={x:a,y:l,startX:a,startY:l,ts:performance.now()};return t.set(e,n),n},n=(e,l,n)=>{let i=t.get(e)||a(e);i.x=l,i.y=n},i=e=>e&&performance.now()-e.ts<=200,r=!1;s(p,"mousedown",t=>{t.preventDefault();let[l,n]=e(t.pageX,t.pageY);C.emit("tap",l,n,0),a(0,l,n),r=!0}),s(p,"mousemove",t=>{t.preventDefault();let[a,l]=e(t.pageX,t.pageY);C.setvar("MOUSEX",a),C.setvar("MOUSEY",l),r&&(C.emit("tapping",a,l,0),n(0,a,l))}),s(p,"mouseup",a=>{a.preventDefault();let l=t.get(0),[n,o]=e(a.pageX,a.pageY);i(l)&&C.emit("tapped",l.startX,l.startY,0),C.emit("untap",n,o,0),t.delete(0),r=!1}),s(p,"touchstart",t=>{for(let l of(t.preventDefault(),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);C.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),s(p,"touchmove",t=>{for(let a of(t.preventDefault(),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);C.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});let o=e=>{e.preventDefault();let a=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)a.push(t.identifier+1);for(let[e,l]of t)a.includes(e)||(i(l)&&C.emit("tapped",l.startX,l.startY,e),C.emit("untap",l.x,l.y,e),t.delete(e))};s(p,"touchend",o),s(p,"touchcancel",o),s(l,"blur",()=>{for(let[e,a]of(r=!1,t))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=new Set;C.setvar("iskeydown",t=>"any"===t?e.size>0:e.has(t.toLowerCase())),s(l,"keydown",t=>{e.add(t.key.toLowerCase())}),s(l,"keyup",t=>{e.delete(t.key.toLowerCase())}),s(l,"blur",()=>e.clear())}e.pauseOnBlur&&(s(l,"blur",()=>{y=!1}),s(l,"focus",()=>{y=!0,r(X)})),C.setfps(e.fps),C.emit("init",C),b=performance.now(),r(X)}function X(e){let t=!h,a=e-b;for(x+=a>100?
|
|
1
|
+
(()=>{var e=new AudioContext,t=(t=1,a=.05,l=220,n=0,i=0,r=.1,o=0,s=1,c=0,f=0,p=0,u=0,d=0,h=0,g=0,m=0,v=0,E=1,b=0,T=0,w=0)=>{let x=Math,y=2*x.PI,D=c*=500*y/44100/44100,H=l*=(1-a+2*a*x.random(a=[]))*y/44100,I=0,S=0,k=0,A=1,C=0,L=0,X=0,O=w<0?-1:1,z=y*O*w*2/44100,M=x.cos(z),P=x.sin,Y=P(z)/4,W=1+Y,F=-2*M/W,_=(1-Y)/W,R=(1+O*M)/2/W,G=-(O+M)/W,N=0,B=0,U=0,j=0;for(n=44100*n+9,b*=44100,i*=44100,r*=44100,v*=44100,f*=500*y/85766121e6,g*=y/44100,p*=y/44100,u*=44100,d=44100*d|0,t*=.3*(globalThis.zzfxV||1),O=n+b+i+r+v|0;k<O;a[k++]=X*t)++L%(100*m|0)||(X=o?1<o?2<o?3<o?P(I*I):x.max(x.min(x.tan(I),1),-1):1-(2*I/y%2+2)%2:1-4*x.abs(x.round(I/y)-I/y):P(I),X=(d?1-T+T*P(y*k/d):1)*(X<0?-1:1)*x.abs(X)**s*(k<n?k/n:k<n+b?1-(k-n)/b*(1-E):k<n+b+i?E:k<O-v?(O-k-v)/r*E:0),X=v?X/2+(v>k?0:(k<O-v?1:(O-k)/v)*a[k-v|0]/2/t):X,w&&(X=j=R*N+G*(N=B)+R*(B=X)-_*U-F*(U=j))),I+=(z=(l+=c+=f)*x.cos(g*S++))+z*h*P(k**5),A&&++A>u&&(l+=p,H+=p,A=0),!d||++C%d||(l=H,c=D,A=A||1);(t=e.createBuffer(1,O,44100)).getChannelData(0).set(a),(l=e.createBufferSource()).buffer=t,l.connect(e.destination),l.start()},a=["#111","#6a7799","#aec2c2","#FFF1E8","#e83b3b","#fabc20","#155fd9","#3cbcfc","#327345","#63c64d","#6c2c1f","#ac7c00"];globalThis.litecanvas=function(e={}){let l=globalThis,n=Math.PI,i=2*n,r=requestAnimationFrame,o=[],s=(e,t,a)=>{e.addEventListener(t,a,!1),o.push(()=>e.removeEventListener(t,a,!1))};e=Object.assign({fps:60,fullscreen:!0,width:null,height:null,autoscale:!0,pixelart:!1,antialias:!1,canvas:null,global:!0,loop:null,pauseOnBlur:!0,tapEvents:!0,keyboardEvents:!0,animate:!0},e);let c=!1,f=[],p=e.canvas||document.createElement("canvas"),u=e.fullscreen,d=e.autoscale,h=e.animate,g=1,m,v=.5,E=1,b,T,w,x=0,y=!0,D="sans-serif",H=32,I=Date.now(),S=e.global,k={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},A={settings:Object.assign({},e),colors:a},C={WIDTH:e.width,HEIGHT:e.height||e.width,CANVAS:null,ELAPSED:0,CENTERX:0,CENTERY:0,MOUSEX:-1,MOUSEY:-1,DEFAULT_SFX:[.5,,1675,,.06,.2,1,1.8,,,637,.06],PI:n,TWO_PI:i,HALF_PI:.5*n,lerp:(e,t,a)=>e+a*(t-e),deg2rad:e=>n/180*e,rad2deg:e=>180/n*e,clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*Math.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let r=(e-t)/(a-t)*(n-l)+l;return i?C.clamp(r,l,n):r},norm:(e,t,a)=>C.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>Math.floor(C.rand(e,t+1)),seed:e=>null==e?I:I=~~e,cls(e){let t=m.canvas.width,a=m.canvas.height;null==e?m.clearRect(0,0,t,a):C.rectfill(0,0,t,a,e)},rect(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e-v,~~t-v,~~a+2*v,~~l+2*v,i),C.stroke(n)},rectfill(e,t,a,l,n,i=null){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),C.fill(n)},circ(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.stroke(l)},circfill(e,t,a,l){m.beginPath(),m.arc(~~e,~~t,~~a,0,i),C.fill(l)},line(e,t,a,l,n){m.beginPath();let i=.5*(0!==v&&~~e==~~a),r=.5*(0!==v&&~~t==~~l);m.moveTo(~~e+i,~~t+r),m.lineTo(~~a+i,~~l+r),C.stroke(n)},linewidth(e){m.lineWidth=~~e,v=.5*(~~e%2!=0)},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,l=3,n="normal"){m.font=`${n} ${H}px ${D}`,m.fillStyle=C.getcolor(l),m.fillText(a,~~e,~~t)},textfont(e){D=e},textsize(e){H=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,a){m.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=m;if(n.width=e*i,n.height=t*i,(m=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(m.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&C.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(m);return m=r,n},ctx:e=>(e&&(m=e),m),push:()=>m.save(),pop:()=>m.restore(),translate:(e,t)=>m.translate(~~e,~~t),scale:(e,t)=>m.scale(e,t||e),rotate:e=>m.rotate(e),alpha(e){m.globalAlpha=C.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){m.fillStyle=C.getcolor(e),t?m.fill(t):m.fill()},stroke(e,t){m.strokeStyle=C.getcolor(e),t?m.stroke(t):m.stroke()},clip(e){m.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||C.DEFAULT_SFX,(a>0||1!==n)&&((e=e.slice())[0]=n*(e[0]||1),e[10]=~~e[10]+a),t.apply(0,e),e),volume(e){l.zzfxV=e},colrect:(e,t,a,l,n,i,r,o)=>e<n+r&&e+a>n&&t<i+o&&t+l>i,colcirc:(e,t,a,l,n,i)=>(l-e)**2+(n-t)**2<=(a+i)**2,use(e,t={}){c?M(e,t):f.push([e,t])},listen:(e,t)=>(k[e]=k[e]||new Set,k[e].add(t),()=>k[e].delete(t)),emit(e,t,a,l,n){z("before:"+e,t,a,l,n),z(e,t,a,l,n),z("after:"+e,t,a,l,n)},getcolor:e=>a[~~e%a.length],setvar(e,t){C[e]=t,S&&(l[e]=t)},resize(e,t){C.setvar("WIDTH",p.width=e),C.setvar("HEIGHT",p.height=t),O()},timescale(e){E=e},setfps(e){w=1e3*(T=1/e),x=0},quit(){for(let e of(C.emit("quit"),o))e();if(y=k=!1,S){for(let e in C)delete l[e];delete l.__litecanvas}}};for(let e of["sin","cos","atan2","hypot","tan","abs","ceil","round","floor","trunc","min","max","pow","sqrt","sign","exp"])C[e]=Math[e];function L(){c=!0;let t=e.loop?e.loop:l;for(let e in k)t[e]&&C.listen(e,t[e]);for(let[e,t]of f)M(e,t);if((u||d)&&s(l,"resize",O),O(),e.tapEvents){let e=(e,t)=>[(e-p.offsetLeft)/g,(t-p.offsetTop)/g],t=new Map,a=(e,a,l)=>{let n={x:a,y:l,startX:a,startY:l,ts:performance.now()};return t.set(e,n),n},n=(e,l,n)=>{let i=t.get(e)||a(e);i.x=l,i.y=n},i=e=>e&&performance.now()-e.ts<=200,r=!1;s(p,"mousedown",t=>{t.preventDefault();let[l,n]=e(t.pageX,t.pageY);C.emit("tap",l,n,0),a(0,l,n),r=!0}),s(p,"mousemove",t=>{t.preventDefault();let[a,l]=e(t.pageX,t.pageY);C.setvar("MOUSEX",a),C.setvar("MOUSEY",l),r&&(C.emit("tapping",a,l,0),n(0,a,l))}),s(p,"mouseup",a=>{a.preventDefault();let l=t.get(0),[n,o]=e(a.pageX,a.pageY);i(l)&&C.emit("tapped",l.startX,l.startY,0),C.emit("untap",n,o,0),t.delete(0),r=!1}),s(p,"touchstart",t=>{for(let l of(t.preventDefault(),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);C.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),s(p,"touchmove",t=>{for(let a of(t.preventDefault(),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);C.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});let o=e=>{e.preventDefault();let a=[];if(e.targetTouches.length>0)for(let t of e.targetTouches)a.push(t.identifier+1);for(let[e,l]of t)a.includes(e)||(i(l)&&C.emit("tapped",l.startX,l.startY,e),C.emit("untap",l.x,l.y,e),t.delete(e))};s(p,"touchend",o),s(p,"touchcancel",o),s(l,"blur",()=>{for(let[e,a]of(r=!1,t))C.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=new Set;C.setvar("iskeydown",t=>"any"===t?e.size>0:e.has(t.toLowerCase())),s(l,"keydown",t=>{e.add(t.key.toLowerCase())}),s(l,"keyup",t=>{e.delete(t.key.toLowerCase())}),s(l,"blur",()=>e.clear())}e.pauseOnBlur&&(s(l,"blur",()=>{y=!1}),s(l,"focus",()=>{y=!0,r(X)})),C.setfps(e.fps),C.emit("init",C),b=performance.now(),r(X)}function X(e){let t=!h,a=e-b;for(x+=a>100?100:a,b=e;x>=w;)C.emit("update",T*E),C.setvar("ELAPSED",C.ELAPSED+T*E),x-=w,t=!0;t&&(C.textalign("start","top"),C.emit("draw")),y&&h&&r(X)}function O(){let t=l.innerWidth,a=l.innerHeight,n=p.style;n.display="block",u?(n.position="absolute",n.inset=0,C.setvar("WIDTH",p.width=t),C.setvar("HEIGHT",p.height=a)):d&&(n.margin="auto",g=Math.min(t/C.WIDTH,a/C.HEIGHT),g=(e.pixelart?~~g:g)||1,n.width=C.WIDTH*g+"px",n.height=C.HEIGHT*g+"px"),C.setvar("CENTERX",C.WIDTH/2),C.setvar("CENTERY",C.HEIGHT/2),(!e.antialias||e.pixelart)&&(m.imageSmoothingEnabled=!1,p.style.imageRendering="pixelated"),C.emit("resized",g),h||r(X)}function z(e,t,a,l,n){if(k[e])for(let i of k[e])i(t,a,l,n)}function M(e,t){let a=e(C,A,t);if("object"==typeof a)for(let e of Object.keys(a))C.setvar(e,a[e])}if(S){if(l.__litecanvas)throw"global litecanvas already instantiated";Object.assign(l,C),l.__litecanvas=C}return p="string"==typeof p?document.querySelector(p):p,C.setvar("CANVAS",p),m=p.getContext("2d"),s(p,"click",()=>l.focus()),C.WIDTH>0&&(u=!1),p.style="",p.width=C.WIDTH,p.height=C.HEIGHT||C.WIDTH,p.parentNode||document.body.appendChild(p),"loading"===document.readyState?s(l,"DOMContentLoaded",()=>r(L)):r(L),C}})();
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -66,7 +66,7 @@ export default function litecanvas(settings = {}) {
|
|
|
66
66
|
/** @type {number} */
|
|
67
67
|
_timeScale = 1,
|
|
68
68
|
/** @type {number} */
|
|
69
|
-
|
|
69
|
+
_lastFrameTime,
|
|
70
70
|
/** @type {number} */
|
|
71
71
|
_step,
|
|
72
72
|
/** @type {number} */
|
|
@@ -666,14 +666,14 @@ export default function litecanvas(settings = {}) {
|
|
|
666
666
|
*
|
|
667
667
|
* @param {number} x
|
|
668
668
|
* @param {number} y
|
|
669
|
-
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement}
|
|
669
|
+
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
670
670
|
*/
|
|
671
|
-
image(x, y,
|
|
671
|
+
image(x, y, source) {
|
|
672
672
|
if (DEV_BUILD) {
|
|
673
673
|
assert(isFinite(x), 'image: 1st param must be a number')
|
|
674
674
|
assert(isFinite(y), 'image: 2nd param must be a number')
|
|
675
675
|
}
|
|
676
|
-
_ctx.drawImage(
|
|
676
|
+
_ctx.drawImage(source, ~~x, ~~y)
|
|
677
677
|
},
|
|
678
678
|
|
|
679
679
|
/**
|
|
@@ -1408,7 +1408,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1408
1408
|
// start the game loop
|
|
1409
1409
|
instance.emit('init', instance)
|
|
1410
1410
|
|
|
1411
|
-
|
|
1411
|
+
_lastFrameTime = performance.now()
|
|
1412
1412
|
raf(drawFrame)
|
|
1413
1413
|
}
|
|
1414
1414
|
|
|
@@ -1417,10 +1417,10 @@ export default function litecanvas(settings = {}) {
|
|
|
1417
1417
|
*/
|
|
1418
1418
|
function drawFrame(now) {
|
|
1419
1419
|
let shouldRender = !_animated,
|
|
1420
|
-
delta = now -
|
|
1420
|
+
delta = now - _lastFrameTime
|
|
1421
1421
|
|
|
1422
|
-
_accumulated += delta > 100 ?
|
|
1423
|
-
|
|
1422
|
+
_accumulated += delta > 100 ? 100 : delta
|
|
1423
|
+
_lastFrameTime = now
|
|
1424
1424
|
|
|
1425
1425
|
while (_accumulated >= _stepMs) {
|
|
1426
1426
|
instance.emit('update', _step * _timeScale)
|
package/src/types.js
CHANGED
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* @callback drawCallback
|
|
40
|
-
* @param {CanvasRenderingContext2D} context
|
|
40
|
+
* @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} context
|
|
41
41
|
* @returns {void}
|
|
42
42
|
*/
|
|
43
43
|
|
|
@@ -45,12 +45,13 @@
|
|
|
45
45
|
* @typedef LitecanvasPluginHelpers
|
|
46
46
|
* @type {object}
|
|
47
47
|
* @property {string[]} colors The instance color palette
|
|
48
|
-
* @property {LitecanvasOptions} settings
|
|
48
|
+
* @property {LitecanvasOptions} settings Litecanvas instance settings (read-only)
|
|
49
49
|
*/
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
52
|
* @callback pluginCallback
|
|
53
53
|
* @param {LitecanvasInstance} instance The litecanvas instance
|
|
54
54
|
* @param {LitecanvasPluginHelpers} helpers
|
|
55
|
+
* @param {any?} [config] - option plugin configuration
|
|
55
56
|
* @returns
|
|
56
57
|
*/
|
package/types/index.d.ts
CHANGED
|
@@ -318,16 +318,16 @@ declare global {
|
|
|
318
318
|
*
|
|
319
319
|
* @param x
|
|
320
320
|
* @param y
|
|
321
|
-
* @param
|
|
321
|
+
* @param message the text message
|
|
322
322
|
* @param [color=3] the color index
|
|
323
|
-
* @param [
|
|
323
|
+
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
324
324
|
*/
|
|
325
325
|
function text(
|
|
326
326
|
x: number,
|
|
327
327
|
y: number,
|
|
328
|
-
|
|
328
|
+
message: string,
|
|
329
329
|
color?: number,
|
|
330
|
-
|
|
330
|
+
fontStyle?: string
|
|
331
331
|
): void
|
|
332
332
|
/**
|
|
333
333
|
* Set the font family
|
|
@@ -357,26 +357,26 @@ declare global {
|
|
|
357
357
|
*
|
|
358
358
|
* @param x
|
|
359
359
|
* @param y
|
|
360
|
-
* @param
|
|
360
|
+
* @param source
|
|
361
361
|
*/
|
|
362
362
|
function image(
|
|
363
363
|
x: number,
|
|
364
364
|
y: number,
|
|
365
|
-
|
|
365
|
+
source: OffscreenCanvas | HTMLImageElement | HTMLCanvasElement
|
|
366
366
|
): void
|
|
367
367
|
/**
|
|
368
368
|
* Creates a offscreen canvas to draw on it
|
|
369
369
|
*
|
|
370
370
|
* @param width
|
|
371
371
|
* @param height
|
|
372
|
-
* @param
|
|
372
|
+
* @param drawing
|
|
373
373
|
* @param [options]
|
|
374
374
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
375
375
|
*/
|
|
376
376
|
function paint(
|
|
377
377
|
width: number,
|
|
378
378
|
height: number,
|
|
379
|
-
|
|
379
|
+
drawing: string[] | drawCallback,
|
|
380
380
|
options?: {
|
|
381
381
|
scale?: number
|
|
382
382
|
canvas?: HTMLCanvasElement | OffscreenCanvas
|
|
@@ -425,10 +425,10 @@ declare global {
|
|
|
425
425
|
/**
|
|
426
426
|
* Sets the alpha (transparency) value to apply when drawing new shapes and images
|
|
427
427
|
*
|
|
428
|
-
* @param
|
|
428
|
+
* @param value float from 0 to 1 (e.g: 0.5 = 50% transparent)
|
|
429
429
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
430
430
|
*/
|
|
431
|
-
function alpha(
|
|
431
|
+
function alpha(value: number): void
|
|
432
432
|
/**
|
|
433
433
|
* Returns a newly instantiated Path2D object, optionally with another
|
|
434
434
|
* path as an argument (creates a copy), or optionally with a string
|
package/types/types.d.ts
CHANGED
|
@@ -296,16 +296,16 @@ type LitecanvasInstance = {
|
|
|
296
296
|
*
|
|
297
297
|
* @param x
|
|
298
298
|
* @param y
|
|
299
|
-
* @param
|
|
299
|
+
* @param message the text message
|
|
300
300
|
* @param [color=3] the color index
|
|
301
|
-
* @param [
|
|
301
|
+
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
302
302
|
*/
|
|
303
303
|
text(
|
|
304
304
|
x: number,
|
|
305
305
|
y: number,
|
|
306
|
-
|
|
306
|
+
message: string,
|
|
307
307
|
color?: number,
|
|
308
|
-
|
|
308
|
+
fontStyle?: string
|
|
309
309
|
): void
|
|
310
310
|
/**
|
|
311
311
|
* Set the font family
|
|
@@ -335,26 +335,26 @@ type LitecanvasInstance = {
|
|
|
335
335
|
*
|
|
336
336
|
* @param x
|
|
337
337
|
* @param y
|
|
338
|
-
* @param
|
|
338
|
+
* @param source
|
|
339
339
|
*/
|
|
340
340
|
image(
|
|
341
341
|
x: number,
|
|
342
342
|
y: number,
|
|
343
|
-
|
|
343
|
+
source: OffscreenCanvas | HTMLImageElement | HTMLCanvasElement
|
|
344
344
|
): void
|
|
345
345
|
/**
|
|
346
346
|
* Creates a offscreen canvas to draw on it
|
|
347
347
|
*
|
|
348
348
|
* @param width
|
|
349
349
|
* @param height
|
|
350
|
-
* @param
|
|
350
|
+
* @param drawing
|
|
351
351
|
* @param [options]
|
|
352
352
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
353
353
|
*/
|
|
354
354
|
paint(
|
|
355
355
|
width: number,
|
|
356
356
|
height: number,
|
|
357
|
-
|
|
357
|
+
drawing: string[] | drawCallback,
|
|
358
358
|
options?: {
|
|
359
359
|
scale?: number
|
|
360
360
|
canvas?: HTMLCanvasElement | OffscreenCanvas
|
|
@@ -403,10 +403,10 @@ type LitecanvasInstance = {
|
|
|
403
403
|
/**
|
|
404
404
|
* Sets the alpha (transparency) value to apply when drawing new shapes and images
|
|
405
405
|
*
|
|
406
|
-
* @param
|
|
406
|
+
* @param value float from 0 to 1 (e.g: 0.5 = 50% transparent)
|
|
407
407
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
408
408
|
*/
|
|
409
|
-
alpha(
|
|
409
|
+
alpha(value: number): void
|
|
410
410
|
/**
|
|
411
411
|
* Returns a newly instantiated Path2D object, optionally with another
|
|
412
412
|
* path as an argument (creates a copy), or optionally with a string
|
|
@@ -668,7 +668,9 @@ type LitecanvasGameLoop = {
|
|
|
668
668
|
tapping?: (tapX: number, tapY: number, tapId: number) => void
|
|
669
669
|
}
|
|
670
670
|
|
|
671
|
-
type drawCallback = (
|
|
671
|
+
type drawCallback = (
|
|
672
|
+
context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
|
|
673
|
+
) => void
|
|
672
674
|
|
|
673
675
|
type LitecanvasPluginHelpers = {
|
|
674
676
|
/**
|
|
@@ -676,12 +678,13 @@ type LitecanvasPluginHelpers = {
|
|
|
676
678
|
*/
|
|
677
679
|
colors: string[]
|
|
678
680
|
/**
|
|
679
|
-
*
|
|
681
|
+
* Litecanvas instance settings (read-only)
|
|
680
682
|
*/
|
|
681
683
|
settings: LitecanvasOptions
|
|
682
684
|
}
|
|
683
685
|
|
|
684
686
|
type pluginCallback = (
|
|
685
687
|
instance: LitecanvasInstance,
|
|
686
|
-
helpers: LitecanvasPluginHelpers
|
|
688
|
+
helpers: LitecanvasPluginHelpers,
|
|
689
|
+
config?: any
|
|
687
690
|
) => any
|