lessons-helper.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright 2012, Gregg Tavares.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Gregg Tavares. nor the names of his
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /* global define */
  32. (function(root, factory) { // eslint-disable-line
  33. if (typeof define === 'function' && define.amd) {
  34. // AMD. Register as an anonymous module.
  35. define([], function() {
  36. return factory.call(root);
  37. });
  38. } else {
  39. // Browser globals
  40. root.lessonsHelper = factory.call(root);
  41. }
  42. }(this, function() {
  43. 'use strict'; // eslint-disable-line
  44. const lessonSettings = window.lessonSettings || {};
  45. const topWindow = this;
  46. /**
  47. * Check if the page is embedded.
  48. * @param {Window?) w window to check
  49. * @return {boolean} True of we are in an iframe
  50. */
  51. function isInIFrame(w) {
  52. w = w || topWindow;
  53. return w !== w.top;
  54. }
  55. function updateCSSIfInIFrame() {
  56. if (isInIFrame()) {
  57. try {
  58. document.getElementsByTagName('html')[0].className = 'iframe';
  59. } catch (e) {
  60. // eslint-disable-line
  61. }
  62. try {
  63. document.body.className = 'iframe';
  64. } catch (e) {
  65. // eslint-disable-line
  66. }
  67. }
  68. }
  69. function isInEditor() {
  70. return window.location.href.substring(0, 4) === 'blob';
  71. }
  72. /**
  73. * Creates a webgl context. If creation fails it will
  74. * change the contents of the container of the <canvas>
  75. * tag to an error message with the correct links for WebGL.
  76. * @param {HTMLCanvasElement} canvas. The canvas element to
  77. * create a context from.
  78. * @param {WebGLContextCreationAttributes} opt_attribs Any
  79. * creation attributes you want to pass in.
  80. * @return {WebGLRenderingContext} The created context.
  81. * @memberOf module:webgl-utils
  82. */
  83. function showNeedWebGL(canvas) {
  84. const doc = canvas.ownerDocument;
  85. if (doc) {
  86. const temp = doc.createElement('div');
  87. temp.innerHTML = `
  88. <div style="
  89. position: absolute;
  90. left: 0;
  91. top: 0;
  92. background-color: #DEF;
  93. width: 100vw;
  94. height: 100vh;
  95. display: flex;
  96. flex-flow: column;
  97. justify-content: center;
  98. align-content: center;
  99. align-items: center;
  100. ">
  101. <div style="text-align: center;">
  102. It doesn't appear your browser supports WebGL.<br/>
  103. <a href="http://get.webgl.org" target="_blank">Click here for more information.</a>
  104. </div>
  105. </div>
  106. `;
  107. const div = temp.querySelector('div');
  108. doc.body.appendChild(div);
  109. }
  110. }
  111. const origConsole = {};
  112. function setupConsole() {
  113. const style = document.createElement('style');
  114. style.innerText = `
  115. .console {
  116. font-family: monospace;
  117. font-size: medium;
  118. max-height: 50%;
  119. position: fixed;
  120. bottom: 0;
  121. left: 0;
  122. width: 100%;
  123. overflow: auto;
  124. background: rgba(221, 221, 221, 0.9);
  125. }
  126. .console .console-line {
  127. white-space: pre;
  128. }
  129. .console .log .warn {
  130. color: black;
  131. }
  132. .console .error {
  133. color: red;
  134. }
  135. `;
  136. const parent = document.createElement('div');
  137. parent.className = 'console';
  138. const toggle = document.createElement('div');
  139. let show = false;
  140. Object.assign(toggle.style, {
  141. position: 'absolute',
  142. right: 0,
  143. bottom: 0,
  144. background: '#EEE',
  145. 'font-size': 'smaller',
  146. cursor: 'pointer',
  147. });
  148. toggle.addEventListener('click', showHideConsole);
  149. function showHideConsole() {
  150. show = !show;
  151. toggle.textContent = show ? '☒' : '☐';
  152. parent.style.display = show ? '' : 'none';
  153. }
  154. showHideConsole();
  155. const maxLines = 100;
  156. const lines = [];
  157. let added = false;
  158. function addLine(type, str, prefix) {
  159. const div = document.createElement('div');
  160. div.textContent = (prefix + str) || ' ';
  161. div.className = `console-line ${type}`;
  162. parent.appendChild(div);
  163. lines.push(div);
  164. if (!added) {
  165. added = true;
  166. document.body.appendChild(style);
  167. document.body.appendChild(parent);
  168. document.body.appendChild(toggle);
  169. }
  170. // scrollIntoView only works in Chrome
  171. // In Firefox and Safari scrollIntoView inside an iframe moves
  172. // that element into the view. It should arguably only move that
  173. // element inside the iframe itself, otherwise that's giving
  174. // any random iframe control to bring itself into view against
  175. // the parent's wishes.
  176. //
  177. // note that even if we used a solution (which is to manually set
  178. // scrollTop) there's a UI issue that if the user manually scrolls
  179. // we want to stop scrolling automatically and if they move back
  180. // to the bottom we want to pick up scrolling automatically.
  181. // Kind of a PITA so TBD
  182. //
  183. // div.scrollIntoView();
  184. }
  185. function addLines(type, str, prefix) {
  186. while (lines.length > maxLines) {
  187. const div = lines.shift();
  188. div.parentNode.removeChild(div);
  189. }
  190. addLine(type, str, prefix);
  191. }
  192. function wrapFunc(obj, funcName, prefix) {
  193. const oldFn = obj[funcName];
  194. origConsole[funcName] = oldFn.bind(obj);
  195. return function(...args) {
  196. addLines(funcName, [...args].join(' '), prefix);
  197. oldFn.apply(obj, arguments);
  198. };
  199. }
  200. window.console.log = wrapFunc(window.console, 'log', '');
  201. window.console.warn = wrapFunc(window.console, 'warn', '⚠');
  202. window.console.error = wrapFunc(window.console, 'error', '❌');
  203. }
  204. function reportJSError(url, lineNo, colNo, msg) {
  205. try {
  206. const {origUrl, actualLineNo} = window.parent.getActualLineNumberAndMoveTo(url, lineNo, colNo);
  207. url = origUrl;
  208. lineNo = actualLineNo;
  209. } catch (ex) {
  210. origConsole.error(ex);
  211. }
  212. console.error(url, "line:", lineNo, ":", msg); // eslint-disable-line
  213. }
  214. /**
  215. * @typedef {Object} StackInfo
  216. * @property {string} url Url of line
  217. * @property {number} lineNo line number of error
  218. * @property {number} colNo column number of error
  219. * @property {string} [funcName] name of function
  220. */
  221. /**
  222. * @parameter {string} stack A stack string as in `(new Error()).stack`
  223. * @returns {StackInfo}
  224. */
  225. const parseStack = function() {
  226. const browser = getBrowser();
  227. let lineNdx;
  228. let matcher;
  229. if ((/chrome|opera/i).test(browser.name)) {
  230. lineNdx = 3;
  231. matcher = function(line) {
  232. const m = /at ([^(]+)*\(*(.*?):(\d+):(\d+)/.exec(line);
  233. if (m) {
  234. let userFnName = m[1];
  235. let url = m[2];
  236. const lineNo = parseInt(m[3]);
  237. const colNo = parseInt(m[4]);
  238. if (url === '') {
  239. url = userFnName;
  240. userFnName = '';
  241. }
  242. return {
  243. url: url,
  244. lineNo: lineNo,
  245. colNo: colNo,
  246. funcName: userFnName,
  247. };
  248. }
  249. return undefined;
  250. };
  251. } else if ((/firefox|safari/i).test(browser.name)) {
  252. lineNdx = 2;
  253. matcher = function(line) {
  254. const m = /@(.*?):(\d+):(\d+)/.exec(line);
  255. if (m) {
  256. const url = m[1];
  257. const lineNo = parseInt(m[2]);
  258. const colNo = parseInt(m[3]);
  259. return {
  260. url: url,
  261. lineNo: lineNo,
  262. colNo: colNo,
  263. };
  264. }
  265. return undefined;
  266. };
  267. }
  268. return function stackParser(stack) {
  269. if (matcher) {
  270. try {
  271. const lines = stack.split('\n');
  272. // window.fooLines = lines;
  273. // lines.forEach(function(line, ndx) {
  274. // origConsole.log("#", ndx, line);
  275. // });
  276. return matcher(lines[lineNdx]);
  277. } catch (e) {
  278. // do nothing
  279. }
  280. }
  281. return undefined;
  282. };
  283. }();
  284. function setupWorkerSupport() {
  285. function log(data) {
  286. const {logType, msg} = data;
  287. console[logType]('[Worker]', msg); /* eslint-disable-line no-console */
  288. }
  289. function lostContext(/* data */) {
  290. addContextLostHTML();
  291. }
  292. function jsError(data) {
  293. const {url, lineNo, colNo, msg} = data;
  294. reportJSError(url, lineNo, colNo, msg);
  295. }
  296. function jsErrorWithStack(data) {
  297. const {url, stack, msg} = data;
  298. const errorInfo = parseStack(stack);
  299. if (errorInfo) {
  300. reportJSError(errorInfo.url || url, errorInfo.lineNo, errorInfo.colNo, msg);
  301. } else {
  302. console.error(errorMsg) // eslint-disable-line
  303. }
  304. }
  305. const handlers = {
  306. log,
  307. lostContext,
  308. jsError,
  309. jsErrorWithStack,
  310. };
  311. const OrigWorker = self.Worker;
  312. class WrappedWorker extends OrigWorker {
  313. constructor(url) {
  314. super(url);
  315. let listener;
  316. this.onmessage = function(e) {
  317. if (!e || !e.data || !e.data.type === '___editor___') {
  318. if (listener) {
  319. listener(e);
  320. }
  321. return;
  322. }
  323. e.stopImmediatePropagation();
  324. const data = e.data.data;
  325. const fn = handlers[data.type];
  326. if (!fn) {
  327. origConsole.error('unknown editor msg:', data.type);
  328. } else {
  329. fn(data);
  330. }
  331. return;
  332. };
  333. Object.defineProperty(this, 'onmessage', {
  334. get() {
  335. return listener;
  336. },
  337. set(fn) {
  338. listener = fn;
  339. },
  340. });
  341. }
  342. }
  343. self.Worker = WrappedWorker;
  344. }
  345. function addContextLostHTML() {
  346. const div = document.createElement('div');
  347. div.className = 'contextlost';
  348. div.innerHTML = '<div>Context Lost: Click To Reload</div>';
  349. div.addEventListener('click', function() {
  350. window.location.reload();
  351. });
  352. document.body.appendChild(div);
  353. }
  354. /**
  355. * Gets a WebGL context.
  356. * makes its backing store the size it is displayed.
  357. * @param {HTMLCanvasElement} canvas a canvas element.
  358. * @memberOf module:webgl-utils
  359. */
  360. let setupLesson = function(canvas) {
  361. // only once
  362. setupLesson = function() {};
  363. if (canvas) {
  364. canvas.addEventListener('webglcontextlost', function(e) {
  365. // the default is to do nothing. Preventing the default
  366. // means allowing context to be restored
  367. e.preventDefault();
  368. addContextLostHTML();
  369. });
  370. canvas.addEventListener('webglcontextrestored', function() {
  371. // just reload the page. Easiest.
  372. window.location.reload();
  373. });
  374. }
  375. if (isInIFrame()) {
  376. updateCSSIfInIFrame();
  377. }
  378. };
  379. /**
  380. * Gets the iframe in the parent document
  381. * that is displaying the specified window .
  382. * @param {Window} window window to check.
  383. * @return {HTMLIFrameElement?) the iframe element if window is in an iframe
  384. */
  385. function getIFrameForWindow(window) {
  386. if (!isInIFrame(window)) {
  387. return;
  388. }
  389. const iframes = window.parent.document.getElementsByTagName('iframe');
  390. for (let ii = 0; ii < iframes.length; ++ii) {
  391. const iframe = iframes[ii];
  392. if (iframe.contentDocument === window.document) {
  393. return iframe; // eslint-disable-line
  394. }
  395. }
  396. }
  397. /**
  398. * Returns true if window is on screen. The main window is
  399. * always on screen windows in iframes might not be.
  400. * @param {Window} window the window to check.
  401. * @return {boolean} true if window is on screen.
  402. */
  403. function isFrameVisible(window) {
  404. try {
  405. const iframe = getIFrameForWindow(window);
  406. if (!iframe) {
  407. return true;
  408. }
  409. const bounds = iframe.getBoundingClientRect();
  410. const isVisible = bounds.top < window.parent.innerHeight && bounds.bottom >= 0 &&
  411. bounds.left < window.parent.innerWidth && bounds.right >= 0;
  412. return isVisible && isFrameVisible(window.parent);
  413. } catch (e) {
  414. return true; // We got a security error?
  415. }
  416. }
  417. /**
  418. * Returns true if element is on screen.
  419. * @param {HTMLElement} element the element to check.
  420. * @return {boolean} true if element is on screen.
  421. */
  422. function isOnScreen(element) {
  423. let isVisible = true;
  424. if (element) {
  425. const bounds = element.getBoundingClientRect();
  426. isVisible = bounds.top < topWindow.innerHeight && bounds.bottom >= 0;
  427. }
  428. return isVisible && isFrameVisible(topWindow);
  429. }
  430. // Replace requestAnimationFrame.
  431. if (topWindow.requestAnimationFrame) {
  432. topWindow.requestAnimationFrame = (function(oldRAF) {
  433. return function(callback, element) {
  434. const handler = function() {
  435. return oldRAF(isOnScreen(element) ? callback : handler, element);
  436. };
  437. return handler();
  438. };
  439. }(topWindow.requestAnimationFrame));
  440. }
  441. updateCSSIfInIFrame();
  442. function captureJSErrors() {
  443. // capture JavaScript Errors
  444. window.addEventListener('error', function(e) {
  445. const msg = e.message || e.error;
  446. const url = e.filename;
  447. const lineNo = e.lineno || 1;
  448. const colNo = e.colno || 1;
  449. reportJSError(url, lineNo, colNo, msg);
  450. origConsole.error(e.error);
  451. });
  452. }
  453. // adapted from http://stackoverflow.com/a/2401861/128511
  454. function getBrowser() {
  455. const userAgent = navigator.userAgent;
  456. let m = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
  457. if (/trident/i.test(m[1])) {
  458. m = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
  459. return {
  460. name: 'IE',
  461. version: m[1],
  462. };
  463. }
  464. if (m[1] === 'Chrome') {
  465. const temp = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
  466. if (temp) {
  467. return {
  468. name: temp[1].replace('OPR', 'Opera'),
  469. version: temp[2],
  470. };
  471. }
  472. }
  473. m = m[2] ? [m[1], m[2]] : [navigator.appName, navigator.appVersion, '-?'];
  474. const version = userAgent.match(/version\/(\d+)/i);
  475. if (version) {
  476. m.splice(1, 1, version[1]);
  477. }
  478. return {
  479. name: m[0],
  480. version: m[1],
  481. };
  482. }
  483. const isWebGLRE = /^(webgl|webgl2|experimental-webgl)$/i;
  484. function installWebGLLessonSetup() {
  485. HTMLCanvasElement.prototype.getContext = (function(oldFn) {
  486. return function() {
  487. const type = arguments[0];
  488. const isWebGL = isWebGLRE.test(type);
  489. if (isWebGL) {
  490. setupLesson(this);
  491. }
  492. const args = [].slice.apply(arguments);
  493. args[1] = Object.assign({
  494. powerPreference: 'low-power',
  495. }, args[1]);
  496. const ctx = oldFn.apply(this, args);
  497. if (!ctx && isWebGL) {
  498. showNeedWebGL(this);
  499. }
  500. return ctx;
  501. };
  502. }(HTMLCanvasElement.prototype.getContext));
  503. }
  504. function installWebGLDebugContextCreator() {
  505. if (!self.webglDebugHelper) {
  506. return;
  507. }
  508. const {
  509. makeDebugContext,
  510. glFunctionArgToString,
  511. glEnumToString,
  512. } = self.webglDebugHelper;
  513. // capture GL errors
  514. HTMLCanvasElement.prototype.getContext = (function(oldFn) {
  515. return function() {
  516. let ctx = oldFn.apply(this, arguments);
  517. // Using bindTexture to see if it's WebGL. Could check for instanceof WebGLRenderingContext
  518. // but that might fail if wrapped by debugging extension
  519. if (ctx && ctx.bindTexture) {
  520. ctx = makeDebugContext(ctx, {
  521. maxDrawCalls: 100,
  522. errorFunc: function(err, funcName, args) {
  523. const numArgs = args.length;
  524. const enumedArgs = [].map.call(args, function(arg, ndx) {
  525. let str = glFunctionArgToString(funcName, numArgs, ndx, arg);
  526. // shorten because of long arrays
  527. if (str.length > 200) {
  528. str = str.substring(0, 200) + '...';
  529. }
  530. return str;
  531. });
  532. const errorMsg = `WebGL error ${glEnumToString(err)} in ${funcName}(${enumedArgs.join(', ')})`;
  533. const errorInfo = parseStack((new Error()).stack);
  534. if (errorInfo) {
  535. reportJSError(errorInfo.url, errorInfo.lineNo, errorInfo.colNo, errorMsg);
  536. } else {
  537. console.error(errorMsg) // eslint-disable-line
  538. }
  539. },
  540. });
  541. }
  542. return ctx;
  543. };
  544. }(HTMLCanvasElement.prototype.getContext));
  545. }
  546. installWebGLLessonSetup();
  547. if (isInEditor()) {
  548. setupWorkerSupport();
  549. setupConsole();
  550. captureJSErrors();
  551. if (lessonSettings.glDebug !== false) {
  552. installWebGLDebugContextCreator();
  553. }
  554. }
  555. return {
  556. setupLesson: setupLesson,
  557. showNeedWebGL: showNeedWebGL,
  558. };
  559. }));