-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (62 loc) · 1.66 KB
/
index.js
File metadata and controls
76 lines (62 loc) · 1.66 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
/**
* pixi-timeout is a plugin which replicates the behaviour of window.setTimout
* but uses PIXI.Ticker (requestAnimationFrame) as the method of progressing time
* The bonus is that any timeouts will be paused and resumed automatically when
* you call PIXI.Application.stop & PIXI.Application.start
*/
(function init(pixi)
{
if (!pixi)
{
throw new Error("PIXI was not found")
}
if (!pixi.ticker || !pixi.ticker.shared)
{
throw new Error("PIXI.ticker was not found")
}
/**
* Sets a timeout.
*
* @param {number} secs The seconds
* @param {Function} cb Callback function to fire after timeout
* @return {object} Timer state object
*/
function setTimeout(secs, cb)
{
let progress = 0
const ticker = ((delta) =>
{
progress += delta;
const elapsed = progress / (60 * pixi.ticker.shared.speed);
if (elapsed > secs) end(true);
});
const end = (fire) =>
{
pixi.ticker.shared.remove(ticker);
if (fire) cb();
}
const clear = () =>
{
end(false)
};
const finish = () =>
{
end(true);
};
// start
pixi.ticker.shared.add(ticker);
return { clear, finish };
}
/**
* Clears a timeout, preventing the function from being fired
*
* @param {object} timerObj A timer state object
*/
function clearTimeout(timerObj)
{
timerObj.clear()
}
pixi.setTimeout = setTimeout
pixi.clearTimeout = clearTimeout
// eslint-disable-next-line
}(PIXI));