q5 2.15.4 → 2.16.4
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/README.md +1 -1
- package/deno.json +1 -1
- package/package.json +1 -1
- package/q5.d.ts +258 -68
- package/q5.js +123 -48
- package/q5.min.js +2 -2
package/q5.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* q5.js
|
|
3
|
-
* @version 2.
|
|
3
|
+
* @version 2.16
|
|
4
4
|
* @author quinton-ashley, Tezumie, and LingDong-
|
|
5
5
|
* @license LGPL-3.0
|
|
6
6
|
* @class Q5
|
|
@@ -71,6 +71,7 @@ function Q5(scope, parent, renderer) {
|
|
|
71
71
|
|
|
72
72
|
$._incrementPreload = () => q._preloadCount++;
|
|
73
73
|
$._decrementPreload = () => q._preloadCount--;
|
|
74
|
+
$.disablePreloadSystem = () => ($._disablePreload = true);
|
|
74
75
|
|
|
75
76
|
$._draw = (timestamp) => {
|
|
76
77
|
let ts = timestamp || performance.now();
|
|
@@ -310,7 +311,7 @@ function createCanvas(w, h, opt) {
|
|
|
310
311
|
}
|
|
311
312
|
}
|
|
312
313
|
|
|
313
|
-
Q5.version = Q5.VERSION = '2.
|
|
314
|
+
Q5.version = Q5.VERSION = '2.16';
|
|
314
315
|
|
|
315
316
|
if (typeof document == 'object') {
|
|
316
317
|
document.addEventListener('DOMContentLoaded', () => {
|
|
@@ -1342,28 +1343,33 @@ Q5.renderers.c2d.image = ($, q) => {
|
|
|
1342
1343
|
let g = $.createImage(1, 1, opt);
|
|
1343
1344
|
let pd = (g._pixelDensity = opt?.pixelDensity || 1);
|
|
1344
1345
|
|
|
1345
|
-
function loaded(img) {
|
|
1346
|
-
img._pixelDensity = pd;
|
|
1347
|
-
g.defaultWidth = img.width * $._defaultImageScale;
|
|
1348
|
-
g.defaultHeight = img.height * $._defaultImageScale;
|
|
1349
|
-
g.naturalWidth = img.naturalWidth || img.width;
|
|
1350
|
-
g.naturalHeight = img.naturalHeight || img.height;
|
|
1351
|
-
g._setImageSize(Math.ceil(g.naturalWidth / pd), Math.ceil(g.naturalHeight / pd));
|
|
1352
|
-
|
|
1353
|
-
g.ctx.drawImage(img, 0, 0);
|
|
1354
|
-
q._preloadCount--;
|
|
1355
|
-
if (cb) cb(g);
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
1346
|
let img = new window.Image();
|
|
1359
1347
|
img.crossOrigin = 'Anonymous';
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1348
|
+
|
|
1349
|
+
g._loader = new Promise((resolve, reject) => {
|
|
1350
|
+
img.onload = () => {
|
|
1351
|
+
img._pixelDensity = pd;
|
|
1352
|
+
g.defaultWidth = img.width * $._defaultImageScale;
|
|
1353
|
+
g.defaultHeight = img.height * $._defaultImageScale;
|
|
1354
|
+
g.naturalWidth = img.naturalWidth || img.width;
|
|
1355
|
+
g.naturalHeight = img.naturalHeight || img.height;
|
|
1356
|
+
g._setImageSize(Math.ceil(g.naturalWidth / pd), Math.ceil(g.naturalHeight / pd));
|
|
1357
|
+
|
|
1358
|
+
g.ctx.drawImage(img, 0, 0);
|
|
1359
|
+
q._preloadCount--;
|
|
1360
|
+
if (cb) cb(g);
|
|
1361
|
+
delete g._loader;
|
|
1362
|
+
resolve(g);
|
|
1363
|
+
};
|
|
1364
|
+
img.onerror = (e) => {
|
|
1365
|
+
q._preloadCount--;
|
|
1366
|
+
reject(e);
|
|
1367
|
+
};
|
|
1368
|
+
});
|
|
1369
|
+
|
|
1365
1370
|
img.src = url;
|
|
1366
1371
|
|
|
1372
|
+
if ($._disablePreload) return g._loader;
|
|
1367
1373
|
return g;
|
|
1368
1374
|
};
|
|
1369
1375
|
|
|
@@ -1785,15 +1791,26 @@ Q5.renderers.c2d.text = ($, q) => {
|
|
|
1785
1791
|
let name = url.split('/').pop().split('.')[0].replace(' ', '');
|
|
1786
1792
|
let f = new FontFace(name, `url(${url})`);
|
|
1787
1793
|
document.fonts.add(f);
|
|
1788
|
-
f.
|
|
1794
|
+
f._loader = (async () => {
|
|
1795
|
+
let err;
|
|
1796
|
+
try {
|
|
1797
|
+
await f.load();
|
|
1798
|
+
} catch (e) {
|
|
1799
|
+
err = e;
|
|
1800
|
+
}
|
|
1789
1801
|
q._preloadCount--;
|
|
1790
|
-
|
|
1791
|
-
|
|
1802
|
+
delete f._loader;
|
|
1803
|
+
if (err) throw err;
|
|
1804
|
+
if (cb) cb(f);
|
|
1805
|
+
return f;
|
|
1806
|
+
})();
|
|
1792
1807
|
$.textFont(name);
|
|
1793
|
-
return
|
|
1808
|
+
if ($._disablePreload) return f._loader;
|
|
1809
|
+
return f;
|
|
1794
1810
|
};
|
|
1795
1811
|
|
|
1796
1812
|
$.textFont = (x) => {
|
|
1813
|
+
if (typeof x != 'string') x = x.family;
|
|
1797
1814
|
if (!x || x == font) return font;
|
|
1798
1815
|
font = x;
|
|
1799
1816
|
fontMod = true;
|
|
@@ -2521,7 +2538,9 @@ main {
|
|
|
2521
2538
|
}
|
|
2522
2539
|
if (displayMode == 'center') displayMode = 'centered';
|
|
2523
2540
|
Object.assign(c, { displayMode, renderQuality, displayScale });
|
|
2541
|
+
if ($.ctx) $.pushStyles();
|
|
2524
2542
|
$._adjustDisplay();
|
|
2543
|
+
if ($.ctx) $.popStyles();
|
|
2525
2544
|
};
|
|
2526
2545
|
|
|
2527
2546
|
$.fullscreen = (v) => {
|
|
@@ -3175,13 +3194,25 @@ Q5.modules.sound = ($, q) => {
|
|
|
3175
3194
|
|
|
3176
3195
|
$.loadSound = (url, cb) => {
|
|
3177
3196
|
q._preloadCount++;
|
|
3197
|
+
|
|
3178
3198
|
let s = new Q5.Sound();
|
|
3179
|
-
s.load(url)
|
|
3180
|
-
.then(() => {
|
|
3181
|
-
if (cb) cb(s);
|
|
3182
|
-
})
|
|
3183
|
-
.finally(() => q._preloadCount--);
|
|
3184
3199
|
sounds.push(s);
|
|
3200
|
+
|
|
3201
|
+
s._loader = (async () => {
|
|
3202
|
+
let err;
|
|
3203
|
+
try {
|
|
3204
|
+
await s.load(url);
|
|
3205
|
+
} catch (e) {
|
|
3206
|
+
err = e;
|
|
3207
|
+
}
|
|
3208
|
+
q._preloadCount--;
|
|
3209
|
+
delete s._loader;
|
|
3210
|
+
if (err) throw err;
|
|
3211
|
+
if (cb) cb(s);
|
|
3212
|
+
return s;
|
|
3213
|
+
})();
|
|
3214
|
+
|
|
3215
|
+
if ($._disablePreload) return s._loader;
|
|
3185
3216
|
return s;
|
|
3186
3217
|
};
|
|
3187
3218
|
|
|
@@ -3364,26 +3395,64 @@ Q5.Sound = class {
|
|
|
3364
3395
|
}
|
|
3365
3396
|
};
|
|
3366
3397
|
Q5.modules.util = ($, q) => {
|
|
3367
|
-
$._loadFile = (
|
|
3398
|
+
$._loadFile = (url, cb, type) => {
|
|
3368
3399
|
q._preloadCount++;
|
|
3369
3400
|
let ret = {};
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3401
|
+
ret._loader = new Promise((resolve, reject) => {
|
|
3402
|
+
fetch(url)
|
|
3403
|
+
.then((res) => {
|
|
3404
|
+
if (!res.ok) {
|
|
3405
|
+
reject('error loading file');
|
|
3406
|
+
return null;
|
|
3407
|
+
}
|
|
3408
|
+
if (type == 'json') return res.json();
|
|
3409
|
+
return res.text();
|
|
3410
|
+
})
|
|
3411
|
+
.then((f) => {
|
|
3412
|
+
if (type == 'csv') f = $.CSV.parse(f);
|
|
3413
|
+
if (typeof f == 'string') ret.text = f;
|
|
3414
|
+
else Object.assign(ret, f);
|
|
3415
|
+
delete ret._loader;
|
|
3416
|
+
if (cb) cb(f);
|
|
3417
|
+
q._preloadCount--;
|
|
3418
|
+
resolve(f);
|
|
3419
|
+
});
|
|
3420
|
+
});
|
|
3381
3421
|
return ret;
|
|
3382
3422
|
};
|
|
3383
3423
|
|
|
3384
|
-
$.loadText = (
|
|
3385
|
-
$.loadJSON = (
|
|
3386
|
-
$.loadCSV = (
|
|
3424
|
+
$.loadText = (url, cb) => $._loadFile(url, cb, 'text');
|
|
3425
|
+
$.loadJSON = (url, cb) => $._loadFile(url, cb, 'json');
|
|
3426
|
+
$.loadCSV = (url, cb) => $._loadFile(url, cb, 'csv');
|
|
3427
|
+
|
|
3428
|
+
$.load = function (...urls) {
|
|
3429
|
+
if (Array.isArray(urls[0])) urls = urls[0];
|
|
3430
|
+
|
|
3431
|
+
let loaders = [];
|
|
3432
|
+
|
|
3433
|
+
for (let url of urls) {
|
|
3434
|
+
let ext = url.split('.').pop().toLowerCase();
|
|
3435
|
+
|
|
3436
|
+
let obj;
|
|
3437
|
+
if (ext == 'json' && !url.includes('-msdf.')) {
|
|
3438
|
+
obj = $.loadJSON(url);
|
|
3439
|
+
} else if (ext == 'csv') {
|
|
3440
|
+
obj = $.loadCSV(url);
|
|
3441
|
+
} else if (/(jpe?g|png|gif|webp|avif|svg)/.test(ext)) {
|
|
3442
|
+
obj = $.loadImage(url);
|
|
3443
|
+
} else if (/(ttf|otf|woff2?|eot|json)/i.test(ext)) {
|
|
3444
|
+
obj = $.loadFont(url);
|
|
3445
|
+
} else if (/(wav|flac|mp3|ogg|m4a|aac|aiff|weba)/.test(ext)) {
|
|
3446
|
+
obj = $.loadSound(url);
|
|
3447
|
+
} else {
|
|
3448
|
+
obj = $.loadText(url);
|
|
3449
|
+
}
|
|
3450
|
+
loaders.push(obj._loader);
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
if (urls.length == 1) return loaders[0];
|
|
3454
|
+
return Promise.all(loaders);
|
|
3455
|
+
};
|
|
3387
3456
|
|
|
3388
3457
|
$.CSV = {};
|
|
3389
3458
|
$.CSV.parse = (csv, sep = ',', lineSep = '\n') => {
|
|
@@ -4887,7 +4956,7 @@ fn fragmentMain(@location(0) color: vec4f) -> @location(0) vec4f {
|
|
|
4887
4956
|
$.background = (r, g, b, a) => {
|
|
4888
4957
|
$.push();
|
|
4889
4958
|
$.resetMatrix();
|
|
4890
|
-
if (r.
|
|
4959
|
+
if (r.canvas) {
|
|
4891
4960
|
let img = r;
|
|
4892
4961
|
$._imageMode = 'corner';
|
|
4893
4962
|
$.image(img, -c.hw, -c.hh, c.w, c.h);
|
|
@@ -5100,7 +5169,7 @@ fn fragmentMain(f: FragmentParams) -> @location(0) vec4f {
|
|
|
5100
5169
|
g.defaultHeight = img.height * $._defaultImageScale;
|
|
5101
5170
|
$._createTexture(img);
|
|
5102
5171
|
q._preloadCount--;
|
|
5103
|
-
if (cb) cb(
|
|
5172
|
+
if (cb) cb(g);
|
|
5104
5173
|
});
|
|
5105
5174
|
return g;
|
|
5106
5175
|
};
|
|
@@ -5473,8 +5542,13 @@ fn fragmentMain(f : FragmentParams) -> @location(0) vec4f {
|
|
|
5473
5542
|
let ext = url.slice(url.lastIndexOf('.') + 1);
|
|
5474
5543
|
if (ext != 'json') return $._g.loadFont(url, cb);
|
|
5475
5544
|
let fontName = url.slice(url.lastIndexOf('/') + 1, url.lastIndexOf('-'));
|
|
5476
|
-
|
|
5477
|
-
|
|
5545
|
+
let f = { family: fontName };
|
|
5546
|
+
f._loader = createFont(url, fontName, () => {
|
|
5547
|
+
delete f._loader;
|
|
5548
|
+
if (cb) cb(f);
|
|
5549
|
+
});
|
|
5550
|
+
if ($._disablePreload) return f._loader;
|
|
5551
|
+
return f;
|
|
5478
5552
|
};
|
|
5479
5553
|
|
|
5480
5554
|
$._loadDefaultFont = (fontName) => {
|
|
@@ -5495,6 +5569,7 @@ fn fragmentMain(f : FragmentParams) -> @location(0) vec4f {
|
|
|
5495
5569
|
leadPercent = 1.25;
|
|
5496
5570
|
|
|
5497
5571
|
$.textFont = (fontName) => {
|
|
5572
|
+
if (typeof fontName != 'string') fontName = fontName.family;
|
|
5498
5573
|
let font = fonts[fontName];
|
|
5499
5574
|
if (font) $._font = font;
|
|
5500
5575
|
else if (font === undefined) $._loadDefaultFont(fontName);
|