on master help sign in
Start using `requestAnimationFrame`
commit95726b7baf520ccb57f9f8711942e423c76c1d17 authorevancz <info@elm-lang.org> dateThu 18 Apr 2013, 22:25 (14 years ago) parentd2846a2
2 files changed+23−3
runtime/Init.js+5−3 runtime/Utils.js+18
runtime/Init.js+5−3
@@ -86,9 +86,11 @@ Elm.init = function(module, baseNode) {
8686 // set up updates so that the DOM is adjusted as necessary.
8787 var update = Render.update;
8888 function domUpdate(value) {
89− update(renderNode, visualModel, value);
90− visualModel = value;
91− adjustWindow();
89+ ElmRuntime.draw(function(_) {
90+ update(renderNode, visualModel, value);
91+ visualModel = value;
92+ adjustWindow();
93+ });
9294 return value;
9395 }
9496
runtime/Utils.js+18
@@ -30,4 +30,22 @@ ElmRuntime.filterDeadInputs = function(inputs) {
3030 return temp;
3131};
3232
33+// define the draw function
34+var vendors = ['ms', 'moz', 'webkit', 'o'];
35+for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
36+ window.requestAnimationFrame = window[vendors[i]+'RequestAnimationFrame'];
37+ window.cancelAnimationFrame = window[vendors[i]+'CancelAnimationFrame']
38+ || window[vendors[i]+'CancelRequestAnimationFrame'];
39+}
40+
41+if (window.requestAnimationFrame && window.cancelAnimationFrame) {
42+ var previous = 0;
43+ ElmRuntime.draw = function(callback) {
44+ window.cancelAnimationFrame(previous);
45+ previous = window.requestAnimationFrame(callback);
46+ };
47+} else {
48+ ElmRuntime.draw = function(callback) { callback(); };
49+}
50+
3351}());