loadingScreen.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { Nullable } from "../types";
  2. import { Engine } from "../Engines/engine";
  3. /**
  4. * Interface used to present a loading screen while loading a scene
  5. * @see https://doc.babylonjs.com/how_to/creating_a_custom_loading_screen
  6. */
  7. export interface ILoadingScreen {
  8. /**
  9. * Function called to display the loading screen
  10. */
  11. displayLoadingUI: () => void;
  12. /**
  13. * Function called to hide the loading screen
  14. */
  15. hideLoadingUI: () => void;
  16. /**
  17. * Gets or sets the color to use for the background
  18. */
  19. loadingUIBackgroundColor: string;
  20. /**
  21. * Gets or sets the text to display while loading
  22. */
  23. loadingUIText: string;
  24. }
  25. /**
  26. * Class used for the default loading screen
  27. * @see https://doc.babylonjs.com/how_to/creating_a_custom_loading_screen
  28. */
  29. export class DefaultLoadingScreen implements ILoadingScreen {
  30. private _loadingDiv: Nullable<HTMLDivElement>;
  31. private _loadingTextDiv: HTMLDivElement;
  32. /** Gets or sets the logo url to use for the default loading screen */
  33. public static DefaultLogoUrl = "";
  34. /** Gets or sets the spinner url to use for the default loading screen */
  35. public static DefaultSpinnerUrl = "";
  36. /**
  37. * Creates a new default loading screen
  38. * @param _renderingCanvas defines the canvas used to render the scene
  39. * @param _loadingText defines the default text to display
  40. * @param _loadingDivBackgroundColor defines the default background color
  41. */
  42. constructor(private _renderingCanvas: HTMLCanvasElement, private _loadingText = "", private _loadingDivBackgroundColor = "black") {
  43. }
  44. /**
  45. * Function called to display the loading screen
  46. */
  47. public displayLoadingUI(): void {
  48. if (this._loadingDiv) {
  49. // Do not add a loading screen if there is already one
  50. return;
  51. }
  52. this._loadingDiv = document.createElement("div");
  53. this._loadingDiv.id = "babylonjsLoadingDiv";
  54. this._loadingDiv.style.opacity = "0";
  55. this._loadingDiv.style.transition = "opacity 1.5s ease";
  56. this._loadingDiv.style.pointerEvents = "none";
  57. this._loadingDiv.style.display = "grid";
  58. this._loadingDiv.style.gridTemplateRows = "100%";
  59. this._loadingDiv.style.gridTemplateColumns = "100%";
  60. this._loadingDiv.style.justifyItems = "center";
  61. this._loadingDiv.style.alignItems = "center";
  62. // Loading text
  63. this._loadingTextDiv = document.createElement("div");
  64. this._loadingTextDiv.style.position = "absolute";
  65. this._loadingTextDiv.style.left = "0";
  66. this._loadingTextDiv.style.top = "50%";
  67. this._loadingTextDiv.style.marginTop = "80px";
  68. this._loadingTextDiv.style.width = "100%";
  69. this._loadingTextDiv.style.height = "20px";
  70. this._loadingTextDiv.style.fontFamily = "Arial";
  71. this._loadingTextDiv.style.fontSize = "14px";
  72. this._loadingTextDiv.style.color = "white";
  73. this._loadingTextDiv.style.textAlign = "center";
  74. this._loadingTextDiv.style.zIndex = "1";
  75. this._loadingTextDiv.innerHTML = "Loading";
  76. this._loadingDiv.appendChild(this._loadingTextDiv);
  77. //set the predefined text
  78. this._loadingTextDiv.innerHTML = this._loadingText;
  79. // Generating keyframes
  80. var style = document.createElement('style');
  81. style.type = 'text/css';
  82. var keyFrames =
  83. `@-webkit-keyframes spin1 {\
  84. 0% { -webkit-transform: rotate(0deg);}
  85. 100% { -webkit-transform: rotate(360deg);}
  86. }\
  87. @keyframes spin1 {\
  88. 0% { transform: rotate(0deg);}
  89. 100% { transform: rotate(360deg);}
  90. }`;
  91. style.innerHTML = keyFrames;
  92. document.getElementsByTagName('head')[0].appendChild(style);
  93. const svgSupport = !!window.SVGSVGElement;
  94. // Loading img
  95. var imgBack = new Image();
  96. if (!DefaultLoadingScreen.DefaultLogoUrl) {
  97. imgBack.src = !svgSupport ? "https://cdn.babylonjs.com/Assets/babylonLogo.png" : `data:image/svg+xml;base64,PHN2ZyB4bWxucz0i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mi4wMSAxNTMuMDUgNjcuNjciLz48cG9seWdvbiBjbGFzcz0iY2xzLTMiIHBvaW50cz0iOTAuMDggNzEuNDYgNjEuODkgODcuNzQgNjEuODkgMTIwLjMgOTAuMDggMTA0LjAyIDExOC4yOCAxMjAuMyAxMTguMjggODcuNzQgOTAuMDggNzEuNDYiLz48cG9seWdvbiBjbGFzcz0iY2xzLTQiIHBvaW50cz0iMTUzLjA1IDY3LjY3IDExOC4yOCA4Ny43NCAxMTguMjggMTIwLjMgOTAuMDggMTM2LjU4IDkwLjA4IDE3Ni43MiAxNTMuMDUgMTQwLjM3IDE1My4wNSA2Ny42NyIvPjxwb2x5Z29uIGNsYXNzPSJjbHMtNSIgcG9pbnRzPSIyNy4xMiA2Ny42NyA2MS44OSA4Ny43NCA2MS44OSAxMjAuMyA5MC4wOCAxMzYuNTggOTAuMDggMTc2LjcyIDI3LjEyIDE0MC4zNyAyNy4xMiA2Ny42NyIvPjwvZz48L2c+PC9zdmc+`;
  98. } else {
  99. imgBack.src = DefaultLoadingScreen.DefaultLogoUrl;
  100. }
  101. imgBack.style.width = "150px";
  102. imgBack.style.gridColumn = "1";
  103. imgBack.style.gridRow = "1";
  104. const imageSpinnerContainer = document.createElement("div");
  105. imageSpinnerContainer.style.width = "300px";
  106. imageSpinnerContainer.style.gridColumn = "1";
  107. imageSpinnerContainer.style.gridRow = "1";
  108. // Loading spinner
  109. var imgSpinner = new Image();
  110. if (!DefaultLoadingScreen.DefaultSpinnerUrl) {
  111. imgSpinner.src = !svgSupport ? "https://cdn.babylonjs.com/Assets/loadingIcon.png" : `data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzOTIgMzkyIj48ZGVmcz48c3R5bGU+LmNscy0xe2ZpbGw6I2UwNjg0Yjt9LmNscy0ye2ZpbGw6bm9uZTt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPlNwaW5uZXJJY29uPC90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NCw0LjljLTEuMTMsMS42Ni0yLjIzLDMuMzUtMy4zMyw1YTEyNywxMjcsMCwwLDAtMTAuOTMsMjEuNDksMS41OCwxLjU4LDAsMSwxLTMtMS4xNVM0MC4xOSwxMjYuNDcsNDAuMjEsMTI2LjQzWiIvPjxyZWN0IGNsYXNzPSJjbHMtMiIgd2lkdGg9IjM5MiIgaGVpZ2h0PSIzOTIiLz48L2c+PC9nPjwvc3ZnPg==`;
  112. } else {
  113. imgSpinner.src = DefaultLoadingScreen.DefaultSpinnerUrl;
  114. }
  115. imgSpinner.style.animation = "spin1 0.75s infinite linear";
  116. imgSpinner.style.webkitAnimation = "spin1 0.75s infinite linear";
  117. imgSpinner.style.transformOrigin = "50% 50%";
  118. imgSpinner.style.webkitTransformOrigin = "50% 50%";
  119. if (!svgSupport) {
  120. const logoSize = { w: 16, h: 18.5 };
  121. const loadingSize = { w: 30, h: 30 };
  122. // set styling correctly
  123. imgBack.style.width = `${logoSize.w}vh`;
  124. imgBack.style.height = `${logoSize.h}vh`;
  125. imgBack.style.left = `calc(50% - ${logoSize.w / 2}vh)`;
  126. imgBack.style.top = `calc(50% - ${logoSize.h / 2}vh)`;
  127. imgSpinner.style.width = `${loadingSize.w}vh`;
  128. imgSpinner.style.height = `${loadingSize.h}vh`;
  129. imgSpinner.style.left = `calc(50% - ${loadingSize.w / 2}vh)`;
  130. imgSpinner.style.top = `calc(50% - ${loadingSize.h / 2}vh)`;
  131. }
  132. imageSpinnerContainer.appendChild(imgSpinner);
  133. this._loadingDiv.appendChild(imgBack);
  134. this._loadingDiv.appendChild(imageSpinnerContainer);
  135. this._resizeLoadingUI();
  136. window.addEventListener("resize", this._resizeLoadingUI);
  137. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  138. document.body.appendChild(this._loadingDiv);
  139. this._loadingDiv.style.opacity = "1";
  140. }
  141. /**
  142. * Function called to hide the loading screen
  143. */
  144. public hideLoadingUI(): void {
  145. if (!this._loadingDiv) {
  146. return;
  147. }
  148. var onTransitionEnd = () => {
  149. if (!this._loadingDiv) {
  150. return;
  151. }
  152. if (this._loadingDiv.parentElement) {
  153. this._loadingDiv.parentElement.removeChild(this._loadingDiv);
  154. }
  155. window.removeEventListener("resize", this._resizeLoadingUI);
  156. this._loadingDiv = null;
  157. };
  158. this._loadingDiv.style.opacity = "0";
  159. this._loadingDiv.addEventListener("transitionend", onTransitionEnd);
  160. }
  161. /**
  162. * Gets or sets the text to display while loading
  163. */
  164. public set loadingUIText(text: string) {
  165. this._loadingText = text;
  166. if (this._loadingTextDiv) {
  167. this._loadingTextDiv.innerHTML = this._loadingText;
  168. }
  169. }
  170. public get loadingUIText(): string {
  171. return this._loadingText;
  172. }
  173. /**
  174. * Gets or sets the color to use for the background
  175. */
  176. public get loadingUIBackgroundColor(): string {
  177. return this._loadingDivBackgroundColor;
  178. }
  179. public set loadingUIBackgroundColor(color: string) {
  180. this._loadingDivBackgroundColor = color;
  181. if (!this._loadingDiv) {
  182. return;
  183. }
  184. this._loadingDiv.style.backgroundColor = this._loadingDivBackgroundColor;
  185. }
  186. // Resize
  187. private _resizeLoadingUI = () => {
  188. var canvasRect = this._renderingCanvas.getBoundingClientRect();
  189. var canvasPositioning = window.getComputedStyle(this._renderingCanvas).position;
  190. if (!this._loadingDiv) {
  191. return;
  192. }
  193. this._loadingDiv.style.position = (canvasPositioning === "fixed") ? "fixed" : "absolute";
  194. this._loadingDiv.style.left = canvasRect.left + "px";
  195. this._loadingDiv.style.top = canvasRect.top + "px";
  196. this._loadingDiv.style.width = canvasRect.width + "px";
  197. this._loadingDiv.style.height = canvasRect.height + "px";
  198. }
  199. }
  200. Engine.DefaultLoadingScreenFactory = (canvas: HTMLCanvasElement) => { return new DefaultLoadingScreen(canvas); };