Skip to content

Commit 3b2733a

Browse files
committed
downplay partial examples (switch code examples for self-contained ones). clarify.
1 parent 11e9322 commit 3b2733a

2 files changed

Lines changed: 60 additions & 39 deletions

File tree

contributor_docs/contributing_to_the_p5js_reference.md

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ When you look at the source code of p5.js, you will see many lines in the librar
4545
* A much longer description normally goes here
4646
*
4747
* @method circle
48-
* @param {Number} x x-coordinate of centre.
49-
* @param {Number} y y-coordinate of centre.
48+
* @param {Number} x x-coordinate of centre.
49+
* @param {Number} y y-coordinate of centre.
5050
* @param {Number} diameter Diameter of circle.
5151
*
5252
* @example
@@ -651,56 +651,71 @@ The `@readonly` tag is present on most p5.js variables and is used internally to
651651

652652
One tag that is present in both `sin()` and `mouseX`’s reference comments that we have not talked about yet is the [`@example`](https://jsdoc.app/tags-example) tag. This tag is the main way to define each code example to be shown and run when you visit the reference page.
653653

654-
![Screenshot of the p5.js reference page of the "red()" function, showing only the example code section.](images/reference-screenshot.png)
654+
Here's an example from the documentation of the `fill()` function.
655+
656+
![Screenshot of the p5.js reference page of the "fill()" function, showing only one code example section.](images/reference-screenshot-example-fill-red.png)
655657

656658
The relevant `@example` tag to create the above is as follows:
657659

658660
```
659-
* @example
660-
* const c = color(255, 204, 0);
661-
* fill(c);
662-
* rect(15, 20, 35, 60);
663-
* // Sets 'redValue' to 255.
664-
* const redValue = red(c);
665-
* fill(redValue, 0, 0);
666-
* rect(50, 20, 35, 60);
667-
* describe(
668-
* 'Two rectangles with black edges. The rectangle on the left is yellow and the one on the right is red.'
669-
* );
661+
* @example
662+
* function setup() {
663+
* createCanvas(100, 100);
664+
*
665+
* background(200);
666+
*
667+
* // A CSS named color.
668+
* fill('red');
669+
* square(20, 20, 60);
670+
*
671+
* describe('A red square with a black outline.');
672+
* };
670673
```
671674

672675
Generally, each example should be a self-contained complete sketch that will run on the reference website and which could be run directly if pasted into (for example) the web editor.
673676

674-
It should declare `setup()` and `draw()` functions as required.
677+
It should declare a `setup()` function, and `draw()` if required.
675678

676679
The example’s canvas should be 100x100 pixels.
677680

678-
### About the complexity of examples
679-
680-
The basic principle of writing good example code for the reference is to keep things simple and minimal. The example should be meaningful and explain how the feature works without being too complicated.
681-
682-
While it may be tempting to make a more interesting, engaging, or "cool" example using other functions (e.g. noise()), or using clever math, that makes it harder for readers to understand. Try to minimize the pre-requisites necessary for the reader to follow your example.
683-
684-
We won’t go through the details about best practices and code style for the example code here; please see the [reference style guide](https://beta.p5js.org/contribute/documentation_style_guide/), instead.
681+
If there's no sensible way to present the code usage in a small sketch, you can create an example which doesn't run. See [norender](#norender).
685682

686683
### Providing multiple examples
687684

688685
You can have multiple examples for one feature. These should be separated by a blank line and a new @example tag for each.
689686

690-
Example:
687+
Example (from `createCanvas()`):
691688

692689
```
693-
* @example
694-
* arc(50, 50, 80, 80, 0, PI + QUARTER_PI, OPEN);
695-
* describe('An ellipse created using an arc with its top right open.');
696-
*
697-
* @example
698-
* arc(50, 50, 80, 80, 0, PI, OPEN);
699-
* describe('The bottom half of an ellipse created using arc.');
690+
* @example
691+
* function setup() {
692+
* createCanvas(100, 50);
693+
*
694+
* background(200);
695+
*
696+
* // Draw a diagonal line.
697+
* line(0, 0, width, height);
698+
*
699+
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
700+
* }
701+
*
702+
* @example
703+
* // Use WebGL mode.
704+
* function setup() {
705+
* createCanvas(100, 100, WEBGL);
706+
*
707+
* background(200);
708+
*
709+
* // Draw a diagonal line.
710+
* line(-width / 2, -height / 2, width / 2, height / 2);
711+
*
712+
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
713+
* }
700714
```
715+
701716
### <a id="inserted-examples"></a>Inserting examples _within_ the description
702717

703-
It is possible, and often desirable, to include one or two early runnable examples _within_ the description section, before the main "Examples" section of the page. (This is particularly useful when a function or class has a lengthy description section.)
718+
It is possible, and often desirable, to include early runnable examples _within_ the description section, before the main "Examples" section of the page. This is particularly useful when a function or class has a lengthy and detailed description section.
704719

705720
The <a href="https://beta.p5js.org/reference/p5/image/">p5.Image reference</a> has a good example of this. ([Source here](https://github.com/processing/p5.js/blob/6a61f7fb3055969fe53d9f82027f891d245b3e9f/src/webgl/material.js#L597)).
706721

@@ -718,15 +733,23 @@ To add such an example, instead of using an `@example` tag, surround the example
718733
To make the code-block, surround your example code before and after by three backticks on a newline.
719734
The opening backticks should be immediately followed by the annotation `js example`. Remember, you should not use an `@example` tag in this case.
720735

721-
**Why is this important?**
736+
Why is this important?
722737

723738
Early examples like this allow the reader to quickly play with, and get an early understanding of, a p5.js feature _without_ having to read or scroll through possibly overwhelming amounts of documentation discussing in-depth details of the feature.
724739

740+
### About the complexity of code examples
741+
742+
The basic principle of writing good example code for the reference is to keep things simple and minimal. The example should be meaningful and explain how the feature works without being too complicated.
743+
744+
While it may be tempting to make a more interesting, engaging, or "cool" example using other functions (e.g. noise()), or using clever math, that makes it harder for readers to understand. Try to minimize the number of concepts used in your example.
745+
746+
We won’t go through the details about best practices and code style for the example code here; please see the [reference style guide](https://beta.p5js.org/contribute/documentation_style_guide/), instead.
747+
725748
### When examples omit setup()
726749

727-
If the `setup()` function is not included, such as in the example above, the code will be automatically wrapped in a `setup()` function with a default 100x100 pixels gray background canvas created. While you will see such examples included in this guide for brevity, it is generally preferred that your example be a complete sketch.
750+
If the `setup()` function is not included, such as in the example above, the code will be automatically wrapped in a `setup()` function with a default 100x100 pixels gray background canvas created. While you may see such examples included in this guide for brevity, it is generally preferred that your example be a complete sketch. This makes it easier for learners to see the intended usage in full context and copy-paste to get a complete runnable example in their own editor.
728751

729-
### Preventing execution of example code with `norender`
752+
### <a id="norender"></a>Preventing execution of example code with `norender`
730753

731754
If you do not want the reference page to run an example's code (i.e., you only want the _code_ to be shown, not its result), follow the `@example` tag with `// META:norender` on the next line:
732755

@@ -757,9 +780,9 @@ font = await loadFont('assets/inconsolata.otf');
757780
```
758781
The above code will load the font file, [/public/assets/inconsolata.otf](https://github.com/processing/p5.js-website/blob/2.0/public/assets/inconsolata.otf), stored in the p5.js-website repo.
759782

760-
### Asset hosting differs from p5 v1
783+
#### Asset hosting differs from p5 v1
761784

762-
Note that this hosting location is a significant change from p5 v1.x where such assets were stored instead in the _p5.js_ repo.
785+
Note that hosting in the _p5.js-website repo_ is a significant change from p5 v1.x where such assets were stored instead in the _p5.js_ repo.
763786

764787
### Re-using existing assets
765788

@@ -1241,15 +1264,13 @@ This will produce _lots_ of warnings, across many files! These do not necessari
12411264
12421265
However, you can look through for any warnings relating to the file _you_ are working on, to see if anything stands out. (e.g. a misspelled tag, mismatching parameter name, or "Unknown content")
12431266
1244-
**Example of a minor issue found by the linter:**
1267+
### Example of a minor issue found by the linter
12451268
12461269
```
12471270
/path/to/p5.js/src/io/p5.TableRow.js
12481271
153:1 warning An explicit parameter named rowID was specified but didn't match inferred information r
12491272
```
12501273
1251-
**Explanation: **
1252-
12531274
The above warning says that there's a problem:
12541275
12551276
* in the file `p5.TableRow.js`
146 KB
Loading

0 commit comments

Comments
 (0)