Backport setUp{Quantum,Proportional}AnimationFrame() from gewgaws.
catseye
11 years ago
28 | 28 | window.oCancelRequestAnimationFrame || |
29 | 29 | window.msCancelRequestAnimationFrame || |
30 | 30 | clearTimeout; |
31 | ||
32 | /* | |
33 | * Convenience function for using requestAnimationFrame. Calls the | |
34 | * object's draw() method on each animation frame, and calls update() | |
35 | * as necessary to ensure it is called once every tickTime milliseconds. | |
36 | * By default, tickTime = 1/60th of a second. It can be configured by | |
37 | * passing in a configuration dictionary after the object. If the | |
38 | * configuration dictionary is assigned to a variable outside this | |
39 | * function, after this function returns, the request entry in the | |
40 | * dictionary will contain the animation request handle. e.g., | |
41 | * | |
42 | * var cfg = {}; | |
43 | * cfg.tickTime = 1000.0 / 50.0; | |
44 | * yoob.setUpQuantumAnimationFrame(this, cfg); | |
45 | * cancelRequestAnimationFrame(cfg.request); | |
46 | * | |
47 | */ | |
48 | yoob.setUpQuantumAnimationFrame = function(object, cfg) { | |
49 | cfg = cfg || {}; | |
50 | cfg.lastTime = cfg.lastTime || null; | |
51 | cfg.accumDelta = cfg.accumDelta || 0; | |
52 | cfg.tickTime = cfg.tickTime || (1000.0 / 60.0); | |
53 | var animFrame = function(time) { | |
54 | object.draw(); | |
55 | if (cfg.lastTime === null) { | |
56 | cfg.lastTime = time; | |
57 | } | |
58 | cfg.accumDelta += (time - cfg.lastTime); | |
59 | while (cfg.accumDelta > cfg.tickTime) { | |
60 | cfg.accumDelta -= cfg.tickTime; | |
61 | object.update(); | |
62 | } | |
63 | cfg.lastTime = time; | |
64 | cfg.request = requestAnimationFrame(animFrame); | |
65 | }; | |
66 | cfg.request = requestAnimationFrame(animFrame); | |
67 | }; | |
68 | ||
69 | /* | |
70 | * Convenience function for using requestAnimationFrame. Calls the | |
71 | * object's draw() method on each animation frame, passing the amount | |
72 | * of time that has elapsed (in milliseconds) since the last time it | |
73 | * was called (or 0 if it was never previously called.) Otherwise | |
74 | * similar to yoob.setUpQuantumAnimationFrame. | |
75 | */ | |
76 | yoob.setUpProportionalAnimationFrame = function(object, cfg) { | |
77 | cfg = cfg || {}; | |
78 | cfg.lastTime = cfg.lastTime || null; | |
79 | var animFrame = function(time) { | |
80 | var timeElapsed = cfg.lastTime == null ? 0 : time - cfg.lastTime; | |
81 | cfg.lastTime = time; | |
82 | object.draw(timeElapsed); | |
83 | cfg.request = requestAnimationFrame(animFrame); | |
84 | }; | |
85 | cfg.request = requestAnimationFrame(animFrame); | |
86 | }; |