litecanvas 0.78.2 → 0.79.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.dev.js +27 -48
- package/dist/dist.js +15 -28
- package/dist/dist.min.js +1 -1
- package/package.json +2 -2
- package/src/index.js +35 -56
- package/types/index.d.ts +0 -7
- package/types/types.d.ts +0 -7
package/dist/dist.dev.js
CHANGED
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
animate: true
|
|
51
51
|
};
|
|
52
52
|
settings = Object.assign(defaults, settings);
|
|
53
|
-
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 =
|
|
53
|
+
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 = 20, _rng_seed = Date.now(), _global = settings.global, _events = {
|
|
54
54
|
init: null,
|
|
55
55
|
update: null,
|
|
56
56
|
draw: null,
|
|
@@ -907,27 +907,6 @@
|
|
|
907
907
|
root[key] = value;
|
|
908
908
|
}
|
|
909
909
|
},
|
|
910
|
-
/**
|
|
911
|
-
* Resizes the game canvas and emit the "resized" event
|
|
912
|
-
*
|
|
913
|
-
* @param {number} width
|
|
914
|
-
* @param {number} height
|
|
915
|
-
*/
|
|
916
|
-
resize(width, height) {
|
|
917
|
-
DEV: assert(
|
|
918
|
-
isFinite(width) && width > 0,
|
|
919
|
-
"resize: 1st param must be a number"
|
|
920
|
-
);
|
|
921
|
-
DEV: assert(
|
|
922
|
-
isFinite(height) && height > 0,
|
|
923
|
-
"resize: 2nd param must be a number"
|
|
924
|
-
);
|
|
925
|
-
instance.setvar("WIDTH", _canvas.width = width);
|
|
926
|
-
instance.setvar("HEIGHT", _canvas.height = height);
|
|
927
|
-
instance.setvar("CENTERX", instance.WIDTH / 2);
|
|
928
|
-
instance.setvar("CENTERY", instance.HEIGHT / 2);
|
|
929
|
-
onResize();
|
|
930
|
-
},
|
|
931
910
|
/**
|
|
932
911
|
* The scale of the game's delta time (dt).
|
|
933
912
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
@@ -985,7 +964,7 @@
|
|
|
985
964
|
loadPlugin(callback, config);
|
|
986
965
|
}
|
|
987
966
|
if (settings.autoscale) {
|
|
988
|
-
on(root, "resize",
|
|
967
|
+
on(root, "resize", resizeCanvas);
|
|
989
968
|
}
|
|
990
969
|
if (settings.tapEvents) {
|
|
991
970
|
const _getXY = (pageX, pageY) => [
|
|
@@ -1185,47 +1164,47 @@
|
|
|
1185
1164
|
_canvas && _canvas.tagName === "CANVAS",
|
|
1186
1165
|
"Invalid canvas element"
|
|
1187
1166
|
);
|
|
1188
|
-
DEV: assert(
|
|
1189
|
-
null == instance.WIDTH || instance.WIDTH > 0,
|
|
1190
|
-
`Litecanvas' "width" option should be null or a positive number`
|
|
1191
|
-
);
|
|
1192
|
-
DEV: assert(
|
|
1193
|
-
null == instance.HEIGHT || instance.HEIGHT > 0,
|
|
1194
|
-
`Litecanvas' "width" option should be null or a positive number`
|
|
1195
|
-
);
|
|
1196
|
-
DEV: assert(
|
|
1197
|
-
null == instance.HEIGHT || instance.WIDTH > 0 && instance.HEIGHT > 0,
|
|
1198
|
-
`Litecanvas' "width" is required when "heigth" is passed`
|
|
1199
|
-
);
|
|
1200
1167
|
instance.setvar("CANVAS", _canvas);
|
|
1201
1168
|
_ctx = _canvas.getContext("2d");
|
|
1202
1169
|
on(_canvas, "click", () => root.focus());
|
|
1203
1170
|
_canvas.style = "";
|
|
1204
|
-
|
|
1205
|
-
instance.WIDTH = root.innerWidth;
|
|
1206
|
-
instance.HEIGHT = root.innerHeight;
|
|
1207
|
-
}
|
|
1208
|
-
instance.resize(instance.WIDTH, instance.HEIGHT, false);
|
|
1171
|
+
resizeCanvas();
|
|
1209
1172
|
if (!_canvas.parentNode) document.body.appendChild(_canvas);
|
|
1210
1173
|
}
|
|
1211
|
-
function
|
|
1212
|
-
|
|
1174
|
+
function resizeCanvas() {
|
|
1175
|
+
DEV: assert(
|
|
1176
|
+
null == settings.width || isFinite(settings.width) && settings.width > 0,
|
|
1177
|
+
`Litecanvas' option "width" should be a positive number when defined`
|
|
1178
|
+
);
|
|
1179
|
+
DEV: assert(
|
|
1180
|
+
null == settings.height || isFinite(settings.height) && settings.height > 0,
|
|
1181
|
+
`Litecanvas' option "height" should be a positive number when defined`
|
|
1182
|
+
);
|
|
1183
|
+
DEV: assert(
|
|
1184
|
+
null == settings.height || settings.width > 0 && settings.height > 0,
|
|
1185
|
+
`Litecanvas' option "width" is required when the option "height" is defined`
|
|
1186
|
+
);
|
|
1187
|
+
const width = settings.width || root.innerWidth, height = settings.height || settings.width || root.innerHeight;
|
|
1188
|
+
instance.setvar("WIDTH", _canvas.width = width);
|
|
1189
|
+
instance.setvar("HEIGHT", _canvas.height = height);
|
|
1190
|
+
instance.setvar("CENTERX", instance.WIDTH / 2);
|
|
1191
|
+
instance.setvar("CENTERY", instance.HEIGHT / 2);
|
|
1213
1192
|
if (settings.autoscale) {
|
|
1214
|
-
if (!
|
|
1215
|
-
|
|
1216
|
-
|
|
1193
|
+
if (!_canvas.style.display) {
|
|
1194
|
+
_canvas.style.display = "block";
|
|
1195
|
+
_canvas.style.margin = "auto";
|
|
1217
1196
|
}
|
|
1218
1197
|
_scale = math.min(
|
|
1219
1198
|
root.innerWidth / instance.WIDTH,
|
|
1220
1199
|
root.innerHeight / instance.HEIGHT
|
|
1221
1200
|
);
|
|
1222
1201
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1;
|
|
1223
|
-
|
|
1224
|
-
|
|
1202
|
+
_canvas.style.width = instance.WIDTH * _scale + "px";
|
|
1203
|
+
_canvas.style.height = instance.HEIGHT * _scale + "px";
|
|
1225
1204
|
}
|
|
1226
1205
|
if (!settings.antialias || settings.pixelart) {
|
|
1227
1206
|
_ctx.imageSmoothingEnabled = false;
|
|
1228
|
-
|
|
1207
|
+
_canvas.style.imageRendering = "pixelated";
|
|
1229
1208
|
}
|
|
1230
1209
|
instance.emit("resized", _scale);
|
|
1231
1210
|
if (!_animated) {
|
package/dist/dist.js
CHANGED
|
@@ -45,7 +45,7 @@
|
|
|
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 =
|
|
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 = 20, _rng_seed = Date.now(), _global = settings.global, _events = {
|
|
49
49
|
init: null,
|
|
50
50
|
update: null,
|
|
51
51
|
draw: null,
|
|
@@ -650,19 +650,6 @@
|
|
|
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.
|
|
@@ -712,7 +699,7 @@
|
|
|
712
699
|
loadPlugin(callback, config);
|
|
713
700
|
}
|
|
714
701
|
if (settings.autoscale) {
|
|
715
|
-
on(root, "resize",
|
|
702
|
+
on(root, "resize", resizeCanvas);
|
|
716
703
|
}
|
|
717
704
|
if (settings.tapEvents) {
|
|
718
705
|
const _getXY = (pageX, pageY) => [
|
|
@@ -904,31 +891,31 @@
|
|
|
904
891
|
_ctx = _canvas.getContext("2d");
|
|
905
892
|
on(_canvas, "click", () => root.focus());
|
|
906
893
|
_canvas.style = "";
|
|
907
|
-
|
|
908
|
-
instance.WIDTH = root.innerWidth;
|
|
909
|
-
instance.HEIGHT = root.innerHeight;
|
|
910
|
-
}
|
|
911
|
-
instance.resize(instance.WIDTH, instance.HEIGHT, false);
|
|
894
|
+
resizeCanvas();
|
|
912
895
|
if (!_canvas.parentNode) document.body.appendChild(_canvas);
|
|
913
896
|
}
|
|
914
|
-
function
|
|
915
|
-
const
|
|
897
|
+
function resizeCanvas() {
|
|
898
|
+
const width = settings.width || root.innerWidth, height = settings.height || settings.width || root.innerHeight;
|
|
899
|
+
instance.setvar("WIDTH", _canvas.width = width);
|
|
900
|
+
instance.setvar("HEIGHT", _canvas.height = height);
|
|
901
|
+
instance.setvar("CENTERX", instance.WIDTH / 2);
|
|
902
|
+
instance.setvar("CENTERY", instance.HEIGHT / 2);
|
|
916
903
|
if (settings.autoscale) {
|
|
917
|
-
if (!
|
|
918
|
-
|
|
919
|
-
|
|
904
|
+
if (!_canvas.style.display) {
|
|
905
|
+
_canvas.style.display = "block";
|
|
906
|
+
_canvas.style.margin = "auto";
|
|
920
907
|
}
|
|
921
908
|
_scale = math.min(
|
|
922
909
|
root.innerWidth / instance.WIDTH,
|
|
923
910
|
root.innerHeight / instance.HEIGHT
|
|
924
911
|
);
|
|
925
912
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1;
|
|
926
|
-
|
|
927
|
-
|
|
913
|
+
_canvas.style.width = instance.WIDTH * _scale + "px";
|
|
914
|
+
_canvas.style.height = instance.HEIGHT * _scale + "px";
|
|
928
915
|
}
|
|
929
916
|
if (!settings.antialias || settings.pixelart) {
|
|
930
917
|
_ctx.imageSmoothingEnabled = false;
|
|
931
|
-
|
|
918
|
+
_canvas.style.imageRendering = "pixelated";
|
|
932
919
|
}
|
|
933
920
|
instance.emit("resized", _scale);
|
|
934
921
|
if (!_animated) {
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=new AudioContext,t=(t=1,a=.05,
|
|
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,d=0,u=0,p=0,h=0,g=0,m=0,v=0,E=1,b=0,w=0,x=0)=>{let y=Math,T=2*y.PI,S=c*=500*T/44100/44100,k=l*=(1-a+2*a*y.random(a=[]))*T/44100,A=0,I=0,H=0,D=1,C=0,X=0,N=0,P=x<0?-1:1,L=T*P*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+P*O)/2/F,W=-(P+O)/F,B=0,U=0,q=0,V=0;for(n=44100*n+9,b*=44100,i*=44100,r*=44100,v*=44100,f*=500*T/85766121e6,g*=T/44100,d*=T/44100,u*=44100,p=44100*p|0,t*=.3*(globalThis.zzfxV||1),P=n+b+i+r+v|0;H<P;a[H++]=N*t)++X%(100*m|0)||(N=o?1<o?2<o?3<o?Y(A*A):y.max(y.min(y.tan(A),1),-1):1-(2*A/T%2+2)%2:1-4*y.abs(y.round(A/T)-A/T):Y(A),N=(p?1-w+w*Y(T*H/p):1)*(N<0?-1:1)*y.abs(N)**s*(H<n?H/n:H<n+b?1-(H-n)/b*(1-E):H<n+b+i?E:H<P-v?(P-H-v)/r*E:0),N=v?N/2+(v>H?0:(H<P-v?1:(P-H)/v)*a[H-v|0]/2/t):N,x&&(N=V=R*B+W*(B=U)+R*(U=N)-M*q-G*(q=V))),A+=(L=(l+=c+=f)*y.cos(g*I++))+L*h*Y(H**5),D&&++D>u&&(l+=d,k+=d,D=0),!p||++C%p||(l=k,c=S,D=D||1);(t=e.createBuffer(1,P,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,i=n.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,h=1,g,m=.5,v=1,E,b=1/60,w=0,x,y="sans-serif",T=20,S=Date.now(),k=e.global,A={init:null,update:null,draw:null,resized:null,tap:null,untap:null,tapping:null,tapped:null},I={settings:Object.assign({},e),colors:a},H={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 n.round(e);let a=10**t;return n.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,wrap:(e,t,a)=>e-(a-t)*n.floor((e-t)/(a-t)),map(e,t,a,l,n,i){let r=(e-t)/(a-t)*(n-l)+l;return i?H.clamp(r,l,n):r},norm:(e,t,a)=>H.map(e,t,a,0,1),rand:(e=0,t=1)=>(S=(1664525*S+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>n.floor(H.rand(e,t+1)),seed:e=>null==e?S:S=~~e,cls(e){null==e?g.clearRect(0,0,g.canvas.width,g.canvas.height):H.rectfill(0,0,g.canvas.width,g.canvas.height,e)},rect(e,t,a,l,n,i=null){g.beginPath(),g[i?"roundRect":"rect"](~~e-m,~~t-m,~~a+2*m,~~l+2*m,i),H.stroke(n)},rectfill(e,t,a,l,n,i=null){g.beginPath(),g[i?"roundRect":"rect"](~~e,~~t,~~a,~~l,i),H.fill(n)},circ(e,t,a,l){g.beginPath(),g.arc(~~e,~~t,~~a,0,r),H.stroke(l)},circfill(e,t,a,l){g.beginPath(),g.arc(~~e,~~t,~~a,0,r),H.fill(l)},line(e,t,a,l,n){g.beginPath();let i=.5*(0!==m&&~~e==~~a),r=.5*(0!==m&&~~t==~~l);g.moveTo(~~e+i,~~t+r),g.lineTo(~~a+i,~~l+r),H.stroke(n)},linewidth(e){g.lineWidth=~~e,m=.5*(~~e%2!=0)},linedash(e,t=0){g.setLineDash(e),g.lineDashOffset=t},text(e,t,a,l=3,n="normal"){g.font=`${n} ${T}px ${y}`,g.fillStyle=H.getcolor(l),g.fillText(a,~~e,~~t)},textfont(e){y=e},textsize(e){T=e},textalign(e,t){e&&(g.textAlign=e),t&&(g.textBaseline=t)},image(e,t,a){g.drawImage(a,~~e,~~t)},paint(e,t,a,l={}){let n=l.canvas||new OffscreenCanvas(1,1),i=l.scale||1,r=g;if(n.width=e*i,n.height=t*i,(g=n.getContext("2d")).scale(i,i),a.push){let e=0,t=0;for(let l of(g.imageSmoothingEnabled=!1,a)){for(let a of l)" "!==a&&"."!==a&&H.rectfill(e,t,1,1,parseInt(a,16)),e++;t++,e=0}}else a(g);return g=r,n},ctx:e=>(e&&(g=e),g),push:()=>g.save(),pop:()=>g.restore(),translate:(e,t)=>g.translate(~~e,~~t),scale:(e,t)=>g.scale(e,t||e),rotate:e=>g.rotate(e),alpha(e){g.globalAlpha=H.clamp(e,0,1)},path:e=>new Path2D(e),fill(e,t){g.fillStyle=H.getcolor(e),t?g.fill(t):g.fill()},stroke(e,t){g.strokeStyle=H.getcolor(e),t?g.stroke(t):g.stroke()},clip(e){g.clip(e)},sfx:(e,a=0,n=1)=>!(l.zzfxV<=0)&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e=e||H.DEFAULT_SFX,(0!==a||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},use(e,t={}){f?P(e,t):d.push([e,t])},listen:(e,t)=>(A[e]=A[e]||new Set,A[e].add(t),()=>A[e].delete(t)),emit(e,t,a,l,n){f&&(N("before:"+e,t,a,l,n),N(e,t,a,l,n),N("after:"+e,t,a,l,n))},getcolor:e=>a[~~e%a.length],setvar(e,t){H[e]=t,k&&(l[e]=t)},timescale(e){v=e},setfps(e){b=1/~~e},quit(){for(let e of(cancelAnimationFrame(x),H.emit("quit"),A=[],s))e();if(k){for(let e in H)delete l[e];delete l.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(","))H[e]=n[e];function D(){f=!0;let t=e.loop?e.loop:l;for(let e in A)t[e]&&H.listen(e,t[e]);for(let[e,t]of d)P(e,t);if(e.autoscale&&c(l,"resize",X),e.tapEvents){let e=(e,t)=>[(e-u.offsetLeft)/h,(t-u.offsetTop)/h],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<=300,r=e=>e.preventDefault(),o=!1;c(u,"mousedown",t=>{if(0===t.button){r(t);let[l,n]=e(t.pageX,t.pageY);H.emit("tap",l,n,0),a(0,l,n),o=!0}}),c(u,"mouseup",a=>{if(0===a.button){r(a);let l=t.get(0),[n,s]=e(a.pageX,a.pageY);i(l)&&H.emit("tapped",l.startX,l.startY,0),H.emit("untap",n,s,0),t.delete(0),o=!1}}),c(u,"mousemove",t=>{r(t);let[a,l]=e(t.pageX,t.pageY);H.setvar("MOUSEX",a),H.setvar("MOUSEY",l),o&&(H.emit("tapping",a,l,0),n(0,a,l))}),c(u,"touchstart",t=>{for(let l of(r(t),t.changedTouches)){let[t,n]=e(l.pageX,l.pageY);H.emit("tap",t,n,l.identifier+1),a(l.identifier+1,t,n)}}),c(u,"touchmove",t=>{for(let a of(r(t),t.changedTouches)){let[t,l]=e(a.pageX,a.pageY);H.emit("tapping",t,l,a.identifier+1),n(a.identifier+1,t,l)}});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,l]of t)a.includes(e)||(i(l)&&H.emit("tapped",l.startX,l.startY,e),H.emit("untap",l.x,l.y,e),t.delete(e))};c(u,"touchend",s),c(u,"touchcancel",s),c(l,"blur",()=>{for(let[e,a]of(o=!1,t))H.emit("untap",a.x,a.y,e),t.delete(e)})}if(e.keyboardEvents){let e=e=>e.toLowerCase(),t=new Set,a=new Set,n=(t,a)=>a?t.has("space"===e(a)?" ":e(a)):t.size>0;c(l,"keydown",l=>{t.has(e(l.key))||(t.add(e(l.key)),a.add(e(l.key)))}),c(l,"keyup",a=>{t.delete(e(a.key))}),c(l,"blur",()=>t.clear()),H.listen("after:draw",()=>a.clear()),H.setvar("iskeydown",e=>n(t,e)),H.setvar("iskeypressed",e=>n(a,e))}e.pauseOnBlur&&(c(l,"blur",()=>{x=cancelAnimationFrame(x)}),c(l,"focus",()=>{x||(x=o(C))})),H.emit("init",H),E=performance.now(),x=o(C)}function C(e){let t=0,a=(e-E)/1e3;if(E=e,p){if(x=o(C),a>.3)return;for(w+=a;w>=b;)H.emit("update",b*v),H.setvar("ELAPSED",H.ELAPSED+b*v),t++,w-=b}else t=1;t&&(H.textalign("start","top"),H.emit("draw"))}function X(){let t=e.width||l.innerWidth,a=e.height||e.width||l.innerHeight;H.setvar("WIDTH",u.width=t),H.setvar("HEIGHT",u.height=a),H.setvar("CENTERX",H.WIDTH/2),H.setvar("CENTERY",H.HEIGHT/2),e.autoscale&&(u.style.display||(u.style.display="block",u.style.margin="auto"),h=n.min(l.innerWidth/H.WIDTH,l.innerHeight/H.HEIGHT),h=(e.pixelart?~~h:h)||1,u.style.width=H.WIDTH*h+"px",u.style.height=H.HEIGHT*h+"px"),(!e.antialias||e.pixelart)&&(g.imageSmoothingEnabled=!1,u.style.imageRendering="pixelated"),H.emit("resized",h),p||o(C)}function N(e,t,a,l,n){if(A[e])for(let i of A[e])i(t,a,l,n)}function P(e,t){let a=e(H,I,t);for(let e in a)H.setvar(e,a[e])}if(k){if(l.ENGINE)throw"two global litecanvas detected";Object.assign(l,H),l.ENGINE=H}return u="string"==typeof u?document.querySelector(u):u,H.setvar("CANVAS",u),g=u.getContext("2d"),c(u,"click",()=>l.focus()),u.style="",X(),u.parentNode||document.body.appendChild(u),"loading"===document.readyState?c(l,"DOMContentLoaded",()=>o(D)):o(D),H}})();
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.79.0",
|
|
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.
|
|
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",
|
package/src/index.js
CHANGED
|
@@ -71,7 +71,7 @@ export default function litecanvas(settings = {}) {
|
|
|
71
71
|
/** @type {string} */
|
|
72
72
|
_fontFamily = 'sans-serif',
|
|
73
73
|
/** @type {number} */
|
|
74
|
-
_fontSize =
|
|
74
|
+
_fontSize = 20,
|
|
75
75
|
/** @type {number} */
|
|
76
76
|
_rng_seed = Date.now(),
|
|
77
77
|
/** @type {boolean} */
|
|
@@ -1078,31 +1078,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1078
1078
|
}
|
|
1079
1079
|
},
|
|
1080
1080
|
|
|
1081
|
-
/**
|
|
1082
|
-
* Resizes the game canvas and emit the "resized" event
|
|
1083
|
-
*
|
|
1084
|
-
* @param {number} width
|
|
1085
|
-
* @param {number} height
|
|
1086
|
-
*/
|
|
1087
|
-
resize(width, height) {
|
|
1088
|
-
DEV: assert(
|
|
1089
|
-
isFinite(width) && width > 0,
|
|
1090
|
-
'resize: 1st param must be a number'
|
|
1091
|
-
)
|
|
1092
|
-
DEV: assert(
|
|
1093
|
-
isFinite(height) && height > 0,
|
|
1094
|
-
'resize: 2nd param must be a number'
|
|
1095
|
-
)
|
|
1096
|
-
|
|
1097
|
-
instance.setvar('WIDTH', (_canvas.width = width))
|
|
1098
|
-
instance.setvar('HEIGHT', (_canvas.height = height))
|
|
1099
|
-
|
|
1100
|
-
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1101
|
-
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1102
|
-
|
|
1103
|
-
onResize()
|
|
1104
|
-
},
|
|
1105
|
-
|
|
1106
1081
|
/**
|
|
1107
1082
|
* The scale of the game's delta time (dt).
|
|
1108
1083
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
@@ -1183,7 +1158,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1183
1158
|
|
|
1184
1159
|
// listen window resize event when "autoscale" is enabled
|
|
1185
1160
|
if (settings.autoscale) {
|
|
1186
|
-
on(root, 'resize',
|
|
1161
|
+
on(root, 'resize', resizeCanvas)
|
|
1187
1162
|
}
|
|
1188
1163
|
|
|
1189
1164
|
// default mouse/touch handlers
|
|
@@ -1435,7 +1410,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1435
1410
|
}
|
|
1436
1411
|
|
|
1437
1412
|
if (updated) {
|
|
1438
|
-
// by default the text
|
|
1439
1413
|
// always set default values for
|
|
1440
1414
|
// _ctx.textAlign and _ctx.textBaseline before draw
|
|
1441
1415
|
instance.textalign('start', 'top')
|
|
@@ -1454,19 +1428,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1454
1428
|
_canvas && _canvas.tagName === 'CANVAS',
|
|
1455
1429
|
'Invalid canvas element'
|
|
1456
1430
|
)
|
|
1457
|
-
DEV: assert(
|
|
1458
|
-
null == instance.WIDTH || instance.WIDTH > 0,
|
|
1459
|
-
'Litecanvas\' "width" option should be null or a positive number'
|
|
1460
|
-
)
|
|
1461
|
-
DEV: assert(
|
|
1462
|
-
null == instance.HEIGHT || instance.HEIGHT > 0,
|
|
1463
|
-
'Litecanvas\' "width" option should be null or a positive number'
|
|
1464
|
-
)
|
|
1465
|
-
DEV: assert(
|
|
1466
|
-
null == instance.HEIGHT ||
|
|
1467
|
-
(instance.WIDTH > 0 && instance.HEIGHT > 0),
|
|
1468
|
-
'Litecanvas\' "width" is required when "heigth" is passed'
|
|
1469
|
-
)
|
|
1470
1431
|
|
|
1471
1432
|
instance.setvar('CANVAS', _canvas)
|
|
1472
1433
|
_ctx = _canvas.getContext('2d')
|
|
@@ -1475,24 +1436,41 @@ export default function litecanvas(settings = {}) {
|
|
|
1475
1436
|
|
|
1476
1437
|
_canvas.style = ''
|
|
1477
1438
|
|
|
1478
|
-
|
|
1479
|
-
if (!instance.WIDTH) {
|
|
1480
|
-
instance.WIDTH = root.innerWidth
|
|
1481
|
-
instance.HEIGHT = root.innerHeight
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
instance.resize(instance.WIDTH, instance.HEIGHT, false)
|
|
1439
|
+
resizeCanvas()
|
|
1485
1440
|
|
|
1486
1441
|
if (!_canvas.parentNode) document.body.appendChild(_canvas)
|
|
1487
1442
|
}
|
|
1488
1443
|
|
|
1489
|
-
function
|
|
1490
|
-
|
|
1444
|
+
function resizeCanvas() {
|
|
1445
|
+
DEV: assert(
|
|
1446
|
+
null == settings.width ||
|
|
1447
|
+
(isFinite(settings.width) && settings.width > 0),
|
|
1448
|
+
'Litecanvas\' option "width" should be a positive number when defined'
|
|
1449
|
+
)
|
|
1450
|
+
DEV: assert(
|
|
1451
|
+
null == settings.height ||
|
|
1452
|
+
(isFinite(settings.height) && settings.height > 0),
|
|
1453
|
+
'Litecanvas\' option "height" should be a positive number when defined'
|
|
1454
|
+
)
|
|
1455
|
+
DEV: assert(
|
|
1456
|
+
null == settings.height ||
|
|
1457
|
+
(settings.width > 0 && settings.height > 0),
|
|
1458
|
+
'Litecanvas\' option "width" is required when the option "height" is defined'
|
|
1459
|
+
)
|
|
1460
|
+
|
|
1461
|
+
const width = settings.width || root.innerWidth,
|
|
1462
|
+
height = settings.height || settings.width || root.innerHeight
|
|
1463
|
+
|
|
1464
|
+
instance.setvar('WIDTH', (_canvas.width = width))
|
|
1465
|
+
instance.setvar('HEIGHT', (_canvas.height = height))
|
|
1466
|
+
|
|
1467
|
+
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1468
|
+
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1491
1469
|
|
|
1492
1470
|
if (settings.autoscale) {
|
|
1493
|
-
if (!
|
|
1494
|
-
|
|
1495
|
-
|
|
1471
|
+
if (!_canvas.style.display) {
|
|
1472
|
+
_canvas.style.display = 'block'
|
|
1473
|
+
_canvas.style.margin = 'auto'
|
|
1496
1474
|
}
|
|
1497
1475
|
|
|
1498
1476
|
_scale = math.min(
|
|
@@ -1501,16 +1479,17 @@ export default function litecanvas(settings = {}) {
|
|
|
1501
1479
|
)
|
|
1502
1480
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1
|
|
1503
1481
|
|
|
1504
|
-
|
|
1505
|
-
|
|
1482
|
+
_canvas.style.width = instance.WIDTH * _scale + 'px'
|
|
1483
|
+
_canvas.style.height = instance.HEIGHT * _scale + 'px'
|
|
1506
1484
|
}
|
|
1507
1485
|
|
|
1508
1486
|
// restore canvas image rendering properties
|
|
1509
1487
|
if (!settings.antialias || settings.pixelart) {
|
|
1510
1488
|
_ctx.imageSmoothingEnabled = false
|
|
1511
|
-
|
|
1489
|
+
_canvas.style.imageRendering = 'pixelated'
|
|
1512
1490
|
}
|
|
1513
1491
|
|
|
1492
|
+
// trigger "resized" event
|
|
1514
1493
|
instance.emit('resized', _scale)
|
|
1515
1494
|
|
|
1516
1495
|
// force redraw
|
package/types/index.d.ts
CHANGED
|
@@ -588,13 +588,6 @@ declare global {
|
|
|
588
588
|
* @param value
|
|
589
589
|
*/
|
|
590
590
|
function setvar(key: string, value: any): void
|
|
591
|
-
/**
|
|
592
|
-
* Resizes the game canvas and emit the "resized" event
|
|
593
|
-
*
|
|
594
|
-
* @param width
|
|
595
|
-
* @param height
|
|
596
|
-
*/
|
|
597
|
-
function resize(width: number, height: number): void
|
|
598
591
|
/**
|
|
599
592
|
* The scale of the game's delta time (dt).
|
|
600
593
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
package/types/types.d.ts
CHANGED
|
@@ -560,13 +560,6 @@ type LitecanvasInstance = {
|
|
|
560
560
|
* @param value
|
|
561
561
|
*/
|
|
562
562
|
setvar(key: string, value: any): void
|
|
563
|
-
/**
|
|
564
|
-
* Resizes the game canvas and emit the "resized" event
|
|
565
|
-
*
|
|
566
|
-
* @param width
|
|
567
|
-
* @param height
|
|
568
|
-
*/
|
|
569
|
-
resize(width: number, height: number): void
|
|
570
563
|
/**
|
|
571
564
|
* The scale of the game's delta time (dt).
|
|
572
565
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|