You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributor_docs/contributing_to_the_p5js_reference.md
+60-39Lines changed: 60 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,8 +45,8 @@ When you look at the source code of p5.js, you will see many lines in the librar
45
45
* A much longer description normally goes here
46
46
*
47
47
* @methodcircle
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.
50
50
* @param{Number}diameter Diameter of circle.
51
51
*
52
52
* @example
@@ -651,56 +651,71 @@ The `@readonly` tag is present on most p5.js variables and is used internally to
651
651
652
652
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.
653
653
654
-

654
+
Here's an example from the documentation of the `fill()` function.
655
+
656
+

655
657
656
658
The relevant `@example` tag to create the above is as follows:
657
659
658
660
```
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
+
* };
670
673
```
671
674
672
675
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.
673
676
674
-
It should declare `setup()` and `draw()`functions as required.
677
+
It should declare a `setup()`function, and `draw()`if required.
675
678
676
679
The example’s canvas should be 100x100 pixels.
677
680
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).
685
682
686
683
### Providing multiple examples
687
684
688
685
You can have multiple examples for one feature. These should be separated by a blank line and a new @example tag for each.
689
686
690
-
Example:
687
+
Example (from `createCanvas()`):
691
688
692
689
```
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.');
* describe('A diagonal line drawn from top-left to bottom-right on a gray background.');
713
+
* }
700
714
```
715
+
701
716
### <aid="inserted-examples"></a>Inserting examples _within_ the description
702
717
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.
704
719
705
720
The <ahref="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)).
706
721
@@ -718,15 +733,23 @@ To add such an example, instead of using an `@example` tag, surround the example
718
733
To make the code-block, surround your example code before and after by three backticks on a newline.
719
734
The opening backticks should be immediately followed by the annotation `js example`. Remember, you should not use an `@example` tag in this case.
720
735
721
-
**Why is this important?**
736
+
Why is this important?
722
737
723
738
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.
724
739
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
+
725
748
### When examples omit setup()
726
749
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.
728
751
729
-
### Preventing execution of example code with `norender`
752
+
### <aid="norender"></a>Preventing execution of example code with `norender`
730
753
731
754
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:
732
755
@@ -757,9 +780,9 @@ font = await loadFont('assets/inconsolata.otf');
757
780
```
758
781
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.
759
782
760
-
### Asset hosting differs from p5 v1
783
+
####Asset hosting differs from p5 v1
761
784
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.
763
786
764
787
### Re-using existing assets
765
788
@@ -1241,15 +1264,13 @@ This will produce _lots_ of warnings, across many files! These do not necessari
1241
1264
1242
1265
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")
1243
1266
1244
-
**Example of a minor issue found by the linter:**
1267
+
### Example of a minor issue found by the linter
1245
1268
1246
1269
```
1247
1270
/path/to/p5.js/src/io/p5.TableRow.js
1248
1271
153:1 warning An explicit parameter named rowID was specified but didn't match inferred information r
0 commit comments