|
27 | 27 | var assert = (condition, message = "Assertion failed") => {
|
28 | 28 | if (!condition) throw new Error(message);
|
29 | 29 | };
|
30 |
| - var version = "0.94.0"; |
| 30 | + var version = "0.94.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, _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, _fpsInterval = 1e3 / 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,
|
|
951 | 951 | isNumber(value) && value >= 1,
|
952 | 952 | "[litecanvas] framerate() 1st param must be a positive number"
|
953 | 953 | );
|
954 |
| - _deltaTime = 1 / ~~value; |
| 954 | + _fpsInterval = 1e3 / ~~value; |
955 | 955 | },
|
956 | 956 | /**
|
957 | 957 | * Returns information about that engine instance.
|
|
967 | 967 | // 1
|
968 | 968 | _initialized,
|
969 | 969 | // 2
|
970 |
| - _deltaTime, |
| 970 | + _fpsInterval / 1e3, |
971 | 971 | // 3
|
972 | 972 | _scale,
|
973 | 973 | // 4
|
|
1021 | 1021 | */
|
1022 | 1022 | resume() {
|
1023 | 1023 | if (_initialized && !_rafid) {
|
1024 |
| - _accumulated = _deltaTime; |
1025 |
| - _lastFrameTime = performance.now(); |
| 1024 | + _accumulated = _fpsInterval; |
| 1025 | + _lastFrameTime = Date.now(); |
1026 | 1026 | _rafid = raf(drawFrame);
|
1027 | 1027 | }
|
1028 | 1028 | },
|
|
1078 | 1078 | // initial y
|
1079 | 1079 | yi: y,
|
1080 | 1080 | // timestamp
|
1081 |
| - t: performance.now() |
| 1081 | + t: Date.now() |
1082 | 1082 | };
|
1083 | 1083 | _taps.set(id, tap);
|
1084 | 1084 | return tap;
|
|
1098 | 1098 | /**
|
1099 | 1099 | * @param {{t: number}} tap
|
1100 | 1100 | */
|
1101 |
| - (tap) => tap && performance.now() - tap.t <= 300 |
| 1101 | + (tap) => tap && Date.now() - tap.t <= 300 |
1102 | 1102 | ), preventDefault = (
|
1103 | 1103 | /**
|
1104 | 1104 | * @param {Event} ev
|
|
1271 | 1271 | }
|
1272 | 1272 | );
|
1273 | 1273 | }
|
1274 |
| - setInterval(() => { |
1275 |
| - if (_rafid) { |
1276 |
| - instance.pause(); |
1277 |
| - instance.resume(); |
1278 |
| - } |
1279 |
| - }, 5e3); |
1280 | 1274 | _initialized = true;
|
1281 | 1275 | instance.emit("init", instance);
|
1282 | 1276 | instance.resume();
|
1283 | 1277 | }
|
1284 |
| - function drawFrame(now) { |
| 1278 | + function drawFrame() { |
1285 | 1279 | if (!settings.animate) {
|
1286 | 1280 | return instance.emit("draw", _ctx);
|
1287 |
| - } else if (_rafid) { |
1288 |
| - _rafid = raf(drawFrame); |
1289 | 1281 | }
|
| 1282 | + let now = Date.now(); |
1290 | 1283 | let updated = 0;
|
1291 |
| - let frameTime = (now - _lastFrameTime) / 1e3; |
| 1284 | + let frameTime = now - _lastFrameTime; |
1292 | 1285 | _lastFrameTime = now;
|
1293 |
| - if (frameTime < 0.1) { |
1294 |
| - _accumulated += frameTime; |
1295 |
| - while (_accumulated >= _deltaTime) { |
1296 |
| - updated++; |
1297 |
| - instance.emit("update", _deltaTime * _timeScale, updated); |
1298 |
| - instance.def("T", instance.T + _deltaTime * _timeScale); |
1299 |
| - _accumulated -= _deltaTime; |
1300 |
| - } |
| 1286 | + _accumulated += frameTime < 100 ? frameTime : _fpsInterval; |
| 1287 | + while (_accumulated >= _fpsInterval) { |
| 1288 | + updated++; |
| 1289 | + _accumulated -= _fpsInterval; |
| 1290 | + let dt = _fpsInterval / 1e3 * _timeScale; |
| 1291 | + instance.emit("update", dt, updated); |
| 1292 | + instance.def("T", instance.T + dt); |
1301 | 1293 | }
|
1302 | 1294 | if (updated) {
|
1303 | 1295 | instance.emit("draw", _ctx);
|
| 1296 | + if (updated > 1) { |
| 1297 | + _accumulated = 0; |
| 1298 | + DEV: console.warn( |
| 1299 | + "[litecanvas] the last frame updated " + updated + " times. This can drop the FPS if it keeps happening." |
| 1300 | + ); |
| 1301 | + } |
1304 | 1302 | }
|
| 1303 | + _rafid = raf(drawFrame); |
1305 | 1304 | }
|
1306 | 1305 | function setupCanvas() {
|
1307 | 1306 | if ("string" === typeof settings.canvas) {
|
|
0 commit comments