Skip to content

Commit 21a7e99

Browse files
committed
Merge branch 'master' of https://github.com/processing/p5.js
* 'master' of https://github.com/processing/p5.js: Add unit test for blendMode() v0.4.9 Add the final two parameters of the second curve example fix for https://github.com/processing/p5.js-sound/issues/72 migrating travis closes #784, adding this._requestAnimId and canvas to this._elements Address PR comments Code review of the Perlin noise generator. Extract numbers into points for curve example. Change bezierPoint example to extract numbers into variables. Line wrap everything to 75 columns. Improve {curve, bezier}{Tangent, Point} docs. Rewrite the docs for bezier. Rewrite the docs for curve. Improve noise docs to reference noiseSeed(). Indicate point is colored by stroke. fix to preload to default to p5 fixes preload bug and adds outside library compatability
2 parents df58577 + 4e12090 commit 21a7e99

File tree

10 files changed

+170
-129
lines changed

10 files changed

+170
-129
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_js:
44
- "0.12"
55
install: npm install
66
script: npm run grunt
7+
sudo: false

lib/addons/p5.sound.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ soundfile = function () {
681681
}
682682
};
683683
// register preload handling of loadSound
684-
p5.prototype.registerPreloadMethod('loadSound');
684+
p5.prototype.registerPreloadMethod('loadSound', p5);
685685
/**
686686
* loadSound() returns a new p5.SoundFile from a specified
687687
* path. If called during preload(), the p5.SoundFile will be ready

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"docs": "grunt yui",
1111
"test": "grunt"
1212
},
13-
"version": "0.4.8",
13+
"version": "0.4.9",
1414
"devDependencies": {
1515
"amdclean": "~0.3.3",
1616
"browserify": "^11.0.1",

src/core/2d_primitives.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ p5.prototype.line = function() {
234234
/**
235235
* Draws a point, a coordinate in space at the dimension of one pixel.
236236
* The first parameter is the horizontal value for the point, the second
237-
* value is the vertical value for the point.
237+
* value is the vertical value for the point. The color of the point is
238+
* determined by the current stroke.
238239
*
239240
* @method point
240241
* @param {Number} x the x-coordinate

src/core/core.js

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ var p5 = function(sketch, node, sync) {
148148
this._userNode = node;
149149
this._curElement = null;
150150
this._elements = [];
151+
this._requestAnimId = 0;
151152
this._preloadCount = 0;
152-
this._updateInterval = 0;
153153
this._isGlobal = false;
154154
this._loop = true;
155155
this._styles = [];
@@ -211,7 +211,6 @@ var p5 = function(sketch, node, sync) {
211211
);
212212

213213
var userPreload = this.preload || window.preload; // look for "preload"
214-
var context = this._isGlobal ? window : this;
215214
if (userPreload) {
216215

217216
// Setup loading screen
@@ -226,16 +225,18 @@ var p5 = function(sketch, node, sync) {
226225
var node = this._userNode || document.body;
227226
node.appendChild(loadingScreen);
228227
}
229-
230-
var methods = this._preloadMethods;
231-
Object.keys(methods).forEach(function(f) {
232-
var methodContext = methods[f];
233-
var originalFunc = methodContext[f];
234-
methodContext[f] = function() {
235-
var argsArray = Array.prototype.slice.call(arguments);
236-
return context._preload(originalFunc, methodContext, argsArray);
237-
};
238-
});
228+
// var methods = this._preloadMethods;
229+
for (var method in this._preloadMethods){
230+
// default to p5 if no object defined
231+
this._preloadMethods[method] = this._preloadMethods[method] || p5;
232+
var obj = this._preloadMethods[method];
233+
//it's p5, check if it's global or instance
234+
if (obj === p5.prototype || obj === p5){
235+
obj = this._isGlobal ? window : this;
236+
}
237+
this._registeredPreloadMethods[method] = obj[method];
238+
obj[method] = this._wrapPreload(obj, method);
239+
}
239240

240241
userPreload();
241242
} else {
@@ -245,32 +246,42 @@ var p5 = function(sketch, node, sync) {
245246
}
246247
}.bind(this);
247248

248-
this._preload = function (func, obj, args) {
249+
this._decrementPreload = function(){
249250
var context = this._isGlobal ? window : this;
250-
context._setProperty('_preloadCount', context._preloadCount + 1);
251-
var preloadCallback = function (resp) {
252-
context._setProperty('_preloadCount', context._preloadCount - 1);
253-
if (context._preloadCount === 0) {
254-
var loadingScreen = document.getElementById(context._loadingScreenId);
255-
if (loadingScreen) {
256-
loadingScreen.parentNode.removeChild(loadingScreen);
257-
}
258-
context._setup();
259-
context._runFrames();
260-
context._draw();
251+
context._setProperty('_preloadCount', context._preloadCount - 1);
252+
if (context._preloadCount === 0) {
253+
var loadingScreen = document.getElementById(context._loadingScreenId);
254+
if (loadingScreen) {
255+
loadingScreen.parentNode.removeChild(loadingScreen);
261256
}
262-
};
263-
args.push(preloadCallback);
264-
return func.apply(obj, args);
265-
}.bind(this);
257+
context._setup();
258+
context._runFrames();
259+
context._draw();
260+
}
261+
};
262+
263+
this._wrapPreload = function(obj, fnName){
264+
return function(){
265+
//increment counter
266+
this._incrementPreload();
267+
//call original function
268+
var args = Array.prototype.slice.call(arguments);
269+
args.push(this._decrementPreload.bind(this));
270+
return this._registeredPreloadMethods[fnName].apply(obj, args);
271+
}.bind(this);
272+
};
273+
274+
this._incrementPreload = function(){
275+
var context = this._isGlobal ? window : this;
276+
context._setProperty('_preloadCount', context._preloadCount + 1);
277+
};
266278

267279
this._setup = function() {
268280

269281
// return preload functions to their normal vals if switched by preload
270282
var context = this._isGlobal ? window : this;
271283
if (typeof context.preload === 'function') {
272284
for (var f in this._preloadMethods) {
273-
//var o = this._preloadMethods[f];
274285
context[f] = this._preloadMethods[f][f];
275286
}
276287
}
@@ -330,7 +341,7 @@ var p5 = function(sketch, node, sync) {
330341
// get notified the next time the browser gives us
331342
// an opportunity to draw.
332343
if (this._loop) {
333-
window.requestAnimationFrame(this._draw);
344+
this._requestAnimId = window.requestAnimationFrame(this._draw);
334345
}
335346
}.bind(this);
336347

@@ -370,8 +381,8 @@ var p5 = function(sketch, node, sync) {
370381

371382
// stop draw
372383
this._loop = false;
373-
if (this._updateInterval) {
374-
clearTimeout(this._updateInterval);
384+
if (this._requestAnimId) {
385+
window.cancelAnimationFrame(this._requestAnimId);
375386
}
376387

377388
// unregister events sketch-wide
@@ -503,10 +514,12 @@ p5.prototype._preloadMethods = {
503514

504515
p5.prototype._registeredMethods = { pre: [], post: [], remove: [] };
505516

506-
p5.prototype.registerPreloadMethod = function(f, o) {
507-
o = o || p5;
508-
if (!p5.prototype._preloadMethods.hasOwnProperty(f)) {
509-
p5.prototype._preloadMethods[f] = o;
517+
p5.prototype._registeredPreloadMethods = {};
518+
519+
p5.prototype.registerPreloadMethod = function(fnString, obj) {
520+
// obj = obj || p5.prototype;
521+
if (!p5.prototype._preloadMethods.hasOwnProperty(fnString)) {
522+
p5.prototype._preloadMethods[fnString] = obj;
510523
}
511524
};
512525

src/core/curves.js

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ var curveDetail = 20;
1616
p5.prototype._curveTightness = 0;
1717

1818
/**
19-
* Draws a Bezier curve on the screen. These curves are defined by a series
20-
* of anchor and control points. The first two parameters specify the first
21-
* anchor point and the last two parameters specify the other anchor point.
22-
* The middle parameters specify the control points which define the shape
23-
* of the curve. Bezier curves were developed by French engineer Pierre
24-
* Bezier.
19+
* Draws a cubic Bezier curve on the screen. These curves are defined by a
20+
* series of anchor and control points. The first two parameters specify
21+
* the first anchor point and the last two parameters specify the other
22+
* anchor point, which become the first and last points on the curve. The
23+
* middle parameters specify the two control points which define the shape
24+
* of the curve. Approximately speaking, control points "pull" the curve
25+
* towards them.<br /><br />Bezier curves were developed by French
26+
* automotive engineer Pierre Bezier, and are commonly used in computer
27+
* graphics to define gently sloping curves. See also curve().
2528
*
2629
* @method bezier
2730
* @param {Number} x1 x-coordinate for the first anchor point
@@ -82,11 +85,10 @@ p5.prototype.bezierDetail = function(d) {
8285
};
8386

8487
/**
85-
* Calculate a point on the Bezier Curve
86-
*
87-
* Evaluates the Bezier at point t for points a, b, c, d.
88-
* The parameter t varies between 0 and 1, a and d are points
88+
* Evaluates the Bezier at position t for points a, b, c, d.
89+
* The parameters a and d are the first and last points
8990
* on the curve, and b and c are the control points.
91+
* The final parameter t varies between 0 and 1.
9092
* This can be done once with the x coordinates and a second time
9193
* with the y coordinates to get the location of a bezier curve at t.
9294
*
@@ -96,18 +98,20 @@ p5.prototype.bezierDetail = function(d) {
9698
* @param {Number} c coordinate of second control point
9799
* @param {Number} d coordinate of second point on the curve
98100
* @param {Number} t value between 0 and 1
99-
* @return {Number} the value of the Bezier at point t
101+
* @return {Number} the value of the Bezier at position t
100102
* @example
101103
* <div>
102104
* <code>
103105
* noFill();
104-
* bezier(85, 20, 10, 10, 90, 90, 15, 80);
106+
* x1 = 85, x2 = 10, x3 = 90, x4 = 15;
107+
* y1 = 20, y2 = 10, y3 = 90, y4 = 80;
108+
* bezier(x1, y1, x2, y2, x3, y3, x4, y4);
105109
* fill(255);
106110
* steps = 10;
107111
* for (i = 0; i <= steps; i++) {
108112
* t = i / steps;
109-
* x = bezierPoint(85, 10, 90, 15, t);
110-
* y = bezierPoint(20, 10, 90, 80, t);
113+
* x = bezierPoint(x1, x2, x3, x4, t);
114+
* y = bezierPoint(y1, y2, y3, y4, t);
111115
* ellipse(x, y, 5, 5);
112116
* }
113117
* </code>
@@ -122,19 +126,18 @@ p5.prototype.bezierPoint = function(a, b, c, d, t) {
122126
};
123127

124128
/**
125-
* Calculates the tangent of a point on a Bezier curve
126-
*
127-
* Evaluates the tangent at point t for points a, b, c, d.
128-
* The parameter t varies between 0 and 1, a and d are points
129-
* on the curve, and b and c are the control points
129+
* Evaluates the tangent to the Bezier at position t for points a, b, c, d.
130+
* The parameters a and d are the first and last points
131+
* on the curve, and b and c are the control points.
132+
* The final parameter t varies between 0 and 1.
130133
*
131134
* @method bezierTangent
132135
* @param {Number} a coordinate of first point on the curve
133136
* @param {Number} b coordinate of first control point
134137
* @param {Number} c coordinate of second control point
135138
* @param {Number} d coordinate of second point on the curve
136139
* @param {Number} t value between 0 and 1
137-
* @return {Number} the tangent at point t
140+
* @return {Number} the tangent at position t
138141
* @example
139142
* <div>
140143
* <code>
@@ -194,11 +197,12 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) {
194197
};
195198

196199
/**
197-
* Draws a curved line on the screen. The first and second parameters specify
198-
* the beginning control point and the last two parameters specify the ending
199-
* control point. The middle parameters specify the start and stop of the
200-
* curve. Longer curves can be created by putting a series of curve()
201-
* functions together or using curveVertex(). An additional function called
200+
* Draws a curved line on the screen between two points, given as the
201+
* middle four parameters. The first two parameters are a control point, as
202+
* if the curve came from this point even though it's not drawn. The last
203+
* two parameters similarly describe the other control point. <br /><br />
204+
* Longer curves can be created by putting a series of curve() functions
205+
* together or using curveVertex(). An additional function called
202206
* curveTightness() provides control for the visual quality of the curve.
203207
* The curve() function is an implementation of Catmull-Rom splines.
204208
*
@@ -224,6 +228,20 @@ p5.prototype.bezierTangent = function(a, b, c, d, t) {
224228
* curve(73, 24, 73, 61, 15, 65, 15, 65);
225229
* </code>
226230
* </div>
231+
* <div>
232+
* <code>
233+
* // Define the curve points as JavaScript objects
234+
* p1 = {x: 5, y: 26}, p2 = {x: 73, y: 24}
235+
* p3 = {x: 73, y: 61}, p4 = {x: 15, y: 65}
236+
* noFill();
237+
* stroke(255, 102, 0);
238+
* curve(p1.x, p1.y, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y)
239+
* stroke(0);
240+
* curve(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y)
241+
* stroke(255, 102, 0);
242+
* curve(p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, p4.x, p4.y)
243+
* </code>
244+
* </div>
227245
*/
228246
p5.prototype.curve = function(x1, y1, x2, y2, x3, y3, x4, y4) {
229247
this._validateParameters(
@@ -304,9 +322,7 @@ p5.prototype.curveTightness = function (t) {
304322
};
305323

306324
/**
307-
* Calculate a point on the Curve
308-
*
309-
* Evaluates the Bezier at point t for points a, b, c, d.
325+
* Evaluates the curve at position t for points a, b, c, d.
310326
* The parameter t varies between 0 and 1, a and d are points
311327
* on the curve, and b and c are the control points.
312328
* This can be done once with the x coordinates and a second time
@@ -318,7 +334,7 @@ p5.prototype.curveTightness = function (t) {
318334
* @param {Number} c coordinate of second control point
319335
* @param {Number} d coordinate of second point on the curve
320336
* @param {Number} t value between 0 and 1
321-
* @return {Number} bezier value at point t
337+
* @return {Number} bezier value at position t
322338
* @example
323339
* <div>
324340
* <code>
@@ -340,7 +356,7 @@ p5.prototype.curveTightness = function (t) {
340356
* </code>
341357
* </div>
342358
*/
343-
p5.prototype.curvePoint = function(a, b,c, d, t) {
359+
p5.prototype.curvePoint = function(a, b, c, d, t) {
344360
var t3 = t*t*t,
345361
t2 = t*t,
346362
f1 = -0.5 * t3 + t2 - 0.5 * t,
@@ -351,19 +367,17 @@ p5.prototype.curvePoint = function(a, b,c, d, t) {
351367
};
352368

353369
/**
354-
* Calculates the tangent of a point on a curve
355-
*
356-
* Evaluates the tangent at point t for points a, b, c, d.
357-
* The parameter t varies between 0 and 1, a and d are points
358-
* on the curve, and b and c are the control points
370+
* Evaluates the tangent to the curve at position t for points a, b, c, d.
371+
* The parameter t varies between 0 and 1, a and d are points on the curve,
372+
* and b and c are the control points
359373
*
360374
* @method curveTangent
361375
* @param {Number} a coordinate of first point on the curve
362376
* @param {Number} b coordinate of first control point
363377
* @param {Number} c coordinate of second control point
364378
* @param {Number} d coordinate of second point on the curve
365379
* @param {Number} t value between 0 and 1
366-
* @return {Number} the tangent at point t
380+
* @return {Number} the tangent at position t
367381
* @example
368382
* <div>
369383
* <code>

src/core/rendering.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ p5.prototype.createCanvas = function(w, h, renderer) {
9595
}
9696
this._graphics.resize(w, h);
9797
this._graphics._applyDefaults();
98+
if (isDefault) { // only push once
99+
this._elements.push(this._graphics);
100+
}
98101
return this._graphics;
99102
};
100103

0 commit comments

Comments
 (0)