@@ -60,10 +60,6 @@ class p5 {
6060 this . _loop = true ;
6161 this . _startListener = null ;
6262 this . _initializeInstanceVariables ( ) ;
63- this . _defaultCanvasSize = {
64- width : 100 ,
65- height : 100
66- } ;
6763 this . _events = {
6864 // keep track of user-events for unregistering later
6965 mousemove : null ,
@@ -87,8 +83,8 @@ class p5 {
8783 } ;
8884 this . _millisStart = - 1 ;
8985 this . _recording = false ;
90- this . touchstart = false ;
91- this . touchend = false ;
86+ this . _touchstart = false ;
87+ this . _touchend = false ;
9288
9389 // States used in the custom random generators
9490 this . _lcg_random_state = null ; // NOTE: move to random.js
@@ -106,7 +102,30 @@ class p5 {
106102 // ensure correct reporting of window dimensions
107103 this . _updateWindowSize ( ) ;
108104
109- const friendlyBindGlobal = this . _createFriendlyGlobalFunctionBinder ( ) ;
105+ const bindGlobal = ( property ) => {
106+ Object . defineProperty ( window , property , {
107+ configurable : true ,
108+ enumerable : true ,
109+ get : ( ) => {
110+ if ( typeof this [ property ] === 'function' ) {
111+ return this [ property ] . bind ( this ) ;
112+ } else {
113+ return this [ property ] ;
114+ }
115+ } ,
116+ set : ( newValue ) => {
117+ Object . defineProperty ( window , property , {
118+ configurable : true ,
119+ enumerable : true ,
120+ value : newValue ,
121+ writable : true
122+ } ) ;
123+ if ( ! p5 . disableFriendlyErrors ) {
124+ console . log ( `You just changed the value of "${ property } ", which was a p5 global value. This could cause problems later if you're not careful.` ) ;
125+ }
126+ }
127+ } )
128+ } ;
110129 // If the user has created a global setup or draw function,
111130 // assume "global" mode and make everything global (i.e. on the window)
112131 if ( ! sketch ) {
@@ -117,28 +136,14 @@ class p5 {
117136 // All methods and properties with name starting with '_' will be skipped
118137 for ( const p of Object . getOwnPropertyNames ( p5 . prototype ) ) {
119138 if ( p [ 0 ] === '_' ) continue ;
120-
121- if ( typeof p5 . prototype [ p ] === 'function' ) {
122- const ev = p . substring ( 2 ) ;
123- if ( ! this . _events . hasOwnProperty ( ev ) ) {
124- if ( Math . hasOwnProperty ( p ) && Math [ p ] === p5 . prototype [ p ] ) {
125- // Multiple p5 methods are just native Math functions. These can be
126- // called without any binding.
127- friendlyBindGlobal ( p , p5 . prototype [ p ] ) ;
128- } else {
129- friendlyBindGlobal ( p , p5 . prototype [ p ] . bind ( this ) ) ;
130- }
131- }
132- } else {
133- friendlyBindGlobal ( p , p5 . prototype [ p ] ) ;
134- }
139+ bindGlobal ( p ) ;
135140 }
136141
137142 // Attach its properties to the window
138143 for ( const p in this ) {
139144 if ( this . hasOwnProperty ( p ) ) {
140145 if ( p [ 0 ] === '_' ) continue ;
141- friendlyBindGlobal ( p , this [ p ] ) ;
146+ bindGlobal ( p ) ;
142147 }
143148 }
144149 } else {
@@ -162,10 +167,10 @@ class p5 {
162167 }
163168
164169 const focusHandler = ( ) => {
165- this . _setProperty ( ' focused' , true ) ;
170+ this . focused = true ;
166171 } ;
167172 const blurHandler = ( ) => {
168- this . _setProperty ( ' focused' , false ) ;
173+ this . focused = false ;
169174 } ;
170175 window . addEventListener ( 'focus' , focusHandler ) ;
171176 window . addEventListener ( 'blur' , blurHandler ) ;
@@ -217,8 +222,8 @@ class p5 {
217222 // Later on if the user calls createCanvas, this default one
218223 // will be replaced
219224 this . createCanvas (
220- this . _defaultCanvasSize . width ,
221- this . _defaultCanvasSize . height ,
225+ 100 ,
226+ 100 ,
222227 constants . P2D
223228 ) ;
224229
@@ -276,7 +281,6 @@ class p5 {
276281 ) {
277282 //mandatory update values(matrixes and stack)
278283 this . deltaTime = now - this . _lastRealFrameTime ;
279- this . _setProperty ( 'deltaTime' , this . deltaTime ) ;
280284 this . _frameRate = 1000.0 / this . deltaTime ;
281285 await this . redraw ( ) ;
282286 this . _lastTargetFrameTime = Math . max ( this . _lastTargetFrameTime
@@ -292,8 +296,8 @@ class p5 {
292296
293297 //reset delta values so they reset even if there is no mouse event to set them
294298 // for example if the mouse is outside the screen
295- this . _setProperty ( ' movedX' , 0 ) ;
296- this . _setProperty ( ' movedY' , 0 ) ;
299+ this . movedX = 0 ;
300+ this . movedY = 0 ;
297301 }
298302 }
299303
@@ -427,83 +431,6 @@ class p5 {
427431
428432 this . _downKeys = { } ; //Holds the key codes of currently pressed keys
429433 }
430-
431- _setProperty ( prop , value ) {
432- this [ prop ] = value ;
433- if ( this . _isGlobal ) {
434- window [ prop ] = value ;
435- }
436- }
437-
438- // create a function which provides a standardized process for binding
439- // globals; this is implemented as a factory primarily so that there's a
440- // way to redefine what "global" means for the binding function so it
441- // can be used in scenarios like unit testing where the window object
442- // might not exist
443- _createFriendlyGlobalFunctionBinder ( options = { } ) {
444- const globalObject = options . globalObject || window ;
445- const log = options . log || console . log . bind ( console ) ;
446- const propsToForciblyOverwrite = {
447- // p5.print actually always overwrites an existing global function,
448- // albeit one that is very unlikely to be used:
449- //
450- // https://developer.mozilla.org/en-US/docs/Web/API/Window/print
451- print : true
452- } ;
453-
454- return ( prop , value ) => {
455- if (
456- ! p5 . disableFriendlyErrors &&
457- typeof IS_MINIFIED === 'undefined' &&
458- typeof value === 'function'
459- ) {
460- try {
461- // Because p5 has so many common function names, it's likely
462- // that users may accidentally overwrite global p5 functions with
463- // their own variables. Let's allow this but log a warning to
464- // help users who may be doing this unintentionally.
465- //
466- // For more information, see:
467- //
468- // https://github.com/processing/p5.js/issues/1317
469-
470- if ( prop in globalObject && ! ( prop in propsToForciblyOverwrite ) ) {
471- throw new Error ( `global "${ prop } " already exists` ) ;
472- }
473-
474- // It's possible that this might throw an error because there
475- // are a lot of edge-cases in which `Object.defineProperty` might
476- // not succeed; since this functionality is only intended to
477- // help beginners anyways, we'll just catch such an exception
478- // if it occurs, and fall back to legacy behavior.
479- Object . defineProperty ( globalObject , prop , {
480- configurable : true ,
481- enumerable : true ,
482- get ( ) {
483- return value ;
484- } ,
485- set ( newValue ) {
486- Object . defineProperty ( globalObject , prop , {
487- configurable : true ,
488- enumerable : true ,
489- value : newValue ,
490- writable : true
491- } ) ;
492- log (
493- `You just changed the value of "${ prop } ", which was a p5 function. This could cause problems later if you're not careful.`
494- ) ;
495- }
496- } ) ;
497- } catch ( e ) {
498- let message = `p5 had problems creating the global function "${ prop } ", possibly because your code is already using that name as a variable. You may want to rename your variable to something else.` ;
499- p5 . _friendlyError ( message , prop ) ;
500- globalObject [ prop ] = value ;
501- }
502- } else {
503- globalObject [ prop ] = value ;
504- }
505- } ;
506- }
507434}
508435
509436// attach constants to p5 prototype
0 commit comments