@@ -9,24 +9,27 @@ import * as C from './constants';
99
1010function environment ( p5 , fn , lifecycles ) {
1111 const standardCursors = [ C . ARROW , C . CROSS , C . HAND , C . MOVE , C . TEXT , C . WAIT ] ;
12+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' ;
1213
1314 fn . _frameRate = 0 ;
14- fn . _lastFrameTime = window . performance . now ( ) ;
15+ fn . _lastFrameTime = globalThis . performance . now ( ) ;
1516 fn . _targetFrameRate = 60 ;
1617
17- const _windowPrint = window . print ;
18+ const windowPrint = isBrowser ? window . print : null ;
1819 let windowPrintDisabled = false ;
1920
2021 lifecycles . presetup = function ( ) {
2122 const events = [
2223 'resize'
2324 ] ;
2425
25- for ( const event of events ) {
26- window . addEventListener ( event , this [ `_on${ event } ` ] . bind ( this ) , {
27- passive : false ,
28- signal : this . _removeSignal
29- } ) ;
26+ if ( isBrowser ) {
27+ for ( const event of events ) {
28+ window . addEventListener ( event , this [ `_on${ event } ` ] . bind ( this ) , {
29+ passive : false ,
30+ signal : this . _removeSignal
31+ } ) ;
32+ }
3033 }
3134 } ;
3235
@@ -57,9 +60,9 @@ function environment(p5, fn, lifecycles){
5760 * }
5861 */
5962 fn . print = function ( ...args ) {
60- if ( ! args . length ) {
63+ if ( ! args . length && windowPrint !== null ) {
6164 if ( ! windowPrintDisabled ) {
62- _windowPrint ( ) ;
65+ windowPrint ( ) ;
6366 if (
6467 window . confirm (
6568 'You just tried to print the webpage. Do you want to prevent this from running again?'
@@ -196,7 +199,7 @@ function environment(p5, fn, lifecycles){
196199 * }
197200 * }
198201 */
199- fn . focused = document . hasFocus ( ) ;
202+ fn . focused = isBrowser ? document . hasFocus ( ) : true ;
200203
201204 /**
202205 * Changes the cursor's appearance.
@@ -578,7 +581,7 @@ function environment(p5, fn, lifecycles){
578581 * @alt
579582 * This example does not render anything.
580583 */
581- fn . displayWidth = screen . width ;
584+ fn . displayWidth = isBrowser ? window . screen . width : 0 ;
582585
583586 /**
584587 * A `Number` variable that stores the height of the screen display.
@@ -606,7 +609,7 @@ function environment(p5, fn, lifecycles){
606609 * @alt
607610 * This example does not render anything.
608611 */
609- fn . displayHeight = screen . height ;
612+ fn . displayHeight = isBrowser ? window . screen . height : 0 ;
610613
611614 /**
612615 * A `Number` variable that stores the width of the browser's viewport.
@@ -732,21 +735,11 @@ function environment(p5, fn, lifecycles){
732735 } ;
733736
734737 function getWindowWidth ( ) {
735- return (
736- window . innerWidth ||
737- ( document . documentElement && document . documentElement . clientWidth ) ||
738- ( document . body && document . body . clientWidth ) ||
739- 0
740- ) ;
738+ return isBrowser ? document . documentElement . clientWidth : 0 ;
741739 }
742740
743741 function getWindowHeight ( ) {
744- return (
745- window . innerHeight ||
746- ( document . documentElement && document . documentElement . clientHeight ) ||
747- ( document . body && document . body . clientHeight ) ||
748- 0
749- ) ;
742+ return isBrowser ? document . documentElement . clientHeight : 0 ;
750743 }
751744
752745 /**
@@ -806,7 +799,6 @@ function environment(p5, fn, lifecycles){
806799 * }
807800 */
808801 fn . fullscreen = function ( val ) {
809- // p5._validateParameters('fullscreen', arguments);
810802 // no arguments, return fullscreen or not
811803 if ( typeof val === 'undefined' ) {
812804 return (
@@ -877,7 +869,6 @@ function environment(p5, fn, lifecycles){
877869 * @returns {Number } current pixel density of the sketch.
878870 */
879871 fn . pixelDensity = function ( val ) {
880- // p5._validateParameters('pixelDensity', arguments);
881872 let returnValue ;
882873 if ( typeof val === 'number' ) {
883874 if ( val !== this . _renderer . _pixelDensity ) {
0 commit comments