-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathstructure.js
More file actions
549 lines (540 loc) · 15.3 KB
/
structure.js
File metadata and controls
549 lines (540 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* @module Structure
* @submodule Structure
* @for p5
*/
function structure(p5, fn){
/**
* Stops the code in <a href="#/p5/draw">draw()</a> from running repeatedly.
*
* By default, <a href="#/p5/draw">draw()</a> tries to run 60 times per
* second. Calling `noLoop()` stops <a href="#/p5/draw">draw()</a> from
* repeating. The draw loop can be restarted by calling
* <a href="#/p5/loop">loop()</a>. <a href="#/p5/draw">draw()</a> can be run
* once by calling <a href="#/p5/redraw">redraw()</a>.
*
* The <a href="#/p5/isLooping">isLooping()</a> function can be used to check
* whether a sketch is looping, as in `isLooping() === true`.
*
* @method noLoop
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Turn off the draw loop.
* noLoop();
*
* describe('A white half-circle on the left edge of a gray square.');
* }
*
* function draw() {
* background(200);
*
* // Calculate the circle's x-coordinate.
* let x = frameCount;
*
* // Draw the circle.
* // Normally, the circle would move from left to right.
* circle(x, 50, 20);
* }
*
* @example
* // Double-click to stop the draw loop.
*
* function setup() {
* createCanvas(100, 100);
*
* // Slow the frame rate.
* frameRate(5);
*
* describe('A white circle moves randomly on a gray background. It stops moving when the user double-clicks.');
* }
*
* function draw() {
* background(200);
*
* // Calculate the circle's coordinates.
* let x = random(0, 100);
* let y = random(0, 100);
*
* // Draw the circle.
* // Normally, the circle would move from left to right.
* circle(x, y, 20);
* }
*
* // Stop the draw loop when the user double-clicks.
* function doubleClicked() {
* noLoop();
* }
*
* @example
* let startButton;
* let stopButton;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create the button elements and place them
* // beneath the canvas.
* startButton = createButton('▶');
* startButton.position(0, 100);
* startButton.size(50, 20);
* stopButton = createButton('◾');
* stopButton.position(50, 100);
* stopButton.size(50, 20);
*
* // Set functions to call when the buttons are pressed.
* startButton.mousePressed(loop);
* stopButton.mousePressed(noLoop);
*
* // Slow the frame rate.
* frameRate(5);
*
* describe(
* 'A white circle moves randomly on a gray background. Play and stop buttons are shown beneath the canvas. The circle stops or starts moving when the user presses a button.'
* );
* }
*
* function draw() {
* background(200);
*
* // Calculate the circle's coordinates.
* let x = random(0, 100);
* let y = random(0, 100);
*
* // Draw the circle.
* // Normally, the circle would move from left to right.
* circle(x, y, 20);
* }
*/
fn.noLoop = function() {
this._loop = false;
};
/**
* Resumes the draw loop after <a href="#/p5/noLoop">noLoop()</a> has been
* called.
*
* By default, <a href="#/p5/draw">draw()</a> tries to run 60 times per
* second. Calling <a href="#/p5/noLoop">noLoop()</a> stops
* <a href="#/p5/draw">draw()</a> from repeating. The draw loop can be
* restarted by calling `loop()`.
*
* The <a href="#/p5/isLooping">isLooping()</a> function can be used to check
* whether a sketch is looping, as in `isLooping() === true`.
*
* @method loop
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* // Turn off the draw loop.
* noLoop();
*
* describe(
* 'A white half-circle on the left edge of a gray square. The circle starts moving to the right when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Calculate the circle's x-coordinate.
* let x = frameCount;
*
* // Draw the circle.
* circle(x, 50, 20);
* }
*
* // Resume the draw loop when the user double-clicks.
* function doubleClicked() {
* loop();
* }
*
* @example
* let startButton;
* let stopButton;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create the button elements and place them
* // beneath the canvas.
* startButton = createButton('▶');
* startButton.position(0, 100);
* startButton.size(50, 20);
* stopButton = createButton('◾');
* stopButton.position(50, 100);
* stopButton.size(50, 20);
*
* // Set functions to call when the buttons are pressed.
* startButton.mousePressed(loop);
* stopButton.mousePressed(noLoop);
*
* // Slow the frame rate.
* frameRate(5);
*
* describe(
* 'A white circle moves randomly on a gray background. Play and stop buttons are shown beneath the canvas. The circle stops or starts moving when the user presses a button.'
* );
* }
*
* function draw() {
* background(200);
*
* // Calculate the circle's coordinates.
* let x = random(0, 100);
* let y = random(0, 100);
*
* // Draw the circle.
* // Normally, the circle would move from left to right.
* circle(x, y, 20);
* }
*/
fn.loop = function() {
if (!this._loop) {
this._loop = true;
if (this._setupDone) {
this._draw();
}
}
};
/**
* Returns `true` if the draw loop is running and `false` if not.
*
* By default, <a href="#/p5/draw">draw()</a> tries to run 60 times per
* second. Calling <a href="#/p5/noLoop">noLoop()</a> stops
* <a href="#/p5/draw">draw()</a> from repeating. The draw loop can be
* restarted by calling <a href="#/p5/loop">loop()</a>.
*
* The `isLooping()` function can be used to check whether a sketch is
* looping, as in `isLooping() === true`.
*
* @method isLooping
* @returns {boolean}
*
* @example
* function setup() {
* createCanvas(100, 100);
*
* describe('A white circle drawn against a gray background. When the user double-clicks, the circle stops or resumes following the mouse.');
* }
*
* function draw() {
* background(200);
*
* // Draw the circle at the mouse's position.
* circle(mouseX, mouseY, 20);
* }
*
* // Toggle the draw loop when the user double-clicks.
* function doubleClicked() {
* if (isLooping() === true) {
* noLoop();
* } else {
* loop();
* }
* }
*/
fn.isLooping = function() {
return this._loop;
};
/**
* Runs the code in <a href="#/p5/draw">draw()</a> once.
*
* By default, <a href="#/p5/draw">draw()</a> tries to run 60 times per
* second. Calling <a href="#/p5/noLoop">noLoop()</a> stops
* <a href="#/p5/draw">draw()</a> from repeating. Calling `redraw()` will
* execute the code in the <a href="#/p5/draw">draw()</a> function a set
* number of times. `await` the result of `redraw` to make sure it has finished.
*
* The parameter, `n`, is optional. If a number is passed, as in `redraw(5)`,
* then the draw loop will run the given number of times. By default, `n` is
* 1.
*
* @method redraw
* @param {Integer} [n] number of times to run <a href="#/p5/draw">draw()</a>. Defaults to 1.
* @returns {Promise<void>}
*
* @example
* // Double-click the canvas to move the circle.
*
* let x = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* // Turn off the draw loop.
* noLoop();
*
* describe(
* 'A white half-circle on the left edge of a gray square. The circle moves a little to the right when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Draw the circle.
* circle(x, 50, 20);
*
* // Increment x.
* x += 5;
* }
*
* // Run the draw loop when the user double-clicks.
* async function doubleClicked() {
* await redraw();
* }
*
* @example
* // Double-click the canvas to move the circle.
*
* let x = 0;
*
* function setup() {
* createCanvas(100, 100);
*
* // Turn off the draw loop.
* noLoop();
*
* describe(
* 'A white half-circle on the left edge of a gray square. The circle hops to the right when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Draw the circle.
* circle(x, 50, 20);
*
* // Increment x.
* x += 5;
* }
*
* // Run the draw loop three times when the user double-clicks.
* async function doubleClicked() {
* await redraw(3);
* }
*/
fn.redraw = async function(n) {
if (this._inUserDraw || !this._setupDone) {
return;
}
let numberOfRedraws = parseInt(n);
if (isNaN(numberOfRedraws) || numberOfRedraws < 1) {
numberOfRedraws = 1;
}
const context = this._isGlobal ? window : this;
if (typeof context.draw === 'function') {
if (typeof context.setup === 'undefined') {
context.scale(context._pixelDensity, context._pixelDensity);
}
for (let idxRedraw = 0; idxRedraw < numberOfRedraws; idxRedraw++) {
context.resetMatrix();
if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
this._updateAccsOutput();
}
if (this._renderer.isP3D) {
this._renderer._update();
}
this.frameCount = context.frameCount + 1;
await this._runLifecycleHook('predraw');
this._inUserDraw = true;
try {
await context.draw();
} finally {
this._inUserDraw = false;
}
await this._runLifecycleHook('postdraw');
}
// Finish drawing
await this._renderer.finishDraw?.();
}
};
/**
* Creates a new sketch in "instance" mode.
*
* All p5.js sketches are instances of the `p5` class. Put another way, all
* p5.js sketches are objects with methods including `pInst.setup()`,
* `pInst.draw()`, `pInst.circle()`, and `pInst.fill()`. By default, sketches
* run in "global mode" to hide some of this complexity.
*
* In global mode, a default instance of the `p5` class is created
* automatically. The default `p5` instance searches the web page's source
* code for declarations of system functions such as `setup()`, `draw()`,
* and `mousePressed()`, then attaches those functions to itself as methods.
* Calling a function such as `circle()` in global mode actually calls the
* default `p5` object's `pInst.circle()` method.
*
* It's often helpful to isolate the code within sketches from the rest of the
* code on a web page. Two common use cases are web pages that use other
* JavaScript libraries and web pages with multiple sketches. "Instance mode"
* makes it easy to support both of these scenarios.
*
* Instance mode sketches support the same API as global mode sketches. They
* use a function to bundle, or encapsulate, an entire sketch. The function
* containing the sketch is then passed to the `p5()` constructor.
*
* The first parameter, `sketch`, is a function that contains the sketch. For
* example, the statement `new p5(mySketch)` would create a new instance mode
* sketch from a function named `mySketch`. The function should have one
* parameter, `p`, that's a `p5` object.
*
* The second parameter, `node`, is optional. If a string is passed, as in
* `new p5(mySketch, 'sketch-one')` the new instance mode sketch will become a
* child of the HTML element with the id `sketch-one`. If an HTML element is
* passed, as in `new p5(mySketch, myElement)`, then the new instance mode
* sketch will become a child of the `Element` object called `myElement`.
*
* @method p5
* @param {Object} sketch function containing the sketch.
* @param {String|HTMLElement} node ID or reference to the HTML element that will contain the sketch.
*
* @example
* // META:norender
* // Declare the function containing the sketch.
* function sketch(p) {
*
* // Declare the setup() method.
* p.setup = function () {
* p.createCanvas(100, 100);
*
* p.describe('A white circle drawn on a gray background.');
* };
*
* // Declare the draw() method.
* p.draw = function () {
* p.background(200);
*
* // Draw the circle.
* p.circle(50, 50, 20);
* };
* }
*
* // Initialize the sketch.
* new p5(sketch);
*
* @example
* // META:norender
* // Declare the function containing the sketch.
* function sketch(p) {
* // Create the sketch's variables within its scope.
* let x = 50;
* let y = 50;
*
* // Declare the setup() method.
* p.setup = function () {
* p.createCanvas(100, 100);
*
* p.describe('A white circle moves randomly on a gray background.');
* };
*
* // Declare the draw() method.
* p.draw = function () {
* p.background(200);
*
* // Update x and y.
* x += p.random(-1, 1);
* y += p.random(-1, 1);
*
* // Draw the circle.
* p.circle(x, y, 20);
* };
* }
*
* // Initialize the sketch.
* new p5(sketch);
*
* @example
* // META:norender
* // Declare the function containing the sketch.
* function sketch(p) {
*
* // Declare the setup() method.
* p.setup = function () {
* p.createCanvas(100, 100);
*
* p.describe('A white circle drawn on a gray background.');
* };
*
* // Declare the draw() method.
* p.draw = function () {
* p.background(200);
*
* // Draw the circle.
* p.circle(50, 50, 20);
* };
* }
*
* // Select the web page's body element.
* let body = document.querySelector('body');
*
* // Initialize the sketch and attach it to the web page's body.
* new p5(sketch, body);
*
* @example
* // META:norender
* // Declare the function containing the sketch.
* function sketch(p) {
*
* // Declare the setup() method.
* p.setup = function () {
* p.createCanvas(100, 100);
*
* p.describe(
* 'A white circle drawn on a gray background. The circle follows the mouse as the user moves.'
* );
* };
*
* // Declare the draw() method.
* p.draw = function () {
* p.background(200);
*
* // Draw the circle.
* p.circle(p.mouseX, p.mouseY, 20);
* };
* }
*
* // Initialize the sketch.
* new p5(sketch);
*
* @example
* // META:norender
* // Declare the function containing the sketch.
* function sketch(p) {
*
* // Declare the setup() method.
* p.setup = function () {
* p.createCanvas(100, 100);
*
* p.describe(
* 'A white circle drawn on a gray background. The circle follows the mouse as the user moves. The circle becomes black when the user double-clicks.'
* );
* };
*
* // Declare the draw() method.
* p.draw = function () {
* p.background(200);
*
* // Draw the circle.
* p.circle(p.mouseX, p.mouseY, 20);
* };
*
* // Declare the doubleClicked() method.
* p.doubleClicked = function () {
* // Change the fill color when the user double-clicks.
* p.fill(0);
* };
* }
*
* // Initialize the sketch.
* new p5(sketch);
*/
}
export default structure;
if(typeof p5 !== 'undefined'){
structure(p5, p5.prototype);
}