Skip to content

Commit 4abb613

Browse files
committed
Use this or window depending on this._isGlobal
Closes #2999
1 parent f13761e commit 4abb613

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/core/main.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var p5 = function(sketch, node, sync) {
4848

4949
/**
5050
* Called directly before <a href="#/p5/setup">setup()</a>, the <a href="#/p5/preload">preload()</a> function is used to handle
51-
* asynchronous loading of external files in a blocking way. If a preload
51+
* asynchronous loading of external files in a blocking way. If a preload
5252
* function is defined, <a href="#/p5/setup">setup()</a> will wait until any load calls within have
5353
* finished. Nothing besides load calls (<a href="#/p5/loadImage">loadImage</a>, <a href="#/p5/loadJSON">loadJSON</a>, <a href="#/p5/loadFont">loadFont</a>,
5454
* <a href="#/p5/loadStrings">loadStrings</a>, etc.) should be inside the preload function. If asynchronous
@@ -238,7 +238,8 @@ var p5 = function(sketch, node, sync) {
238238
}
239239
}
240240

241-
var userPreload = this.preload || window.preload; // look for "preload"
241+
var context = this._isGlobal ? window : this;
242+
var userPreload = context.preload;
242243
if (userPreload) {
243244
// Setup loading screen
244245
// Set loading screen into dom if not present
@@ -252,11 +253,11 @@ var p5 = function(sketch, node, sync) {
252253
var node = this._userNode || document.body;
253254
node.appendChild(loadingScreen);
254255
}
255-
// var methods = this._preloadMethods;
256-
for (var method in this._preloadMethods) {
256+
var methods = this._preloadMethods;
257+
for (var method in methods) {
257258
// default to p5 if no object defined
258-
this._preloadMethods[method] = this._preloadMethods[method] || p5;
259-
var obj = this._preloadMethods[method];
259+
methods[method] = methods[method] || p5;
260+
var obj = methods[method];
260261
//it's p5, check if it's global or instance
261262
if (obj === p5.prototype || obj === p5) {
262263
if (this._isGlobal) {

src/core/structure.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,25 @@ p5.prototype.redraw = function(n) {
313313
numberOfRedraws = 1;
314314
}
315315

316-
var userSetup = this.setup || window.setup;
317-
var userDraw = this.draw || window.draw;
316+
var context = this._isGlobal ? window : this;
317+
var userSetup = context.setup;
318+
var userDraw = context.draw;
318319
if (typeof userDraw === 'function') {
319320
if (typeof userSetup === 'undefined') {
320-
this.scale(this._pixelDensity, this._pixelDensity);
321+
context.scale(context._pixelDensity, context._pixelDensity);
321322
}
322-
var self = this;
323323
var callMethod = function(f) {
324-
f.call(self);
324+
f.call(context);
325325
};
326326
for (var idxRedraw = 0; idxRedraw < numberOfRedraws; idxRedraw++) {
327-
this.resetMatrix();
328-
if (this._renderer.isP3D) {
329-
this._renderer._update();
327+
context.resetMatrix();
328+
if (context._renderer.isP3D) {
329+
context._renderer._update();
330330
}
331-
this._setProperty('frameCount', this.frameCount + 1);
332-
this._registeredMethods.pre.forEach(callMethod);
331+
context._setProperty('frameCount', context.frameCount + 1);
332+
context._registeredMethods.pre.forEach(callMethod);
333333
userDraw();
334-
this._registeredMethods.post.forEach(callMethod);
334+
context._registeredMethods.post.forEach(callMethod);
335335
}
336336
}
337337
};

0 commit comments

Comments
 (0)