|
27 | 27 | var assert = (condition, message = "Assertion failed") => { |
28 | 28 | if (!condition) throw new Error(message); |
29 | 29 | }; |
30 | | - var version = "0.93.0"; |
| 30 | + var version = "0.93.1"; |
31 | 31 | function litecanvas(settings = {}) { |
32 | 32 | const root = window, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => { |
33 | 33 | elem.addEventListener(evt, callback, false); |
|
45 | 45 | animate: true |
46 | 46 | }; |
47 | 47 | settings = Object.assign(defaults, settings); |
48 | | - let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _coreEvents = "init,update,draw,tap,untap,tapping,tapped,resized", _mathFunctions = "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp", _eventListeners = {}; |
| 48 | + let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _coreEvents = "init,update,draw,tap,untap,tapping,tapped,resized", _mathFunctions = "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp", _eventListeners = {}; |
49 | 49 | const instance = { |
50 | 50 | /** @type {number} */ |
51 | 51 | W: 0, |
|
1020 | 1020 | * Resumes (if paused) the engine loop. |
1021 | 1021 | */ |
1022 | 1022 | resume() { |
1023 | | - if (!_rafid && _initialized) { |
| 1023 | + if (_initialized && !_rafid) { |
| 1024 | + _accumulated = 0; |
| 1025 | + _lastFrameTime = performance.now(); |
1024 | 1026 | _rafid = raf(drawFrame); |
1025 | 1027 | } |
1026 | 1028 | }, |
|
1269 | 1271 | } |
1270 | 1272 | ); |
1271 | 1273 | } |
| 1274 | + on(root, "focus", () => { |
| 1275 | + DEV: console.warn('[litecanvas] engine loop restarted on "focus" event'); |
| 1276 | + instance.pause(); |
| 1277 | + instance.resume(); |
| 1278 | + }); |
1272 | 1279 | _initialized = true; |
1273 | 1280 | instance.emit("init", instance); |
1274 | | - _lastFrameTime = performance.now(); |
1275 | 1281 | instance.resume(); |
1276 | 1282 | } |
1277 | 1283 | function drawFrame(now) { |
1278 | 1284 | if (!settings.animate) { |
1279 | 1285 | return instance.emit("draw"); |
| 1286 | + } else if (_rafid) { |
| 1287 | + _rafid = raf(drawFrame); |
1280 | 1288 | } |
1281 | 1289 | let updated = 0; |
1282 | 1290 | let frameTime = (now - _lastFrameTime) / 1e3; |
|
1293 | 1301 | if (updated) { |
1294 | 1302 | instance.emit("draw"); |
1295 | 1303 | } |
1296 | | - if (_rafid) { |
1297 | | - _rafid = raf(drawFrame); |
1298 | | - } |
1299 | 1304 | } |
1300 | 1305 | function setupCanvas() { |
1301 | 1306 | if ("string" === typeof settings.canvas) { |
|
0 commit comments