|
63 | 63 | animate: true
|
64 | 64 | };
|
65 | 65 | settings = Object.assign(defaults, settings);
|
66 |
| - 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, _fixedDeltaTime, _accumulated, _focused = true, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = { |
| 66 | + 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, _deltaTime, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = { |
67 | 67 | init: null,
|
68 | 68 | update: null,
|
69 | 69 | draw: null,
|
|
593 | 593 | textalign(align, baseline) {
|
594 | 594 | if (true) {
|
595 | 595 | assert(
|
596 |
| - null == align || "string" === typeof align, |
| 596 | + null == align || ["left", "right", "center", "start", "end"].includes( |
| 597 | + align |
| 598 | + ), |
597 | 599 | "textalign: 1st param must be a string"
|
598 | 600 | );
|
599 | 601 | assert(
|
600 |
| - null == baseline || "string" === typeof baseline, |
| 602 | + null == baseline || [ |
| 603 | + "top", |
| 604 | + "bottom", |
| 605 | + "middle", |
| 606 | + "hanging", |
| 607 | + "alphabetic", |
| 608 | + "ideographic" |
| 609 | + ].includes(baseline), |
601 | 610 | "textalign: 2nd param must be a string"
|
602 | 611 | );
|
603 | 612 | }
|
|
1015 | 1024 | "setvar: 1st param must be a string"
|
1016 | 1025 | );
|
1017 | 1026 | if (value == null) {
|
1018 |
| - console.warn(`setvar: key "${key}" was defined as ${value}`); |
1019 | 1027 | }
|
1020 | 1028 | }
|
1021 | 1029 | instance[key] = value;
|
|
1063 | 1071 | "setfps: 1st param must be a positive number"
|
1064 | 1072 | );
|
1065 | 1073 | }
|
1066 |
| - _fixedDeltaTime = 1 / ~~value; |
1067 |
| - _accumulated = 0; |
| 1074 | + _deltaTime = 1 / ~~value; |
1068 | 1075 | },
|
1069 | 1076 | /**
|
1070 | 1077 | * Stops the litecanvas instance and remove all event listeners.
|
|
1074 | 1081 | for (const removeListener of _browserEventListeners) {
|
1075 | 1082 | removeListener();
|
1076 | 1083 | }
|
1077 |
| - _focused = _events = false; |
| 1084 | + cancelAnimationFrame(_rafid); |
| 1085 | + _events = false; |
1078 | 1086 | if (_global) {
|
1079 | 1087 | for (const key in instance) {
|
1080 | 1088 | delete root[key];
|
|
1231 | 1239 | }
|
1232 | 1240 | if (settings.pauseOnBlur) {
|
1233 | 1241 | on(root, "blur", () => {
|
1234 |
| - _focused = false; |
| 1242 | + _rafid = cancelAnimationFrame(_rafid); |
1235 | 1243 | });
|
1236 | 1244 | on(root, "focus", () => {
|
1237 |
| - _focused = true; |
1238 |
| - raf(drawFrame); |
| 1245 | + if (!_rafid) { |
| 1246 | + _lastFrameTime = performance.now(); |
| 1247 | + _rafid = raf(drawFrame); |
| 1248 | + } |
1239 | 1249 | });
|
1240 | 1250 | }
|
1241 | 1251 | instance.setfps(60);
|
1242 | 1252 | instance.emit("init", instance);
|
1243 | 1253 | _lastFrameTime = performance.now();
|
1244 |
| - raf(drawFrame); |
| 1254 | + _rafid = raf(drawFrame); |
1245 | 1255 | }
|
1246 | 1256 | function drawFrame(now) {
|
1247 |
| - let shouldRender = !_animated, frameTime = (now - _lastFrameTime) / 1e3, frameTimeMax = _fixedDeltaTime * 5; |
1248 |
| - _accumulated += frameTime > frameTimeMax ? frameTimeMax : frameTime; |
| 1257 | + if (_animated) { |
| 1258 | + _rafid = raf(drawFrame); |
| 1259 | + } |
| 1260 | + let updated = 0, frameTime = (now - _lastFrameTime) / 1e3; |
| 1261 | + _accumulated += frameTime; |
1249 | 1262 | _lastFrameTime = now;
|
1250 |
| - while (_accumulated >= _fixedDeltaTime) { |
1251 |
| - instance.emit("update", _fixedDeltaTime * _timeScale); |
| 1263 | + if (!_animated) { |
| 1264 | + _accumulated = _deltaTime; |
| 1265 | + } |
| 1266 | + for (; _accumulated >= _deltaTime; _accumulated -= _deltaTime) { |
| 1267 | + instance.emit("update", _deltaTime * _timeScale); |
1252 | 1268 | instance.setvar(
|
1253 | 1269 | "ELAPSED",
|
1254 |
| - instance.ELAPSED + _fixedDeltaTime * _timeScale |
| 1270 | + instance.ELAPSED + _deltaTime * _timeScale |
1255 | 1271 | );
|
1256 |
| - _accumulated -= _fixedDeltaTime; |
1257 |
| - shouldRender = true; |
| 1272 | + updated++; |
1258 | 1273 | }
|
1259 |
| - if (shouldRender) { |
| 1274 | + if (updated) { |
1260 | 1275 | instance.textalign("start", "top");
|
1261 | 1276 | instance.emit("draw");
|
1262 | 1277 | }
|
1263 |
| - if (_focused && _animated) { |
1264 |
| - raf(drawFrame); |
1265 |
| - } |
1266 | 1278 | }
|
1267 | 1279 | function setupCanvas() {
|
1268 | 1280 | _canvas = "string" === typeof _canvas ? document.querySelector(_canvas) : _canvas;
|
|
0 commit comments