Skip to content

Commit 4b39f1d

Browse files
author
Lauren McCarthy
committed
Merge branch 'master' of github.com:processing/p5.js
2 parents 23283f4 + 8f0317e commit 4b39f1d

11 files changed

+96
-136
lines changed

src/color/creating_reading.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,10 @@ p5.prototype.color = function() {
311311
p5._validateParameters('color', arguments);
312312
if (arguments[0] instanceof p5.Color) {
313313
return arguments[0]; // Do nothing if argument is already a color object.
314-
} else if (arguments[0] instanceof Array) {
315-
if (this instanceof p5.Renderer) {
316-
return new p5.Color(this, arguments[0]);
317-
} else {
318-
return new p5.Color(this._renderer, arguments[0]);
319-
}
320-
} else {
321-
if (this instanceof p5.Renderer) {
322-
return new p5.Color(this, arguments);
323-
} else {
324-
return new p5.Color(this._renderer, arguments);
325-
}
326314
}
315+
316+
var args = arguments[0] instanceof Array ? arguments[0] : arguments;
317+
return new p5.Color(this, args);
327318
};
328319

329320
/**
@@ -439,8 +430,8 @@ p5.prototype.hue = function(c) {
439430

440431
p5.prototype.lerpColor = function(c1, c2, amt) {
441432
p5._validateParameters('lerpColor', arguments);
442-
var mode = this._renderer._colorMode;
443-
var maxes = this._renderer._colorMaxes;
433+
var mode = this._colorMode;
434+
var maxes = this._colorMaxes;
444435
var l0, l1, l2, l3;
445436
var fromArray, toArray;
446437

src/color/p5.Color.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ var color_conversion = require('./color_conversion');
3030
* @class p5.Color
3131
* @constructor
3232
*/
33-
p5.Color = function(renderer, vals) {
33+
p5.Color = function(pInst, vals) {
3434
// Record color mode and maxes at time of construction.
35-
this._storeModeAndMaxes(renderer._colorMode, renderer._colorMaxes);
35+
this._storeModeAndMaxes(pInst._colorMode, pInst._colorMaxes);
3636

3737
// Calculate normalized RGBA values.
3838
if (
@@ -659,7 +659,7 @@ p5.Color._parseInputs = function(r, g, b, a) {
659659

660660
// Return if string is a named colour.
661661
if (namedColors[str]) {
662-
return p5.Color._parseInputs.apply(this, [namedColors[str]]);
662+
return p5.Color._parseInputs.call(this, namedColors[str]);
663663
}
664664

665665
// Try RGBA pattern matching.

src/color/setting.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ p5.prototype.colorMode = function(mode, max1, max2, max3, maxA) {
317317
mode === constants.HSL
318318
) {
319319
// Set color mode.
320-
this._renderer._colorMode = mode;
320+
this._colorMode = mode;
321321

322322
// Set color maxes.
323-
var maxes = this._renderer._colorMaxes[mode];
323+
var maxes = this._colorMaxes[mode];
324324
if (arguments.length === 2) {
325325
maxes[0] = max1; // Red
326326
maxes[1] = max1; // Green

src/core/2d_primitives.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var canvas = require('./canvas');
1414
require('./error_helpers');
1515

1616
/**
17-
* Draw an arc to the screen. If called with only a, b, c, d, start, and
17+
* Draw an arc to the screen. If called with only x, y, w, h, start, and
1818
* stop, the arc will be drawn and filled as an open pie segment. If a mode parameter is provided, the arc
1919
* will be filled like an open semi-circle (OPEN) , a closed semi-circle (CHORD), or as a closed pie segment (PIE). The
2020
* origin may be changed with the ellipseMode() function.<br><br>
@@ -25,10 +25,10 @@ require('./error_helpers');
2525
* only to draw parts of an ellipse.
2626
*
2727
* @method arc
28-
* @param {Number} a x-coordinate of the arc's ellipse
29-
* @param {Number} b y-coordinate of the arc's ellipse
30-
* @param {Number} c width of the arc's ellipse by default
31-
* @param {Number} d height of the arc's ellipse by default
28+
* @param {Number} x x-coordinate of the arc's ellipse
29+
* @param {Number} y y-coordinate of the arc's ellipse
30+
* @param {Number} w width of the arc's ellipse by default
31+
* @param {Number} h height of the arc's ellipse by default
3232
* @param {Number} start angle to start the arc, specified in radians
3333
* @param {Number} stop angle to stop the arc, specified in radians
3434
* @param {Constant} [mode] optional parameter to determine the way of drawing

src/core/core.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ var p5 = function(sketch, node, sync) {
180180
this._preloadCount = 0;
181181
this._isGlobal = false;
182182
this._loop = true;
183-
this._bezierDetail = 20;
184-
this._curveDetail = 20;
185-
this._styles = [];
183+
this._initializeInstanceVariables();
186184
this._defaultCanvasSize = {
187185
width: 100,
188186
height: 100
@@ -558,6 +556,20 @@ var p5 = function(sketch, node, sync) {
558556
}
559557
};
560558

559+
p5.prototype._initializeInstanceVariables = function() {
560+
this._styles = [];
561+
562+
this._bezierDetail = 20;
563+
this._curveDetail = 20;
564+
565+
this._colorMode = constants.RGB;
566+
this._colorMaxes = {
567+
rgb: [255, 255, 255, 255],
568+
hsb: [360, 100, 100, 1],
569+
hsl: [360, 100, 100, 1]
570+
};
571+
};
572+
561573
// This is a pointer to our global mode p5 instance, if we're in
562574
// global mode.
563575
p5.instance = null;

src/core/p5.Graphics.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@ p5.Graphics = function(w, h, renderer, pInst) {
2828
var r = renderer || constants.P2D;
2929

3030
this.canvas = document.createElement('canvas');
31-
var node = this._userNode || document.body;
31+
var node = pInst._userNode || document.body;
3232
node.appendChild(this.canvas);
3333

3434
p5.Element.call(this, this.canvas, pInst, false);
35-
this._styles = [];
35+
36+
// bind methods and props of p5 to the new object
37+
for (var p in p5.prototype) {
38+
if (!this[p]) {
39+
if (typeof p5.prototype[p] === 'function') {
40+
this[p] = p5.prototype[p].bind(this);
41+
} else {
42+
this[p] = p5.prototype[p];
43+
}
44+
}
45+
}
46+
47+
p5.prototype._initializeInstanceVariables.apply(this);
3648
this.width = w;
3749
this.height = h;
3850
this._pixelDensity = pInst._pixelDensity;
@@ -42,22 +54,11 @@ p5.Graphics = function(w, h, renderer, pInst) {
4254
} else {
4355
this._renderer = new p5.Renderer2D(this.canvas, this, false);
4456
}
57+
pInst._elements.push(this);
4558

4659
this._renderer.resize(w, h);
4760
this._renderer._applyDefaults();
4861

49-
pInst._elements.push(this);
50-
51-
// bind methods and props of p5 to the new object
52-
for (var p in p5.prototype) {
53-
if (!this[p]) {
54-
if (typeof p5.prototype[p] === 'function') {
55-
this[p] = p5.prototype[p].bind(this);
56-
} else {
57-
this[p] = p5.prototype[p];
58-
}
59-
}
60-
}
6162
this.name = 'p5.Graphics'; // for friendly debugger system
6263
return this;
6364
};

src/core/p5.Renderer.js

-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ p5.Renderer = function(elt, pInst, isMainCanvas) {
5656
this._doFill = true;
5757
this._strokeSet = false;
5858
this._fillSet = false;
59-
this._colorMode = constants.RGB;
60-
this._colorMaxes = {
61-
rgb: [255, 255, 255, 255],
62-
hsb: [360, 100, 100, 1],
63-
hsl: [360, 100, 100, 1]
64-
};
6559
};
6660

6761
p5.Renderer.prototype = Object.create(p5.Element.prototype);

src/core/p5.Renderer2D.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ p5.Renderer2D.prototype.background = function() {
5858
} else {
5959
var curFill = this._getFill();
6060
// create background rect
61-
var color = this._pInst.color.apply(this, arguments);
61+
var color = this._pInst.color.apply(this._pInst, arguments);
6262
var newFill = color.toString();
6363
this._setFill(newFill);
6464
this.drawingContext.fillRect(0, 0, this.width, this.height);
@@ -73,12 +73,12 @@ p5.Renderer2D.prototype.clear = function() {
7373
};
7474

7575
p5.Renderer2D.prototype.fill = function() {
76-
var color = this._pInst.color.apply(this, arguments);
76+
var color = this._pInst.color.apply(this._pInst, arguments);
7777
this._setFill(color.toString());
7878
};
7979

8080
p5.Renderer2D.prototype.stroke = function() {
81-
var color = this._pInst.color.apply(this, arguments);
81+
var color = this._pInst.color.apply(this._pInst, arguments);
8282
this._setStroke(color.toString());
8383
};
8484

src/core/structure.js

+32-16
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ p5.prototype.loop = function() {
116116
this._draw();
117117
};
118118

119+
function assign(dest, varArgs) {
120+
for (var index = 1; index < arguments.length; index++) {
121+
var src = arguments[index];
122+
if (src != null) {
123+
for (var key in src) {
124+
if (src.hasOwnProperty(key)) {
125+
dest[key] = src[key];
126+
}
127+
}
128+
}
129+
}
130+
}
131+
119132
/**
120133
* The push() function saves the current drawing style settings and
121134
* transformations, while pop() restores these settings. Note that these
@@ -175,19 +188,23 @@ p5.prototype.loop = function() {
175188
p5.prototype.push = function() {
176189
this._renderer.push();
177190
this._styles.push({
178-
_doStroke: this._renderer._doStroke,
179-
_strokeSet: this._renderer._strokeSet,
180-
_doFill: this._renderer._doFill,
181-
_fillSet: this._renderer._fillSet,
182-
_tint: this._renderer._tint,
183-
_imageMode: this._renderer._imageMode,
184-
_rectMode: this._renderer._rectMode,
185-
_ellipseMode: this._renderer._ellipseMode,
186-
_colorMode: this._renderer._colorMode,
187-
_textFont: this._renderer._textFont,
188-
_textLeading: this._renderer._textLeading,
189-
_textSize: this._renderer._textSize,
190-
_textStyle: this._renderer._textStyle
191+
props: {
192+
_colorMode: this._colorMode
193+
},
194+
renderer: {
195+
_doStroke: this._renderer._doStroke,
196+
_strokeSet: this._renderer._strokeSet,
197+
_doFill: this._renderer._doFill,
198+
_fillSet: this._renderer._fillSet,
199+
_tint: this._renderer._tint,
200+
_imageMode: this._renderer._imageMode,
201+
_rectMode: this._renderer._rectMode,
202+
_ellipseMode: this._renderer._ellipseMode,
203+
_textFont: this._renderer._textFont,
204+
_textLeading: this._renderer._textLeading,
205+
_textSize: this._renderer._textSize,
206+
_textStyle: this._renderer._textStyle
207+
}
191208
});
192209
};
193210

@@ -250,9 +267,8 @@ p5.prototype.push = function() {
250267
p5.prototype.pop = function() {
251268
this._renderer.pop();
252269
var lastS = this._styles.pop();
253-
for (var prop in lastS) {
254-
this._renderer[prop] = lastS[prop];
255-
}
270+
assign(this._renderer, lastS.renderer);
271+
assign(this, lastS.props);
256272
};
257273

258274
p5.prototype.pushStyle = function() {

src/webgl/light.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,7 @@ p5.prototype.directionalLight = function(v1, v2, v3, x, y, z) {
263263
*/
264264
p5.prototype.pointLight = function(v1, v2, v3, x, y, z) {
265265
//@TODO: check parameters number
266-
var color = this._renderer._pInst.color.apply(this._renderer._pInst, [
267-
v1,
268-
v2,
269-
v3
270-
]);
266+
var color = this._renderer._pInst.color.apply(this, [v1, v2, v3]);
271267

272268
var _x, _y, _z;
273269
var v = arguments[arguments.length - 1];

0 commit comments

Comments
 (0)