Skip to content

Commit 61bbdf9

Browse files
authored
Merge pull request #7295 from processing/initial-modules-refactor
Initial modules refactor
2 parents 3131111 + 1b03c6b commit 61bbdf9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1147
-1292
lines changed

src/accessibility/outputs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ function outputs(p5, fn){
543543
fn._getPos = function (x, y) {
544544
const untransformedPosition = new DOMPointReadOnly(x, y);
545545
const currentTransform = this._renderer.isP3D ?
546-
new DOMMatrix(this._renderer.uMVMatrix.mat4) :
546+
new DOMMatrix(this._renderer.states.uMVMatrix.mat4) :
547547
this.drawingContext.getTransform();
548548
const { x: transformedX, y: transformedY } = untransformedPosition
549549
.matrixTransform(currentTransform);
@@ -663,7 +663,7 @@ function outputs(p5, fn){
663663
];
664664
// Apply the inverse of the current transformations to the canvas corners
665665
const currentTransform = this._renderer.isP3D ?
666-
new DOMMatrix(this._renderer.uMVMatrix.mat4) :
666+
new DOMMatrix(this._renderer.states.uMVMatrix.mat4) :
667667
this.drawingContext.getTransform();
668668
const invertedTransform = currentTransform.inverse();
669669
const tc = canvasCorners.map(

src/color/setting.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,8 @@ function setting(p5, fn){
12091209
* @chainable
12101210
*/
12111211
fn.fill = function(...args) {
1212-
this._renderer._setProperty('_fillSet', true);
1213-
this._renderer._setProperty('_doFill', true);
1212+
this._renderer.states.fillSet = true;
1213+
this._renderer.states.doFill = true;
12141214
this._renderer.fill(...args);
12151215
return this;
12161216
};
@@ -1271,7 +1271,7 @@ function setting(p5, fn){
12711271
* </div>
12721272
*/
12731273
fn.noFill = function() {
1274-
this._renderer._setProperty('_doFill', false);
1274+
this._renderer.states.doFill = false;
12751275
return this;
12761276
};
12771277

@@ -1327,7 +1327,7 @@ function setting(p5, fn){
13271327
* </div>
13281328
*/
13291329
fn.noStroke = function() {
1330-
this._renderer._setProperty('_doStroke', false);
1330+
this._renderer.states.doStroke = false;
13311331
return this;
13321332
};
13331333

@@ -1581,8 +1581,8 @@ function setting(p5, fn){
15811581
*/
15821582

15831583
fn.stroke = function(...args) {
1584-
this._renderer._setProperty('_strokeSet', true);
1585-
this._renderer._setProperty('_doStroke', true);
1584+
this._renderer.states.strokeSet = true;
1585+
this._renderer.states.doStroke = true;
15861586
this._renderer.stroke(...args);
15871587
return this;
15881588
};

src/core/environment.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ p5.prototype.frameRate = function(fps) {
410410
if (typeof fps !== 'number' || fps < 0) {
411411
return this._frameRate;
412412
} else {
413-
this._setProperty('_targetFrameRate', fps);
413+
this._targetFrameRate = fps;
414414
if (fps === 0) {
415-
this._setProperty('_frameRate', fps);
415+
this._frameRate = fps;
416416
}
417417
return this;
418418
}
@@ -770,8 +770,8 @@ p5.prototype.windowHeight = 0;
770770
* This example does not render anything.
771771
*/
772772
p5.prototype._onresize = function(e) {
773-
this._setProperty('windowWidth', getWindowWidth());
774-
this._setProperty('windowHeight', getWindowHeight());
773+
this.windowWidth = getWindowWidth();
774+
this.windowHeight = getWindowHeight();
775775
const context = this._isGlobal ? window : this;
776776
let executeDefault;
777777
if (typeof context.windowResized === 'function') {
@@ -805,8 +805,8 @@ function getWindowHeight() {
805805
* possibility of the window being resized when no sketch is active.
806806
*/
807807
p5.prototype._updateWindowSize = function() {
808-
this._setProperty('windowWidth', getWindowWidth());
809-
this._setProperty('windowHeight', getWindowHeight());
808+
this.windowWidth = getWindowWidth();
809+
this.windowHeight = getWindowHeight();
810810
};
811811

812812
/**

src/core/main.js

Lines changed: 34 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/core/p5.Element.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ p5.Element = class {
335335
// This is required so that mouseButton is set correctly prior to calling the callback (fxn).
336336
// For details, see https://github.com/processing/p5.js/issues/3087.
337337
const eventPrependedFxn = function (event) {
338-
this._pInst._setProperty('mouseIsPressed', true);
338+
this._pInst.mouseIsPressed = true;
339339
this._pInst._setMouseButton(event);
340340
// Pass along the return-value of the callback:
341341
return fxn.call(this, event);
@@ -942,13 +942,6 @@ p5.Element = class {
942942
ctx.elt.removeEventListener(ev, f, false);
943943
ctx._events[ev] = null;
944944
}
945-
946-
/**
947-
* Helper fxn for sharing pixel methods
948-
*/
949-
_setProperty(prop, value) {
950-
this[prop] = value;
951-
}
952945
};
953946

954947
/**

0 commit comments

Comments
 (0)