diff --git a/docs/yuidoc-p5-theme/assets/animals.xml b/docs/yuidoc-p5-theme/assets/animals.xml
new file mode 100644
index 0000000000..7c8b159555
--- /dev/null
+++ b/docs/yuidoc-p5-theme/assets/animals.xml
@@ -0,0 +1,7 @@
+
+
+ * let myData;
*
- *
- * // Examples use USGS Earthquake API:
- * // https://earthquake.usgs.gov/fdsnws/event/1/#methods
- * let earthquakes;
+ * // Load the JSON and create an object.
* function preload() {
- * // Get the most recent earthquake in the database
- * let url =
- 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/' +
- * 'summary/all_day.geojson';
- * earthquakes = loadJSON(url);
+ * myData = loadJSON('assets/data.json');
* }
*
* function setup() {
- * noLoop();
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the circle.
+ * fill(myData.color);
+ * noStroke();
+ *
+ * // Draw the circle.
+ * circle(myData.x, myData.y, myData.d);
+ *
+ * describe('A pink circle on a gray background.');
* }
+ *
+ *
+ *
+ *
+ *
+ * let myData;
+ *
+ * // Load the JSON and create an object.
+ * function preload() {
+ * myData = loadJSON('assets/data.json');
+ * }
+ *
+ * function setup() {
+ * createCanvas(100, 100);
*
- * function draw() {
* background(200);
- * // Get the magnitude and name of the earthquake out of the loaded JSON
- * let earthquakeMag = earthquakes.features[0].properties.mag;
- * let earthquakeName = earthquakes.features[0].properties.place;
- * ellipse(width / 2, height / 2, earthquakeMag * 10, earthquakeMag * 10);
- * textAlign(CENTER);
- * text(earthquakeName, 0, height - 30, width, 30);
- * describe(`50×50 ellipse that changes from black to white
- * depending on the current humidity`);
+ *
+ * // Create a p5.Color object and make it transparent.
+ * let c = color(myData.color);
+ * c.setAlpha(80);
+ *
+ * // Style the circles.
+ * fill(c);
+ * noStroke();
+ *
+ * // Iterate over the myData.bubbles array.
+ * for (let b of myData.bubbles) {
+ * // Draw a circle for each bubble.
+ * circle(b.x, b.y, b.d);
+ * }
+ *
+ * describe('Several pink bubbles floating in a blue sky.');
+ * }
+ *
+ *
+ *
+ *
+ *
+ * let myData;
+ *
+ * // Load the GeoJSON and create an object.
+ * function preload() {
+ * myData = loadJSON('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson');
* }
- *
*
- * Outside of preload(), you may supply a callback function to handle the
- * object:
- *
* function setup() {
- * noLoop();
- * let url =
- 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/' +
- * 'summary/all_day.geojson';
- * loadJSON(url, drawEarthquake);
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Get data about the most recent earthquake.
+ * let quake = myData.features[0].properties;
+ *
+ * // Draw a circle based on the earthquake's magnitude.
+ * circle(50, 50, quake.mag * 10);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(11);
+ *
+ * // Display the earthquake's location.
+ * text(quake.place, 5, 80, 100);
+ *
+ * describe(`A white circle on a gray background. The text "${quake.place}" is written beneath the circle.`);
* }
+ *
+ *
+ *
+ *
+ *
+ * let bigQuake;
+ *
+ * // Load the GeoJSON and preprocess it.
+ * function preload() {
+ * loadJSON(
+ * 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson',
+ * handleData
+ * );
+ * }
+ *
+ * function setup() {
+ * createCanvas(100, 100);
*
- * function draw() {
* background(200);
- * describe(`50×50 ellipse that changes from black to white
- * depending on the current humidity`);
+ *
+ * // Draw a circle based on the earthquake's magnitude.
+ * circle(50, 50, bigQuake.mag * 10);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(11);
+ *
+ * // Display the earthquake's location.
+ * text(bigQuake.place, 5, 80, 100);
+ *
+ * describe(`A white circle on a gray background. The text "${bigQuake.place}" is written beneath the circle.`);
* }
*
- * function drawEarthquake(earthquakes) {
- * // Get the magnitude and name of the earthquake out of the loaded JSON
- * let earthquakeMag = earthquakes.features[0].properties.mag;
- * let earthquakeName = earthquakes.features[0].properties.place;
- * ellipse(width / 2, height / 2, earthquakeMag * 10, earthquakeMag * 10);
- * textAlign(CENTER);
- * text(earthquakeName, 0, height - 30, width, 30);
+ * // Find the biggest recent earthquake.
+ * function handleData(data) {
+ * let maxMag = 0;
+ * // Iterate over the earthquakes array.
+ * for (let quake of data.features) {
+ * // Reassign bigQuake if a larger
+ * // magnitude quake is found.
+ * if (quake.properties.mag > maxMag) {
+ * bigQuake = quake.properties;
+ * }
+ * }
* }
- *
- */
-/**
- * @method loadJSON
- * @param {String} path
- * @param {String} datatype
- * @param {function} [callback]
- * @param {function} [errorCallback]
- * @return {Object|Array}
- */
-/**
- * @method loadJSON
- * @param {String} path
- * @param {function} callback
- * @param {function} [errorCallback]
- * @return {Object|Array}
+ *
+ *
+ * let bigQuake;
+ *
+ * // Load the GeoJSON and preprocess it.
+ * function preload() {
+ * loadJSON(
+ * 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson',
+ * handleData,
+ * handleError
+ * );
+ * }
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Draw a circle based on the earthquake's magnitude.
+ * circle(50, 50, bigQuake.mag * 10);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(11);
+ *
+ * // Display the earthquake's location.
+ * text(bigQuake.place, 5, 80, 100);
+ *
+ * describe(`A white circle on a gray background. The text "${bigQuake.place}" is written beneath the circle.`);
+ * }
+ *
+ * // Find the biggest recent earthquake.
+ * function handleData(data) {
+ * let maxMag = 0;
+ * // Iterate over the earthquakes array.
+ * for (let quake of data.features) {
+ * // Reassign bigQuake if a larger
+ * // magnitude quake is found.
+ * if (quake.properties.mag > maxMag) {
+ * bigQuake = quake.properties;
+ * }
+ * }
+ * }
+ *
+ * // Log any errors to the console.
+ * function handleError(error) {
+ * console.log('Oops!', error);
+ * }
+ *
+ *
+ * let myData;
*
- *
- * let result;
+ * // Load the text and create an array.
* function preload() {
- * result = loadStrings('assets/test.txt');
+ * myData = loadStrings('assets/test.txt');
* }
-
+ *
* function setup() {
+ * createCanvas(100, 100);
+ *
* background(200);
- * text(random(result), 10, 10, 80, 80);
- * describe(`randomly generated text from a file,
- * for example "i smell like butter"`);
+ *
+ * // Select a random line from the text.
+ * let phrase = random(myData);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display the text.
+ * text(phrase, 10, 50, 90);
+ *
+ * describe(`The text "${phrase}" written in black on a gray background.`);
* }
- *
+ *
+ *
+ * let lastLine;
+ *
+ * // Load the text and preprocess it.
+ * function preload() {
+ * loadStrings('assets/test.txt', handleData);
+ * }
*
- *
* function setup() {
- * loadStrings('assets/test.txt', pickString);
- * describe(`randomly generated text from a file,
- * for example "i have three feet"`);
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display the text.
+ * text(lastLine, 10, 50, 90);
+ *
+ * describe('The text "I talk like an orange" written in black on a gray background.');
+ * }
+ *
+ * // Select the last line from the text.
+ * function handleData(data) {
+ * lastLine = data[data.length - 1];
* }
+ *
+ *
+ *
+ *
+ *
+ * let lastLine;
+ *
+ * // Load the text and preprocess it.
+ * function preload() {
+ * loadStrings('assets/test.txt', handleData, handleError);
+ * }
+ *
+ * function setup() {
+ * createCanvas(100, 100);
*
- * function pickString(result) {
* background(200);
- * text(random(result), 10, 10, 80, 80);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display the text.
+ * text(lastLine, 10, 50, 90);
+ *
+ * describe('The text "I talk like an orange" written in black on a gray background.');
* }
- *
+ *
+ * // Select the last line from the text.
+ * function handleData(data) {
+ * lastLine = data[data.length - 1];
+ * }
+ *
+ * // Log any errors to the console.
+ * function handleError(error) {
+ * console.error('Oops!', error);
+ * }
+ *
+ *
+ * let myXML;
*
- * Outside of preload(), you may supply a callback function to handle the
- * object.
+ * // Load the XML and create a p5.XML object.
+ * function preload() {
+ * myXML = loadXML('assets/animals.xml');
+ * }
*
- * This method is suitable for fetching files up to size of 64MB.
- * @method loadXML
- * @param {String} filename name of the file or URL to load
- * @param {function} [callback] function to be executed after loadXML()
- * completes, XML object is passed in as
- * first argument
- * @param {function} [errorCallback] function to be executed if
- * there is an error, response is passed
- * in as first argument
- * @return {Object} XML object containing data
- * @example
- *
- * // The following short XML file called "mammals.xml" is parsed
- * // in the code below.
- * //
- * //
- * // <mammals>
- * // <animal id="0" species="Capra hircus">Goat</animal>
- * // <animal id="1" species="Panthera pardus">Leopard</animal>
- * // <animal id="2" species="Equus zebra">Zebra</animal>
- * // </mammals>
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Get an array with all mammal tags.
+ * let mammals = myXML.getChildren('mammal');
*
- * let xml;
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(14);
*
+ * // Iterate over the mammals array.
+ * for (let i = 0; i < mammals.length; i += 1) {
+ *
+ * // Calculate the y-coordinate.
+ * let y = (i + 1) * 25;
+ *
+ * // Get the mammal's common name.
+ * let name = mammals[i].getContent();
+ *
+ * // Display the mammal's name.
+ * text(name, 20, y);
+ * }
+ *
+ * describe(
+ * 'The words "Goat", "Leopard", and "Zebra" written on three separate lines. The text is black on a gray background.'
+ * );
+ * }
+ *
+ *
+ *
+ *
+ *
+ * let lastMammal;
+ *
+ * // Load the XML and create a p5.XML object.
* function preload() {
- * xml = loadXML('assets/mammals.xml');
+ * loadXML('assets/animals.xml', handleData);
* }
*
* function setup() {
- * let children = xml.getChildren('animal');
+ * createCanvas(100, 100);
*
- * for (let i = 0; i < children.length; i++) {
- * let id = children[i].getNum('id');
- * let coloring = children[i].getString('species');
- * let name = children[i].getContent();
- * print(id + ', ' + coloring + ', ' + name);
- * }
- * describe('no image displayed');
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(CENTER, CENTER);
+ * textFont('Courier New');
+ * textSize(16);
+ *
+ * // Display the content of the last mammal element.
+ * text(lastMammal, 50, 50);
+ *
+ * describe('The word "Zebra" written in black on a gray background.');
* }
*
- * // Sketch prints:
- * // 0, Capra hircus, Goat
- * // 1, Panthera pardus, Leopard
- * // 2, Equus zebra, Zebra
- *
+ * // Get the content of the last mammal element.
+ * function handleData(data) {
+ * // Get an array with all mammal elements.
+ * let mammals = data.getChildren('mammal');
+ *
+ * // Get the content of the last mammal.
+ * lastMammal = mammals[mammals.length - 1].getContent();
+ * }
+ *
+ *
+ * let lastMammal;
+ *
+ * // Load the XML and preprocess it.
+ * function preload() {
+ * loadXML('assets/animals.xml', handleData, handleError);
+ * }
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(CENTER, CENTER);
+ * textFont('Courier New');
+ * textSize(16);
+ *
+ * // Display the content of the last mammal element.
+ * text(lastMammal, 50, 50);
+ *
+ * describe('The word "Zebra" written in black on a gray background.');
+ * }
+ *
+ * // Get the content of the last mammal element.
+ * function handleData(data) {
+ * // Get an array with all mammal elements.
+ * let mammals = data.getChildren('mammal');
+ *
+ * // Get the content of the last mammal.
+ * lastMammal = mammals[mammals.length - 1].getContent();
+ * }
+ *
+ * // Log any errors to the console.
+ * function handleError(error) {
+ * console.error('Oops!', error);
+ * }
+ *
+ *
* function setup() {
* createCanvas(100, 100);
+ *
* background(200);
- * text('click here to save', 10, 10, 70, 80);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function mousePressed() {
- * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
- * const writer = createWriter('squares.txt');
- * for (let i = 0; i < 10; i++) {
- * writer.print(i * i);
- * }
- * writer.close();
- * writer.clear();
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('xo.txt');
+ *
+ * // Add some lines to the print stream.
+ * myWriter.print('XOO');
+ * myWriter.print('OXO');
+ * myWriter.print('OOX');
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
+ * }
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create a p5.PrintWriter object.
+ * // Use the file format .csv.
+ * let myWriter = createWriter('mauna_loa_co2', 'csv');
+ *
+ * // Add some lines to the print stream.
+ * myWriter.print('date,ppm_co2');
+ * myWriter.print('1960-01-01,316.43');
+ * myWriter.print('1970-01-01,325.06');
+ * myWriter.print('1980-01-01,337.9');
+ * myWriter.print('1990-01-01,353.86');
+ * myWriter.print('2000-01-01,369.45');
+ * myWriter.print('2020-01-01,413.61');
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
* }
* }
*
@@ -1184,67 +1563,104 @@ p5.prototype.createWriter = function(name, extension) {
};
/**
- * @class p5.PrintWriter
- * @param {String} filename
- * @param {String} [extension]
+ * A class to describe a print stream.
+ *
+ * Each `p5.PrintWriter` object provides a way to save a sequence of text
+ * data, called the *print stream*, to the user's computer. It's a low-level
+ * object that enables precise control of text output. Functions such as
+ * saveStrings() and
+ * saveJSON() are easier to use for simple file
+ * saving.
+ *
+ * Note: createWriter() is the recommended way
+ * to make an instance of this class.
+ *
+ * @class p5.PrintWriter
+ * @param {String} filename name of the file to create.
+ * @param {String} [extension] format to use for the file.
+ *
+ * @example
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('xo.txt');
+ *
+ * // Add some lines to the print stream.
+ * myWriter.print('XOO');
+ * myWriter.print('OXO');
+ * myWriter.print('OOX');
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
+ * }
+ *
+ *
- * // creates a file called 'newFile.txt'
- * let writer = createWriter('newFile.txt');
- * // write 'Hello world!'' to the file
- * writer.write(['Hello world!']);
- * // close the PrintWriter and save the file
- * writer.close();
- *
- *
- * // creates a file called 'newFile2.txt'
- * let writer = createWriter('newFile2.txt');
- * // write 'apples,bananas,123' to the file
- * writer.write(['apples', 'bananas', 123]);
- * // close the PrintWriter and save the file
- * writer.close();
- *
- *
- * // creates a file called 'newFile3.txt'
- * let writer = createWriter('newFile3.txt');
- * // write 'My name is: Teddy' to the file
- * writer.write('My name is:');
- * writer.write(' Teddy');
- * // close the PrintWriter and save the file
- * writer.close();
- *
- *
* function setup() {
* createCanvas(100, 100);
- * button = createButton('SAVE FILE');
- * button.position(21, 40);
- * button.mousePressed(createFile);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function createFile() {
- * // creates a file called 'newFile.txt'
- * let writer = createWriter('newFile.txt');
- * // write 'Hello world!'' to the file
- * writer.write(['Hello world!']);
- * // close the PrintWriter and save the file
- * writer.close();
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('numbers.txt');
+ *
+ * // Add some data to the print stream.
+ * myWriter.write('1,2,3,');
+ * myWriter.write(['4', '5', '6']);
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
* }
*
*
- * // creates a file called 'newFile.txt'
- * let writer = createWriter('newFile.txt');
- * // creates a file containing
- * // My name is:
- * // Teddy
- * writer.print('My name is:');
- * writer.print('Teddy');
- * // close the PrintWriter and save the file
- * writer.close();
- *
- *
- * let writer;
- *
* function setup() {
- * createCanvas(400, 400);
- * // create a PrintWriter
- * writer = createWriter('newFile.txt');
- * }
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
*
- * function draw() {
- * writer.print([mouseX, mouseY]);
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function mouseClicked() {
- * writer.close();
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('numbers.txt');
+ *
+ * // Add some data to the print stream.
+ * myWriter.print('1,2,3,');
+ * myWriter.print(['4', '5', '6']);
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
* }
*
*
- * // create writer object
- * let writer = createWriter('newFile.txt');
- * writer.write(['clear me']);
- * // clear writer object here
- * writer.clear();
- * // close writer
- * writer.close();
- *
* function setup() {
- * button = createButton('CLEAR ME');
- * button.position(21, 40);
- * button.mousePressed(createFile);
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function createFile() {
- * let writer = createWriter('newFile.txt');
- * writer.write(['clear me']);
- * writer.clear();
- * writer.close();
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('numbers.txt');
+ *
+ * // Add some data to the print stream.
+ * myWriter.print('Hello p5*js!');
+ *
+ * // Clear the print stream.
+ * myWriter.clear();
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
* }
*
*
- * // create a file called 'newFile.txt'
- * let writer = createWriter('newFile.txt');
- * // close the PrintWriter and save the file
- * writer.close();
- *
- *
- * // create a file called 'newFile2.txt'
- * let writer = createWriter('newFile2.txt');
- * // write some data to the file
- * writer.write([100, 101, 102]);
- * // close the PrintWriter and save the file
- * writer.close();
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * // Create a p5.PrintWriter object.
+ * let myWriter = createWriter('cat.txt');
+ *
+ * // Add some data to the print stream.
+ * // ASCII art courtesy Wikipedia:
+ * // https://en.wikipedia.org/wiki/ASCII_art
+ * myWriter.print(' (\\_/) ');
+ * myWriter.print("(='.'=)");
+ * myWriter.print('(")_(")');
+ *
+ * // Save the file and close the print stream.
+ * myWriter.close();
+ * }
*
*
- * let json = {}; // new JSON Object
+ * JavaScript Object Notation
+ * (JSON)
+ * is a standard format for sending data between applications. The format is
+ * based on JavaScript objects which have keys and values. JSON files store
+ * data in an object with strings as keys. Values can be strings, numbers,
+ * Booleans, arrays, `null`, or other objects.
+ *
+ * The first parameter, `json`, is the data to save. The data can be an array,
+ * as in `[1, 2, 3]`, or an object, as in
+ * `{ x: 50, y: 50, color: 'deeppink' }`.
+ *
+ * The second parameter, `filename`, is a string that sets the file's name.
+ * For example, calling `saveJSON([1, 2, 3], 'data.json')` saves the array
+ * `[1, 2, 3]` to a file called `data.json` on the user's computer.
+ *
+ * The third parameter, `optimize`, is optional. If `true` is passed, as in
+ * `saveJSON([1, 2, 3], 'data.json', true)`, then all unneeded whitespace will
+ * be removed to reduce the file size.
*
- * json.id = 0;
- * json.species = 'Panthera leo';
- * json.name = 'Lion';
+ * Note: The browser will either save the file immediately or prompt the user
+ * with a dialogue window.
*
+ * @method saveJSON
+ * @param {Array|Object} json data to save.
+ * @param {String} filename name of the file to be saved.
+ * @param {Boolean} [optimize] whether to trim unneeded whitespace. Defaults
+ * to `true`.
+ *
+ * @example
+ *
+ *
* function setup() {
* createCanvas(100, 100);
+ *
* background(200);
- * text('click here to save', 10, 10, 70, 80);
- * describe('no image displayed');
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function mousePressed() {
- * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
- * saveJSON(json, 'lion.json');
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an array.
+ * let data = [1, 2, 3];
+ *
+ * // Save the JSON file.
+ * saveJSON(data, 'numbers.json');
* }
* }
+ *
+ *
*
- * // saves the following to a file called "lion.json":
- * // {
- * // "id": 0,
- * // "species": "Panthera leo",
- * // "name": "Lion"
- * // }
- *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an object.
+ * let data = { x: mouseX, y: mouseY };
+ *
+ * // Save the JSON file.
+ * saveJSON(data, 'state.json');
+ * }
+ * }
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an object.
+ * let data = { x: mouseX, y: mouseY };
+ *
+ * // Save the JSON file and reduce its size.
+ * saveJSON(data, 'state.json', true);
+ * }
+ * }
+ *
+ *
- * let words = 'apple bear cat dog';
+ * @param {String[]} list data to save.
+ * @param {String} filename name of file to be saved.
+ * @param {String} [extension] format to use for the file.
+ * @param {Boolean} [isCRLF] whether to add `\r\n` to the end of each
+ * string. Defaults to `false`.
*
- * // .split() outputs an Array
- * let list = split(words, ' ');
+ * @example
+ *
+ *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an array.
+ * let data = ['0', '01', '011'];
+ *
+ * // Save the text file.
+ * saveStrings(data, 'data.txt');
+ * }
+ * }
+ *
+ *
*
+ *
+ *
* function setup() {
* createCanvas(100, 100);
+ *
* background(200);
- * text('click here to save', 10, 10, 70, 80);
- * describe('no image displayed');
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
* }
*
- * function mousePressed() {
- * if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
- * saveStrings(list, 'nouns.txt');
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an array.
+ * // ASCII art courtesy Wikipedia:
+ * // https://en.wikipedia.org/wiki/ASCII_art
+ * let data = [' (\\_/) ', "(='.'=)", '(")_(")'];
+ *
+ * // Save the text file.
+ * saveStrings(data, 'cat', 'txt');
* }
* }
+ *
+ *
*
- * // Saves the following to a file called 'nouns.txt':
- * //
- * // apple
- * // bear
- * // cat
- * // dog
- *
+ * function setup() {
+ * createCanvas(100, 100);
+ *
+ * background(200);
+ *
+ * // Style the text.
+ * textAlign(LEFT, CENTER);
+ * textFont('Courier New');
+ * textSize(12);
+ *
+ * // Display instructions.
+ * text('Double-click to save', 5, 50, 90);
+ *
+ * describe('The text "Double-click to save" written in black on a gray background.');
+ * }
+ *
+ * // Save the file when the user double-clicks.
+ * function doubleClicked() {
+ * if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
+ * // Create an array.
+ * // +--+
+ * // / /|
+ * // +--+ +
+ * // | |/
+ * // +--+
+ * let data = [' +--+', ' / /|', '+--+ +', '| |/', '+--+'];
+ *
+ * // Save the text file.
+ * // Use CRLF for line endings.
+ * saveStrings(data, 'box', 'txt', true);
+ * }
+ * }
+ *
+ *