@@ -60,10 +60,6 @@ class p5 {
60
60
this . _loop = true ;
61
61
this . _startListener = null ;
62
62
this . _initializeInstanceVariables ( ) ;
63
- this . _defaultCanvasSize = {
64
- width : 100 ,
65
- height : 100
66
- } ;
67
63
this . _events = {
68
64
// keep track of user-events for unregistering later
69
65
mousemove : null ,
@@ -87,8 +83,8 @@ class p5 {
87
83
} ;
88
84
this . _millisStart = - 1 ;
89
85
this . _recording = false ;
90
- this . touchstart = false ;
91
- this . touchend = false ;
86
+ this . _touchstart = false ;
87
+ this . _touchend = false ;
92
88
93
89
// States used in the custom random generators
94
90
this . _lcg_random_state = null ; // NOTE: move to random.js
@@ -106,7 +102,30 @@ class p5 {
106
102
// ensure correct reporting of window dimensions
107
103
this . _updateWindowSize ( ) ;
108
104
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
+ } ;
110
129
// If the user has created a global setup or draw function,
111
130
// assume "global" mode and make everything global (i.e. on the window)
112
131
if ( ! sketch ) {
@@ -117,28 +136,14 @@ class p5 {
117
136
// All methods and properties with name starting with '_' will be skipped
118
137
for ( const p of Object . getOwnPropertyNames ( p5 . prototype ) ) {
119
138
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 ) ;
135
140
}
136
141
137
142
// Attach its properties to the window
138
143
for ( const p in this ) {
139
144
if ( this . hasOwnProperty ( p ) ) {
140
145
if ( p [ 0 ] === '_' ) continue ;
141
- friendlyBindGlobal ( p , this [ p ] ) ;
146
+ bindGlobal ( p ) ;
142
147
}
143
148
}
144
149
} else {
@@ -162,10 +167,10 @@ class p5 {
162
167
}
163
168
164
169
const focusHandler = ( ) => {
165
- this . _setProperty ( ' focused' , true ) ;
170
+ this . focused = true ;
166
171
} ;
167
172
const blurHandler = ( ) => {
168
- this . _setProperty ( ' focused' , false ) ;
173
+ this . focused = false ;
169
174
} ;
170
175
window . addEventListener ( 'focus' , focusHandler ) ;
171
176
window . addEventListener ( 'blur' , blurHandler ) ;
@@ -217,8 +222,8 @@ class p5 {
217
222
// Later on if the user calls createCanvas, this default one
218
223
// will be replaced
219
224
this . createCanvas (
220
- this . _defaultCanvasSize . width ,
221
- this . _defaultCanvasSize . height ,
225
+ 100 ,
226
+ 100 ,
222
227
constants . P2D
223
228
) ;
224
229
@@ -276,7 +281,6 @@ class p5 {
276
281
) {
277
282
//mandatory update values(matrixes and stack)
278
283
this . deltaTime = now - this . _lastRealFrameTime ;
279
- this . _setProperty ( 'deltaTime' , this . deltaTime ) ;
280
284
this . _frameRate = 1000.0 / this . deltaTime ;
281
285
await this . redraw ( ) ;
282
286
this . _lastTargetFrameTime = Math . max ( this . _lastTargetFrameTime
@@ -292,8 +296,8 @@ class p5 {
292
296
293
297
//reset delta values so they reset even if there is no mouse event to set them
294
298
// 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 ;
297
301
}
298
302
}
299
303
@@ -427,83 +431,6 @@ class p5 {
427
431
428
432
this . _downKeys = { } ; //Holds the key codes of currently pressed keys
429
433
}
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
- }
507
434
}
508
435
509
436
// attach constants to p5 prototype
0 commit comments