on master help sign in
Start using `requestAnimationFrame`
95726b7evancz
runtime/Init.js+5−3
@@ -86,9 +86,11 @@ Elm.init = function(module, baseNode) {
86 // set up updates so that the DOM is adjusted as necessary.
86 // set up updates so that the DOM is adjusted as necessary.
87 var update = Render.update;
87 var update = Render.update;
88 function domUpdate(value) {
88 function domUpdate(value) {
89+ ElmRuntime.draw(function(_) {
89− update(renderNode, visualModel, value);
90+ update(renderNode, visualModel, value);
90− visualModel = value;
91+ visualModel = value;
91− adjustWindow();
92+ adjustWindow();
93+ });
92 return value;
94 return value;
93 }
95 }
94
96
runtime/Utils.js+18
@@ -30,4 +30,22 @@ ElmRuntime.filterDeadInputs = function(inputs) {
30 return temp;
30 return temp;
31};
31};
32
32
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+
33}());
51}());