Browse Source

Fix occasional failure to init event handlers

If the 'load' event fired on the `window` before Pixi.js ran `.load()`, then the
canvas element would not be in the DOM and an exception would be thrown before
the event handlers could be registered. Now, the event handlers are added at the
end of `loopStart`, the function called on Pixi's `.load()`.
Tyler Hallada 5 years ago
parent
commit
b350f1b271
1 changed files with 61 additions and 59 deletions
  1. 61 59
      js/proximity.js

+ 61 - 59
js/proximity.js

@@ -818,64 +818,7 @@ function loop () {
818 818
     window.requestAnimationFrame(loop);
819 819
 }
820 820
 
821
-function loopStart () {
822
-    screenWidth = window.innerWidth;
823
-    screenHeight = window.innerHeight;
824
-    // Create the renderer
825
-    renderer = window.PIXI.autoDetectRenderer(screenWidth, screenHeight, {antialias: true, resolution: resolution});
826
-
827
-    // Add the canvas to the HTML document
828
-    document.body.appendChild(renderer.view);
829
-
830
-    // Create a container object called the `stage`
831
-    stage = new window.PIXI.Container();
832
-
833
-    renderer.view.style.position = 'absolute';
834
-    renderer.view.style.display = 'block';
835
-    renderer.autoResize = true;
836
-
837
-    counter = 0;
838
-    totalScreenPixels = screenWidth + screenHeight;
839
-    connectionDistance = Math.min(Math.round(totalScreenPixels / 16), 75);
840
-    pointShiftDistance = Math.round(totalScreenPixels / 45);
841
-    clickMaxDistStart = Math.min(Math.round(totalScreenPixels / 20), clickMaxDistStart);
842
-    hoverMaxDistStart = Math.min(Math.round(totalScreenPixels / 16), hoverMaxDistStart);
843
-    polygon = new window.PIXI.Graphics();
844
-    stage.addChild(polygon);
845
-    numPoints = Math.round(totalScreenPixels / 6);
846
-    startPoints = getRandomPoints(numPoints, screenWidth, screenHeight, zRange, tweeningFns);
847
-    polygonPoints = {
848
-        original: startPoints,
849
-        target: JSON.parse(JSON.stringify(startPoints)),
850
-        tweened: JSON.parse(JSON.stringify(startPoints))
851
-    };
852
-
853
-    fpsGraphic = new window.PIXI.Text('0', {font: '25px monospace', fill: 'yellow'});
854
-    fpsGraphic.anchor = new window.PIXI.Point(1, 0);
855
-    fpsGraphic.x = screenWidth - 1;
856
-    fpsGraphic.y = 0;
857
-
858
-    if (fpsEnabled) {
859
-        stage.addChild(fpsGraphic);
860
-    }
861
-
862
-    lastLoop = new Date();
863
-
864
-    scrollDelta = 0;
865
-
866
-    // Try to fix bug where click initializes to a bogus value
867
-    click = null;
868
-    hover = null;
869
-
870
-    window.requestAnimationFrame(loop);
871
-}
872
-
873
-// Use PIXI loader to load image and then start animation
874
-window.PIXI.loader
875
-    .add(nodeImg)
876
-    .load(loopStart);
877
-
878
-window.addEventListener('load', function () {
821
+function registerEventHandlers() {
879 822
     var tweeningInputs, debugCheckbox, fpsCheckbox, nodeCheckbox, linesCheckbox;
880 823
     tweeningInputs = document.getElementsByName('tweening');
881 824
     debugCheckbox = document.getElementsByName('debugToggle')[0];
@@ -1148,4 +1091,63 @@ window.addEventListener('load', function () {
1148 1091
     linesCheckbox.addEventListener('change', function (e) {
1149 1092
         toggleLines();
1150 1093
     });
1151
-}, false);
1094
+}
1095
+
1096
+function loopStart () {
1097
+    screenWidth = window.innerWidth;
1098
+    screenHeight = window.innerHeight;
1099
+    // Create the renderer
1100
+    renderer = window.PIXI.autoDetectRenderer(screenWidth, screenHeight, {antialias: true, resolution: resolution});
1101
+
1102
+    // Add the canvas to the HTML document
1103
+    document.body.appendChild(renderer.view);
1104
+
1105
+    // Create a container object called the `stage`
1106
+    stage = new window.PIXI.Container();
1107
+
1108
+    renderer.view.style.position = 'absolute';
1109
+    renderer.view.style.display = 'block';
1110
+    renderer.autoResize = true;
1111
+
1112
+    counter = 0;
1113
+    totalScreenPixels = screenWidth + screenHeight;
1114
+    connectionDistance = Math.min(Math.round(totalScreenPixels / 16), 75);
1115
+    pointShiftDistance = Math.round(totalScreenPixels / 45);
1116
+    clickMaxDistStart = Math.min(Math.round(totalScreenPixels / 20), clickMaxDistStart);
1117
+    hoverMaxDistStart = Math.min(Math.round(totalScreenPixels / 16), hoverMaxDistStart);
1118
+    polygon = new window.PIXI.Graphics();
1119
+    stage.addChild(polygon);
1120
+    numPoints = Math.round(totalScreenPixels / 6);
1121
+    startPoints = getRandomPoints(numPoints, screenWidth, screenHeight, zRange, tweeningFns);
1122
+    polygonPoints = {
1123
+        original: startPoints,
1124
+        target: JSON.parse(JSON.stringify(startPoints)),
1125
+        tweened: JSON.parse(JSON.stringify(startPoints))
1126
+    };
1127
+
1128
+    fpsGraphic = new window.PIXI.Text('0', {font: '25px monospace', fill: 'yellow'});
1129
+    fpsGraphic.anchor = new window.PIXI.Point(1, 0);
1130
+    fpsGraphic.x = screenWidth - 1;
1131
+    fpsGraphic.y = 0;
1132
+
1133
+    if (fpsEnabled) {
1134
+        stage.addChild(fpsGraphic);
1135
+    }
1136
+
1137
+    lastLoop = new Date();
1138
+
1139
+    scrollDelta = 0;
1140
+
1141
+    // Try to fix bug where click initializes to a bogus value
1142
+    click = null;
1143
+    hover = null;
1144
+
1145
+    registerEventHandlers();
1146
+
1147
+    window.requestAnimationFrame(loop);
1148
+}
1149
+
1150
+// Use PIXI loader to load image and then start animation
1151
+window.PIXI.loader
1152
+    .add(nodeImg)
1153
+    .load(loopStart);