Skip to content

Commit 1cd4202

Browse files
author
Lauren McCarthy
committed
Merge pull request #576 from Spaxe/master
Amended documentation for async load*() functions.
2 parents 11d38fd + 53c0f0c commit 1cd4202

File tree

1 file changed

+72
-60
lines changed

1 file changed

+72
-60
lines changed

src/input/files.js

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,63 @@ define(function (require) {
3131
/**
3232
* Loads a JSON file from a file or a URL, and returns an Object or Array.
3333
* This method is asynchronous, meaning it may not finish before the next
34-
* line in your sketch is executed. Either use preload() to guarantee the
35-
* file loads before setup() and draw() are called, or supply a callback
36-
* function that is executed when loadStrings() completes.
37-
*
34+
* line in your sketch is executed.
35+
*
3836
* @method loadJSON
3937
* @param {String} path name of the file or url to load
4038
* @param {Function} [callback] function to be executed after
4139
* loadJSON()
4240
* completes, Array is passed in as first
4341
* argument
44-
* @param {String} [datatype] "json" or "jsonp"
42+
* @param {String} [datatype] "json" or "jsonp"
4543
* @return {Object|Array} JSON data
4644
* @example
45+
*
46+
* <p>Calling loadJSON() inside preload() guarantees to complete the
47+
* operation before setup() and draw() are called.</p>
48+
*
4749
* <div><code>
50+
* var weather;
51+
* function preload() {
52+
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=London,UK';
53+
* weather = loadJSON(url);
54+
* }
55+
*
4856
* function setup() {
4957
* noLoop();
50-
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=NewYork,USA';
51-
* loadJSON(url, drawWeather);
5258
* }
5359
*
5460
* function draw() {
5561
* background(200);
56-
* }
57-
*
58-
* function drawWeather(weather) {
5962
* // get the humidity value out of the loaded JSON
6063
* var humidity = weather.main.humidity;
6164
* fill(0, humidity); // use the humidity value to set the alpha
6265
* ellipse(width/2, height/2, 50, 50);
6366
* }
6467
* </code></div>
6568
*
69+
* <p>Outside preload(), you may supply a callback function to handle the
70+
* object:</p>
71+
6672
* <div><code>
67-
* var weather;
68-
* function preload() {
69-
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=London,UK';
70-
* weather = loadJSON(url);
71-
* }
72-
*
7373
* function setup() {
7474
* noLoop();
75+
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=NewYork,USA';
76+
* loadJSON(url, drawWeather);
7577
* }
7678
*
7779
* function draw() {
7880
* background(200);
81+
* }
82+
*
83+
* function drawWeather(weather) {
7984
* // get the humidity value out of the loaded JSON
8085
* var humidity = weather.main.humidity;
8186
* fill(0, humidity); // use the humidity value to set the alpha
8287
* ellipse(width/2, height/2, 50, 50);
8388
* }
8489
* </code></div>
90+
*
8591
*/
8692
p5.prototype.loadJSON = function() {
8793
var path = arguments[0];
@@ -113,24 +119,26 @@ define(function (require) {
113119
* Reads the contents of a file and creates a String array of its individual
114120
* lines. If the name of the file is used as the parameter, as in the above
115121
* example, the file must be located in the sketch directory/folder.
116-
*
122+
*
117123
* Alternatively, the file maybe be loaded from anywhere on the local
118124
* computer using an absolute path (something that starts with / on Unix and
119125
* Linux, or a drive letter on Windows), or the filename parameter can be a
120126
* URL for a file found on a network.
121127
*
122128
* This method is asynchronous, meaning it may not finish before the next
123-
* line in your sketch is executed. Either use preload() to guarantee the
124-
* file loads before setup() and draw() are called, or supply a callback
125-
* function that is executed when loadStrings() completes.
126-
*
129+
* line in your sketch is executed.
130+
*
127131
* @method loadStrings
128132
* @param {String} filename name of the file or url to load
129133
* @param {Function} [callback] function to be executed after loadStrings()
130134
* completes, Array is passed in as first
131135
* argument
132136
* @return {Array} Array of Strings
133137
* @example
138+
*
139+
* <p>Calling loadStrings() inside preload() guarantees to complete the
140+
* operation before setup() and draw() are called.</p>
141+
*
134142
* <div><code>
135143
* var result;
136144
* function preload() {
@@ -144,6 +152,9 @@ define(function (require) {
144152
* }
145153
* </code></div>
146154
*
155+
* <p>Outside preload(), you may supply a callback function to handle the
156+
* object:</p>
157+
*
147158
* <div><code>
148159
* function setup() {
149160
* loadStrings('assets/test.txt', pickString);
@@ -176,38 +187,39 @@ define(function (require) {
176187
};
177188

178189
/**
179-
* <p>Reads the contents of a file or URL and creates a p5.Table
180-
* object with its values. If a file is specified, it must be
181-
* located in the sketch's "data" folder. The filename parameter
182-
* can also be a URL to a file found online. By default, the file
183-
* is assumed to be comma-separated (in CSV format). Table only
184-
* looks for a header row if the 'header' option is included.</p>
190+
* <p>Reads the contents of a file or URL and creates a p5.Table object with
191+
* its values. If a file is specified, it must be located in the sketch's
192+
* "data" folder. The filename parameter can also be a URL to a file found
193+
* online. By default, the file is assumed to be comma-separated (in CSV
194+
* format). Table only looks for a header row if the 'header' option is
195+
* included.</p>
185196
*
186-
* <p>Possible options include:
187-
* <ul>
188-
* <li>csv - parse the table as comma-separated values</li>
189-
* <li>tsv - parse the table as tab-separated values</li>
190-
* <li>header - this table has a header (title) row</li>
191-
* </ul>
192-
* </p>
193-
*
194-
* <p>When passing in multiple options, pass them in as separate parameters,
195-
* seperated by commas.</p>
197+
* <p>Possible options include:
198+
* <ul>
199+
* <li>csv - parse the table as comma-separated values</li>
200+
* <li>tsv - parse the table as tab-separated values</li>
201+
* <li>header - this table has a header (title) row</li>
202+
* </ul>
203+
* </p>
196204
*
197-
* <p> All files loaded and saved use UTF-8 encoding.</p>
198-
*
199-
* <p>This method is asynchronous, meaning it may not finish before the next
200-
* line in your sketch is executed. Either use preload() to guarantee the
201-
* file loads before setup() and draw() are called, or supply a callback
202-
* function that is executed when loadTable() completes.</p>
203-
*
204-
* @method loadTable
205-
* @param {String} filename name of the file or URL to load
206-
* @param {String|Strings} [options] "header" "csv" "tsv"
207-
* @param {Function} [callback] function to be executed after loadTable()
208-
* completes, Table object is passed in as
209-
* first argument
210-
* @return {Object} Table object containing data
205+
* <p>When passing in multiple options, pass them in as separate parameters,
206+
* seperated by commas. For example: "csv, header".</p>
207+
*
208+
* <p> All files loaded and saved use UTF-8 encoding.</p>
209+
*
210+
* <p>This method is asynchronous, meaning it may not finish before the next
211+
* line in your sketch is executed. Calling loadTable() inside preload()
212+
* guarantees to complete the operation before setup() and draw() are called.
213+
* Outside preload(), you may supply a callback function to handle the object.
214+
* </p>
215+
*
216+
* @method loadTable
217+
* @param {String} filename name of the file or URL to load
218+
* @param {String|Strings} [options] "header" "csv" "tsv"
219+
* @param {Function} [callback] function to be executed after
220+
* loadTable() completes, Table object is
221+
* passed in as first argument
222+
* @return {Object} Table object containing data
211223
*/
212224
p5.prototype.loadTable = function (path) {
213225
var callback = null;
@@ -411,12 +423,12 @@ define(function (require) {
411423
* computer using an absolute path (something that starts with / on Unix and
412424
* Linux, or a drive letter on Windows), or the filename parameter can be a
413425
* URL for a file found on a network.
414-
*
426+
*
415427
* This method is asynchronous, meaning it may not finish before the next
416-
* line in your sketch is executed. Either use preload() to guarantee the
417-
* file loads before setup() and draw() are called, or supply a callback
418-
* function that is executed when loadXML() completes.
419-
*
428+
* line in your sketch is executed. Calling loadXML() inside preload()
429+
* guarantees to complete the operation before setup() and draw() are called.
430+
* Outside preload(), you may supply a callback function to handle the object.
431+
*
420432
* @method loadXML
421433
* @param {String} filename name of the file or URL to load
422434
* @param {Function} [callback] function to be executed after loadXML()
@@ -464,7 +476,7 @@ define(function (require) {
464476
/**
465477
* Method for executing an HTTP GET request. If data type is not specified,
466478
* p5 will try to guess based on the URL, defaulting to text.
467-
*
479+
*
468480
* @method httpGet
469481
* @param {String} path name of the file or url to load
470482
* @param {Object} [data] param data passed sent with request
@@ -483,7 +495,7 @@ define(function (require) {
483495
/**
484496
* Method for executing an HTTP POST request. If data type is not specified,
485497
* p5 will try to guess based on the URL, defaulting to text.
486-
*
498+
*
487499
* @method httpPost
488500
* @param {String} path name of the file or url to load
489501
* @param {Object} [data] param data passed sent with request
@@ -501,10 +513,10 @@ define(function (require) {
501513
/**
502514
* Method for executing an HTTP request. If data type is not specified,
503515
* p5 will try to guess based on the URL, defaulting to text.
504-
*
516+
*
505517
* @method httpDo
506518
* @param {String} path name of the file or url to load
507-
* @param {String} [method] either "GET", "POST", or "PUT",
519+
* @param {String} [method] either "GET", "POST", or "PUT",
508520
* defaults to "GET"
509521
* @param {Object} [data] param data passed sent with request
510522
* @param {String} [datatype] "json", "jsonp", "xml", or "text"

0 commit comments

Comments
 (0)