-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathart-runtime.js
More file actions
79 lines (67 loc) · 1.97 KB
/
art-runtime.js
File metadata and controls
79 lines (67 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function (global) {
'use strict';
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function frameLerp(current, target, alphaPerFrame, frameDelta) {
const alpha = clamp(alphaPerFrame, 0, 1);
const d = Math.max(0, frameDelta || 0);
const blended = 1 - Math.pow(1 - alpha, d);
return current + (target - current) * blended;
}
function frameDecay(value, decayPerFrame, frameDelta) {
const decay = clamp(decayPerFrame, 0, 1);
const d = Math.max(0, frameDelta || 0);
return value * Math.pow(decay, d);
}
function frameChance(probPerFrame, frameDelta) {
const p = clamp(probPerFrame, 0, 1);
const d = Math.max(0, frameDelta || 0);
const chance = 1 - Math.pow(1 - p, d);
return Math.random() < chance;
}
function createClock(options) {
const config = Object.assign({
baselineFps: 60,
minDtSec: 1 / 240,
maxDtSec: 0.05,
timeScale: 1
}, options || {});
let lastNowSec = null;
let elapsedSec = 0;
return {
tick(nowMs) {
const nowSec = (typeof nowMs === 'number' ? nowMs : performance.now()) / 1000;
if (lastNowSec === null) {
lastNowSec = nowSec;
const dtSec = clamp(1 / config.baselineFps, config.minDtSec, config.maxDtSec) * config.timeScale;
elapsedSec += dtSec;
return {
dtSec,
frameDelta: dtSec * config.baselineFps,
elapsedSec
};
}
const rawDtSec = nowSec - lastNowSec;
lastNowSec = nowSec;
const dtSec = clamp(rawDtSec, config.minDtSec, config.maxDtSec) * config.timeScale;
elapsedSec += dtSec;
return {
dtSec,
frameDelta: dtSec * config.baselineFps,
elapsedSec
};
},
reset() {
lastNowSec = null;
elapsedSec = 0;
}
};
}
global.PhosphorRuntime = {
createClock,
frameLerp,
frameDecay,
frameChance
};
})(window);