p5 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src → dist}/accessibility/color_namer.js +48 -3
- package/{src → dist}/accessibility/describe.js +12 -2
- package/{src → dist}/accessibility/gridOutput.js +2 -2
- package/dist/accessibility/index.js +60 -0
- package/{src → dist}/accessibility/outputs.js +23 -2
- package/{src → dist}/accessibility/textOutput.js +2 -2
- package/dist/app.js +120 -0
- package/{src → dist}/color/color_conversion.js +48 -10
- package/{src → dist}/color/color_spaces/hsb.js +3 -1
- package/dist/color/creating_reading.js +3 -0
- package/dist/color/index.js +13 -0
- package/dist/color/p5.Color.culori.js +1 -0
- package/dist/color/p5.Color.js +3 -0
- package/{src → dist}/color/setting.js +9 -6
- package/{src/core/constants.js → dist/constants-tYr0tCl8.js} +284 -132
- package/{src → dist}/core/States.js +3 -1
- package/dist/core/constants.js +1 -0
- package/{src → dist}/core/environment.js +12 -10
- package/{src → dist}/core/friendly_errors/browser_errors.js +1 -1
- package/{src → dist}/core/friendly_errors/fes_core.js +14 -44
- package/{src → dist}/core/friendly_errors/file_errors.js +6 -3
- package/dist/core/friendly_errors/index.js +23 -0
- package/dist/core/friendly_errors/param_validator.js +5421 -0
- package/{src → dist}/core/friendly_errors/sketch_reader.js +50 -4
- package/{src → dist}/core/friendly_errors/sketch_verifier.js +6 -6
- package/{src → dist}/core/friendly_errors/stacktrace.js +3 -5
- package/{src → dist}/core/friendly_errors/validate_params.js +50 -41
- package/{src → dist}/core/helpers.js +9 -6
- package/dist/core/init.js +105 -0
- package/dist/core/internationalization.js +302 -0
- package/dist/core/legacy.js +73 -0
- package/dist/core/main.js +44 -0
- package/dist/core/noop.js +3 -0
- package/dist/core/p5.Graphics.js +40 -0
- package/dist/core/p5.Renderer.js +11 -0
- package/dist/core/p5.Renderer2D.js +44 -0
- package/dist/core/reference.js +1 -0
- package/dist/core/rendering.js +40 -0
- package/{src → dist}/core/structure.js +3 -3
- package/{src → dist}/core/transform.js +2 -2
- package/{src/color/creating_reading.js → dist/creating_reading-Cr8L2Jnm.js} +841 -13
- package/{src → dist}/data/index.js +3 -1
- package/{src → dist}/data/local_storage.js +2 -8
- package/{src → dist}/dom/dom.js +19 -13
- package/dist/dom/index.js +18 -0
- package/{src → dist}/dom/p5.Element.js +14 -12
- package/{src → dist}/dom/p5.File.js +4 -4
- package/{src → dist}/dom/p5.MediaElement.js +10 -4
- package/{src → dist}/events/acceleration.js +26 -26
- package/{src → dist}/events/index.js +3 -1
- package/{src → dist}/events/keyboard.js +14 -12
- package/{src → dist}/events/pointer.js +16 -17
- package/dist/image/const.js +9 -0
- package/{src → dist}/image/filterRenderer2D.js +57 -37
- package/{src → dist}/image/filters.js +1 -3
- package/dist/image/image.js +40 -0
- package/dist/image/index.js +51 -0
- package/dist/image/loading_displaying.js +40 -0
- package/dist/image/p5.Image.js +11 -0
- package/{src → dist}/image/pixels.js +5 -3
- package/{src → dist}/io/csv.js +72 -70
- package/dist/io/files.js +40 -0
- package/dist/io/index.js +51 -0
- package/{src → dist}/io/p5.Table.js +6 -6
- package/{src → dist}/io/p5.TableRow.js +5 -6
- package/{src → dist}/io/p5.XML.js +2 -5
- package/{src → dist}/io/utilities.js +1 -1
- package/{src/core/p5.Renderer2D.js → dist/main-CAxvgiOV.js} +738 -57
- package/{src → dist}/math/Matrices/Matrix.js +10 -8
- package/{src → dist}/math/Matrices/MatrixInterface.js +5 -3
- package/{src → dist}/math/Matrices/MatrixNumjs.js +12 -26
- package/{src → dist}/math/calculation.js +2 -2
- package/{src → dist}/math/index.js +6 -3
- package/{src → dist}/math/math.js +3 -3
- package/{src → dist}/math/noise.js +2 -2
- package/{src → dist}/math/p5.Matrix.js +7 -4
- package/{src → dist}/math/p5.Vector.js +6 -6
- package/{src → dist}/math/random.js +2 -2
- package/{src → dist}/math/trigonometry.js +16 -15
- package/{src/image/p5.Image.js → dist/p5.Renderer-Swjl9HQO.js} +393 -22
- package/dist/rendering-B5TRR7aY.js +24960 -0
- package/{src → dist}/shape/2d_primitives.js +18 -17
- package/{src → dist}/shape/attributes.js +18 -17
- package/{src → dist}/shape/curves.js +2 -2
- package/{src → dist}/shape/custom_shapes.js +44 -64
- package/{src → dist}/shape/index.js +10 -2
- package/{src → dist}/shape/vertex.js +2 -3
- package/dist/type/index.js +25 -0
- package/{src → dist}/type/lib/Typr.js +76 -94
- package/{src → dist}/type/p5.Font.js +37 -63
- package/{src → dist}/type/textCore.js +35 -58
- package/{src → dist}/type/unicodeRanges.js +3 -1
- package/{src → dist}/utilities/conversion.js +2 -2
- package/{src → dist}/utilities/index.js +3 -1
- package/{src → dist}/utilities/time_date.js +6 -7
- package/{src → dist}/utilities/utility_functions.js +2 -2
- package/dist/webgl/3d_primitives.js +40 -0
- package/{src → dist}/webgl/GeometryBufferCache.js +3 -1
- package/{src → dist}/webgl/GeometryBuilder.js +12 -8
- package/{src → dist}/webgl/ShaderGenerator.js +79 -82
- package/{src → dist}/webgl/ShapeBuilder.js +26 -23
- package/dist/webgl/index.js +76 -0
- package/{src → dist}/webgl/interaction.js +7 -6
- package/dist/webgl/light.js +40 -0
- package/{src → dist}/webgl/loading.js +45 -12
- package/dist/webgl/material.js +40 -0
- package/dist/webgl/p5.Camera.js +40 -0
- package/{src → dist}/webgl/p5.DataArray.js +3 -5
- package/dist/webgl/p5.Framebuffer.js +40 -0
- package/{src → dist}/webgl/p5.Geometry.js +12 -15
- package/{src → dist}/webgl/p5.Quat.js +5 -4
- package/{src → dist}/webgl/p5.RenderBuffer.js +2 -3
- package/dist/webgl/p5.RendererGL.js +40 -0
- package/dist/webgl/p5.Shader.js +40 -0
- package/dist/webgl/p5.Texture.js +40 -0
- package/{src → dist}/webgl/text.js +78 -38
- package/lib/p5.esm.js +296 -194
- package/lib/p5.js +296 -194
- package/lib/p5.min.js +1 -1
- package/package.json +17 -17
- package/translations/dev.js +6 -6
- package/translations/index.js +1 -1
- package/types/accessibility/color_namer.d.ts +8 -0
- package/types/accessibility/describe.d.ts +184 -0
- package/types/accessibility/gridOutput.d.ts +8 -0
- package/types/accessibility/outputs.d.ts +235 -0
- package/types/accessibility/textOutput.d.ts +8 -0
- package/types/color/color_conversion.d.ts +47 -0
- package/types/color/creating_reading.d.ts +1348 -0
- package/types/color/p5.Color.d.ts +1070 -0
- package/types/color/setting.d.ts +2085 -0
- package/types/core/constants.d.ts +341 -0
- package/types/core/environment.d.ts +668 -0
- package/types/core/friendly_errors/fes_core.d.ts +8 -0
- package/types/core/friendly_errors/file_errors.d.ts +8 -0
- package/types/core/friendly_errors/param_validator.d.ts +30 -0
- package/types/core/friendly_errors/sketch_reader.d.ts +8 -0
- package/types/core/friendly_errors/stacktrace.d.ts +11 -0
- package/types/core/friendly_errors/validate_params.d.ts +8 -0
- package/types/core/helpers.d.ts +8 -0
- package/types/core/legacy.d.ts +8 -0
- package/types/core/main.d.ts +5996 -0
- package/types/core/p5.Graphics.d.ts +484 -0
- package/types/core/p5.Renderer.d.ts +14 -0
- package/types/core/reference.d.ts +8 -0
- package/types/core/rendering.d.ts +481 -0
- package/types/core/structure.d.ts +492 -0
- package/types/core/transform.d.ts +1638 -0
- package/types/data/local_storage.d.ts +323 -0
- package/types/dom/dom.d.ts +1295 -0
- package/types/dom/p5.Element.d.ts +2011 -0
- package/types/dom/p5.File.d.ts +13 -0
- package/types/dom/p5.MediaElement.d.ts +1249 -0
- package/types/events/acceleration.d.ts +193 -0
- package/types/events/keyboard.d.ts +499 -0
- package/types/events/pointer.d.ts +782 -0
- package/types/global.d.ts +5542 -0
- package/types/image/filterRenderer2D.d.ts +54 -0
- package/types/image/image.d.ts +326 -0
- package/types/image/loading_displaying.d.ts +580 -0
- package/types/image/p5.Image.d.ts +5882 -0
- package/types/image/pixels.d.ts +832 -0
- package/types/io/files.d.ts +1447 -0
- package/types/io/p5.Table.d.ts +1247 -0
- package/types/io/p5.TableRow.d.ts +343 -0
- package/types/io/p5.XML.d.ts +1188 -0
- package/types/math/Matrices/Matrix.d.ts +1029 -0
- package/types/math/Matrices/MatrixNumjs.d.ts +8 -0
- package/types/math/calculation.d.ts +923 -0
- package/types/math/math.d.ts +90 -0
- package/types/math/noise.d.ts +311 -0
- package/types/math/p5.Matrix.d.ts +8 -0
- package/types/math/p5.Vector.d.ts +3416 -0
- package/types/math/random.d.ts +267 -0
- package/types/math/trigonometry.d.ts +663 -0
- package/types/p5.d.ts +6663 -0
- package/types/shape/2d_primitives.d.ts +1033 -0
- package/types/shape/attributes.d.ts +466 -0
- package/types/shape/curves.d.ts +740 -0
- package/types/shape/custom_shapes.d.ts +888 -0
- package/types/shape/vertex.d.ts +1141 -0
- package/types/type/p5.Font.d.ts +575 -0
- package/types/type/textCore.d.ts +1198 -0
- package/types/utilities/conversion.d.ts +894 -0
- package/types/utilities/time_date.d.ts +295 -0
- package/types/utilities/utility_functions.d.ts +587 -0
- package/types/webgl/3d_primitives.d.ts +1432 -0
- package/types/webgl/ShaderGenerator.d.ts +8 -0
- package/types/webgl/interaction.d.ts +371 -0
- package/types/webgl/light.d.ts +1184 -0
- package/types/webgl/loading.d.ts +481 -0
- package/types/webgl/material.d.ts +2656 -0
- package/types/webgl/p5.Camera.d.ts +3023 -0
- package/types/webgl/p5.DataArray.d.ts +61 -0
- package/types/webgl/p5.Framebuffer.d.ts +760 -0
- package/types/webgl/p5.Geometry.d.ts +1191 -0
- package/types/webgl/p5.Quat.d.ts +45 -0
- package/types/webgl/p5.RendererGL.d.ts +234 -0
- package/types/webgl/p5.Shader.d.ts +660 -0
- package/types/webgl/p5.Texture.d.ts +61 -0
- package/types/webgl/text.d.ts +74 -0
- package/src/README.md +0 -27
- package/src/accessibility/index.js +0 -13
- package/src/app.js +0 -61
- package/src/color/index.js +0 -9
- package/src/color/p5.Color.culori.js +0 -66
- package/src/color/p5.Color.js +0 -851
- package/src/core/README.md +0 -91
- package/src/core/friendly_errors/index.js +0 -13
- package/src/core/friendly_errors/param_validator.js +0 -561
- package/src/core/init.js +0 -58
- package/src/core/internationalization.js +0 -195
- package/src/core/legacy.js +0 -29
- package/src/core/main.js +0 -689
- package/src/core/noop.js +0 -1
- package/src/core/p5.Graphics.js +0 -696
- package/src/core/p5.Renderer.js +0 -408
- package/src/core/reference.js +0 -2060
- package/src/core/rendering.js +0 -697
- package/src/dom/index.js +0 -11
- package/src/image/const.js +0 -6
- package/src/image/image.js +0 -731
- package/src/image/index.js +0 -15
- package/src/image/loading_displaying.js +0 -1431
- package/src/io/files.js +0 -2210
- package/src/io/index.js +0 -11
- package/src/math/README.md +0 -40
- package/src/type/index.js +0 -9
- package/src/webgl/3d_primitives.js +0 -2741
- package/src/webgl/index.js +0 -37
- package/src/webgl/light.js +0 -1851
- package/src/webgl/material.js +0 -3854
- package/src/webgl/p5.Camera.js +0 -4010
- package/src/webgl/p5.Framebuffer.js +0 -1865
- package/src/webgl/p5.RendererGL.js +0 -2867
- package/src/webgl/p5.Shader.js +0 -1505
- package/src/webgl/p5.Texture.js +0 -541
- package/src/webgl/shaders/basic.frag +0 -6
- package/src/webgl/shaders/filters/base.frag +0 -22
- package/src/webgl/shaders/filters/base.vert +0 -19
- package/src/webgl/shaders/filters/blur.frag +0 -60
- package/src/webgl/shaders/filters/default.vert +0 -18
- package/src/webgl/shaders/filters/dilate.frag +0 -39
- package/src/webgl/shaders/filters/erode.frag +0 -39
- package/src/webgl/shaders/filters/gray.frag +0 -16
- package/src/webgl/shaders/filters/invert.frag +0 -15
- package/src/webgl/shaders/filters/opaque.frag +0 -12
- package/src/webgl/shaders/filters/posterize.frag +0 -29
- package/src/webgl/shaders/filters/threshold.frag +0 -23
- package/src/webgl/shaders/font.frag +0 -216
- package/src/webgl/shaders/font.vert +0 -44
- package/src/webgl/shaders/imageLight.vert +0 -33
- package/src/webgl/shaders/imageLightDiffused.frag +0 -82
- package/src/webgl/shaders/imageLightSpecular.frag +0 -134
- package/src/webgl/shaders/light.vert +0 -37
- package/src/webgl/shaders/light_texture.frag +0 -26
- package/src/webgl/shaders/lighting.glsl +0 -227
- package/src/webgl/shaders/line.frag +0 -74
- package/src/webgl/shaders/line.vert +0 -294
- package/src/webgl/shaders/normal.frag +0 -6
- package/src/webgl/shaders/normal.vert +0 -72
- package/src/webgl/shaders/phong.frag +0 -84
- package/src/webgl/shaders/phong.vert +0 -87
- package/src/webgl/shaders/point.frag +0 -29
- package/src/webgl/shaders/point.vert +0 -19
- package/src/webgl/shaders/sphereMapping.frag +0 -26
- package/src/webgl/shaders/webgl2Compatibility.glsl +0 -34
package/src/core/reference.js
DELETED
|
@@ -1,2060 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module Foundation
|
|
3
|
-
* @submodule Foundation
|
|
4
|
-
* @for p5
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Declares a new variable.
|
|
9
|
-
*
|
|
10
|
-
* A variable is a container for a value. For example, a variable might
|
|
11
|
-
* contain a creature's x-coordinate as a `Number` or its name as a
|
|
12
|
-
* `String`. Variables can change value by reassigning them as follows:
|
|
13
|
-
*
|
|
14
|
-
* ```js
|
|
15
|
-
* // Declare the variable x and assign it the value 10.
|
|
16
|
-
* let x = 10;
|
|
17
|
-
*
|
|
18
|
-
* // Reassign x to 50.
|
|
19
|
-
* x = 50;
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* Variables have block scope. When a variable is declared between curly
|
|
23
|
-
* braces `{}`, it only exists within the block defined by those braces. For
|
|
24
|
-
* example, the following code would throw a `ReferenceError` because `x` is
|
|
25
|
-
* declared within the `setup()` function's block:
|
|
26
|
-
*
|
|
27
|
-
* ```js
|
|
28
|
-
* function setup() {
|
|
29
|
-
* createCanvas(100, 100);
|
|
30
|
-
*
|
|
31
|
-
* let x = 50;
|
|
32
|
-
* }
|
|
33
|
-
*
|
|
34
|
-
* function draw() {
|
|
35
|
-
* background(200);
|
|
36
|
-
*
|
|
37
|
-
* // x was declared in setup(), so it can't be referenced here.
|
|
38
|
-
* circle(x, 50, 20);
|
|
39
|
-
* }
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* Variables declared outside of all curly braces `{}` are in the global
|
|
43
|
-
* scope. A variable that's in the global scope can be used and changed
|
|
44
|
-
* anywhere in a sketch:
|
|
45
|
-
*
|
|
46
|
-
* ```js
|
|
47
|
-
* let x = 50;
|
|
48
|
-
*
|
|
49
|
-
* function setup() {
|
|
50
|
-
* createCanvas(100, 100);
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* function draw() {
|
|
54
|
-
* background(200);
|
|
55
|
-
*
|
|
56
|
-
* // Change the value of x.
|
|
57
|
-
* x += 10;
|
|
58
|
-
*
|
|
59
|
-
* circle(x, 50, 20);
|
|
60
|
-
* }
|
|
61
|
-
* ```
|
|
62
|
-
*
|
|
63
|
-
* @property let
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* <div>
|
|
67
|
-
* <code>
|
|
68
|
-
* function setup() {
|
|
69
|
-
* createCanvas(100, 100);
|
|
70
|
-
*
|
|
71
|
-
* background(220);
|
|
72
|
-
*
|
|
73
|
-
* // Style the text.
|
|
74
|
-
* textAlign(CENTER);
|
|
75
|
-
* textSize(16);
|
|
76
|
-
*
|
|
77
|
-
* // Create the message variable.
|
|
78
|
-
* let message = 'Hello, 🌍!';
|
|
79
|
-
*
|
|
80
|
-
* // Display the message.
|
|
81
|
-
* text(message, 50, 50);
|
|
82
|
-
*
|
|
83
|
-
* describe('The text "Hello, 🌍!" written on a gray background.');
|
|
84
|
-
* }
|
|
85
|
-
* </code>
|
|
86
|
-
* </div>
|
|
87
|
-
*
|
|
88
|
-
* <div>
|
|
89
|
-
* <code>
|
|
90
|
-
* let x = 0;
|
|
91
|
-
*
|
|
92
|
-
* function setup() {
|
|
93
|
-
* createCanvas(100, 100);
|
|
94
|
-
*
|
|
95
|
-
* describe('A white circle moves from left to right against a gray background.');
|
|
96
|
-
* }
|
|
97
|
-
*
|
|
98
|
-
* function draw() {
|
|
99
|
-
* background(220);
|
|
100
|
-
*
|
|
101
|
-
* // Change the value of x.
|
|
102
|
-
* x += 1;
|
|
103
|
-
*
|
|
104
|
-
* circle(x, 50, 20);
|
|
105
|
-
* }
|
|
106
|
-
* </code>
|
|
107
|
-
* </div>
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* A way to choose whether to run a block of code.
|
|
112
|
-
*
|
|
113
|
-
* `if` statements are helpful for running a block of code based on a
|
|
114
|
-
* condition. For example, an `if` statement makes it easy to express the
|
|
115
|
-
* idea "Draw a circle if the mouse is pressed.":
|
|
116
|
-
*
|
|
117
|
-
* ```js
|
|
118
|
-
* if (mouseIsPressed === true) {
|
|
119
|
-
* circle(mouseX, mouseY, 20);
|
|
120
|
-
* }
|
|
121
|
-
* ```
|
|
122
|
-
*
|
|
123
|
-
* The statement header begins with the keyword `if`. The expression in
|
|
124
|
-
* parentheses `mouseIsPressed === true` is a `Boolean` expression that's
|
|
125
|
-
* either `true` or `false`. The code between the curly braces `{}` is the if
|
|
126
|
-
* statement's body. The body only runs if the `Boolean` expression is `true`.
|
|
127
|
-
*
|
|
128
|
-
* The <a href="#/p5/mouseIsPressed">mouseIsPressed</a> system variable is
|
|
129
|
-
* always `true` or `false`, so the code snippet above could also be written
|
|
130
|
-
* as follows:
|
|
131
|
-
*
|
|
132
|
-
* ```js
|
|
133
|
-
* if (mouseIsPressed) {
|
|
134
|
-
* circle(mouseX, mouseY, 20);
|
|
135
|
-
* }
|
|
136
|
-
* ```
|
|
137
|
-
*
|
|
138
|
-
* An `if`-`else` statement adds a block of code that runs if the `Boolean`
|
|
139
|
-
* expression is `false`. For example, here's an `if`-`else` statement that
|
|
140
|
-
* expresses the idea "Draw a circle if the mouse is pressed. Otherwise,
|
|
141
|
-
* display a message.":
|
|
142
|
-
*
|
|
143
|
-
* ```js
|
|
144
|
-
* if (mouseIsPressed === true) {
|
|
145
|
-
* // When true.
|
|
146
|
-
* circle(mouseX, mouseY, 20);
|
|
147
|
-
* } else {
|
|
148
|
-
* // When false.
|
|
149
|
-
* text('Click me!', 50, 50);
|
|
150
|
-
* }
|
|
151
|
-
* ```
|
|
152
|
-
*
|
|
153
|
-
* There are two possible paths, or branches, in this code snippet. The
|
|
154
|
-
* program must follow one branch or the other.
|
|
155
|
-
*
|
|
156
|
-
* An `else`-`if` statement makes it possible to add more branches.
|
|
157
|
-
* `else`-`if` statements run different blocks of code under different
|
|
158
|
-
* conditions. For example, an `else`-`if` statement makes it easy to express
|
|
159
|
-
* the idea "If the mouse is on the left, paint the canvas white. If the mouse
|
|
160
|
-
* is in the middle, paint the canvas gray. Otherwise, paint the canvas
|
|
161
|
-
* black.":
|
|
162
|
-
*
|
|
163
|
-
* ```js
|
|
164
|
-
* if (mouseX < 33) {
|
|
165
|
-
* background(255);
|
|
166
|
-
* } else if (mouseX < 67) {
|
|
167
|
-
* background(200);
|
|
168
|
-
* } else {
|
|
169
|
-
* background(0);
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
172
|
-
*
|
|
173
|
-
* `if` statements can add as many `else`-`if` statements as needed. However,
|
|
174
|
-
* there can only be one `else` statement and it must be last.
|
|
175
|
-
*
|
|
176
|
-
* `if` statements can also check for multiple conditions at once. For
|
|
177
|
-
* example, the `Boolean` operator `&&` (AND) checks whether two expressions
|
|
178
|
-
* are both `true`:
|
|
179
|
-
*
|
|
180
|
-
* ```js
|
|
181
|
-
* if (keyIsPressed === true && key === 'p') {
|
|
182
|
-
* text('You pressed the "p" key!', 50, 50);
|
|
183
|
-
* }
|
|
184
|
-
* ```
|
|
185
|
-
*
|
|
186
|
-
* If the user is pressing a key AND that key is `'p'`, then a message will
|
|
187
|
-
* display.
|
|
188
|
-
*
|
|
189
|
-
* The `Boolean` operator `||` (OR) checks whether at least one of two
|
|
190
|
-
* expressions is `true`:
|
|
191
|
-
*
|
|
192
|
-
* ```js
|
|
193
|
-
* if (keyIsPressed === true || mouseIsPressed === true) {
|
|
194
|
-
* text('You did something!', 50, 50);
|
|
195
|
-
* }
|
|
196
|
-
* ```
|
|
197
|
-
*
|
|
198
|
-
* If the user presses a key, or presses a mouse button, or both, then a
|
|
199
|
-
* message will display.
|
|
200
|
-
*
|
|
201
|
-
* The body of an `if` statement can contain another `if` statement. This is
|
|
202
|
-
* called a "nested `if` statement." For example, nested `if` statements make
|
|
203
|
-
* it easy to express the idea "If a key is pressed, then check if the key is
|
|
204
|
-
* `'r'`. If it is, then set the fill to red.":
|
|
205
|
-
*
|
|
206
|
-
* ```js
|
|
207
|
-
* if (keyIsPressed === true) {
|
|
208
|
-
* if (key === 'r') {
|
|
209
|
-
* fill('red');
|
|
210
|
-
* }
|
|
211
|
-
* }
|
|
212
|
-
* ```
|
|
213
|
-
*
|
|
214
|
-
* See <a href="#/p5/Boolean">Boolean</a> and <a href="#/p5/Number">Number</a>
|
|
215
|
-
* to learn more about these data types and the operations they support.
|
|
216
|
-
*
|
|
217
|
-
* @property if
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* <div>
|
|
221
|
-
* <code>
|
|
222
|
-
* // Click the mouse to show the circle.
|
|
223
|
-
*
|
|
224
|
-
* function setup() {
|
|
225
|
-
* createCanvas(100, 100);
|
|
226
|
-
*
|
|
227
|
-
* describe(
|
|
228
|
-
* 'A white circle on a gray background. The circle follows the mouse user clicks on the canvas.'
|
|
229
|
-
* );
|
|
230
|
-
* }
|
|
231
|
-
*
|
|
232
|
-
* function draw() {
|
|
233
|
-
* background(200);
|
|
234
|
-
*
|
|
235
|
-
* if (mouseIsPressed === true) {
|
|
236
|
-
* circle(mouseX, mouseY, 20);
|
|
237
|
-
* }
|
|
238
|
-
* }
|
|
239
|
-
* </code>
|
|
240
|
-
* </div>
|
|
241
|
-
*
|
|
242
|
-
* <div>
|
|
243
|
-
* <code>
|
|
244
|
-
* // Click the mouse to show different shapes.
|
|
245
|
-
*
|
|
246
|
-
* function setup() {
|
|
247
|
-
* createCanvas(100, 100);
|
|
248
|
-
*
|
|
249
|
-
* describe(
|
|
250
|
-
* 'A white ellipse on a gray background. The ellipse becomes a circle when the user presses the mouse.'
|
|
251
|
-
* );
|
|
252
|
-
* }
|
|
253
|
-
*
|
|
254
|
-
* function draw() {
|
|
255
|
-
* background(200);
|
|
256
|
-
*
|
|
257
|
-
* if (mouseIsPressed === true) {
|
|
258
|
-
* circle(50, 50, 20);
|
|
259
|
-
* } else {
|
|
260
|
-
* ellipse(50, 50, 20, 50);
|
|
261
|
-
* }
|
|
262
|
-
* }
|
|
263
|
-
* </code>
|
|
264
|
-
* </div>
|
|
265
|
-
*
|
|
266
|
-
* <div>
|
|
267
|
-
* <code>
|
|
268
|
-
* // Move the mouse to change the background color.
|
|
269
|
-
*
|
|
270
|
-
* function setup() {
|
|
271
|
-
* createCanvas(100, 100);
|
|
272
|
-
*
|
|
273
|
-
* describe(
|
|
274
|
-
* 'A square changes color from white to black as the user moves the mouse from left to right.'
|
|
275
|
-
* );
|
|
276
|
-
* }
|
|
277
|
-
*
|
|
278
|
-
* function draw() {
|
|
279
|
-
* if (mouseX < 33) {
|
|
280
|
-
* background(255);
|
|
281
|
-
* } else if (mouseX < 67) {
|
|
282
|
-
* background(200);
|
|
283
|
-
* } else {
|
|
284
|
-
* background(0);
|
|
285
|
-
* }
|
|
286
|
-
* }
|
|
287
|
-
* </code>
|
|
288
|
-
* </div>
|
|
289
|
-
*
|
|
290
|
-
* <div>
|
|
291
|
-
* <code>
|
|
292
|
-
* // Click on the canvas to begin detecting key presses.
|
|
293
|
-
*
|
|
294
|
-
* function setup() {
|
|
295
|
-
* createCanvas(100, 100);
|
|
296
|
-
*
|
|
297
|
-
* describe(
|
|
298
|
-
* 'A white circle drawn on a gray background. The circle changes color to red when the user presses the "r" key.'
|
|
299
|
-
* );
|
|
300
|
-
* }
|
|
301
|
-
*
|
|
302
|
-
* function draw() {
|
|
303
|
-
* background(200);
|
|
304
|
-
*
|
|
305
|
-
* if (keyIsPressed === true) {
|
|
306
|
-
* if (key === 'r') {
|
|
307
|
-
* fill('red');
|
|
308
|
-
* }
|
|
309
|
-
* }
|
|
310
|
-
*
|
|
311
|
-
* circle(50, 50, 40);
|
|
312
|
-
* }
|
|
313
|
-
* </code>
|
|
314
|
-
* </div>
|
|
315
|
-
*/
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* A named group of statements.
|
|
319
|
-
*
|
|
320
|
-
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions" target="_blank">Functions</a>
|
|
321
|
-
* help with organizing and reusing code. For example, functions make it easy
|
|
322
|
-
* to express the idea "Draw a flower.":
|
|
323
|
-
*
|
|
324
|
-
* ```js
|
|
325
|
-
* function drawFlower() {
|
|
326
|
-
* // Style the text.
|
|
327
|
-
* textAlign(CENTER, CENTER);
|
|
328
|
-
* textSize(20);
|
|
329
|
-
*
|
|
330
|
-
* // Draw a flower emoji.
|
|
331
|
-
* text('🌸', 50, 50);
|
|
332
|
-
* }
|
|
333
|
-
* ```
|
|
334
|
-
*
|
|
335
|
-
* The function header begins with the keyword `function`. The function's
|
|
336
|
-
* name, `drawFlower`, is followed by parentheses `()` and curly braces `{}`.
|
|
337
|
-
* The code between the curly braces is called the function's body. The
|
|
338
|
-
* function's body runs when the function is called like so:
|
|
339
|
-
*
|
|
340
|
-
* ```js
|
|
341
|
-
* drawFlower();
|
|
342
|
-
* ```
|
|
343
|
-
*
|
|
344
|
-
* Functions can accept inputs by adding parameters to their headers.
|
|
345
|
-
* Parameters are placeholders for values that will be provided when the
|
|
346
|
-
* function is called. For example, the `drawFlower()` function could include
|
|
347
|
-
* a parameter for the flower's size:
|
|
348
|
-
*
|
|
349
|
-
* ```js
|
|
350
|
-
* function drawFlower(size) {
|
|
351
|
-
* // Style the text.
|
|
352
|
-
* textAlign(CENTER, CENTER);
|
|
353
|
-
*
|
|
354
|
-
* // Use the size parameter.
|
|
355
|
-
* textSize(size);
|
|
356
|
-
*
|
|
357
|
-
* // Draw a flower emoji.
|
|
358
|
-
* text('🌸', 50, 50);
|
|
359
|
-
* }
|
|
360
|
-
* ```
|
|
361
|
-
*
|
|
362
|
-
* Parameters are part of the function's declaration. Arguments are provided
|
|
363
|
-
* by the code that calls a function. When a function is called, arguments are
|
|
364
|
-
* assigned to parameters:
|
|
365
|
-
*
|
|
366
|
-
* ```js
|
|
367
|
-
* // The argument 20 is assigned to the parameter size.
|
|
368
|
-
* drawFlower(20);
|
|
369
|
-
* ```
|
|
370
|
-
*
|
|
371
|
-
* Functions can have multiple parameters separated by commas. Parameters
|
|
372
|
-
* can have any type. For example, the `drawFlower()` function could accept
|
|
373
|
-
* `Number` parameters for the flower's x- and y-coordinates along with its
|
|
374
|
-
* size:
|
|
375
|
-
*
|
|
376
|
-
* ```js
|
|
377
|
-
* function drawFlower(x, y, size) {
|
|
378
|
-
* // Style the text.
|
|
379
|
-
* textAlign(CENTER, CENTER);
|
|
380
|
-
*
|
|
381
|
-
* // Use the size parameter.
|
|
382
|
-
* textSize(size);
|
|
383
|
-
*
|
|
384
|
-
* // Draw a flower emoji.
|
|
385
|
-
* // Use the x and y parameters.
|
|
386
|
-
* text('🌸', x, y);
|
|
387
|
-
* }
|
|
388
|
-
* ```
|
|
389
|
-
*
|
|
390
|
-
* Functions can also produce outputs by adding a `return` statement:
|
|
391
|
-
*
|
|
392
|
-
* ```js
|
|
393
|
-
* function double(x) {
|
|
394
|
-
* let answer = 2 * x;
|
|
395
|
-
* return answer;
|
|
396
|
-
* }
|
|
397
|
-
* ```
|
|
398
|
-
*
|
|
399
|
-
* The expression following `return` can produce an output that's used
|
|
400
|
-
* elsewhere. For example, the output of the `double()` function can be
|
|
401
|
-
* assigned to a variable:
|
|
402
|
-
*
|
|
403
|
-
* ```js
|
|
404
|
-
* let six = double(3);
|
|
405
|
-
* text(`3 x 2 = ${six}`, 50, 50);
|
|
406
|
-
* ```
|
|
407
|
-
*
|
|
408
|
-
* @property function
|
|
409
|
-
*
|
|
410
|
-
* @example
|
|
411
|
-
* <div>
|
|
412
|
-
* <code>
|
|
413
|
-
* function setup() {
|
|
414
|
-
* createCanvas(100, 100);
|
|
415
|
-
*
|
|
416
|
-
* describe('A pink flower on a gray background.');
|
|
417
|
-
* }
|
|
418
|
-
*
|
|
419
|
-
* function draw() {
|
|
420
|
-
* background(200);
|
|
421
|
-
*
|
|
422
|
-
* // Call the drawFlower() function.
|
|
423
|
-
* drawFlower();
|
|
424
|
-
* }
|
|
425
|
-
*
|
|
426
|
-
* // Declare a function that draws a flower at the
|
|
427
|
-
* // center of the canvas.
|
|
428
|
-
* function drawFlower() {
|
|
429
|
-
* // Style the text.
|
|
430
|
-
* textAlign(CENTER, CENTER);
|
|
431
|
-
* textSize(20);
|
|
432
|
-
*
|
|
433
|
-
* // Draw a flower emoji.
|
|
434
|
-
* text('🌸', 50, 50);
|
|
435
|
-
* }
|
|
436
|
-
* </code>
|
|
437
|
-
* </div>
|
|
438
|
-
*
|
|
439
|
-
* <div>
|
|
440
|
-
* <code>
|
|
441
|
-
* function setup() {
|
|
442
|
-
* createCanvas(100, 100);
|
|
443
|
-
*
|
|
444
|
-
* describe('A pink flower on a gray background.');
|
|
445
|
-
* }
|
|
446
|
-
*
|
|
447
|
-
* function draw() {
|
|
448
|
-
* background(200);
|
|
449
|
-
*
|
|
450
|
-
* // Call the drawFlower() function and pass values for
|
|
451
|
-
* // its position and size.
|
|
452
|
-
* drawFlower(50, 50, 20);
|
|
453
|
-
* }
|
|
454
|
-
*
|
|
455
|
-
* // Declare a function that draws a flower at the
|
|
456
|
-
* // center of the canvas.
|
|
457
|
-
* function drawFlower(x, y, size) {
|
|
458
|
-
* // Style the text.
|
|
459
|
-
* textAlign(CENTER, CENTER);
|
|
460
|
-
*
|
|
461
|
-
* // Use the size parameter.
|
|
462
|
-
* textSize(size);
|
|
463
|
-
*
|
|
464
|
-
* // Draw a flower emoji.
|
|
465
|
-
* // Use the x and y parameters.
|
|
466
|
-
* text('🌸', x, y);
|
|
467
|
-
* }
|
|
468
|
-
* </code>
|
|
469
|
-
* </div>
|
|
470
|
-
*
|
|
471
|
-
* <div>
|
|
472
|
-
* <code>
|
|
473
|
-
* function setup() {
|
|
474
|
-
* createCanvas(100, 100);
|
|
475
|
-
*
|
|
476
|
-
* describe('The message "Hello, 🌍!" written on a gray background.');
|
|
477
|
-
* }
|
|
478
|
-
*
|
|
479
|
-
* function draw() {
|
|
480
|
-
* background(200);
|
|
481
|
-
*
|
|
482
|
-
* // Create a greeting.
|
|
483
|
-
* let greeting = createGreeting('🌍');
|
|
484
|
-
*
|
|
485
|
-
* // Style the text.
|
|
486
|
-
* textAlign(CENTER, CENTER);
|
|
487
|
-
* textSize(16);
|
|
488
|
-
*
|
|
489
|
-
* // Display the greeting.
|
|
490
|
-
* text(greeting, 50, 50);
|
|
491
|
-
* }
|
|
492
|
-
*
|
|
493
|
-
* // Return a string with a personalized greeting.
|
|
494
|
-
* function createGreeting(name) {
|
|
495
|
-
* let message = `Hello, ${name}!`;
|
|
496
|
-
* return message;
|
|
497
|
-
* }
|
|
498
|
-
* </code>
|
|
499
|
-
* </div>
|
|
500
|
-
*/
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
* A value that's either `true` or `false`.
|
|
504
|
-
*
|
|
505
|
-
* `Boolean` values help to make decisions in code. They appear any time a
|
|
506
|
-
* logical condition is checked. For example, the condition
|
|
507
|
-
* "Is a mouse button being pressed?" must be either `true` or
|
|
508
|
-
* `false`:
|
|
509
|
-
*
|
|
510
|
-
* ```js
|
|
511
|
-
* // If the user presses the mouse, draw a circle at
|
|
512
|
-
* // the mouse's location.
|
|
513
|
-
* if (mouseIsPressed === true) {
|
|
514
|
-
* circle(mouseX, mouseY, 20);
|
|
515
|
-
* }
|
|
516
|
-
* ```
|
|
517
|
-
*
|
|
518
|
-
* The `if` statement checks whether
|
|
519
|
-
* <a href="#/p5/mouseIsPressed">mouseIsPressed</a> is `true` and draws a
|
|
520
|
-
* circle if it is. `Boolean` expressions such as `mouseIsPressed === true`
|
|
521
|
-
* evaluate to one of the two possible `Boolean` values: `true` or `false`.
|
|
522
|
-
*
|
|
523
|
-
* The `===` operator (EQUAL) checks whether two values are equal. If they
|
|
524
|
-
* are, the expression evaluates to `true`. Otherwise, it evaluates to
|
|
525
|
-
* `false`.
|
|
526
|
-
*
|
|
527
|
-
* Note: There's also a `==` operator with two `=` instead of three. Don't use
|
|
528
|
-
* it.
|
|
529
|
-
*
|
|
530
|
-
* The <a href="#/p5/mouseIsPressed">mouseIsPressed</a> system variable is
|
|
531
|
-
* always `true` or `false`, so the code snippet above could also be written
|
|
532
|
-
* as follows:
|
|
533
|
-
*
|
|
534
|
-
* ```js
|
|
535
|
-
* if (mouseIsPressed) {
|
|
536
|
-
* circle(mouseX, mouseY, 20);
|
|
537
|
-
* }
|
|
538
|
-
* ```
|
|
539
|
-
*
|
|
540
|
-
* The `!==` operator (NOT EQUAL) checks whether two values are not equal, as
|
|
541
|
-
* in the following example:
|
|
542
|
-
*
|
|
543
|
-
* ```js
|
|
544
|
-
* if (2 + 2 !== 4) {
|
|
545
|
-
* text('War is peace.', 50, 50);
|
|
546
|
-
* }
|
|
547
|
-
* ```
|
|
548
|
-
*
|
|
549
|
-
* Starting from the left, the arithmetic expression `2 + 2` produces the
|
|
550
|
-
* value `4`. The `Boolean` expression `4 !== 4` evaluates to `false` because
|
|
551
|
-
* `4` is equal to itself. As a result, the `if` statement's body is skipped.
|
|
552
|
-
*
|
|
553
|
-
* Note: There's also a `!=` operator with one `=` instead of two. Don't use
|
|
554
|
-
* it.
|
|
555
|
-
*
|
|
556
|
-
* The `Boolean` operator `&&` (AND) checks whether two expressions are both
|
|
557
|
-
* `true`:
|
|
558
|
-
*
|
|
559
|
-
* ```js
|
|
560
|
-
* if (keyIsPressed === true && key === 'p') {
|
|
561
|
-
* text('You pressed the "p" key!', 50, 50);
|
|
562
|
-
* }
|
|
563
|
-
* ```
|
|
564
|
-
*
|
|
565
|
-
* If the user is pressing a key AND that key is `'p'`, then a message will
|
|
566
|
-
* display.
|
|
567
|
-
*
|
|
568
|
-
* The `Boolean` operator `||` (OR) checks whether at least one of two
|
|
569
|
-
* expressions is `true`:
|
|
570
|
-
*
|
|
571
|
-
* ```js
|
|
572
|
-
* if (keyIsPressed === true || mouseIsPressed === true) {
|
|
573
|
-
* text('You did something!', 50, 50);
|
|
574
|
-
* }
|
|
575
|
-
* ```
|
|
576
|
-
*
|
|
577
|
-
* If the user presses a key, or presses a mouse button, or both, then a
|
|
578
|
-
* message will display.
|
|
579
|
-
*
|
|
580
|
-
* The following truth table summarizes a few common scenarios with `&&` and
|
|
581
|
-
* `||`:
|
|
582
|
-
*
|
|
583
|
-
* ```js
|
|
584
|
-
* true && true // true
|
|
585
|
-
* true && false // false
|
|
586
|
-
* false && false // false
|
|
587
|
-
* true || true // true
|
|
588
|
-
* true || false // true
|
|
589
|
-
* false || false // false
|
|
590
|
-
* ```
|
|
591
|
-
*
|
|
592
|
-
* The relational operators `>`, `<`, `>=`, and `<=` also produce `Boolean`
|
|
593
|
-
* values:
|
|
594
|
-
*
|
|
595
|
-
* ```js
|
|
596
|
-
* 2 > 1 // true
|
|
597
|
-
* 2 < 1 // false
|
|
598
|
-
* 2 >= 2 // true
|
|
599
|
-
* 2 <= 2 // true
|
|
600
|
-
* ```
|
|
601
|
-
*
|
|
602
|
-
* See <a href="#/p5/if">if</a> for more information about `if` statements and
|
|
603
|
-
* <a href="#/p5/Number">Number</a> for more information about `Number`s.
|
|
604
|
-
*
|
|
605
|
-
* @property Boolean
|
|
606
|
-
*
|
|
607
|
-
* @example
|
|
608
|
-
* <div>
|
|
609
|
-
* <code>
|
|
610
|
-
* function setup() {
|
|
611
|
-
* createCanvas(100, 100);
|
|
612
|
-
*
|
|
613
|
-
* describe('A gray square. When the user presses the mouse, a circle appears at that location.');
|
|
614
|
-
* }
|
|
615
|
-
*
|
|
616
|
-
* function draw() {
|
|
617
|
-
* background(200);
|
|
618
|
-
*
|
|
619
|
-
* // If the user presses the mouse, draw a circle at that location.
|
|
620
|
-
* if (mouseIsPressed) {
|
|
621
|
-
* circle(mouseX, mouseY, 20);
|
|
622
|
-
* }
|
|
623
|
-
* }
|
|
624
|
-
* </code>
|
|
625
|
-
* </div>
|
|
626
|
-
*
|
|
627
|
-
* <div>
|
|
628
|
-
* <code>
|
|
629
|
-
* function setup() {
|
|
630
|
-
* createCanvas(100, 100);
|
|
631
|
-
*
|
|
632
|
-
* describe('A gray square. When the user presses the mouse, a circle appears at that location.');
|
|
633
|
-
* }
|
|
634
|
-
*
|
|
635
|
-
* function draw() {
|
|
636
|
-
* background(200);
|
|
637
|
-
*
|
|
638
|
-
* // If the user presses the mouse, draw a circle at that location.
|
|
639
|
-
* if (mouseIsPressed === true) {
|
|
640
|
-
* circle(mouseX, mouseY, 20);
|
|
641
|
-
* }
|
|
642
|
-
* }
|
|
643
|
-
* </code>
|
|
644
|
-
* </div>
|
|
645
|
-
*
|
|
646
|
-
* <div>
|
|
647
|
-
* <code>
|
|
648
|
-
* // Click on the canvas to begin detecting key presses.
|
|
649
|
-
*
|
|
650
|
-
* function setup() {
|
|
651
|
-
* createCanvas(100, 100);
|
|
652
|
-
*
|
|
653
|
-
* describe('A gray square that turns pink when the user presses the mouse or a key.');
|
|
654
|
-
* }
|
|
655
|
-
*
|
|
656
|
-
* function draw() {
|
|
657
|
-
* background(200);
|
|
658
|
-
*
|
|
659
|
-
* // If the user presses the mouse, change the background color.
|
|
660
|
-
* if (mouseIsPressed === true || keyIsPressed === true) {
|
|
661
|
-
* background('deeppink');
|
|
662
|
-
* }
|
|
663
|
-
* }
|
|
664
|
-
* </code>
|
|
665
|
-
* </div>
|
|
666
|
-
*
|
|
667
|
-
* <div>
|
|
668
|
-
* <code>
|
|
669
|
-
* // Click the canvas to begin detecting key presses.
|
|
670
|
-
*
|
|
671
|
-
* // Create a Boolean variable.
|
|
672
|
-
* let isPlaying = false;
|
|
673
|
-
*
|
|
674
|
-
* function setup() {
|
|
675
|
-
* createCanvas(100, 100);
|
|
676
|
-
*
|
|
677
|
-
* describe(
|
|
678
|
-
* 'The message "Begin?\nY or N" written in green on a black background. The message "Good luck!" appears when they press the "y" key.'
|
|
679
|
-
* );
|
|
680
|
-
* }
|
|
681
|
-
*
|
|
682
|
-
* function draw() {
|
|
683
|
-
* background(0);
|
|
684
|
-
*
|
|
685
|
-
* // Style the text.
|
|
686
|
-
* textAlign(CENTER, CENTER);
|
|
687
|
-
* textFont('Courier New');
|
|
688
|
-
* textSize(16);
|
|
689
|
-
* fill(0, 255, 0);
|
|
690
|
-
*
|
|
691
|
-
* // Display a different message when the user begins playing.
|
|
692
|
-
* if (isPlaying === false) {
|
|
693
|
-
* text('Begin?', 50, 40);
|
|
694
|
-
* text('Y or N', 50, 60);
|
|
695
|
-
* } else {
|
|
696
|
-
* text('Good luck!', 50, 50);
|
|
697
|
-
* }
|
|
698
|
-
* }
|
|
699
|
-
*
|
|
700
|
-
* // Start playing when the user presses the 'y' key.
|
|
701
|
-
* function keyPressed() {
|
|
702
|
-
* if (key === 'y') {
|
|
703
|
-
* isPlaying = true;
|
|
704
|
-
* }
|
|
705
|
-
* }
|
|
706
|
-
* </code>
|
|
707
|
-
* </div>
|
|
708
|
-
*/
|
|
709
|
-
|
|
710
|
-
/**
|
|
711
|
-
* A sequence of text characters.
|
|
712
|
-
*
|
|
713
|
-
* The `String` data type is helpful for working with text. For example, a
|
|
714
|
-
* string could contain a welcome message:
|
|
715
|
-
*
|
|
716
|
-
* ```js
|
|
717
|
-
* // Use a string literal.
|
|
718
|
-
* text('Hello!', 10, 10);
|
|
719
|
-
* ```
|
|
720
|
-
*
|
|
721
|
-
* ```js
|
|
722
|
-
* // Create a string variable.
|
|
723
|
-
* let message = 'Hello!';
|
|
724
|
-
*
|
|
725
|
-
* // Use the string variable.
|
|
726
|
-
* text(message, 10, 10);
|
|
727
|
-
* ```
|
|
728
|
-
*
|
|
729
|
-
* The most common way to create strings is to use some form of quotations as
|
|
730
|
-
* follows:
|
|
731
|
-
*
|
|
732
|
-
* ```js
|
|
733
|
-
* text("hi", 50, 50);
|
|
734
|
-
* ```
|
|
735
|
-
*
|
|
736
|
-
* ```js
|
|
737
|
-
* text('hi', 50, 50);
|
|
738
|
-
* ```
|
|
739
|
-
*
|
|
740
|
-
* ```js
|
|
741
|
-
* text(`hi`, 50, 50);
|
|
742
|
-
* ```
|
|
743
|
-
*
|
|
744
|
-
* `"hi"`, `'hi'`, and ``hi`` are all string literals. A "literal" means a
|
|
745
|
-
* value was actually written, as in `text('hi', 50, 50)`. By contrast,
|
|
746
|
-
* `text(message, 50, 50)` uses the variable `message`, so it isn't a string
|
|
747
|
-
* literal.
|
|
748
|
-
*
|
|
749
|
-
* Single quotes `''` and double quotes `""` mean the same thing. It's nice to
|
|
750
|
-
* have the option for cases when a string contains one type of quote:
|
|
751
|
-
*
|
|
752
|
-
* ```js
|
|
753
|
-
* text("What's up?", 50, 50);
|
|
754
|
-
* ```
|
|
755
|
-
*
|
|
756
|
-
* ```js
|
|
757
|
-
* text('Air quotes make you look "cool."', 50, 50);
|
|
758
|
-
* ```
|
|
759
|
-
*
|
|
760
|
-
* Backticks ` `` ` create template literals. Template literals have many uses.
|
|
761
|
-
* For example, they can contain both single and double quotes as needed:
|
|
762
|
-
*
|
|
763
|
-
* ```js
|
|
764
|
-
* text(`"Don't you forget about me"`, 10, 10);
|
|
765
|
-
* ```
|
|
766
|
-
*
|
|
767
|
-
* Template literals are helpful when strings are created from variables like
|
|
768
|
-
* so:
|
|
769
|
-
*
|
|
770
|
-
* ```js
|
|
771
|
-
* let size = random(10, 20);
|
|
772
|
-
* circle(50, 50, size);
|
|
773
|
-
*
|
|
774
|
-
* text(`The circle's diameter is ${size} pixels.`, 10, 10);
|
|
775
|
-
* ```
|
|
776
|
-
*
|
|
777
|
-
* The `size` variable's value will replace `${size}` when the string is
|
|
778
|
-
* created. `${}` is a placeholder for any value. That means an expression can
|
|
779
|
-
* be used, as in `${round(PI, 3)}`. All of the following are valid template
|
|
780
|
-
* literals:
|
|
781
|
-
*
|
|
782
|
-
* ```js
|
|
783
|
-
* text(`π is about ${round(PI, 2)} pixels.`, 10, 10);
|
|
784
|
-
* text(`It's ${mouseX < width / 2} that I'm on the left half of the canvas.`, 10, 30);
|
|
785
|
-
* ```
|
|
786
|
-
*
|
|
787
|
-
* Template literals can include several variables:
|
|
788
|
-
*
|
|
789
|
-
* ```js
|
|
790
|
-
* let x = random(0, 100);
|
|
791
|
-
* let y = random(0, 100);
|
|
792
|
-
* let size = random(10, 20);
|
|
793
|
-
* circle(x, y, size);
|
|
794
|
-
*
|
|
795
|
-
* text(`The circle at (${x}, ${y}) has a diameter of ${size} pixels.`, 10, 10);
|
|
796
|
-
* ```
|
|
797
|
-
*
|
|
798
|
-
* Template literals are also helpful for creating multi-line text like so:
|
|
799
|
-
*
|
|
800
|
-
* ```js
|
|
801
|
-
* let poem = `My sketch doesn't run;
|
|
802
|
-
* it waits for me patiently
|
|
803
|
-
* while bugs point the way.`;
|
|
804
|
-
*
|
|
805
|
-
* text(poem, 10, 10);
|
|
806
|
-
* ```
|
|
807
|
-
*
|
|
808
|
-
* @property String
|
|
809
|
-
*
|
|
810
|
-
* @example
|
|
811
|
-
* <div>
|
|
812
|
-
* <code>
|
|
813
|
-
* function setup() {
|
|
814
|
-
* createCanvas(100, 100);
|
|
815
|
-
*
|
|
816
|
-
* background(200);
|
|
817
|
-
*
|
|
818
|
-
* // Style the text.
|
|
819
|
-
* textAlign(CENTER, CENTER);
|
|
820
|
-
* textSize(20);
|
|
821
|
-
*
|
|
822
|
-
* // Display a welcome message.
|
|
823
|
-
* text('Hello!', 50, 50);
|
|
824
|
-
*
|
|
825
|
-
* describe('The text "Hello!" written on a gray background.');
|
|
826
|
-
* }
|
|
827
|
-
* </code>
|
|
828
|
-
* </div>
|
|
829
|
-
*
|
|
830
|
-
* <div>
|
|
831
|
-
* <code>
|
|
832
|
-
* function setup() {
|
|
833
|
-
* createCanvas(100, 100);
|
|
834
|
-
*
|
|
835
|
-
* background(200);
|
|
836
|
-
*
|
|
837
|
-
* // Style the text.
|
|
838
|
-
* textAlign(CENTER, CENTER);
|
|
839
|
-
* textSize(20);
|
|
840
|
-
*
|
|
841
|
-
* // Create a string variable.
|
|
842
|
-
* let world = '🌍';
|
|
843
|
-
*
|
|
844
|
-
* // Display a welcome message using a template string.
|
|
845
|
-
* text(`Hello, ${world}!`, 50, 50);
|
|
846
|
-
*
|
|
847
|
-
* describe('The text "Hello, 🌍!" written on a gray background.');
|
|
848
|
-
* }
|
|
849
|
-
* </code>
|
|
850
|
-
* </div>
|
|
851
|
-
*/
|
|
852
|
-
|
|
853
|
-
/**
|
|
854
|
-
* A number that can be positive, negative, or zero.
|
|
855
|
-
*
|
|
856
|
-
* The `Number` data type is useful for describing values such as position,
|
|
857
|
-
* size, and color. A number can be an integer such as 20 or a decimal number
|
|
858
|
-
* such as 12.34. For example, a circle's position and size can be described
|
|
859
|
-
* by three numbers:
|
|
860
|
-
*
|
|
861
|
-
* ```js
|
|
862
|
-
* circle(50, 50, 20);
|
|
863
|
-
* ```
|
|
864
|
-
*
|
|
865
|
-
* ```js
|
|
866
|
-
* circle(50, 50, 12.34);
|
|
867
|
-
* ```
|
|
868
|
-
*
|
|
869
|
-
* Numbers support basic arithmetic and follow the standard order of
|
|
870
|
-
* operations: Parentheses, Exponents, Multiplication, Division, Addition,
|
|
871
|
-
* and Subtraction (PEMDAS). For example, it's common to use arithmetic
|
|
872
|
-
* operators with p5.js' system variables that are numbers:
|
|
873
|
-
*
|
|
874
|
-
* ```js
|
|
875
|
-
* // Draw a circle at the center.
|
|
876
|
-
* circle(width / 2, height / 2, 20);
|
|
877
|
-
* ```
|
|
878
|
-
*
|
|
879
|
-
* ```js
|
|
880
|
-
* // Draw a circle that moves from left to right.
|
|
881
|
-
* circle(frameCount * 0.01, 50, 20);
|
|
882
|
-
* ```
|
|
883
|
-
*
|
|
884
|
-
* Here's a quick overview of the arithmetic operators:
|
|
885
|
-
*
|
|
886
|
-
* ```js
|
|
887
|
-
* 1 + 2 // Add
|
|
888
|
-
* 1 - 2 // Subtract
|
|
889
|
-
* 1 * 2 // Multiply
|
|
890
|
-
* 1 / 2 // Divide
|
|
891
|
-
* 1 % 2 // Remainder
|
|
892
|
-
* 1 ** 2 // Exponentiate
|
|
893
|
-
* ```
|
|
894
|
-
*
|
|
895
|
-
* It's common to update a number variable using arithmetic. For example, an
|
|
896
|
-
* object's location can be updated like so:
|
|
897
|
-
*
|
|
898
|
-
* ```js
|
|
899
|
-
* x = x + 1;
|
|
900
|
-
* ```
|
|
901
|
-
*
|
|
902
|
-
* The statement above adds 1 to a variable `x` using the `+` operator. The
|
|
903
|
-
* addition assignment operator `+=` expresses the same idea:
|
|
904
|
-
*
|
|
905
|
-
* ```js
|
|
906
|
-
* x += 1;
|
|
907
|
-
* ```
|
|
908
|
-
*
|
|
909
|
-
* Here's a quick overview of the assignment operators:
|
|
910
|
-
*
|
|
911
|
-
* ```js
|
|
912
|
-
* x += 2 // Addition assignment
|
|
913
|
-
* x -= 2 // Subtraction assignment
|
|
914
|
-
* x *= 2 // Multiplication assignment
|
|
915
|
-
* x /= 2 // Division assignment
|
|
916
|
-
* x %= 2 // Remainder assignment
|
|
917
|
-
* ```
|
|
918
|
-
*
|
|
919
|
-
* Numbers can be compared using the
|
|
920
|
-
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#relational_operators" target="_blank">relational operators</a>
|
|
921
|
-
* `>`, `<`, `>=`, `<=`, `===`, and `!==`. For example, a sketch's
|
|
922
|
-
* <a href="#/p5/frameCount">frameCount</a> can be used as a timer:
|
|
923
|
-
*
|
|
924
|
-
* ```js
|
|
925
|
-
* if (frameCount > 1000) {
|
|
926
|
-
* text('Game over!', 50, 50);
|
|
927
|
-
* }
|
|
928
|
-
* ```
|
|
929
|
-
*
|
|
930
|
-
* An expression such as `frameCount > 1000` evaluates to a `Boolean` value
|
|
931
|
-
* that's either `true` or `false`. The relational operators all produce
|
|
932
|
-
* `Boolean` values:
|
|
933
|
-
*
|
|
934
|
-
* ```js
|
|
935
|
-
* 2 > 1 // true
|
|
936
|
-
* 2 < 1 // false
|
|
937
|
-
* 2 >= 2 // true
|
|
938
|
-
* 2 <= 2 // true
|
|
939
|
-
* 2 === 2 // true
|
|
940
|
-
* 2 !== 2 // false
|
|
941
|
-
* ```
|
|
942
|
-
*
|
|
943
|
-
* See <a href="#/p5/Boolean">Boolean</a> for more information about comparisons and conditions.
|
|
944
|
-
*
|
|
945
|
-
* Note: There are also `==` and `!=` operators with one fewer `=`. Don't use them.
|
|
946
|
-
*
|
|
947
|
-
* Expressions with numbers can also produce special values when something
|
|
948
|
-
* goes wrong:
|
|
949
|
-
*
|
|
950
|
-
* ```js
|
|
951
|
-
* sqrt(-1) // NaN
|
|
952
|
-
* 1 / 0 // Infinity
|
|
953
|
-
* ```
|
|
954
|
-
*
|
|
955
|
-
* The value `NaN` stands for
|
|
956
|
-
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN" target="_blank">Not-A-Number</a>.
|
|
957
|
-
* `NaN` appears when calculations or conversions don't work. `Infinity` is a
|
|
958
|
-
* value that's larger than any number. It appears during certain
|
|
959
|
-
* calculations.
|
|
960
|
-
*
|
|
961
|
-
* @property Number
|
|
962
|
-
*
|
|
963
|
-
* @example
|
|
964
|
-
* <div>
|
|
965
|
-
* <code>
|
|
966
|
-
* function setup() {
|
|
967
|
-
* createCanvas(100, 100);
|
|
968
|
-
*
|
|
969
|
-
* background(200);
|
|
970
|
-
*
|
|
971
|
-
* // Draw a circle at the center.
|
|
972
|
-
* circle(50, 50, 70);
|
|
973
|
-
*
|
|
974
|
-
* // Draw a smaller circle at the center.
|
|
975
|
-
* circle(width / 2, height / 2, 30);
|
|
976
|
-
*
|
|
977
|
-
* describe('Two concentric, white circles drawn on a gray background.');
|
|
978
|
-
* }
|
|
979
|
-
* </code>
|
|
980
|
-
* </div>
|
|
981
|
-
*
|
|
982
|
-
* <div>
|
|
983
|
-
* <code>
|
|
984
|
-
* function setup() {
|
|
985
|
-
* createCanvas(100, 100);
|
|
986
|
-
*
|
|
987
|
-
* describe('A white circle travels from left to right on a gray background.');
|
|
988
|
-
* }
|
|
989
|
-
*
|
|
990
|
-
* function draw() {
|
|
991
|
-
* background(200);
|
|
992
|
-
*
|
|
993
|
-
* circle(frameCount * 0.05, 50, 20);
|
|
994
|
-
* }
|
|
995
|
-
* </code>
|
|
996
|
-
* </div>
|
|
997
|
-
*/
|
|
998
|
-
|
|
999
|
-
/**
|
|
1000
|
-
* A container for data that's stored as key-value pairs.
|
|
1001
|
-
*
|
|
1002
|
-
* Objects help to organize related data of any type, including other objects.
|
|
1003
|
-
* A value stored in an object can be accessed by name, called its key. Each
|
|
1004
|
-
* key-value pair is called a "property." Objects are similar to dictionaries
|
|
1005
|
-
* in Python and maps in Java and Ruby.
|
|
1006
|
-
*
|
|
1007
|
-
* For example, an object could contain the location, size, and appearance of
|
|
1008
|
-
* a dog:
|
|
1009
|
-
*
|
|
1010
|
-
* ```js
|
|
1011
|
-
* // Declare the dog variable and assign it an object.
|
|
1012
|
-
* let dog = { x: 50, y: 50, size: 20, emoji: '🐶' };
|
|
1013
|
-
*
|
|
1014
|
-
* // Style the text.
|
|
1015
|
-
* textAlign(CENTER, CENTER);
|
|
1016
|
-
* textSize(dog.size);
|
|
1017
|
-
*
|
|
1018
|
-
* // Draw the dog.
|
|
1019
|
-
* text(dog.emoji, dog.x, dog.y);
|
|
1020
|
-
* ```
|
|
1021
|
-
*
|
|
1022
|
-
* The variable `dog` is assigned an object with four properties. Objects
|
|
1023
|
-
* are declared with curly braces `{}`. Values can be accessed using the dot
|
|
1024
|
-
* operator, as in `dog.size`. In the example above, the key `size`
|
|
1025
|
-
* corresponds to the value `20`. Objects can also be empty to start:
|
|
1026
|
-
*
|
|
1027
|
-
* ```js
|
|
1028
|
-
* // Declare a cat variable and assign it an empty object.
|
|
1029
|
-
* let cat = {};
|
|
1030
|
-
*
|
|
1031
|
-
* // Add properties to the object.
|
|
1032
|
-
* cat.x = 50;
|
|
1033
|
-
* cat.y = 50;
|
|
1034
|
-
* cat.size = 20;
|
|
1035
|
-
* cat.emoji = '🐱';
|
|
1036
|
-
*
|
|
1037
|
-
* // Style the text.
|
|
1038
|
-
* textAlign(CENTER, CENTER);
|
|
1039
|
-
* textSize(cat.size);
|
|
1040
|
-
*
|
|
1041
|
-
* // Draw the cat.
|
|
1042
|
-
* text(cat.emoji, cat.x, cat.y);
|
|
1043
|
-
* ```
|
|
1044
|
-
*
|
|
1045
|
-
* An object's data can be updated while a sketch runs. For example, the `cat`
|
|
1046
|
-
* could run away from the `dog` by updating its location:
|
|
1047
|
-
*
|
|
1048
|
-
* ```js
|
|
1049
|
-
* // Run to the right.
|
|
1050
|
-
* cat.x += 5;
|
|
1051
|
-
* ```
|
|
1052
|
-
*
|
|
1053
|
-
* If needed, an object's values can be accessed using square brackets `[]`
|
|
1054
|
-
* and strings instead of dot notation:
|
|
1055
|
-
*
|
|
1056
|
-
* ```js
|
|
1057
|
-
* // Run to the right.
|
|
1058
|
-
* cat["x"] += 5;
|
|
1059
|
-
* ```
|
|
1060
|
-
*
|
|
1061
|
-
* This syntax can be helpful when the key's name has spaces, as in
|
|
1062
|
-
* `cat['height (m)']`.
|
|
1063
|
-
*
|
|
1064
|
-
* @property Object
|
|
1065
|
-
*
|
|
1066
|
-
* @example
|
|
1067
|
-
* <div>
|
|
1068
|
-
* <code>
|
|
1069
|
-
* // Declare the variable data and assign it an object with three properties.
|
|
1070
|
-
* let data = { x: 50, y: 50, d: 20 };
|
|
1071
|
-
*
|
|
1072
|
-
* function setup() {
|
|
1073
|
-
* createCanvas(100, 100);
|
|
1074
|
-
*
|
|
1075
|
-
* describe('A white circle on a gray background.');
|
|
1076
|
-
* }
|
|
1077
|
-
*
|
|
1078
|
-
* function draw() {
|
|
1079
|
-
* background(200);
|
|
1080
|
-
*
|
|
1081
|
-
* // Access the object's values using the . operator.
|
|
1082
|
-
* circle(data.x, data.y, data.d);
|
|
1083
|
-
* }
|
|
1084
|
-
* </code>
|
|
1085
|
-
* </div>
|
|
1086
|
-
*
|
|
1087
|
-
* <div>
|
|
1088
|
-
* <code>
|
|
1089
|
-
* // Declare the variable data and assign it an object with three properties.
|
|
1090
|
-
* let data = { x: 50, y: 50, d: 20 };
|
|
1091
|
-
*
|
|
1092
|
-
* // Add another property for color.
|
|
1093
|
-
* data.color = 'deeppink';
|
|
1094
|
-
*
|
|
1095
|
-
* function setup() {
|
|
1096
|
-
* createCanvas(100, 100);
|
|
1097
|
-
*
|
|
1098
|
-
* describe('A pink circle on a gray background.');
|
|
1099
|
-
* }
|
|
1100
|
-
*
|
|
1101
|
-
* function draw() {
|
|
1102
|
-
* background(200);
|
|
1103
|
-
*
|
|
1104
|
-
* // Access the object's values using the . operator.
|
|
1105
|
-
* fill(data.color);
|
|
1106
|
-
* circle(data.x, data.y, data.d);
|
|
1107
|
-
* }
|
|
1108
|
-
* </code>
|
|
1109
|
-
* </div>
|
|
1110
|
-
*
|
|
1111
|
-
* <div>
|
|
1112
|
-
* <code>
|
|
1113
|
-
* // Declare the variable data and assign it an object with three properties.
|
|
1114
|
-
* let data = { x: 50, y: 50, d: 20 };
|
|
1115
|
-
*
|
|
1116
|
-
* // Add another property for color.
|
|
1117
|
-
* data.color = 'deeppink';
|
|
1118
|
-
*
|
|
1119
|
-
* function setup() {
|
|
1120
|
-
* createCanvas(100, 100);
|
|
1121
|
-
*
|
|
1122
|
-
* describe('A white circle on a gray background.');
|
|
1123
|
-
* }
|
|
1124
|
-
*
|
|
1125
|
-
* function draw() {
|
|
1126
|
-
* background(200);
|
|
1127
|
-
*
|
|
1128
|
-
* // Access the object's values using the . operator.
|
|
1129
|
-
* fill(data.color);
|
|
1130
|
-
* circle(data.x, data.y, data.d);
|
|
1131
|
-
*
|
|
1132
|
-
* // Update the object's x and y properties.
|
|
1133
|
-
* data.x += random(-1, 1);
|
|
1134
|
-
* data.y += random(-1, 1);
|
|
1135
|
-
* }
|
|
1136
|
-
* </code>
|
|
1137
|
-
* </div>
|
|
1138
|
-
*/
|
|
1139
|
-
|
|
1140
|
-
/**
|
|
1141
|
-
* A list that keeps several pieces of data in order.
|
|
1142
|
-
*
|
|
1143
|
-
* Arrays are helpful for storing related data. They can contain data of any
|
|
1144
|
-
* type. For example, an array could contain a list of someone's favorite
|
|
1145
|
-
* colors as strings. Arrays are created as follows:
|
|
1146
|
-
*
|
|
1147
|
-
* ```js
|
|
1148
|
-
* let myArray = ['deeppink', 'darkorchid', 'magenta'];
|
|
1149
|
-
* ```
|
|
1150
|
-
*
|
|
1151
|
-
* Each piece of data in an array is called an element. Each element has an
|
|
1152
|
-
* address, or index, within its array. The variable `myArray` refers to an
|
|
1153
|
-
* array with three <a href="#/p5/String">String</a> elements, `'deeppink'`,
|
|
1154
|
-
* `'darkorchid'`, and `'magenta'`. Arrays are zero-indexed, which means
|
|
1155
|
-
* that `'deeppink'` is at index 0, `'darkorchid'` is at index 1, and
|
|
1156
|
-
* '`magenta'` is at index 2. Array elements can be accessed using their
|
|
1157
|
-
* indices as follows:
|
|
1158
|
-
*
|
|
1159
|
-
* ```js
|
|
1160
|
-
* let zeroth = myArray[0]; // 'deeppink'
|
|
1161
|
-
* let first = myArray[1]; // 'darkorchid'
|
|
1162
|
-
* let second = myArray[2]; // 'magenta'
|
|
1163
|
-
* ```
|
|
1164
|
-
*
|
|
1165
|
-
* Elements can be added to the end of an array by calling the
|
|
1166
|
-
* <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push" target="_blank">push()</a>
|
|
1167
|
-
* method as follows:
|
|
1168
|
-
*
|
|
1169
|
-
* ```js
|
|
1170
|
-
* myArray.push('lavender');
|
|
1171
|
-
*
|
|
1172
|
-
* let third = myArray[3]; // 'lavender'
|
|
1173
|
-
* ```
|
|
1174
|
-
*
|
|
1175
|
-
* See <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Arrays" target="_blank">MDN</a>
|
|
1176
|
-
* for more information about arrays.
|
|
1177
|
-
*
|
|
1178
|
-
* @property Array
|
|
1179
|
-
*
|
|
1180
|
-
* @example
|
|
1181
|
-
* <div>
|
|
1182
|
-
* <code>
|
|
1183
|
-
* // Declare the variable xCoordinates and assign it an array
|
|
1184
|
-
* // with three numeric elements.
|
|
1185
|
-
* let xCoordinates = [25, 50, 75];
|
|
1186
|
-
*
|
|
1187
|
-
* function setup() {
|
|
1188
|
-
* createCanvas(100, 100);
|
|
1189
|
-
*
|
|
1190
|
-
* describe(
|
|
1191
|
-
* 'Three white circles drawn in a horizontal line on a gray background.'
|
|
1192
|
-
* );
|
|
1193
|
-
* }
|
|
1194
|
-
*
|
|
1195
|
-
* function draw() {
|
|
1196
|
-
* background(200);
|
|
1197
|
-
*
|
|
1198
|
-
* // Access the element at index 0, which is 25.
|
|
1199
|
-
* circle(xCoordinates[0], 50, 20);
|
|
1200
|
-
*
|
|
1201
|
-
* // Access the element at index 1, which is 50.
|
|
1202
|
-
* circle(xCoordinates[1], 50, 20);
|
|
1203
|
-
*
|
|
1204
|
-
* // Access the element at index 2, which is 75.
|
|
1205
|
-
* circle(xCoordinates[2], 50, 20);
|
|
1206
|
-
* }
|
|
1207
|
-
* </code>
|
|
1208
|
-
* </div>
|
|
1209
|
-
*
|
|
1210
|
-
* <div>
|
|
1211
|
-
* <code>
|
|
1212
|
-
* // Declare the variable xCoordinates and assign it an array with three numeric elements.
|
|
1213
|
-
* let xCoordinates = [20, 40, 60];
|
|
1214
|
-
*
|
|
1215
|
-
* // Add another element to the end of the array.
|
|
1216
|
-
* xCoordinates.push(80);
|
|
1217
|
-
*
|
|
1218
|
-
* function setup() {
|
|
1219
|
-
* createCanvas(100, 100);
|
|
1220
|
-
*
|
|
1221
|
-
* describe('Four white circles drawn in a horizontal line on a gray background.');
|
|
1222
|
-
* }
|
|
1223
|
-
*
|
|
1224
|
-
* function draw() {
|
|
1225
|
-
* background(200);
|
|
1226
|
-
*
|
|
1227
|
-
* // Access the element at index 0, which is 20.
|
|
1228
|
-
* circle(xCoordinates[0], 50, 20);
|
|
1229
|
-
*
|
|
1230
|
-
* // Access the element at index 1, which is 40.
|
|
1231
|
-
* circle(xCoordinates[1], 50, 20);
|
|
1232
|
-
*
|
|
1233
|
-
* // Access the element at index 2, which is 60.
|
|
1234
|
-
* circle(xCoordinates[2], 50, 20);
|
|
1235
|
-
*
|
|
1236
|
-
* // Access the element at index 3, which is 80.
|
|
1237
|
-
* circle(xCoordinates[3], 50, 20);
|
|
1238
|
-
* }
|
|
1239
|
-
* </code>
|
|
1240
|
-
* </div>
|
|
1241
|
-
*
|
|
1242
|
-
* <div>
|
|
1243
|
-
* <code>
|
|
1244
|
-
* // Declare the variable xCoordinates and assign it an empty array.
|
|
1245
|
-
* let xCoordinates = [];
|
|
1246
|
-
*
|
|
1247
|
-
* function setup() {
|
|
1248
|
-
* createCanvas(100, 100);
|
|
1249
|
-
*
|
|
1250
|
-
* // Add elements to the array using a loop.
|
|
1251
|
-
* for (let x = 20; x < 100; x += 20) {
|
|
1252
|
-
* xCoordinates.push(x);
|
|
1253
|
-
* }
|
|
1254
|
-
*
|
|
1255
|
-
* describe('Four white circles drawn in a horizontal line on a gray background.');
|
|
1256
|
-
* }
|
|
1257
|
-
*
|
|
1258
|
-
* function draw() {
|
|
1259
|
-
* background(200);
|
|
1260
|
-
*
|
|
1261
|
-
* // Access the element at index i and use it to draw a circle.
|
|
1262
|
-
* for (let i = 0; i < xCoordinates.length; i += 1) {
|
|
1263
|
-
* circle(xCoordinates[i], 50, 20);
|
|
1264
|
-
* }
|
|
1265
|
-
* }
|
|
1266
|
-
* </code>
|
|
1267
|
-
* </div>
|
|
1268
|
-
*
|
|
1269
|
-
* <div>
|
|
1270
|
-
* <code>
|
|
1271
|
-
* // Declare the variable xCoordinates and assign it an empty array.
|
|
1272
|
-
* let xCoordinates = [];
|
|
1273
|
-
*
|
|
1274
|
-
* function setup() {
|
|
1275
|
-
* createCanvas(100, 100);
|
|
1276
|
-
*
|
|
1277
|
-
* // Add elements to the array using a loop.
|
|
1278
|
-
* for (let x = 20; x < 100; x += 20) {
|
|
1279
|
-
* xCoordinates.push(x);
|
|
1280
|
-
* }
|
|
1281
|
-
*
|
|
1282
|
-
* describe('Four white circles drawn in a horizontal line on a gray background.');
|
|
1283
|
-
* }
|
|
1284
|
-
*
|
|
1285
|
-
* function draw() {
|
|
1286
|
-
* background(200);
|
|
1287
|
-
*
|
|
1288
|
-
* // Access each element of the array and use it to draw a circle.
|
|
1289
|
-
* for (let x of xCoordinates) {
|
|
1290
|
-
* circle(x, 50, 20);
|
|
1291
|
-
* }
|
|
1292
|
-
* }
|
|
1293
|
-
* </code>
|
|
1294
|
-
* </div>
|
|
1295
|
-
*
|
|
1296
|
-
* <div>
|
|
1297
|
-
* <code>
|
|
1298
|
-
* // Declare the variable xCoordinates and assign it an empty array.
|
|
1299
|
-
* let xCoordinates = [];
|
|
1300
|
-
*
|
|
1301
|
-
* function setup() {
|
|
1302
|
-
* createCanvas(100, 100);
|
|
1303
|
-
*
|
|
1304
|
-
* // Add elements to the array using a loop.
|
|
1305
|
-
* for (let x = 20; x < 100; x += 20) {
|
|
1306
|
-
* xCoordinates.push(x);
|
|
1307
|
-
* }
|
|
1308
|
-
*
|
|
1309
|
-
* describe(
|
|
1310
|
-
* 'Four white circles in a horizontal line on a gray background. The circles move randomly.'
|
|
1311
|
-
* );
|
|
1312
|
-
* }
|
|
1313
|
-
*
|
|
1314
|
-
* function draw() {
|
|
1315
|
-
* background(200);
|
|
1316
|
-
*
|
|
1317
|
-
* for (let i = 0; i < xCoordinates.length; i += 1) {
|
|
1318
|
-
* // Update the element at index i.
|
|
1319
|
-
* xCoordinates[i] += random(-1, 1);
|
|
1320
|
-
*
|
|
1321
|
-
* // Use the element at index i to draw a circle.
|
|
1322
|
-
* circle(xCoordinates[i], 50, 20);
|
|
1323
|
-
* }
|
|
1324
|
-
* }
|
|
1325
|
-
* </code>
|
|
1326
|
-
* </div>
|
|
1327
|
-
*/
|
|
1328
|
-
|
|
1329
|
-
/**
|
|
1330
|
-
* A template for creating objects of a particular type.
|
|
1331
|
-
*
|
|
1332
|
-
* Classes can make it easier to program with objects. For example, a `Frog`
|
|
1333
|
-
* class could create objects that behave like frogs. Each object created
|
|
1334
|
-
* using a class is called an instance of that class. All instances of a class
|
|
1335
|
-
* are the same type. Here's an example of creating an instance of a `Frog`
|
|
1336
|
-
* class:
|
|
1337
|
-
*
|
|
1338
|
-
* ```js
|
|
1339
|
-
* let fifi = new Frog(50, 50, 20);
|
|
1340
|
-
* ```
|
|
1341
|
-
*
|
|
1342
|
-
* The variable `fifi` refers to an instance of the `Frog` class. The keyword
|
|
1343
|
-
* `new` is used to call the `Frog` class' constructor in the statement
|
|
1344
|
-
* `new Frog()`. Altogether, a new `Frog` object was created and assigned to
|
|
1345
|
-
* the variable `fifi`. Classes are templates, so they can be used to create
|
|
1346
|
-
* more than one instance:
|
|
1347
|
-
*
|
|
1348
|
-
* ```js
|
|
1349
|
-
* // First Frog instance.
|
|
1350
|
-
* let frog1 = new Frog(25, 50, 10);
|
|
1351
|
-
*
|
|
1352
|
-
* // Second Frog instance.
|
|
1353
|
-
* let frog2 = new Frog(75, 50, 10);
|
|
1354
|
-
* ```
|
|
1355
|
-
*
|
|
1356
|
-
* A simple `Frog` class could be declared as follows:
|
|
1357
|
-
*
|
|
1358
|
-
* ```js
|
|
1359
|
-
* class Frog {
|
|
1360
|
-
* constructor(x, y, size) {
|
|
1361
|
-
* // This code runs once when an instance is created.
|
|
1362
|
-
* this.x = x;
|
|
1363
|
-
* this.y = y;
|
|
1364
|
-
* this.size = size;
|
|
1365
|
-
* }
|
|
1366
|
-
*
|
|
1367
|
-
* show() {
|
|
1368
|
-
* // This code runs once when myFrog.show() is called.
|
|
1369
|
-
* textAlign(CENTER, CENTER);
|
|
1370
|
-
* textSize(this.size);
|
|
1371
|
-
* text('🐸', this.x, this.y);
|
|
1372
|
-
* }
|
|
1373
|
-
*
|
|
1374
|
-
* hop() {
|
|
1375
|
-
* // This code runs once when myFrog.hop() is called.
|
|
1376
|
-
* this.x += random(-10, 10);
|
|
1377
|
-
* this.y += random(-10, 10);
|
|
1378
|
-
* }
|
|
1379
|
-
* }
|
|
1380
|
-
* ```
|
|
1381
|
-
*
|
|
1382
|
-
* Class declarations begin with the keyword `class` followed by the class
|
|
1383
|
-
* name, such as `Frog`, and curly braces `{}`. Class names should use
|
|
1384
|
-
* `PascalCase` and can't have spaces in their names. For example, naming a
|
|
1385
|
-
* class `Kermit The Frog` with spaces between each word would throw a
|
|
1386
|
-
* `SyntaxError`. The code between the curly braces `{}` defines the class.
|
|
1387
|
-
*
|
|
1388
|
-
* Functions that belong to a class are called methods. `constructor()`,
|
|
1389
|
-
* `show()`, and `hop()` are methods in the `Frog` class. Methods define an
|
|
1390
|
-
* object's behavior. Methods can accept parameters and return values, just
|
|
1391
|
-
* like functions. Note that methods don't use the `function` keyword.
|
|
1392
|
-
*
|
|
1393
|
-
* `constructor()` is a special method that's called once when an instance of
|
|
1394
|
-
* the class is created. The statement `new Frog()` calls the `Frog` class'
|
|
1395
|
-
* `constructor()` method.
|
|
1396
|
-
*
|
|
1397
|
-
* A class definition is a template for instances. The keyword `this` refers
|
|
1398
|
-
* to an instance's data and methods. For example, each `Frog` instance has
|
|
1399
|
-
* unique coordinates stored in `this.x` and `this.y`. The `show()` method
|
|
1400
|
-
* uses those coordinates to draw the frog. The `hop()` method updates those
|
|
1401
|
-
* coordinates when called. Once a `Frog` instance is created, its data and
|
|
1402
|
-
* methods can be accessed using the dot operator `.` as follows:
|
|
1403
|
-
*
|
|
1404
|
-
* ```js
|
|
1405
|
-
* // Draw a lily pad.
|
|
1406
|
-
* fill('green');
|
|
1407
|
-
* stroke('green');
|
|
1408
|
-
* circle(fifi.x, fifi.y, 2 * fifi.size);
|
|
1409
|
-
*
|
|
1410
|
-
* // Show the Frog.
|
|
1411
|
-
* fifi.show();
|
|
1412
|
-
*
|
|
1413
|
-
* // Hop.
|
|
1414
|
-
* fifi.hop();
|
|
1415
|
-
* ```
|
|
1416
|
-
*
|
|
1417
|
-
* @property class
|
|
1418
|
-
*
|
|
1419
|
-
* @example
|
|
1420
|
-
* <div>
|
|
1421
|
-
* <code>
|
|
1422
|
-
* // Declare a frog variable.
|
|
1423
|
-
* let fifi;
|
|
1424
|
-
*
|
|
1425
|
-
* function setup() {
|
|
1426
|
-
* createCanvas(100, 100);
|
|
1427
|
-
*
|
|
1428
|
-
* // Assign the frog variable a new Frog object.
|
|
1429
|
-
* fifi = new Frog(50, 50, 20);
|
|
1430
|
-
*
|
|
1431
|
-
* describe('A frog face drawn on a blue background.');
|
|
1432
|
-
* }
|
|
1433
|
-
*
|
|
1434
|
-
* function draw() {
|
|
1435
|
-
* background('cornflowerblue');
|
|
1436
|
-
*
|
|
1437
|
-
* // Show the frog.
|
|
1438
|
-
* fifi.show();
|
|
1439
|
-
* }
|
|
1440
|
-
*
|
|
1441
|
-
* class Frog {
|
|
1442
|
-
* constructor(x, y, size) {
|
|
1443
|
-
* this.x = x;
|
|
1444
|
-
* this.y = y;
|
|
1445
|
-
* this.size = size;
|
|
1446
|
-
* }
|
|
1447
|
-
*
|
|
1448
|
-
* show() {
|
|
1449
|
-
* textAlign(CENTER, CENTER);
|
|
1450
|
-
* textSize(this.size);
|
|
1451
|
-
* text('🐸', this.x, this.y);
|
|
1452
|
-
* }
|
|
1453
|
-
* }
|
|
1454
|
-
* </code>
|
|
1455
|
-
* </div>
|
|
1456
|
-
*
|
|
1457
|
-
* <div>
|
|
1458
|
-
* <code>
|
|
1459
|
-
* // Declare two frog variables.
|
|
1460
|
-
* let frog1;
|
|
1461
|
-
* let frog2;
|
|
1462
|
-
*
|
|
1463
|
-
* function setup() {
|
|
1464
|
-
* createCanvas(100, 100);
|
|
1465
|
-
*
|
|
1466
|
-
* // Assign the frog variables a new Frog object.
|
|
1467
|
-
* frog1 = new Frog(25, 50, 10);
|
|
1468
|
-
* frog2 = new Frog(75, 50, 20);
|
|
1469
|
-
*
|
|
1470
|
-
* describe('Two frog faces drawn next to each other on a blue background.');
|
|
1471
|
-
* }
|
|
1472
|
-
*
|
|
1473
|
-
* function draw() {
|
|
1474
|
-
* background('cornflowerblue');
|
|
1475
|
-
*
|
|
1476
|
-
* // Show the frogs.
|
|
1477
|
-
* frog1.show();
|
|
1478
|
-
* frog2.show();
|
|
1479
|
-
* }
|
|
1480
|
-
*
|
|
1481
|
-
* class Frog {
|
|
1482
|
-
* constructor(x, y, size) {
|
|
1483
|
-
* this.x = x;
|
|
1484
|
-
* this.y = y;
|
|
1485
|
-
* this.size = size;
|
|
1486
|
-
* }
|
|
1487
|
-
*
|
|
1488
|
-
* show() {
|
|
1489
|
-
* textAlign(CENTER, CENTER);
|
|
1490
|
-
* textSize(this.size);
|
|
1491
|
-
* text('🐸', this.x, this.y);
|
|
1492
|
-
* }
|
|
1493
|
-
* }
|
|
1494
|
-
* </code>
|
|
1495
|
-
* </div>
|
|
1496
|
-
*
|
|
1497
|
-
* <div>
|
|
1498
|
-
* <code>
|
|
1499
|
-
* // Declare two frog variables.
|
|
1500
|
-
* let frog1;
|
|
1501
|
-
* let frog2;
|
|
1502
|
-
*
|
|
1503
|
-
* function setup() {
|
|
1504
|
-
* createCanvas(100, 100);
|
|
1505
|
-
*
|
|
1506
|
-
* // Assign the frog variables a new Frog object.
|
|
1507
|
-
* frog1 = new Frog(25, 50, 10);
|
|
1508
|
-
* frog2 = new Frog(75, 50, 20);
|
|
1509
|
-
*
|
|
1510
|
-
* // Slow the frame rate.
|
|
1511
|
-
* frameRate(1);
|
|
1512
|
-
*
|
|
1513
|
-
* describe('Two frog faces on a blue background. The frogs hop around randomly.');
|
|
1514
|
-
* }
|
|
1515
|
-
*
|
|
1516
|
-
* function draw() {
|
|
1517
|
-
* background('cornflowerblue');
|
|
1518
|
-
*
|
|
1519
|
-
* // Show the frogs.
|
|
1520
|
-
* frog1.show();
|
|
1521
|
-
* frog2.show();
|
|
1522
|
-
*
|
|
1523
|
-
* // Move the frogs.
|
|
1524
|
-
* frog1.hop();
|
|
1525
|
-
* frog2.hop();
|
|
1526
|
-
*
|
|
1527
|
-
* // Wrap around if they've hopped off the edge.
|
|
1528
|
-
* frog1.checkEdges();
|
|
1529
|
-
* frog2.checkEdges();
|
|
1530
|
-
* }
|
|
1531
|
-
*
|
|
1532
|
-
* class Frog {
|
|
1533
|
-
* constructor(x, y, size) {
|
|
1534
|
-
* this.x = x;
|
|
1535
|
-
* this.y = y;
|
|
1536
|
-
* this.size = size;
|
|
1537
|
-
* }
|
|
1538
|
-
*
|
|
1539
|
-
* show() {
|
|
1540
|
-
* textAlign(CENTER, CENTER);
|
|
1541
|
-
* textSize(this.size);
|
|
1542
|
-
* text('🐸', this.x, this.y);
|
|
1543
|
-
* }
|
|
1544
|
-
*
|
|
1545
|
-
* hop() {
|
|
1546
|
-
* this.x += random(-10, 10);
|
|
1547
|
-
* this.y += random(-10, 10);
|
|
1548
|
-
* }
|
|
1549
|
-
*
|
|
1550
|
-
* checkEdges() {
|
|
1551
|
-
* if (this.x > width) {
|
|
1552
|
-
* this.x = this.x - width;
|
|
1553
|
-
* } else if (this.x < 0) {
|
|
1554
|
-
* this.x = width - this.x;
|
|
1555
|
-
* }
|
|
1556
|
-
*
|
|
1557
|
-
* if (this.y > height) {
|
|
1558
|
-
* this.y = this.y - height;
|
|
1559
|
-
* } else if (this.y < 0) {
|
|
1560
|
-
* this.y = height - this.y;
|
|
1561
|
-
* }
|
|
1562
|
-
* }
|
|
1563
|
-
* }
|
|
1564
|
-
* </code>
|
|
1565
|
-
* </div>
|
|
1566
|
-
*
|
|
1567
|
-
* <div>
|
|
1568
|
-
* <code>
|
|
1569
|
-
* // Create an array that will hold frogs.
|
|
1570
|
-
* let frogs = [];
|
|
1571
|
-
*
|
|
1572
|
-
* function setup() {
|
|
1573
|
-
* createCanvas(100, 100);
|
|
1574
|
-
*
|
|
1575
|
-
* // Add Frog objects to the array.
|
|
1576
|
-
* for (let i = 0; i < 5; i += 1) {
|
|
1577
|
-
* // Calculate random coordinates and size.
|
|
1578
|
-
* let x = random(0, 100);
|
|
1579
|
-
* let y = random(0, 100);
|
|
1580
|
-
* let s = random(2, 20);
|
|
1581
|
-
*
|
|
1582
|
-
* // Create a new Frog object.
|
|
1583
|
-
* let frog = new Frog(x, y, s);
|
|
1584
|
-
*
|
|
1585
|
-
* // Add the Frog to the array.
|
|
1586
|
-
* frogs.push(frog);
|
|
1587
|
-
* }
|
|
1588
|
-
*
|
|
1589
|
-
* // Slow the frame rate.
|
|
1590
|
-
* frameRate(1);
|
|
1591
|
-
*
|
|
1592
|
-
* describe(
|
|
1593
|
-
* 'Five frog faces on a blue background. The frogs hop around randomly.'
|
|
1594
|
-
* );
|
|
1595
|
-
* }
|
|
1596
|
-
*
|
|
1597
|
-
* function draw() {
|
|
1598
|
-
* background('cornflowerblue');
|
|
1599
|
-
*
|
|
1600
|
-
* for (let frog of frogs) {
|
|
1601
|
-
* // Show the frog.
|
|
1602
|
-
* frog.show();
|
|
1603
|
-
*
|
|
1604
|
-
* // Move the frog.
|
|
1605
|
-
* frog.hop();
|
|
1606
|
-
*
|
|
1607
|
-
* // Wrap around if they've hopped off the edge.
|
|
1608
|
-
* frog.checkEdges();
|
|
1609
|
-
* }
|
|
1610
|
-
* }
|
|
1611
|
-
*
|
|
1612
|
-
* class Frog {
|
|
1613
|
-
* constructor(x, y, size) {
|
|
1614
|
-
* this.x = x;
|
|
1615
|
-
* this.y = y;
|
|
1616
|
-
* this.size = size;
|
|
1617
|
-
* }
|
|
1618
|
-
*
|
|
1619
|
-
* show() {
|
|
1620
|
-
* textAlign(CENTER, CENTER);
|
|
1621
|
-
* textSize(this.size);
|
|
1622
|
-
* text('🐸', this.x, this.y);
|
|
1623
|
-
* }
|
|
1624
|
-
*
|
|
1625
|
-
* hop() {
|
|
1626
|
-
* this.x += random(-10, 10);
|
|
1627
|
-
* this.y += random(-10, 10);
|
|
1628
|
-
* }
|
|
1629
|
-
*
|
|
1630
|
-
* checkEdges() {
|
|
1631
|
-
* if (this.x > width) {
|
|
1632
|
-
* this.x = this.x - width;
|
|
1633
|
-
* } else if (this.x < 0) {
|
|
1634
|
-
* this.x = width - this.x;
|
|
1635
|
-
* }
|
|
1636
|
-
*
|
|
1637
|
-
* if (this.y > height) {
|
|
1638
|
-
* this.y = this.y - height;
|
|
1639
|
-
* } else if (this.y < 0) {
|
|
1640
|
-
* this.y = height - this.y;
|
|
1641
|
-
* }
|
|
1642
|
-
* }
|
|
1643
|
-
* }
|
|
1644
|
-
* </code>
|
|
1645
|
-
* </div>
|
|
1646
|
-
*/
|
|
1647
|
-
|
|
1648
|
-
/**
|
|
1649
|
-
* A way to repeat a block of code when the number of iterations is known.
|
|
1650
|
-
*
|
|
1651
|
-
* `for` loops are helpful for repeating statements a certain number of times.
|
|
1652
|
-
* For example, a `for` loop makes it easy to express the idea
|
|
1653
|
-
* "draw five lines" like so:
|
|
1654
|
-
*
|
|
1655
|
-
* ```js
|
|
1656
|
-
* for (let x = 10; x < 100; x += 20) {
|
|
1657
|
-
* line(x, 25, x, 75);
|
|
1658
|
-
* }
|
|
1659
|
-
* ```
|
|
1660
|
-
*
|
|
1661
|
-
* The loop's header begins with the keyword `for`. Loops generally count up
|
|
1662
|
-
* or count down as they repeat, or iterate. The statements in parentheses
|
|
1663
|
-
* `let x = 10; x < 100; x += 20` tell the loop how it should repeat:
|
|
1664
|
-
* - `let x = 10` tells the loop to start counting at `10` and keep track of iterations using the variable `x`.
|
|
1665
|
-
* - `x < 100` tells the loop to count up to, but not including, `100`.
|
|
1666
|
-
* - `x += 20` tells the loop to count up by `20` at the end of each iteration.
|
|
1667
|
-
*
|
|
1668
|
-
* The code between the curly braces `{}` is the loop's body. Statements in the
|
|
1669
|
-
* loop body are repeated during each iteration of the loop.
|
|
1670
|
-
*
|
|
1671
|
-
* It's common to create infinite loops accidentally. When this happens,
|
|
1672
|
-
* sketches may become unresponsive and the web browser may display a warning.
|
|
1673
|
-
* For example, the following loop never stops iterating because it doesn't
|
|
1674
|
-
* count up:
|
|
1675
|
-
*
|
|
1676
|
-
* ```js
|
|
1677
|
-
* for (let x = 10; x < 100; x = 20) {
|
|
1678
|
-
* line(x, 25, x, 75);
|
|
1679
|
-
* }
|
|
1680
|
-
* ```
|
|
1681
|
-
*
|
|
1682
|
-
* The statement `x = 20` keeps the variable `x` stuck at `20`, which is
|
|
1683
|
-
* always less than `100`.
|
|
1684
|
-
*
|
|
1685
|
-
* `for` loops can also count down:
|
|
1686
|
-
*
|
|
1687
|
-
* ```js
|
|
1688
|
-
* for (let d = 100; d > 0; d -= 10) {
|
|
1689
|
-
* circle(50, 50, d);
|
|
1690
|
-
* }
|
|
1691
|
-
* ```
|
|
1692
|
-
*
|
|
1693
|
-
* `for` loops can also contain other loops. The following nested loop draws a
|
|
1694
|
-
* grid of points:
|
|
1695
|
-
*
|
|
1696
|
-
* ```js
|
|
1697
|
-
* // Loop from left to right.
|
|
1698
|
-
* for (let x = 10; x < 100; x += 10) {
|
|
1699
|
-
*
|
|
1700
|
-
* // Loop from top to bottom.
|
|
1701
|
-
* for (let y = 10; y < 100; y += 10) {
|
|
1702
|
-
* point(x, y);
|
|
1703
|
-
* }
|
|
1704
|
-
*
|
|
1705
|
-
* }
|
|
1706
|
-
* ```
|
|
1707
|
-
*
|
|
1708
|
-
* `for` loops are also helpful for iterating through the elements of an
|
|
1709
|
-
* array. For example, it's common to iterate through an array that contains
|
|
1710
|
-
* information about where or what to draw:
|
|
1711
|
-
*
|
|
1712
|
-
* ```js
|
|
1713
|
-
* // Create an array of x-coordinates.
|
|
1714
|
-
* let xCoordinates = [20, 40, 60];
|
|
1715
|
-
*
|
|
1716
|
-
* for (let i = 0; i < xCoordinates.length; i += 1) {
|
|
1717
|
-
* // Update the element.
|
|
1718
|
-
* xCoordinates[i] += random(-1, 1);
|
|
1719
|
-
*
|
|
1720
|
-
* // Draw a circle.
|
|
1721
|
-
* circle(xCoordinates[i], 50, 20);
|
|
1722
|
-
* }
|
|
1723
|
-
* ```
|
|
1724
|
-
*
|
|
1725
|
-
* If the array's values aren't modified, the `for...of` statement can
|
|
1726
|
-
* simplify the code. They're similar to `for` loops in Python and `for-each`
|
|
1727
|
-
* loops in C++ and Java. The following loops have the same effect:
|
|
1728
|
-
*
|
|
1729
|
-
* ```js
|
|
1730
|
-
* // Draw circles with a for loop.
|
|
1731
|
-
* let xCoordinates = [20, 40, 60];
|
|
1732
|
-
*
|
|
1733
|
-
* for (let i = 0; i < xCoordinates.length; i += 1) {
|
|
1734
|
-
* circle(xCoordinates[i], 50, 20);
|
|
1735
|
-
* }
|
|
1736
|
-
* ```
|
|
1737
|
-
*
|
|
1738
|
-
* ```js
|
|
1739
|
-
* // Draw circles with a for...of statement.
|
|
1740
|
-
* let xCoordinates = [20, 40, 60];
|
|
1741
|
-
*
|
|
1742
|
-
* for (let x of xCoordinates) {
|
|
1743
|
-
* circle(x, 50, 20);
|
|
1744
|
-
* }
|
|
1745
|
-
* ```
|
|
1746
|
-
*
|
|
1747
|
-
* In the code snippets above, the variables `i` and `x` have different roles.
|
|
1748
|
-
*
|
|
1749
|
-
* In the first snippet, `i` counts from `0` up to `2`, which is one less than
|
|
1750
|
-
* `xCoordinates.length`. `i` is used to access the element in `xCoordinates`
|
|
1751
|
-
* at index `i`.
|
|
1752
|
-
*
|
|
1753
|
-
* In the second code snippet, `x` isn't keeping track of the loop's progress
|
|
1754
|
-
* or an index. During each iteration, `x` contains the next element of
|
|
1755
|
-
* `xCoordinates`. `x` starts from the beginning of `xCoordinates` (`20`) and
|
|
1756
|
-
* updates its value to `40` and then `60` during the next iterations.
|
|
1757
|
-
*
|
|
1758
|
-
* @property for
|
|
1759
|
-
*
|
|
1760
|
-
* @example
|
|
1761
|
-
* <div>
|
|
1762
|
-
* <code>
|
|
1763
|
-
* function setup() {
|
|
1764
|
-
* createCanvas(100, 100);
|
|
1765
|
-
*
|
|
1766
|
-
* describe('Five black vertical lines on a gray background.');
|
|
1767
|
-
* }
|
|
1768
|
-
*
|
|
1769
|
-
* function draw() {
|
|
1770
|
-
* background(200);
|
|
1771
|
-
*
|
|
1772
|
-
* // Draw vertical lines using a loop.
|
|
1773
|
-
* for (let x = 10; x < 100; x += 20) {
|
|
1774
|
-
* line(x, 25, x, 75);
|
|
1775
|
-
* }
|
|
1776
|
-
* }
|
|
1777
|
-
* </code>
|
|
1778
|
-
* </div>
|
|
1779
|
-
*
|
|
1780
|
-
* <div>
|
|
1781
|
-
* <code>
|
|
1782
|
-
* function setup() {
|
|
1783
|
-
* createCanvas(100, 100);
|
|
1784
|
-
*
|
|
1785
|
-
* describe('Five white concentric circles drawn on a gray background.');
|
|
1786
|
-
* }
|
|
1787
|
-
*
|
|
1788
|
-
* function draw() {
|
|
1789
|
-
* background(200);
|
|
1790
|
-
*
|
|
1791
|
-
* // Draw concentric circles using a loop.
|
|
1792
|
-
* for (let d = 100; d > 0; d -= 20) {
|
|
1793
|
-
* circle(50, 50, d);
|
|
1794
|
-
* }
|
|
1795
|
-
* }
|
|
1796
|
-
* </code>
|
|
1797
|
-
* </div>
|
|
1798
|
-
*
|
|
1799
|
-
* <div>
|
|
1800
|
-
* <code>
|
|
1801
|
-
* function setup() {
|
|
1802
|
-
* createCanvas(100, 100);
|
|
1803
|
-
*
|
|
1804
|
-
* describe('A grid of black dots on a gray background.');
|
|
1805
|
-
* }
|
|
1806
|
-
*
|
|
1807
|
-
* function draw() {
|
|
1808
|
-
* background(200);
|
|
1809
|
-
*
|
|
1810
|
-
* // Set the spacing for points on the grid.
|
|
1811
|
-
* let space = 10;
|
|
1812
|
-
*
|
|
1813
|
-
* // Increase the stroke weight.
|
|
1814
|
-
* strokeWeight(3);
|
|
1815
|
-
*
|
|
1816
|
-
* // Loop from the left to the right.
|
|
1817
|
-
* for (let x = space; x < 100; x += space) {
|
|
1818
|
-
* // Loop from the top to the bottom.
|
|
1819
|
-
* for (let y = space; y < 100; y += space) {
|
|
1820
|
-
* point(x, y);
|
|
1821
|
-
* }
|
|
1822
|
-
* }
|
|
1823
|
-
* }
|
|
1824
|
-
* </code>
|
|
1825
|
-
* </div>
|
|
1826
|
-
*
|
|
1827
|
-
* <div>
|
|
1828
|
-
* <code>
|
|
1829
|
-
* // Declare the variable xCoordinates and assign it an array of numbers.
|
|
1830
|
-
* let xCoordinates = [20, 40, 60, 80];
|
|
1831
|
-
*
|
|
1832
|
-
* function setup() {
|
|
1833
|
-
* createCanvas(100, 100);
|
|
1834
|
-
*
|
|
1835
|
-
* describe('Four white circles drawn in a horizontal line on a gray background.');
|
|
1836
|
-
* }
|
|
1837
|
-
*
|
|
1838
|
-
* function draw() {
|
|
1839
|
-
* background(200);
|
|
1840
|
-
*
|
|
1841
|
-
* // Access the element at index i and use it to draw a circle.
|
|
1842
|
-
* for (let i = 0; i < xCoordinates.length; i += 1) {
|
|
1843
|
-
* circle(xCoordinates[i], 50, 20);
|
|
1844
|
-
* }
|
|
1845
|
-
* }
|
|
1846
|
-
* </code>
|
|
1847
|
-
* </div>
|
|
1848
|
-
*
|
|
1849
|
-
* <div>
|
|
1850
|
-
* <code>
|
|
1851
|
-
* // Declare the variable xCoordinates and assign it an array of numbers.
|
|
1852
|
-
* let xCoordinates = [20, 40, 60, 80];
|
|
1853
|
-
*
|
|
1854
|
-
* function setup() {
|
|
1855
|
-
* createCanvas(100, 100);
|
|
1856
|
-
*
|
|
1857
|
-
* describe('Four white circles drawn in a horizontal line on a gray background.');
|
|
1858
|
-
* }
|
|
1859
|
-
*
|
|
1860
|
-
* function draw() {
|
|
1861
|
-
* background(200);
|
|
1862
|
-
*
|
|
1863
|
-
* // Access each element of the array and use it to draw a circle.
|
|
1864
|
-
* for (let x of xCoordinates) {
|
|
1865
|
-
* circle(x, 50, 20);
|
|
1866
|
-
* }
|
|
1867
|
-
* }
|
|
1868
|
-
* </code>
|
|
1869
|
-
* </div>
|
|
1870
|
-
*/
|
|
1871
|
-
|
|
1872
|
-
/**
|
|
1873
|
-
* A way to repeat a block of code.
|
|
1874
|
-
*
|
|
1875
|
-
* `while` loops are helpful for repeating statements while a condition is
|
|
1876
|
-
* `true`. They're like `if` statements that repeat. For example, a `while`
|
|
1877
|
-
* loop makes it easy to express the idea "draw several lines" like so:
|
|
1878
|
-
*
|
|
1879
|
-
* ```js
|
|
1880
|
-
* // Declare a variable to keep track of iteration.
|
|
1881
|
-
* let x = 10;
|
|
1882
|
-
*
|
|
1883
|
-
* // Repeat as long as x < 100
|
|
1884
|
-
* while (x < 100) {
|
|
1885
|
-
* line(x, 25, x, 75);
|
|
1886
|
-
*
|
|
1887
|
-
* // Increment by 20.
|
|
1888
|
-
* x += 20;
|
|
1889
|
-
* }
|
|
1890
|
-
* ```
|
|
1891
|
-
*
|
|
1892
|
-
* The loop's header begins with the keyword `while`. Loops generally count up
|
|
1893
|
-
* or count down as they repeat, or iterate. The statement in parentheses
|
|
1894
|
-
* `x < 100` is a condition the loop checks each time it iterates. If the
|
|
1895
|
-
* condition is `true`, the loop runs the code between the curly braces `{}`,
|
|
1896
|
-
* The code between the curly braces is called the loop's body. If the
|
|
1897
|
-
* condition is `false`, the body is skipped and the loop is stopped.
|
|
1898
|
-
*
|
|
1899
|
-
* It's common to create infinite loops accidentally. For example, the
|
|
1900
|
-
* following loop never stops iterating because it doesn't count up:
|
|
1901
|
-
*
|
|
1902
|
-
* ```js
|
|
1903
|
-
* // Declare a variable to keep track of iteration.
|
|
1904
|
-
* let x = 10;
|
|
1905
|
-
*
|
|
1906
|
-
* // Repeat as long as x < 100
|
|
1907
|
-
* while (x < 100) {
|
|
1908
|
-
* line(x, 25, x, 75);
|
|
1909
|
-
* }
|
|
1910
|
-
*
|
|
1911
|
-
* // This should be in the loop's body!
|
|
1912
|
-
* x += 20;
|
|
1913
|
-
* ```
|
|
1914
|
-
*
|
|
1915
|
-
* The statement `x += 20` appears after the loop's body. That means the
|
|
1916
|
-
* variable `x` is stuck at `10`, which is always less than `100`.
|
|
1917
|
-
*
|
|
1918
|
-
* `while` loops are useful when the number of iterations isn't known in
|
|
1919
|
-
* advance. For example, concentric circles could be drawn at random
|
|
1920
|
-
* increments:
|
|
1921
|
-
*
|
|
1922
|
-
* ```js
|
|
1923
|
-
* let d = 100;
|
|
1924
|
-
* let minSize = 5;
|
|
1925
|
-
*
|
|
1926
|
-
* while (d > minSize) {
|
|
1927
|
-
* circle(50, 50, d);
|
|
1928
|
-
* d -= random(10);
|
|
1929
|
-
* }
|
|
1930
|
-
* ```
|
|
1931
|
-
*
|
|
1932
|
-
* @property while
|
|
1933
|
-
*
|
|
1934
|
-
* @example
|
|
1935
|
-
* <div>
|
|
1936
|
-
* <code>
|
|
1937
|
-
* function setup() {
|
|
1938
|
-
* createCanvas(100, 100);
|
|
1939
|
-
*
|
|
1940
|
-
* describe('Five black vertical lines on a gray background.');
|
|
1941
|
-
* }
|
|
1942
|
-
*
|
|
1943
|
-
* function draw() {
|
|
1944
|
-
* background(200);
|
|
1945
|
-
*
|
|
1946
|
-
* // Declare a variable to keep track of iteration.
|
|
1947
|
-
* let x = 10;
|
|
1948
|
-
*
|
|
1949
|
-
* // Repeat as long as x < 100
|
|
1950
|
-
* while (x < 100) {
|
|
1951
|
-
* line(x, 25, x, 75);
|
|
1952
|
-
*
|
|
1953
|
-
* // Increment by 20.
|
|
1954
|
-
* x += 20;
|
|
1955
|
-
* }
|
|
1956
|
-
* }
|
|
1957
|
-
* </code>
|
|
1958
|
-
* </div>
|
|
1959
|
-
*
|
|
1960
|
-
* <div>
|
|
1961
|
-
* <code>
|
|
1962
|
-
* function setup() {
|
|
1963
|
-
* createCanvas(100, 100);
|
|
1964
|
-
*
|
|
1965
|
-
* // Slow the frame rate.
|
|
1966
|
-
* frameRate(5);
|
|
1967
|
-
*
|
|
1968
|
-
* describe(
|
|
1969
|
-
* "A gray square with several concentric circles at the center. The circles' sizes decrease at random increments."
|
|
1970
|
-
* );
|
|
1971
|
-
* }
|
|
1972
|
-
*
|
|
1973
|
-
* function draw() {
|
|
1974
|
-
* background(200);
|
|
1975
|
-
*
|
|
1976
|
-
* let d = 100;
|
|
1977
|
-
* let minSize = 5;
|
|
1978
|
-
*
|
|
1979
|
-
* while (d > minSize) {
|
|
1980
|
-
* circle(50, 50, d);
|
|
1981
|
-
* d -= random(5, 15);
|
|
1982
|
-
* }
|
|
1983
|
-
* }
|
|
1984
|
-
* </code>
|
|
1985
|
-
* </div>
|
|
1986
|
-
*/
|
|
1987
|
-
|
|
1988
|
-
/**
|
|
1989
|
-
* Prints a message to the web browser's console.
|
|
1990
|
-
*
|
|
1991
|
-
* The <a href="https://developer.mozilla.org/en-US/docs/Web/API/console" target="_blank">console</a>
|
|
1992
|
-
* object is helpful for printing messages while debugging. For example, it's
|
|
1993
|
-
* common to add a `console.log()` statement while studying how a section of
|
|
1994
|
-
* code works:
|
|
1995
|
-
*
|
|
1996
|
-
* ```js
|
|
1997
|
-
* if (isPlaying === true) {
|
|
1998
|
-
* // Add a console.log() statement to make sure this block of code runs.
|
|
1999
|
-
* console.log('Got here!');
|
|
2000
|
-
*
|
|
2001
|
-
* // Game logic.
|
|
2002
|
-
* }
|
|
2003
|
-
* ```
|
|
2004
|
-
*
|
|
2005
|
-
* `console.error()` is helpful for tracking errors because it prints
|
|
2006
|
-
* formatted messages. For example, it's common to encounter errors when
|
|
2007
|
-
* loading media assets:
|
|
2008
|
-
*
|
|
2009
|
-
* ```js
|
|
2010
|
-
* // Logs an error message with special formatting.
|
|
2011
|
-
* function handleFailure(error) {
|
|
2012
|
-
* console.error('Oops!', error);
|
|
2013
|
-
* }
|
|
2014
|
-
*
|
|
2015
|
-
* // Try to load an image and call handleError() if it fails.
|
|
2016
|
-
* loadImage('https://example.com/cat.jpg', handleImage, handleError);
|
|
2017
|
-
* ```
|
|
2018
|
-
*
|
|
2019
|
-
* @property console
|
|
2020
|
-
*
|
|
2021
|
-
* @example
|
|
2022
|
-
* <div>
|
|
2023
|
-
* <code>
|
|
2024
|
-
* function setup() {
|
|
2025
|
-
* noCanvas();
|
|
2026
|
-
*
|
|
2027
|
-
* // Prints "Hello!" to the console.
|
|
2028
|
-
* console.log('Hello!');
|
|
2029
|
-
* }
|
|
2030
|
-
* </code>
|
|
2031
|
-
* </div>
|
|
2032
|
-
*
|
|
2033
|
-
* <div>
|
|
2034
|
-
* <code>
|
|
2035
|
-
* function setup() {
|
|
2036
|
-
* createCanvas(100, 100);
|
|
2037
|
-
*
|
|
2038
|
-
* background(200);
|
|
2039
|
-
*
|
|
2040
|
-
* // Try to load an image from a fake URL.
|
|
2041
|
-
* // Call handleError() if the image fails to load.
|
|
2042
|
-
* loadImage('https://example.com/cat.jpg', handleImage, handleError);
|
|
2043
|
-
* }
|
|
2044
|
-
*
|
|
2045
|
-
* // Displays the image.
|
|
2046
|
-
* function handleImage(img) {
|
|
2047
|
-
* image(img, 0, 0);
|
|
2048
|
-
*
|
|
2049
|
-
* describe('A cat on a gray background.');
|
|
2050
|
-
* }
|
|
2051
|
-
*
|
|
2052
|
-
* // Prints the error.
|
|
2053
|
-
* function handleError(error) {
|
|
2054
|
-
* console.error('Oops!', error);
|
|
2055
|
-
*
|
|
2056
|
-
* describe('A gray square.');
|
|
2057
|
-
* }
|
|
2058
|
-
* </code>
|
|
2059
|
-
* </div>
|
|
2060
|
-
*/
|