p5 2.3.0 → 2.3.1-rc.1
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/README.md +3 -11
- package/dist/accessibility/color_namer.js +7 -7
- package/dist/accessibility/index.js +7 -7
- package/dist/app.js +7 -7
- package/dist/app.node.js +7 -7
- package/dist/color/color_conversion.js +7 -7
- package/dist/color/creating_reading.js +1 -1
- package/dist/color/index.js +2 -2
- package/dist/color/p5.Color.js +1 -1
- package/dist/color/setting.js +2 -2
- package/dist/{constants-Dv6ZqA6s.js → constants-Bx3_xAam.js} +1 -1
- package/dist/core/constants.js +1 -1
- package/dist/core/environment.js +1 -1
- package/dist/core/filterShaders.js +1 -1
- package/dist/core/friendly_errors/fes_core.js +1 -1
- package/dist/core/friendly_errors/file_errors.js +1 -1
- package/dist/core/friendly_errors/index.js +1 -1
- package/dist/core/friendly_errors/param_validator.js +1 -1
- package/dist/core/friendly_errors/sketch_verifier.js +1 -1
- package/dist/core/helpers.js +1 -1
- package/dist/core/init.js +7 -7
- package/dist/core/internationalization.js +1 -1
- package/dist/core/legacy.js +7 -7
- package/dist/core/main.js +7 -7
- package/dist/core/p5.Graphics.js +4 -4
- package/dist/core/p5.Renderer.js +3 -3
- package/dist/core/p5.Renderer2D.js +7 -7
- package/dist/core/p5.Renderer3D.js +4 -4
- package/dist/core/rendering.js +4 -4
- package/dist/{creating_reading-DLkHH80h.js → creating_reading-YUSK33h8.js} +8 -0
- package/dist/dom/dom.js +2 -2
- package/dist/dom/index.js +2 -2
- package/dist/dom/p5.Element.js +2 -2
- package/dist/dom/p5.MediaElement.js +2 -2
- package/dist/events/pointer.js +15 -0
- package/dist/image/const.js +1 -1
- package/dist/image/filterRenderer2D.js +6 -6
- package/dist/image/image.js +4 -4
- package/dist/image/index.js +4 -4
- package/dist/image/loading_displaying.js +4 -4
- package/dist/image/p5.Image.js +3 -3
- package/dist/io/files.js +4 -4
- package/dist/io/index.js +4 -4
- package/dist/{ir_builders-C8mrXj5z.js → ir_builders-CMXkjMoV.js} +34 -24
- package/dist/{main-BR2ALC_6.js → main-BpW7VbDr.js} +39 -208
- package/dist/math/Matrices/Matrix.js +1 -1
- package/dist/math/Matrices/MatrixNumjs.js +1 -1
- package/dist/math/calculation.js +669 -84
- package/dist/math/index.js +1 -1
- package/dist/math/p5.Matrix.js +1 -1
- package/dist/math/p5.Vector.js +9 -1
- package/dist/math/patch-vector.js +3 -1
- package/dist/math/trigonometry.js +399 -48
- package/dist/{p5.Renderer-XOvhEbJn.js → p5.Renderer-op0Y1WO6.js} +2 -2
- package/dist/{rendering-C5lX6zKm.js → rendering-SZ71KIDM.js} +24 -9
- package/dist/shape/2d_primitives.js +1 -1
- package/dist/shape/attributes.js +4 -3
- package/dist/shape/custom_shapes.js +205 -3
- package/dist/shape/index.js +2 -2
- package/dist/strands/ir_builders.js +1 -1
- package/dist/strands/p5.strands.js +26 -9
- package/dist/strands/strands_FES.js +8 -1
- package/dist/strands/strands_api.js +316 -21
- package/dist/strands/strands_conditionals.js +1 -1
- package/dist/strands/strands_for.js +1 -1
- package/dist/strands/strands_node.js +1 -1
- package/dist/strands/strands_ternary.js +1 -1
- package/dist/{strands_glslBackend-D6G3UyCs.js → strands_glslBackend-DMhOnoGl.js} +2 -2
- package/dist/type/index.js +3 -3
- package/dist/type/p5.Font.js +13 -4
- package/dist/type/textCore.js +3 -3
- package/dist/webgl/3d_primitives.js +4 -4
- package/dist/webgl/GeometryBuilder.js +1 -1
- package/dist/webgl/ShapeBuilder.js +1 -1
- package/dist/webgl/enums.js +1 -1
- package/dist/webgl/index.js +6 -6
- package/dist/webgl/interaction.js +1 -1
- package/dist/webgl/light.js +4 -4
- package/dist/webgl/loading.js +4 -4
- package/dist/webgl/material.js +4 -4
- package/dist/webgl/p5.Camera.js +4 -4
- package/dist/webgl/p5.Framebuffer.js +4 -4
- package/dist/webgl/p5.Geometry.js +1 -1
- package/dist/webgl/p5.Quat.js +1 -1
- package/dist/webgl/p5.RendererGL.js +6 -6
- package/dist/webgl/p5.Shader.js +5 -1
- package/dist/webgl/p5.Texture.js +4 -4
- package/dist/webgl/strands_glslBackend.js +2 -2
- package/dist/webgl/text.js +4 -4
- package/dist/webgl/utils.js +4 -4
- package/dist/webgpu/index.js +2 -2
- package/dist/webgpu/p5.RendererWebGPU.js +2 -2
- package/dist/webgpu/strands_wgslBackend.js +2 -2
- package/lib/p5.esm.js +1765 -405
- package/lib/p5.esm.min.js +1 -1
- package/lib/p5.js +1765 -405
- package/lib/p5.min.js +1 -1
- package/lib/p5.webgpu.esm.js +34 -3
- package/lib/p5.webgpu.js +34 -3
- package/lib/p5.webgpu.min.js +1 -1
- package/package.json +1 -1
- package/types/global.d.ts +5539 -2604
- package/types/p5.d.ts +2917 -1432
package/dist/math/calculation.js
CHANGED
|
@@ -12,11 +12,7 @@ function calculation(p5, fn){
|
|
|
12
12
|
* -5 and 5 are both five units away from zero, so calling `abs(-5)` and
|
|
13
13
|
* `abs(5)` both return 5. The absolute value of a number is always positive.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
* @param {Number} n number to compute.
|
|
17
|
-
* @return {Number} absolute value of given number.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
15
|
+
* ```js example
|
|
20
16
|
* function setup() {
|
|
21
17
|
* createCanvas(100, 100);
|
|
22
18
|
*
|
|
@@ -36,6 +32,55 @@ function calculation(p5, fn){
|
|
|
36
32
|
* // from the middle.
|
|
37
33
|
* rect(0, 100 - h, 100, h);
|
|
38
34
|
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* `abs()` can also be used in shaders with p5.strands. The following example
|
|
38
|
+
* uses `abs()` to create a mirror effect on the color of a shape.
|
|
39
|
+
*
|
|
40
|
+
* ```js example
|
|
41
|
+
* let myShader;
|
|
42
|
+
*
|
|
43
|
+
* function setup() {
|
|
44
|
+
* createCanvas(100, 100, WEBGL);
|
|
45
|
+
* myShader = buildColorShader(shaderCallback);
|
|
46
|
+
* describe('A sphere with colors that fold back like a mirror.');
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* function shaderCallback() {
|
|
50
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
51
|
+
* let t = millis() * 0.001;
|
|
52
|
+
*
|
|
53
|
+
* // sin(t) goes between -1 and 1 over time.
|
|
54
|
+
* let sinVal = sin(t);
|
|
55
|
+
*
|
|
56
|
+
* // abs() folds the negative values to positive.
|
|
57
|
+
* // Now value goes between 0 and 1, creating a mirror effect.
|
|
58
|
+
* let value = abs(sinVal);
|
|
59
|
+
*
|
|
60
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
61
|
+
* let navy = [0.2, 0.2, 0.8, 1];
|
|
62
|
+
* let coral = [0.8, 0.2, 0.2, 1];
|
|
63
|
+
*
|
|
64
|
+
* finalColor.begin();
|
|
65
|
+
*
|
|
66
|
+
* // mix() blends between navy (when value = 0) and coral (when value = 1).
|
|
67
|
+
* finalColor.set(mix(navy, coral, value));
|
|
68
|
+
*
|
|
69
|
+
* finalColor.end();
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* function draw() {
|
|
73
|
+
* background(220);
|
|
74
|
+
* shader(myShader);
|
|
75
|
+
* noStroke();
|
|
76
|
+
* sphere(30);
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @method abs
|
|
81
|
+
* @param {Number} n number to compute.
|
|
82
|
+
* @return {Number} absolute value of given number.
|
|
83
|
+
*
|
|
39
84
|
*/
|
|
40
85
|
fn.abs = Math.abs;
|
|
41
86
|
|
|
@@ -46,11 +91,7 @@ function calculation(p5, fn){
|
|
|
46
91
|
* For example, calling `ceil(9.03)` and `ceil(9.97)` both return the value
|
|
47
92
|
* 10.
|
|
48
93
|
*
|
|
49
|
-
*
|
|
50
|
-
* @param {Number} n number to round up.
|
|
51
|
-
* @return {Integer} rounded up number.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
94
|
+
* ```js example
|
|
54
95
|
* function setup() {
|
|
55
96
|
* createCanvas(100, 100);
|
|
56
97
|
*
|
|
@@ -75,6 +116,57 @@ function calculation(p5, fn){
|
|
|
75
116
|
*
|
|
76
117
|
* describe('Two rectangles. The one on the left is dark red and the one on the right is bright red.');
|
|
77
118
|
* }
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* `ceil()` can also be used in shaders with p5.strands. The following example
|
|
122
|
+
* uses `ceil()` to create a stepped color effect on a shape.
|
|
123
|
+
*
|
|
124
|
+
* ```js example
|
|
125
|
+
* let myShader;
|
|
126
|
+
*
|
|
127
|
+
* function setup() {
|
|
128
|
+
* createCanvas(100, 100, WEBGL);
|
|
129
|
+
* myShader = buildColorShader(shaderCallback);
|
|
130
|
+
* describe('A sphere with stepped color bands.');
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* function shaderCallback() {
|
|
134
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
135
|
+
* let t = millis() * 0.001;
|
|
136
|
+
*
|
|
137
|
+
* // sin(t) goes between -1 and 1.
|
|
138
|
+
* // 0.5 + 0.5 * sin(t) remaps this to the 0 to 1 range.
|
|
139
|
+
* let sinVal = 0.5 + 0.5 * sin(t);
|
|
140
|
+
*
|
|
141
|
+
* // Multiply by 4 then ceil to get the next whole number up.
|
|
142
|
+
* // Divide by 4 to bring the result back to the 0 to 1 range.
|
|
143
|
+
* // This creates 4 distinct stepped color levels: 0.25, 0.5, 0.75, 1.
|
|
144
|
+
* let value = ceil(sinVal * 4) / 4;
|
|
145
|
+
*
|
|
146
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
147
|
+
* let cyan = [0, 0.5, 1, 1];
|
|
148
|
+
* let orange = [1, 0.5, 0, 1];
|
|
149
|
+
*
|
|
150
|
+
* finalColor.begin();
|
|
151
|
+
*
|
|
152
|
+
* // mix() blends between cyan (when value = 0) and orange (when value = 1).
|
|
153
|
+
* // ceil() creates sharp steps between color levels.
|
|
154
|
+
* finalColor.set(mix(cyan, orange, value));
|
|
155
|
+
*
|
|
156
|
+
* finalColor.end();
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* function draw() {
|
|
160
|
+
* background(220);
|
|
161
|
+
* shader(myShader);
|
|
162
|
+
* noStroke();
|
|
163
|
+
* sphere(30);
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @method ceil
|
|
168
|
+
* @param {Number} n number to round up.
|
|
169
|
+
* @return {Integer} rounded up number.
|
|
78
170
|
*/
|
|
79
171
|
fn.ceil = Math.ceil;
|
|
80
172
|
|
|
@@ -212,11 +304,7 @@ function calculation(p5, fn){
|
|
|
212
304
|
* Calculates the value of Euler's number e (2.71828...) raised to the power
|
|
213
305
|
* of a number.
|
|
214
306
|
*
|
|
215
|
-
*
|
|
216
|
-
* @param {Number} n exponent to raise.
|
|
217
|
-
* @return {Number} e^n
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
307
|
+
* ```js example
|
|
220
308
|
* function setup() {
|
|
221
309
|
* createCanvas(100, 100);
|
|
222
310
|
*
|
|
@@ -240,8 +328,9 @@ function calculation(p5, fn){
|
|
|
240
328
|
*
|
|
241
329
|
* describe('A series of circles that grow exponentially from top left to bottom right.');
|
|
242
330
|
* }
|
|
331
|
+
* ```
|
|
243
332
|
*
|
|
244
|
-
*
|
|
333
|
+
* ```js example
|
|
245
334
|
* function setup() {
|
|
246
335
|
* createCanvas(100, 100);
|
|
247
336
|
*
|
|
@@ -262,6 +351,53 @@ function calculation(p5, fn){
|
|
|
262
351
|
* // Draw a point.
|
|
263
352
|
* point(x, y);
|
|
264
353
|
* }
|
|
354
|
+
* ```
|
|
355
|
+
*
|
|
356
|
+
* `exp()` can also be used in shaders with p5.strands. The following example
|
|
357
|
+
* uses `exp()` to create an accelerating color transition on a shape.
|
|
358
|
+
*
|
|
359
|
+
* ```js example
|
|
360
|
+
* let myShader;
|
|
361
|
+
*
|
|
362
|
+
* function setup() {
|
|
363
|
+
* createCanvas(100, 100, WEBGL);
|
|
364
|
+
* myShader = buildColorShader(shaderCallback);
|
|
365
|
+
* describe('A sphere that brightens with accelerating speed.');
|
|
366
|
+
* }
|
|
367
|
+
*
|
|
368
|
+
* function shaderCallback() {
|
|
369
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.0005 to slow it.
|
|
370
|
+
* let t = millis() * 0.0005;
|
|
371
|
+
*
|
|
372
|
+
* // exp(t) grows slowly at first, then accelerates (exponential growth).
|
|
373
|
+
* // Multiply by 0.01 to keep it from growing too fast.
|
|
374
|
+
* // min(..., 1) caps the value at 1 so it doesn't go past white.
|
|
375
|
+
* let value = min(exp(t) * 0.01, 1);
|
|
376
|
+
*
|
|
377
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
378
|
+
* let darkBlue = [0.1, 0.1, 0.3, 1];
|
|
379
|
+
* let lightYellow = [1, 1, 0.5, 1];
|
|
380
|
+
*
|
|
381
|
+
* finalColor.begin();
|
|
382
|
+
*
|
|
383
|
+
* // mix() blends between darkBlue (when value = 0) and lightYellow (when value = 1).
|
|
384
|
+
* // Because exp() accelerates, the color transition gets faster over time.
|
|
385
|
+
* finalColor.set(mix(darkBlue, lightYellow, value));
|
|
386
|
+
*
|
|
387
|
+
* finalColor.end();
|
|
388
|
+
* }
|
|
389
|
+
*
|
|
390
|
+
* function draw() {
|
|
391
|
+
* background(220);
|
|
392
|
+
* shader(myShader);
|
|
393
|
+
* noStroke();
|
|
394
|
+
* sphere(30);
|
|
395
|
+
* }
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* @method exp
|
|
399
|
+
* @param {Number} n exponent to raise.
|
|
400
|
+
* @return {Number} e^n
|
|
265
401
|
*/
|
|
266
402
|
fn.exp = Math.exp;
|
|
267
403
|
|
|
@@ -269,11 +405,7 @@ function calculation(p5, fn){
|
|
|
269
405
|
* Calculates the closest integer value that is less than or equal to the
|
|
270
406
|
* value of a number.
|
|
271
407
|
*
|
|
272
|
-
*
|
|
273
|
-
* @param {Number} n number to round down.
|
|
274
|
-
* @return {Integer} rounded down number.
|
|
275
|
-
*
|
|
276
|
-
* @example
|
|
408
|
+
* ```js example
|
|
277
409
|
* function setup() {
|
|
278
410
|
* createCanvas(100, 100);
|
|
279
411
|
*
|
|
@@ -296,6 +428,57 @@ function calculation(p5, fn){
|
|
|
296
428
|
*
|
|
297
429
|
* describe('Two rectangles. The one on the left is bright red and the one on the right is black.');
|
|
298
430
|
* }
|
|
431
|
+
* ```
|
|
432
|
+
*
|
|
433
|
+
* `floor()` can also be used in shaders with p5.strands. The following example
|
|
434
|
+
* uses `floor()` to create banding effects on a shape.
|
|
435
|
+
*
|
|
436
|
+
* ```js example
|
|
437
|
+
* let myShader;
|
|
438
|
+
*
|
|
439
|
+
* function setup() {
|
|
440
|
+
* createCanvas(100, 100, WEBGL);
|
|
441
|
+
* myShader = buildColorShader(shaderCallback);
|
|
442
|
+
* describe('A sphere with posterized color bands.');
|
|
443
|
+
* }
|
|
444
|
+
*
|
|
445
|
+
* function shaderCallback() {
|
|
446
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
447
|
+
* let t = millis() * 0.001;
|
|
448
|
+
*
|
|
449
|
+
* // sin(t) goes between -1 and 1.
|
|
450
|
+
* // 0.5 + 0.5 * sin(t) remaps this to the 0 to 1 range.
|
|
451
|
+
* let sinVal = 0.5 + 0.5 * sin(t);
|
|
452
|
+
*
|
|
453
|
+
* // Multiply by 4 then floor to get the next whole number down.
|
|
454
|
+
* // Divide by 4 to bring the result back to the 0 to 1 range.
|
|
455
|
+
* // This creates 5 distinct stepped color levels: 0, 0.25, 0.5, 0.75, 1.
|
|
456
|
+
* let value = floor(sinVal * 4) / 4;
|
|
457
|
+
*
|
|
458
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
459
|
+
* let darkPurple = [0.2, 0, 0.8, 1];
|
|
460
|
+
* let brightTeal = [0.2, 1, 0.8, 1];
|
|
461
|
+
*
|
|
462
|
+
* finalColor.begin();
|
|
463
|
+
*
|
|
464
|
+
* // mix() blends between darkPurple (when value = 0) and brightTeal (when value = 1).
|
|
465
|
+
* // The floor() creates visible banding/posterization in the color.
|
|
466
|
+
* finalColor.set(mix(darkPurple, brightTeal, value));
|
|
467
|
+
*
|
|
468
|
+
* finalColor.end();
|
|
469
|
+
* }
|
|
470
|
+
*
|
|
471
|
+
* function draw() {
|
|
472
|
+
* background(220);
|
|
473
|
+
* shader(myShader);
|
|
474
|
+
* noStroke();
|
|
475
|
+
* sphere(30);
|
|
476
|
+
* }
|
|
477
|
+
* ```
|
|
478
|
+
*
|
|
479
|
+
* @method floor
|
|
480
|
+
* @param {Number} n number to round down.
|
|
481
|
+
* @return {Integer} rounded down number.
|
|
299
482
|
*/
|
|
300
483
|
fn.floor = Math.floor;
|
|
301
484
|
|
|
@@ -312,13 +495,7 @@ function calculation(p5, fn){
|
|
|
312
495
|
* number outside of the original interval. For example, calling
|
|
313
496
|
* `lerp(0, 10, 1.5)` will return 15.
|
|
314
497
|
*
|
|
315
|
-
*
|
|
316
|
-
* @param {Number} start first value.
|
|
317
|
-
* @param {Number} stop second value.
|
|
318
|
-
* @param {Number} amt number.
|
|
319
|
-
* @return {Number} lerped value.
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
498
|
+
* ```js example
|
|
322
499
|
* function setup() {
|
|
323
500
|
* createCanvas(100, 100);
|
|
324
501
|
*
|
|
@@ -346,8 +523,9 @@ function calculation(p5, fn){
|
|
|
346
523
|
*
|
|
347
524
|
* describe('Five points in a horizontal line. The outer points are black and the inner points are gray.');
|
|
348
525
|
* }
|
|
526
|
+
* ```
|
|
349
527
|
*
|
|
350
|
-
*
|
|
528
|
+
* ```js example
|
|
351
529
|
* let x = 50;
|
|
352
530
|
* let y = 50;
|
|
353
531
|
* let targetX = 50;
|
|
@@ -377,6 +555,54 @@ function calculation(p5, fn){
|
|
|
377
555
|
* x = mouseX;
|
|
378
556
|
* y = mouseY;
|
|
379
557
|
* }
|
|
558
|
+
* ```
|
|
559
|
+
*
|
|
560
|
+
* `lerp()` can also be used in shaders with p5.strands, where it maps to the
|
|
561
|
+
* <a href="#/p5/mix">mix()</a> function in GLSL. The following example
|
|
562
|
+
* uses `lerp()` to blend colors on a shape over time.
|
|
563
|
+
*
|
|
564
|
+
* ```js example
|
|
565
|
+
* let myShader;
|
|
566
|
+
*
|
|
567
|
+
* function setup() {
|
|
568
|
+
* createCanvas(100, 100, WEBGL);
|
|
569
|
+
* myShader = buildColorShader(shaderCallback);
|
|
570
|
+
* describe('A sphere that blends between teal and coral.');
|
|
571
|
+
* }
|
|
572
|
+
*
|
|
573
|
+
* function shaderCallback() {
|
|
574
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
575
|
+
* let t = millis() * 0.001;
|
|
576
|
+
*
|
|
577
|
+
* // sin(t) goes between -1 and 1 over time.
|
|
578
|
+
* // 0.5 + 0.5 * sin(t) remaps this to the 0 to 1 range.
|
|
579
|
+
* let value = 0.5 + 0.5 * sin(t);
|
|
580
|
+
*
|
|
581
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
582
|
+
* let teal = [0, 0.8, 0.8, 1];
|
|
583
|
+
* let coral = [1, 0.5, 0.3, 1];
|
|
584
|
+
*
|
|
585
|
+
* finalColor.begin();
|
|
586
|
+
*
|
|
587
|
+
* // lerp() blends teal (when value = 0) and coral (when value = 1).
|
|
588
|
+
* finalColor.set(lerp(teal, coral, value));
|
|
589
|
+
*
|
|
590
|
+
* finalColor.end();
|
|
591
|
+
* }
|
|
592
|
+
*
|
|
593
|
+
* function draw() {
|
|
594
|
+
* background(220);
|
|
595
|
+
* shader(myShader);
|
|
596
|
+
* noStroke();
|
|
597
|
+
* sphere(30);
|
|
598
|
+
* }
|
|
599
|
+
* ```
|
|
600
|
+
*
|
|
601
|
+
* @method lerp
|
|
602
|
+
* @param {Number} start first value.
|
|
603
|
+
* @param {Number} stop second value.
|
|
604
|
+
* @param {Number} amt number.
|
|
605
|
+
* @return {Number} lerped value.
|
|
380
606
|
*/
|
|
381
607
|
fn.lerp = function(start, stop, amt) {
|
|
382
608
|
// p5._validateParameters('lerp', arguments);
|
|
@@ -389,11 +615,7 @@ function calculation(p5, fn){
|
|
|
389
615
|
* `log()` expects the `n` parameter to be a value greater than 0 because
|
|
390
616
|
* the natural logarithm is defined that way.
|
|
391
617
|
*
|
|
392
|
-
*
|
|
393
|
-
* @param {Number} n number greater than 0.
|
|
394
|
-
* @return {Number} natural logarithm of n.
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
618
|
+
* ```js example
|
|
397
619
|
* function setup() {
|
|
398
620
|
* createCanvas(100, 100);
|
|
399
621
|
*
|
|
@@ -409,8 +631,9 @@ function calculation(p5, fn){
|
|
|
409
631
|
*
|
|
410
632
|
* describe('Two white circles. The circle at the top-left is small. The circle at the bottom-right is about five times larger.');
|
|
411
633
|
* }
|
|
634
|
+
* ```
|
|
412
635
|
*
|
|
413
|
-
*
|
|
636
|
+
* ```js example
|
|
414
637
|
* function setup() {
|
|
415
638
|
* createCanvas(100, 100);
|
|
416
639
|
*
|
|
@@ -431,6 +654,53 @@ function calculation(p5, fn){
|
|
|
431
654
|
* // Draw a point.
|
|
432
655
|
* point(x, y);
|
|
433
656
|
* }
|
|
657
|
+
* ```
|
|
658
|
+
*
|
|
659
|
+
* `log()` can also be used in shaders with p5.strands. The following example
|
|
660
|
+
* uses `log()` to create a decelerating color transition on a shape.
|
|
661
|
+
*
|
|
662
|
+
* ```js example
|
|
663
|
+
* let myShader;
|
|
664
|
+
*
|
|
665
|
+
* function setup() {
|
|
666
|
+
* createCanvas(100, 100, WEBGL);
|
|
667
|
+
* myShader = buildColorShader(shaderCallback);
|
|
668
|
+
* describe('A sphere that slowly shifts from purple to yellow.');
|
|
669
|
+
* }
|
|
670
|
+
*
|
|
671
|
+
* function shaderCallback() {
|
|
672
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
673
|
+
* let t = millis() * 0.001;
|
|
674
|
+
*
|
|
675
|
+
* // log(1 + t * 2) grows at a moderate pace over time.
|
|
676
|
+
* // Multiply by 0.4 to bring it into the 0 to 1 range.
|
|
677
|
+
* // min(..., 1) caps the value at 1.
|
|
678
|
+
* let value = min(log(1 + t * 2) * 0.4, 1);
|
|
679
|
+
*
|
|
680
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
681
|
+
* let purple = [0.5, 0, 0.5, 1];
|
|
682
|
+
* let yellow = [1, 1, 0, 1];
|
|
683
|
+
*
|
|
684
|
+
* finalColor.begin();
|
|
685
|
+
*
|
|
686
|
+
* // mix() blends between purple (when value = 0) and yellow (when value = 1).
|
|
687
|
+
* // Because log() slows down over time, the color transition decelerates.
|
|
688
|
+
* finalColor.set(mix(purple, yellow, value));
|
|
689
|
+
*
|
|
690
|
+
* finalColor.end();
|
|
691
|
+
* }
|
|
692
|
+
*
|
|
693
|
+
* function draw() {
|
|
694
|
+
* background(220);
|
|
695
|
+
* shader(myShader);
|
|
696
|
+
* noStroke();
|
|
697
|
+
* sphere(30);
|
|
698
|
+
* }
|
|
699
|
+
* ```
|
|
700
|
+
*
|
|
701
|
+
* @method log
|
|
702
|
+
* @param {Number} n number greater than 0.
|
|
703
|
+
* @return {Number} natural logarithm of n.
|
|
434
704
|
*/
|
|
435
705
|
fn.log = Math.log;
|
|
436
706
|
|
|
@@ -498,16 +768,7 @@ function calculation(p5, fn){
|
|
|
498
768
|
* constrains the remapped value to the target range. For example,
|
|
499
769
|
* `map(11, 0, 10, 0, 100, true)` returns 100.
|
|
500
770
|
*
|
|
501
|
-
*
|
|
502
|
-
* @param {Number} value the value to be remapped.
|
|
503
|
-
* @param {Number} start1 lower bound of the value's current range.
|
|
504
|
-
* @param {Number} stop1 upper bound of the value's current range.
|
|
505
|
-
* @param {Number} start2 lower bound of the value's target range.
|
|
506
|
-
* @param {Number} stop2 upper bound of the value's target range.
|
|
507
|
-
* @param {Boolean} [withinBounds] constrain the value to the newly mapped range.
|
|
508
|
-
* @return {Number} remapped number.
|
|
509
|
-
*
|
|
510
|
-
* @example
|
|
771
|
+
* ```js example
|
|
511
772
|
* function setup() {
|
|
512
773
|
* createCanvas(100, 100);
|
|
513
774
|
*
|
|
@@ -526,8 +787,9 @@ function calculation(p5, fn){
|
|
|
526
787
|
* // Draw the bottom line.
|
|
527
788
|
* line(0, 75, 0, x);
|
|
528
789
|
* }
|
|
790
|
+
* ```
|
|
529
791
|
*
|
|
530
|
-
*
|
|
792
|
+
* ```js example
|
|
531
793
|
* function setup() {
|
|
532
794
|
* createCanvas(100, 100);
|
|
533
795
|
*
|
|
@@ -546,6 +808,58 @@ function calculation(p5, fn){
|
|
|
546
808
|
* // Draw the circle.
|
|
547
809
|
* circle(50, 50, 20);
|
|
548
810
|
* }
|
|
811
|
+
* ```
|
|
812
|
+
*
|
|
813
|
+
* `map()` can also be used in shaders with p5.strands. The following example
|
|
814
|
+
* uses `map()` to remap time values to color in a shader.
|
|
815
|
+
*
|
|
816
|
+
* ```js example
|
|
817
|
+
* let myShader;
|
|
818
|
+
*
|
|
819
|
+
* function setup() {
|
|
820
|
+
* createCanvas(100, 100, WEBGL);
|
|
821
|
+
* myShader = buildColorShader(shaderCallback);
|
|
822
|
+
* describe('A sphere that shifts between cyan and orange over time.');
|
|
823
|
+
* }
|
|
824
|
+
*
|
|
825
|
+
* function shaderCallback() {
|
|
826
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
827
|
+
* let t = millis() * 0.001;
|
|
828
|
+
*
|
|
829
|
+
* // sin(t) goes between -1 and 1 over time.
|
|
830
|
+
* let sinVal = sin(t);
|
|
831
|
+
*
|
|
832
|
+
* // map() remaps this from the range [-1, 1] to the range [0, 1].
|
|
833
|
+
* let value = map(sinVal, -1, 1, 0, 1);
|
|
834
|
+
*
|
|
835
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
836
|
+
* let cyan = [0, 0.5, 1, 1];
|
|
837
|
+
* let orange = [1, 0.5, 0, 1];
|
|
838
|
+
*
|
|
839
|
+
* finalColor.begin();
|
|
840
|
+
*
|
|
841
|
+
* // mix() blends between cyan (when value = 0) and orange (when value = 1).
|
|
842
|
+
* finalColor.set(mix(cyan, orange, value));
|
|
843
|
+
*
|
|
844
|
+
* finalColor.end();
|
|
845
|
+
* }
|
|
846
|
+
*
|
|
847
|
+
* function draw() {
|
|
848
|
+
* background(220);
|
|
849
|
+
* shader(myShader);
|
|
850
|
+
* noStroke();
|
|
851
|
+
* sphere(30);
|
|
852
|
+
* }
|
|
853
|
+
* ```
|
|
854
|
+
*
|
|
855
|
+
* @method map
|
|
856
|
+
* @param {Number} value the value to be remapped.
|
|
857
|
+
* @param {Number} start1 lower bound of the value's current range.
|
|
858
|
+
* @param {Number} stop1 upper bound of the value's current range.
|
|
859
|
+
* @param {Number} start2 lower bound of the value's target range.
|
|
860
|
+
* @param {Number} stop2 upper bound of the value's target range.
|
|
861
|
+
* @param {Boolean} [withinBounds] constrain the value to the newly mapped range.
|
|
862
|
+
* @return {Number} remapped number.
|
|
549
863
|
*/
|
|
550
864
|
fn.map = function(n, start1, stop1, start2, stop2, withinBounds) {
|
|
551
865
|
// p5._validateParameters('map', arguments);
|
|
@@ -569,12 +883,7 @@ function calculation(p5, fn){
|
|
|
569
883
|
* The version of `max()` with two or more parameters interprets them as
|
|
570
884
|
* individual numbers and returns the largest number.
|
|
571
885
|
*
|
|
572
|
-
*
|
|
573
|
-
* @param {Number} n0 first number to compare.
|
|
574
|
-
* @param {Number} n1 second number to compare.
|
|
575
|
-
* @return {Number} maximum number.
|
|
576
|
-
*
|
|
577
|
-
* @example
|
|
886
|
+
* ```js example
|
|
578
887
|
* function setup() {
|
|
579
888
|
* createCanvas(100, 100);
|
|
580
889
|
*
|
|
@@ -592,8 +901,9 @@ function calculation(p5, fn){
|
|
|
592
901
|
*
|
|
593
902
|
* describe('The number 20 written in the middle of a gray square.');
|
|
594
903
|
* }
|
|
904
|
+
* ```
|
|
595
905
|
*
|
|
596
|
-
*
|
|
906
|
+
* ```js example
|
|
597
907
|
* function setup() {
|
|
598
908
|
* createCanvas(100, 100);
|
|
599
909
|
*
|
|
@@ -614,6 +924,53 @@ function calculation(p5, fn){
|
|
|
614
924
|
*
|
|
615
925
|
* describe('The number 20 written in the middle of a gray square.');
|
|
616
926
|
* }
|
|
927
|
+
* ```
|
|
928
|
+
*
|
|
929
|
+
* `max()` can also be used in shaders with p5.strands. The following example
|
|
930
|
+
* uses `max()` to clamp values in a shader.
|
|
931
|
+
*
|
|
932
|
+
* ```js example
|
|
933
|
+
* let myShader;
|
|
934
|
+
*
|
|
935
|
+
* function setup() {
|
|
936
|
+
* createCanvas(100, 100, WEBGL);
|
|
937
|
+
* myShader = buildColorShader(shaderCallback);
|
|
938
|
+
* describe('A sphere that shifts from rose to steelBlue and stops.');
|
|
939
|
+
* }
|
|
940
|
+
*
|
|
941
|
+
* function shaderCallback() {
|
|
942
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
943
|
+
* let t = millis() * 0.001;
|
|
944
|
+
*
|
|
945
|
+
* // 1 - t * 0.2 decreases steadily over time.
|
|
946
|
+
* // max(..., 0) ensures the value never goes below 0.
|
|
947
|
+
* let value = max(1 - t * 0.2, 0);
|
|
948
|
+
*
|
|
949
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
950
|
+
* let steelBlue = [0, 0.3, 0.8, 1];
|
|
951
|
+
* let rose = [1, 0.3, 0.8, 1];
|
|
952
|
+
*
|
|
953
|
+
* finalColor.begin();
|
|
954
|
+
*
|
|
955
|
+
* // mix() blends between steelBlue (when value = 0) and rose (when value = 1).
|
|
956
|
+
* // max() clamps the blend so the color stops changing once it reaches steelBlue.
|
|
957
|
+
* finalColor.set(mix(steelBlue, rose, value));
|
|
958
|
+
*
|
|
959
|
+
* finalColor.end();
|
|
960
|
+
* }
|
|
961
|
+
*
|
|
962
|
+
* function draw() {
|
|
963
|
+
* background(220);
|
|
964
|
+
* shader(myShader);
|
|
965
|
+
* noStroke();
|
|
966
|
+
* sphere(30);
|
|
967
|
+
* }
|
|
968
|
+
* ```
|
|
969
|
+
*
|
|
970
|
+
* @method max
|
|
971
|
+
* @param {Number} n0 first number to compare.
|
|
972
|
+
* @param {Number} n1 second number to compare.
|
|
973
|
+
* @return {Number} maximum number.
|
|
617
974
|
*/
|
|
618
975
|
/**
|
|
619
976
|
* @method max
|
|
@@ -645,12 +1002,7 @@ function calculation(p5, fn){
|
|
|
645
1002
|
* The version of `min()` with two or more parameters interprets them as
|
|
646
1003
|
* individual numbers and returns the smallest number.
|
|
647
1004
|
*
|
|
648
|
-
*
|
|
649
|
-
* @param {Number} n0 first number to compare.
|
|
650
|
-
* @param {Number} n1 second number to compare.
|
|
651
|
-
* @return {Number} minimum number.
|
|
652
|
-
*
|
|
653
|
-
* @example
|
|
1005
|
+
* ```js example
|
|
654
1006
|
* function setup() {
|
|
655
1007
|
* createCanvas(100, 100);
|
|
656
1008
|
*
|
|
@@ -668,8 +1020,9 @@ function calculation(p5, fn){
|
|
|
668
1020
|
*
|
|
669
1021
|
* describe('The number 5 written in the middle of a gray square.');
|
|
670
1022
|
* }
|
|
1023
|
+
* ```
|
|
671
1024
|
*
|
|
672
|
-
*
|
|
1025
|
+
* ```js example
|
|
673
1026
|
* function setup() {
|
|
674
1027
|
* createCanvas(100, 100);
|
|
675
1028
|
*
|
|
@@ -690,6 +1043,53 @@ function calculation(p5, fn){
|
|
|
690
1043
|
*
|
|
691
1044
|
* describe('The number 5 written in the middle of a gray square.');
|
|
692
1045
|
* }
|
|
1046
|
+
* ```
|
|
1047
|
+
*
|
|
1048
|
+
* `min()` can also be used in shaders with p5.strands. The following example
|
|
1049
|
+
* uses `min()` to clamp values in a shader.
|
|
1050
|
+
*
|
|
1051
|
+
* ```js example
|
|
1052
|
+
* let myShader;
|
|
1053
|
+
*
|
|
1054
|
+
* function setup() {
|
|
1055
|
+
* createCanvas(100, 100, WEBGL);
|
|
1056
|
+
* myShader = buildColorShader(shaderCallback);
|
|
1057
|
+
* describe('A sphere that shifts from red to green and stops.');
|
|
1058
|
+
* }
|
|
1059
|
+
*
|
|
1060
|
+
* function shaderCallback() {
|
|
1061
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
1062
|
+
* let t = millis() * 0.001;
|
|
1063
|
+
*
|
|
1064
|
+
* // t * 0.2 grows steadily over time.
|
|
1065
|
+
* // min(..., 1) caps the value at 1 so it doesn't go past the target color.
|
|
1066
|
+
* let value = min(t * 0.2, 1);
|
|
1067
|
+
*
|
|
1068
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
1069
|
+
* let deepRed = [0.8, 0, 0.2, 1];
|
|
1070
|
+
* let yellowGreen = [0.8, 1, 0.2, 1];
|
|
1071
|
+
*
|
|
1072
|
+
* finalColor.begin();
|
|
1073
|
+
*
|
|
1074
|
+
* // mix() blends between deepRed (when value = 0) and yellowGreen (when value = 1).
|
|
1075
|
+
* // min() clamps the blend so the color stops changing once it reaches yellowGreen.
|
|
1076
|
+
* finalColor.set(mix(deepRed, yellowGreen, value));
|
|
1077
|
+
*
|
|
1078
|
+
* finalColor.end();
|
|
1079
|
+
* }
|
|
1080
|
+
*
|
|
1081
|
+
* function draw() {
|
|
1082
|
+
* background(220);
|
|
1083
|
+
* shader(myShader);
|
|
1084
|
+
* noStroke();
|
|
1085
|
+
* sphere(30);
|
|
1086
|
+
* }
|
|
1087
|
+
* ```
|
|
1088
|
+
*
|
|
1089
|
+
* @method min
|
|
1090
|
+
* @param {Number} n0 first number to compare.
|
|
1091
|
+
* @param {Number} n1 second number to compare.
|
|
1092
|
+
* @return {Number} minimum number.
|
|
693
1093
|
*/
|
|
694
1094
|
/**
|
|
695
1095
|
* @method min
|
|
@@ -758,12 +1158,7 @@ function calculation(p5, fn){
|
|
|
758
1158
|
* 2 × 2 × 2. `pow(2, -3)` evaluates 1 ÷
|
|
759
1159
|
* (2 × 2 × 2).
|
|
760
1160
|
*
|
|
761
|
-
*
|
|
762
|
-
* @param {Number} n base of the exponential expression.
|
|
763
|
-
* @param {Number} e power by which to raise the base.
|
|
764
|
-
* @return {Number} n^e.
|
|
765
|
-
*
|
|
766
|
-
* @example
|
|
1161
|
+
* ```js example
|
|
767
1162
|
* function setup() {
|
|
768
1163
|
* createCanvas(100, 100);
|
|
769
1164
|
*
|
|
@@ -790,6 +1185,52 @@ function calculation(p5, fn){
|
|
|
790
1185
|
*
|
|
791
1186
|
* describe('A series of circles that grow exponentially from top left to bottom right.');
|
|
792
1187
|
* }
|
|
1188
|
+
* ```
|
|
1189
|
+
*
|
|
1190
|
+
* `pow()` can also be used in shaders with p5.strands. The following example
|
|
1191
|
+
* uses `pow()` to create a gamma curve effect on colors in a shader.
|
|
1192
|
+
*
|
|
1193
|
+
* ```js example
|
|
1194
|
+
* let myShader;
|
|
1195
|
+
*
|
|
1196
|
+
* function setup() {
|
|
1197
|
+
* createCanvas(100, 100, WEBGL);
|
|
1198
|
+
* myShader = buildColorShader(shaderCallback);
|
|
1199
|
+
* describe('A sphere with colors that shift with a power curve.');
|
|
1200
|
+
* }
|
|
1201
|
+
*
|
|
1202
|
+
* function shaderCallback() {
|
|
1203
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.0005 to slow it.
|
|
1204
|
+
* let t = millis() * 0.0005;
|
|
1205
|
+
*
|
|
1206
|
+
* // pow(t, 2) squares the time value: it starts slow then accelerates.
|
|
1207
|
+
* let value = pow(t, 2);
|
|
1208
|
+
*
|
|
1209
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
1210
|
+
* let deepBlue = [0, 0.1, 0.5, 1];
|
|
1211
|
+
* let gold = [1, 0.8, 0, 1];
|
|
1212
|
+
*
|
|
1213
|
+
* finalColor.begin();
|
|
1214
|
+
*
|
|
1215
|
+
* // mix() blends between deepBlue (when value = 0) and gold (when value = 1).
|
|
1216
|
+
* // Because pow() accelerates, the color transition gets faster over time.
|
|
1217
|
+
* finalColor.set(mix(deepBlue, gold, value));
|
|
1218
|
+
*
|
|
1219
|
+
* finalColor.end();
|
|
1220
|
+
* }
|
|
1221
|
+
*
|
|
1222
|
+
* function draw() {
|
|
1223
|
+
* background(220);
|
|
1224
|
+
* shader(myShader);
|
|
1225
|
+
* noStroke();
|
|
1226
|
+
* sphere(30);
|
|
1227
|
+
* }
|
|
1228
|
+
* ```
|
|
1229
|
+
*
|
|
1230
|
+
* @method pow
|
|
1231
|
+
* @param {Number} n base of the exponential expression.
|
|
1232
|
+
* @param {Number} e power by which to raise the base.
|
|
1233
|
+
* @return {Number} n^e.
|
|
793
1234
|
*/
|
|
794
1235
|
fn.pow = Math.pow;
|
|
795
1236
|
|
|
@@ -802,12 +1243,7 @@ function calculation(p5, fn){
|
|
|
802
1243
|
* decimal places to use when rounding. For example, `round(12.34, 1)` returns
|
|
803
1244
|
* 12.3. `decimals` is 0 by default.
|
|
804
1245
|
*
|
|
805
|
-
*
|
|
806
|
-
* @param {Number} n number to round.
|
|
807
|
-
* @param {Number} [decimals] number of decimal places to round to, default is 0.
|
|
808
|
-
* @return {Integer} rounded number.
|
|
809
|
-
*
|
|
810
|
-
* @example
|
|
1246
|
+
* ```js example
|
|
811
1247
|
* function setup() {
|
|
812
1248
|
* createCanvas(100, 100);
|
|
813
1249
|
*
|
|
@@ -825,8 +1261,9 @@ function calculation(p5, fn){
|
|
|
825
1261
|
*
|
|
826
1262
|
* describe('The number 4 written in middle of the canvas.');
|
|
827
1263
|
* }
|
|
1264
|
+
* ```
|
|
828
1265
|
*
|
|
829
|
-
*
|
|
1266
|
+
* ```js example
|
|
830
1267
|
* function setup() {
|
|
831
1268
|
* createCanvas(100, 100);
|
|
832
1269
|
*
|
|
@@ -844,6 +1281,57 @@ function calculation(p5, fn){
|
|
|
844
1281
|
*
|
|
845
1282
|
* describe('The number 12.78 written in middle of canvas.');
|
|
846
1283
|
* }
|
|
1284
|
+
* ```
|
|
1285
|
+
*
|
|
1286
|
+
* `round()` can also be used in shaders with p5.strands. The following example
|
|
1287
|
+
* uses `round()` to quantize colors in a shader.
|
|
1288
|
+
*
|
|
1289
|
+
* ```js example
|
|
1290
|
+
* let myShader;
|
|
1291
|
+
*
|
|
1292
|
+
* function setup() {
|
|
1293
|
+
* createCanvas(100, 100, WEBGL);
|
|
1294
|
+
* myShader = buildColorShader(shaderCallback);
|
|
1295
|
+
* describe('A sphere with posterized quantized colors.');
|
|
1296
|
+
* }
|
|
1297
|
+
*
|
|
1298
|
+
* function shaderCallback() {
|
|
1299
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
1300
|
+
* let t = millis() * 0.001;
|
|
1301
|
+
*
|
|
1302
|
+
* // sin(t) goes between -1 and 1.
|
|
1303
|
+
* // 0.5 + 0.5 * sin(t) remaps this to the 0 to 1 range.
|
|
1304
|
+
* let sinVal = 0.5 + 0.5 * sin(t);
|
|
1305
|
+
*
|
|
1306
|
+
* // Multiply by 4 then round to get 5 distinct levels (0, 0.25, 0.5, 0.75, 1).
|
|
1307
|
+
* // Divide by 4 to bring the result back to the 0 to 1 range.
|
|
1308
|
+
* let value = round(sinVal * 4) / 4;
|
|
1309
|
+
*
|
|
1310
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
1311
|
+
* let mutedBlue = [0.3, 0.4, 0.7, 1];
|
|
1312
|
+
* let rose = [0.8, 0.3, 0.4, 1];
|
|
1313
|
+
*
|
|
1314
|
+
* finalColor.begin();
|
|
1315
|
+
*
|
|
1316
|
+
* // mix() blends between mutedBlue (when value = 0) and rose (when value = 1).
|
|
1317
|
+
* // The round() creates stepped bands of color like a posterization effect.
|
|
1318
|
+
* finalColor.set(mix(mutedBlue, rose, value));
|
|
1319
|
+
*
|
|
1320
|
+
* finalColor.end();
|
|
1321
|
+
* }
|
|
1322
|
+
*
|
|
1323
|
+
* function draw() {
|
|
1324
|
+
* background(220);
|
|
1325
|
+
* shader(myShader);
|
|
1326
|
+
* noStroke();
|
|
1327
|
+
* sphere(30);
|
|
1328
|
+
* }
|
|
1329
|
+
* ```
|
|
1330
|
+
*
|
|
1331
|
+
* @method round
|
|
1332
|
+
* @param {Number} n number to round.
|
|
1333
|
+
* @param {Number} [decimals] number of decimal places to round to, default is 0.
|
|
1334
|
+
* @return {Integer} rounded number.
|
|
847
1335
|
*/
|
|
848
1336
|
fn.round = function(n, decimals) {
|
|
849
1337
|
if (!decimals) {
|
|
@@ -914,11 +1402,7 @@ function calculation(p5, fn){
|
|
|
914
1402
|
* always returns a positive value. `sqrt()` doesn't work with negative arguments
|
|
915
1403
|
* such as `sqrt(-9)`.
|
|
916
1404
|
*
|
|
917
|
-
*
|
|
918
|
-
* @param {Number} n non-negative number to square root.
|
|
919
|
-
* @return {Number} square root of number.
|
|
920
|
-
*
|
|
921
|
-
* @example
|
|
1405
|
+
* ```js example
|
|
922
1406
|
* function setup() {
|
|
923
1407
|
* createCanvas(100, 100);
|
|
924
1408
|
*
|
|
@@ -934,8 +1418,9 @@ function calculation(p5, fn){
|
|
|
934
1418
|
*
|
|
935
1419
|
* describe('Two white circles. The circle at the top-left is small. The circle at the bottom-right is ten times larger.');
|
|
936
1420
|
* }
|
|
1421
|
+
* ```
|
|
937
1422
|
*
|
|
938
|
-
*
|
|
1423
|
+
* ```js example
|
|
939
1424
|
* function setup() {
|
|
940
1425
|
* createCanvas(100, 100);
|
|
941
1426
|
*
|
|
@@ -956,6 +1441,63 @@ function calculation(p5, fn){
|
|
|
956
1441
|
* // Draw the point.
|
|
957
1442
|
* point(x, y);
|
|
958
1443
|
* }
|
|
1444
|
+
* ```
|
|
1445
|
+
*
|
|
1446
|
+
* `sqrt()` can also be used in shaders with p5.strands. The following example
|
|
1447
|
+
* uses `sqrt()` to create a smooth ease-out curve on color and size.
|
|
1448
|
+
*
|
|
1449
|
+
* ```js example
|
|
1450
|
+
* let myShader;
|
|
1451
|
+
*
|
|
1452
|
+
* function setup() {
|
|
1453
|
+
* createCanvas(100, 100, WEBGL);
|
|
1454
|
+
* myShader = buildColorShader(shaderCallback);
|
|
1455
|
+
* describe('A sphere that grows and shifts from navy to orange with an ease-out curve.');
|
|
1456
|
+
* }
|
|
1457
|
+
*
|
|
1458
|
+
* function shaderCallback() {
|
|
1459
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
1460
|
+
* let t = millis() * 0.001;
|
|
1461
|
+
*
|
|
1462
|
+
* // sin(t) goes between -1 and 1.
|
|
1463
|
+
* // 0.5 + 0.5 * sin(t) remaps this to the 0 to 1 range.
|
|
1464
|
+
* let sinVal = 0.5 + 0.5 * sin(t);
|
|
1465
|
+
*
|
|
1466
|
+
* // sqrt(sinVal) creates an ease-out curve: fast start, slow finish.
|
|
1467
|
+
* // Since sinVal is in [0,1], sqrt() stays in [0,1].
|
|
1468
|
+
* let value = sqrt(sinVal);
|
|
1469
|
+
*
|
|
1470
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
1471
|
+
* let navy = [0, 0.1, 0.4, 1];
|
|
1472
|
+
* let brightOrange = [1, 0.6, 0, 1];
|
|
1473
|
+
*
|
|
1474
|
+
* finalColor.begin();
|
|
1475
|
+
*
|
|
1476
|
+
* // mix() blends between navy (when value = 0) and brightOrange (when value = 1).
|
|
1477
|
+
* // The sqrt() ease-out makes the color change fast at first, then slow down.
|
|
1478
|
+
* finalColor.set(mix(navy, brightOrange, value));
|
|
1479
|
+
*
|
|
1480
|
+
* finalColor.end();
|
|
1481
|
+
* }
|
|
1482
|
+
*
|
|
1483
|
+
* function drawShape() {
|
|
1484
|
+
* let t = millis() * 0.001;
|
|
1485
|
+
* let sinVal = 0.5 + 0.5 * sin(t);
|
|
1486
|
+
* let size = 10 + sqrt(sinVal) * 30;
|
|
1487
|
+
* sphere(size);
|
|
1488
|
+
* }
|
|
1489
|
+
*
|
|
1490
|
+
* function draw() {
|
|
1491
|
+
* background(220);
|
|
1492
|
+
* shader(myShader);
|
|
1493
|
+
* noStroke();
|
|
1494
|
+
* drawShape();
|
|
1495
|
+
* }
|
|
1496
|
+
* ```
|
|
1497
|
+
*
|
|
1498
|
+
* @method sqrt
|
|
1499
|
+
* @param {Number} n non-negative number to square root.
|
|
1500
|
+
* @return {Number} square root of number.
|
|
959
1501
|
*/
|
|
960
1502
|
fn.sqrt = Math.sqrt;
|
|
961
1503
|
|
|
@@ -965,11 +1507,7 @@ function calculation(p5, fn){
|
|
|
965
1507
|
* A number's fractional part includes its decimal values. For example,
|
|
966
1508
|
* `fract(12.34)` returns 0.34.
|
|
967
1509
|
*
|
|
968
|
-
*
|
|
969
|
-
* @param {Number} n number whose fractional part will be found.
|
|
970
|
-
* @returns {Number} fractional part of n.
|
|
971
|
-
*
|
|
972
|
-
* @example
|
|
1510
|
+
* ```js example
|
|
973
1511
|
* function setup() {
|
|
974
1512
|
* createCanvas(100, 100);
|
|
975
1513
|
*
|
|
@@ -989,6 +1527,53 @@ function calculation(p5, fn){
|
|
|
989
1527
|
*
|
|
990
1528
|
* describe('The number 56.78 written above the number 0.78.');
|
|
991
1529
|
* }
|
|
1530
|
+
* ```
|
|
1531
|
+
*
|
|
1532
|
+
* `fract()` can also be used in shaders with p5.strands. The following example
|
|
1533
|
+
* uses `fract()` to create repeating patterns in a shader.
|
|
1534
|
+
*
|
|
1535
|
+
* ```js example
|
|
1536
|
+
* let myShader;
|
|
1537
|
+
*
|
|
1538
|
+
* function setup() {
|
|
1539
|
+
* createCanvas(100, 100, WEBGL);
|
|
1540
|
+
* myShader = buildColorShader(shaderCallback);
|
|
1541
|
+
* describe('A sphere with a repeating gradient pattern.');
|
|
1542
|
+
* }
|
|
1543
|
+
*
|
|
1544
|
+
* function shaderCallback() {
|
|
1545
|
+
* // shaderCallback runs on the GPU. millis() gives ms since start; multiply by 0.001 for seconds.
|
|
1546
|
+
* let t = millis() * 0.001;
|
|
1547
|
+
*
|
|
1548
|
+
* // Multiply by 0.5 to slow the animation to half speed.
|
|
1549
|
+
* // fract(t * 0.5) extracts only the decimal part of the number.
|
|
1550
|
+
* // This creates a smooth sawtooth wave that repeats every 2 seconds.
|
|
1551
|
+
* let value = fract(t * 0.5);
|
|
1552
|
+
*
|
|
1553
|
+
* // Each color is [R, G, B, A] with values from 0 to 1.
|
|
1554
|
+
* let cyan = [0, 0.5, 1, 1];
|
|
1555
|
+
* let orange = [1, 0.5, 0, 1];
|
|
1556
|
+
*
|
|
1557
|
+
* finalColor.begin();
|
|
1558
|
+
*
|
|
1559
|
+
* // mix() blends between cyan (when value = 0) and orange (when value = 1).
|
|
1560
|
+
* // Because fract() resets to 0 each cycle, the color loops smoothly.
|
|
1561
|
+
* finalColor.set(mix(cyan, orange, value));
|
|
1562
|
+
*
|
|
1563
|
+
* finalColor.end();
|
|
1564
|
+
* }
|
|
1565
|
+
*
|
|
1566
|
+
* function draw() {
|
|
1567
|
+
* background(220);
|
|
1568
|
+
* shader(myShader);
|
|
1569
|
+
* noStroke();
|
|
1570
|
+
* sphere(30);
|
|
1571
|
+
* }
|
|
1572
|
+
* ```
|
|
1573
|
+
*
|
|
1574
|
+
* @method fract
|
|
1575
|
+
* @param {Number} n number whose fractional part will be found.
|
|
1576
|
+
* @returns {Number} fractional part of n.
|
|
992
1577
|
*/
|
|
993
1578
|
fn.fract = function(toConvert) {
|
|
994
1579
|
// p5._validateParameters('fract', arguments);
|