Przeglądaj źródła

[ts] updated viewport check in utils.js

badlogic 8 lat temu
rodzic
commit
27096d64cf
1 zmienionych plików z 9 dodań i 7 usunięć
  1. 9 7
      spine-ts/webgl/demos/utils.js

+ 9 - 7
spine-ts/webgl/demos/utils.js

@@ -25,13 +25,15 @@ var spineDemos = {
 		}
 	}
 
-	function checkElementVisible (demo) {
-		var rect = demo.canvas.getBoundingClientRect();
-		var x = 0, y = 0;
-		var width = (window.innerHeight || document.documentElement.clientHeight);
-		var height = (window.innerWidth || document.documentElement.clientWidth);
-		demo.visible = rect.left < x + width && rect.right > x && rect.top < y + height && rect.bottom > y;		 
-	};
+	function checkElementVisible(demo) {
+		const rect = demo.canvas.getBoundingClientRect();
+		const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
+		const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
+		const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
+		const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
+
+		demo.visible = (vertInView && horInView);
+	}
 
 	spineDemos.setupRendering = function (canvas, renderFunc) {
 		var demo = {canvas: canvas, renderFunc: renderFunc, visible: false};