-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a delayed sim startup to wait for suitable conditions, see #764 …
…and phetsims/tasks#1002
- Loading branch information
1 parent
b38f400
commit e40da9a
Showing
4 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2019, University of Colorado Boulder | ||
|
||
/* | ||
* Delayed simulation/runnable startup so that we can ensure we are in an environment suitable for the sim to start up. | ||
* See https://github.com/phetsims/chipper/issues/764 for more information. | ||
* | ||
* The require.js part is wrapped in a phet.chipper.runRequireJS() method. | ||
*/ | ||
|
||
(function() { | ||
'use strict'; | ||
|
||
// constants | ||
const svgns = 'http://www.w3.org/2000/svg'; | ||
|
||
// Whether phet.chipper.runRequireJS has been called yet. | ||
let started = false; | ||
|
||
function isReady() { | ||
// NOTE: We do NOT care about window.innerWidth/innerHeight. We can start up with those equal to 0, as long as our | ||
// SVG bounds detection works. See https://github.com/phetsims/tasks/issues/1002. | ||
if ( !document.body ) { | ||
return false; | ||
} | ||
|
||
// Initialize the container and element, very similar in form to TextBounds.initializeTextBounds() | ||
|
||
const container = document.createElementNS( svgns, 'svg' ); | ||
container.setAttribute( 'width', '2' ); | ||
container.setAttribute( 'height', '2' ); | ||
container.setAttribute( 'style', 'visibility: hidden; pointer-events: none; position: absolute; left: -65535px; right: -65535px;' ); | ||
|
||
const element = document.createElementNS( svgns, 'text' ); | ||
element.appendChild( document.createTextNode( '' ) ); | ||
element.setAttribute( 'dominant-baseline', 'alphabetic' ); // to match Canvas right now | ||
element.setAttribute( 'text-rendering', 'geometricPrecision' ); | ||
element.setAttributeNS( 'http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve' ); | ||
container.appendChild( element ); | ||
|
||
document.body.appendChild( container ); | ||
|
||
// Equivalent setup to TextBounds.approximateSVGBounds | ||
element.setAttribute( 'direction', 'ltr' ); | ||
element.setAttribute( 'font-family', 'Arial' ); | ||
element.setAttribute( 'font-size', 16 ); | ||
element.setAttribute( 'font-style', 'normal' ); | ||
element.setAttribute( 'font-weight', 'normal' ); | ||
element.setAttribute( 'font-stretch', 'normal' ); | ||
element.lastChild.nodeValue = 'm'; | ||
|
||
// Extract the measurement, and see if we have zero bounds. | ||
const rect = element.getBBox(); | ||
const result = rect.width !== 0 || rect.height !== 0; | ||
|
||
// Wait to remove the element until we've measured everything. | ||
document.body.removeChild( container ); | ||
|
||
return result; | ||
} | ||
|
||
// We can call this anytime to attempt to start things (if they haven't been started already). | ||
function attemptStart() { | ||
if ( !started && isReady() ) { | ||
started = true; | ||
|
||
phet.chipper.runRequireJS(); | ||
} | ||
} | ||
|
||
// Attempt to start based on timeouts (in case other methods don't work). This will call attemptStart() at least once. | ||
(function timeoutListener() { | ||
attemptStart(); | ||
|
||
if ( !started ) { | ||
setTimeout( timeoutListener, 1000 ); | ||
} | ||
})(); | ||
|
||
if ( !started ) { | ||
// Attempt to start on window resizes | ||
window.addEventListener( 'resize', function resizeListener() { | ||
attemptStart(); | ||
|
||
if ( started ) { | ||
window.removeEventListener( 'resize', resizeListener ); | ||
} | ||
} ); | ||
} | ||
})(); |