-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathdom.js
5847 lines (5651 loc) · 159 KB
/
dom.js
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The web is much more than just canvas and the DOM functionality makes it easy to interact
* with other HTML5 objects, including text, hyperlink, image, input, video,
* audio, and webcam.
* There is a set of creation methods, DOM manipulation methods, and
* an extended <a href="#/p5.Element">p5.Element</a> that supports a range of HTML elements. See the
* <a href='https://github.com/processing/p5.js/wiki/Beyond-the-canvas'>
* beyond the canvas tutorial</a> for a full overview of how this addon works.
*
* See <a href='https://github.com/processing/p5.js/wiki/Beyond-the-canvas'>tutorial: beyond the canvas</a>
* for more info on how to use this library.</a>
*
* @module DOM
* @submodule DOM
* @for p5
* @requires p5
*/
import p5 from '../core/main';
/**
* Searches the page for the first element that matches the given
* <a href="https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/CSS_basics#different_types_of_selectors" target="_blank">CSS selector string</a>.
*
* The selector string can be an ID, class, tag name, or a combination.
* `select()` returns a <a href="#/p5.Element">p5.Element</a> object if it
* finds a match and `null` if not.
*
* The second parameter, `container`, is optional. It specifies a container to
* search within. `container` can be CSS selector string, a
* <a href="#/p5.Element">p5.Element</a> object, or an
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" target="_blank">HTMLElement</a> object.
*
* @method select
* @param {String} selectors CSS selector string of element to search for.
* @param {String|p5.Element|HTMLElement} [container] CSS selector string, <a href="#/p5.Element">p5.Element</a>, or
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" target="_blank">HTMLElement</a> to search within.
* @return {p5.Element|null} <a href="#/p5.Element">p5.Element</a> containing the element.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
* background(200);
*
* // Select the canvas by its tag.
* let cnv = select('canvas');
* cnv.style('border', '5px deeppink dashed');
*
* describe('A gray square with a dashed pink border.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* let cnv = createCanvas(100, 100);
*
* // Add a class attribute to the canvas.
* cnv.class('pinkborder');
*
* background(200);
*
* // Select the canvas by its class.
* cnv = select('.pinkborder');
*
* // Style its border.
* cnv.style('border', '5px deeppink dashed');
*
* describe('A gray square with a dashed pink border.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* let cnv = createCanvas(100, 100);
*
* // Set the canvas' ID.
* cnv.id('mycanvas');
*
* background(200);
*
* // Select the canvas by its ID.
* cnv = select('#mycanvas');
*
* // Style its border.
* cnv.style('border', '5px deeppink dashed');
*
* describe('A gray square with a dashed pink border.');
* }
* </code>
* </div>
*/
p5.prototype.select = function (e, p) {
p5._validateParameters('select', arguments);
const container = this._getContainer(p);
const res = container.querySelector(e);
if (res) {
return this._wrapElement(res);
} else {
return null;
}
};
/**
* Searches the page for all elements that matches the given
* <a href="https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/CSS_basics#different_types_of_selectors" target="_blank">CSS selector string</a>.
*
* The selector string can be an ID, class, tag name, or a combination.
* `selectAll()` returns an array of <a href="#/p5.Element">p5.Element</a>
* objects if it finds any matches and an empty array if none are found.
*
* The second parameter, `container`, is optional. It specifies a container to
* search within. `container` can be CSS selector string, a
* <a href="#/p5.Element">p5.Element</a> object, or an
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" target="_blank">HTMLElement</a> object.
*
* @method selectAll
* @param {String} selectors CSS selector string of element to search for.
* @param {String|p5.Element|HTMLElement} [container] CSS selector string, <a href="#/p5.Element">p5.Element</a>, or
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement" target="_blank">HTMLElement</a> to search within.
* @return {p5.Element[]} array of <a href="#/p5.Element">p5.Element</a>s containing any elements found.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* // Create three buttons.
* createButton('1');
* createButton('2');
* createButton('3');
*
* // Select the buttons by their tag.
* let buttons = selectAll('button');
*
* // Position the buttons.
* for (let i = 0; i < 3; i += 1) {
* buttons[i].position(0, i * 30);
* }
*
* describe('Three buttons stacked vertically. The buttons are labeled, "1", "2", and "3".');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* // Create three buttons and position them.
* let b1 = createButton('1');
* b1.position(0, 0);
* let b2 = createButton('2');
* b2.position(0, 30);
* let b3 = createButton('3');
* b3.position(0, 60);
*
* // Add a class attribute to each button.
* b1.class('btn');
* b2.class('btn btn-pink');
* b3.class('btn');
*
* // Select the buttons by their class.
* let buttons = selectAll('.btn');
* let pinkButtons = selectAll('.btn-pink');
*
* // Style the selected buttons.
* buttons.forEach(setFont);
* pinkButtons.forEach(setColor);
*
* describe('Three buttons stacked vertically. The buttons are labeled, "1", "2", and "3". Buttons "1" and "3" are gray. Button "2" is pink.');
* }
*
* // Set a button's font to Comic Sans MS.
* function setFont(btn) {
* btn.style('font-family', 'Comic Sans MS');
* }
*
* // Set a button's background and font color.
* function setColor(btn) {
* btn.style('background', 'deeppink');
* btn.style('color', 'white');
* }
* </code>
* </div>
*/
p5.prototype.selectAll = function (e, p) {
p5._validateParameters('selectAll', arguments);
const arr = [];
const container = this._getContainer(p);
const res = container.querySelectorAll(e);
if (res) {
for (let j = 0; j < res.length; j++) {
const obj = this._wrapElement(res[j]);
arr.push(obj);
}
}
return arr;
};
/**
* Helper function for select and selectAll
*/
p5.prototype._getContainer = function (p) {
let container = document;
if (typeof p === 'string') {
container = document.querySelector(p) || document;
} else if (p instanceof p5.Element) {
container = p.elt;
} else if (p instanceof HTMLElement) {
container = p;
}
return container;
};
/**
* Helper function for getElement and getElements.
*/
p5.prototype._wrapElement = function (elt) {
const children = Array.prototype.slice.call(elt.children);
if (elt.tagName === 'INPUT' && elt.type === 'checkbox') {
let converted = new p5.Element(elt, this);
converted.checked = function(...args) {
if (args.length === 0) {
return this.elt.checked;
} else if (args[0]) {
this.elt.checked = true;
} else {
this.elt.checked = false;
}
return this;
};
return converted;
} else if (elt.tagName === 'VIDEO' || elt.tagName === 'AUDIO') {
return new p5.MediaElement(elt, this);
} else if (elt.tagName === 'SELECT') {
return this.createSelect(new p5.Element(elt, this));
} else if (
children.length > 0 &&
children.every(function (c) {
return c.tagName === 'INPUT' || c.tagName === 'LABEL';
}) &&
(elt.tagName === 'DIV' || elt.tagName === 'SPAN')
) {
return this.createRadio(new p5.Element(elt, this));
} else {
return new p5.Element(elt, this);
}
};
/**
* Removes all elements created by p5.js, including any event handlers.
*
* There are two exceptions:
* canvas elements created by <a href="#/p5/createCanvas">createCanvas()</a>
* and <a href="#/p5.Renderer">p5.Render</a> objects created by
* <a href="#/p5/createGraphics">createGraphics()</a>.
*
* @method removeElements
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a paragraph element and place
* // it in the middle of the canvas.
* let p = createP('p5*js');
* p.position(25, 25);
*
* describe('A gray square with the text "p5*js" written in its center. The text disappears when the mouse is pressed.');
* }
*
* // Remove all elements when the mouse is pressed.
* function mousePressed() {
* removeElements();
* }
* </code>
* </div>
*
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create a paragraph element and place
* // it at the top of the canvas.
* let p = createP('p5*js');
* p.position(25, 25);
*
* // Create a slider element and place it
* // beneath the canvas.
* slider = createSlider(0, 255, 200);
* slider.position(0, 100);
*
* describe('A gray square with the text "p5*js" written in its center and a range slider beneath it. The square changes color when the slider is moved. The text and slider disappear when the square is double-clicked.');
* }
*
* function draw() {
* // Use the slider value to change the background color.
* let g = slider.value();
* background(g);
* }
*
* // Remove all elements when the mouse is double-clicked.
* function doubleClicked() {
* removeElements();
* }
* </code>
* </div>
*/
p5.prototype.removeElements = function (e) {
p5._validateParameters('removeElements', arguments);
// el.remove splices from this._elements, so don't mix iteration with it
const isNotCanvasElement = el => !(el.elt instanceof HTMLCanvasElement);
const removeableElements = this._elements.filter(isNotCanvasElement);
removeableElements.map(el => el.remove());
};
/**
* Calls a function when the element changes.
*
* Calling `myElement.changed(false)` disables the function.
*
* @method changed
* @param {Function|Boolean} fxn function to call when the element changes.
* `false` disables the function.
* @chainable
*
* @example
* <div>
* <code>
* let dropdown;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a dropdown menu and add a few color options.
* dropdown = createSelect();
* dropdown.position(0, 0);
* dropdown.option('red');
* dropdown.option('green');
* dropdown.option('blue');
*
* // Call paintBackground() when the color option changes.
* dropdown.changed(paintBackground);
*
* describe('A gray square with a dropdown menu at the top. The square changes color when an option is selected.');
* }
*
* // Paint the background with the selected color.
* function paintBackground() {
* let c = dropdown.value();
* background(c);
* }
* </code>
* </div>
*
* <div>
* <code>
* let checkbox;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a checkbox and place it beneath the canvas.
* checkbox = createCheckbox(' circle');
* checkbox.position(0, 100);
*
* // Call repaint() when the checkbox changes.
* checkbox.changed(repaint);
*
* describe('A gray square with a checkbox underneath it that says "circle". A white circle appears when the box is checked and disappears otherwise.');
* }
*
* // Paint the background gray and determine whether to draw a circle.
* function repaint() {
* background(200);
* if (checkbox.checked() === true) {
* circle(50, 50, 30);
* }
* }
* </code>
* </div>
*/
p5.Element.prototype.changed = function (fxn) {
p5.Element._adjustListener('change', fxn, this);
return this;
};
/**
* Calls a function when the element receives input.
*
* `myElement.input()` is often used to with text inputs and sliders. Calling
* `myElement.input(false)` disables the function.
*
* @method input
* @param {Function|Boolean} fxn function to call when input is detected within
* the element.
* `false` disables the function.
* @chainable
*
* @example
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a slider and place it beneath the canvas.
* slider = createSlider(0, 255, 200);
* slider.position(0, 100);
*
* // Call repaint() when the slider changes.
* slider.input(repaint);
*
* describe('A gray square with a range slider underneath it. The background changes shades of gray when the slider is moved.');
* }
*
* // Paint the background using slider's value.
* function repaint() {
* let g = slider.value();
* background(g);
* }
* </code>
* </div>
*
* <div>
* <code>
* let input;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create an input and place it beneath the canvas.
* input = createInput('');
* input.position(0, 100);
*
* // Call repaint() when input is detected.
* input.input(repaint);
*
* describe('A gray square with a text input bar beneath it. Any text written in the input appears in the middle of the square.');
* }
*
* // Paint the background gray and display the input's value.
* function repaint() {
* background(200);
* let msg = input.value();
* text(msg, 5, 50);
* }
* </code>
* </div>
*/
p5.Element.prototype.input = function (fxn) {
p5.Element._adjustListener('input', fxn, this);
return this;
};
/**
* Helpers for create methods.
*/
function addElement(elt, pInst, media) {
const node = pInst._userNode ? pInst._userNode : document.body;
node.appendChild(elt);
const c = media
? new p5.MediaElement(elt, pInst)
: new p5.Element(elt, pInst);
pInst._elements.push(c);
return c;
}
/**
* Creates a `<div></div>` element.
*
* `<div></div>` elements are commonly used as containers for
* other elements.
*
* The parameter `html` is optional. It accepts a string that sets the
* inner HTML of the new `<div></div>`.
*
* @method createDiv
* @param {String} [html] inner HTML for the new `<div></div>` element.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a div element and set its position.
* let div = createDiv('p5*js');
* div.position(25, 35);
*
* describe('A gray square with the text "p5*js" written in its center.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create an h3 element within the div.
* let div = createDiv('<h3>p5*js</h3>');
* div.position(20, 5);
*
* describe('A gray square with the text "p5*js" written in its center.');
* }
* </code>
* </div>
*/
p5.prototype.createDiv = function (html = '') {
let elt = document.createElement('div');
elt.innerHTML = html;
return addElement(elt, this);
};
/**
* Creates a `<p></p>` element.
*
* `<p></p>` elements are commonly used for paragraph-length text.
*
* The parameter `html` is optional. It accepts a string that sets the
* inner HTML of the new `<p></p>`.
*
* @method createP
* @param {String} [html] inner HTML for the new `<p></p>` element.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a paragraph element and set its position.
* let p = createP('Tell me a story.');
* p.position(5, 0);
*
* describe('A gray square displaying the text "Tell me a story." written in black.');
* }
* </code>
* </div>
*/
p5.prototype.createP = function (html = '') {
let elt = document.createElement('p');
elt.innerHTML = html;
return addElement(elt, this);
};
/**
* Creates a `<span></span>` element.
*
* `<span></span>` elements are commonly used as containers
* for inline elements. For example, a `<span></span>`
* can hold part of a sentence that's a
* <span style="color: deeppink;">different</span> style.
*
* The parameter `html` is optional. It accepts a string that sets the
* inner HTML of the new `<span></span>`.
*
* @method createSpan
* @param {String} [html] inner HTML for the new `<span></span>` element.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a span element and set its position.
* let span = createSpan('p5*js');
* span.position(25, 35);
*
* describe('A gray square with the text "p5*js" written in its center.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* background(200);
*
* // Create a div element as a container.
* let div = createDiv();
*
* // Place the div at the center.
* div.position(25, 35);
*
* // Create a span element.
* let s1 = createSpan('p5');
*
* // Create a second span element.
* let s2 = createSpan('*');
*
* // Set the second span's font color.
* s2.style('color', 'deeppink');
*
* // Create a third span element.
* let s3 = createSpan('js');
*
* // Add all the spans to the container div.
* s1.parent(div);
* s2.parent(div);
* s3.parent(div);
*
* describe('A gray square with the text "p5*js" written in black at its center. The asterisk is pink.');
* }
* </code>
* </div>
*/
p5.prototype.createSpan = function (html = '') {
let elt = document.createElement('span');
elt.innerHTML = html;
return addElement(elt, this);
};
/**
* Creates an `<img>` element that can appear outside of the canvas.
*
* The first parameter, `src`, is a string with the path to the image file.
* `src` should be a relative path, as in `'assets/image.png'`, or a URL, as
* in `'https://example.com/image.png'`.
*
* The second parameter, `alt`, is a string with the
* <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#usage_notes" target="_blank">alternate text</a>
* for the image. An empty string `''` can be used for images that aren't displayed.
*
* The third parameter, `crossOrigin`, is optional. It's a string that sets the
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes" target="_blank">crossOrigin property</a>
* of the image. Use `'anonymous'` or `'use-credentials'` to fetch the image
* with cross-origin access.
*
* The fourth parameter, `callback`, is also optional. It sets a function to
* call after the image loads. The new image is passed to the callback
* function as a <a href="#/p5.Element">p5.Element</a> object.
*
* @method createImg
* @param {String} src relative path or URL for the image.
* @param {String} alt alternate text for the image.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* let img = createImg(
* 'https://p5js.org/assets/img/asterisk-01.png',
* 'The p5.js magenta asterisk.'
* );
* img.position(0, -10);
*
* describe('A gray square with a magenta asterisk in its center.');
* }
* </code>
* </div>
*/
/**
* @method createImg
* @param {String} src
* @param {String} alt
* @param {String} [crossOrigin] crossOrigin property to use when fetching the image.
* @param {Function} [successCallback] function to call once the image loads. The new image will be passed
* to the function as a <a href="#/p5.Element">p5.Element</a> object.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*/
p5.prototype.createImg = function () {
p5._validateParameters('createImg', arguments);
const elt = document.createElement('img');
const args = arguments;
let self;
if (args.length > 1 && typeof args[1] === 'string') {
elt.alt = args[1];
}
if (args.length > 2 && typeof args[2] === 'string') {
elt.crossOrigin = args[2];
}
elt.src = args[0];
self = addElement(elt, this);
elt.addEventListener('load', function () {
self.width = elt.offsetWidth || elt.width;
self.height = elt.offsetHeight || elt.height;
const last = args[args.length - 1];
if (typeof last === 'function') last(self);
});
return self;
};
/**
* Creates an `<a></a>` element that links to another web page.
*
* The first parmeter, `href`, is a string that sets the URL of the linked
* page.
*
* The second parameter, `html`, is a string that sets the inner HTML of the
* link. It's common to use text, images, or buttons as links.
*
* The third parameter, `target`, is optional. It's a string that tells the
* web browser where to open the link. By default, links open in the current
* browser tab. Passing `'_blank'` will cause the link to open in a new
* browser tab. MDN describes a few
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target" target="_blank">other options</a>.
*
* @method createA
* @param {String} href URL of linked page.
* @param {String} html inner HTML of link element to display.
* @param {String} [target] target where the new link should open,
* either `'_blank'`, `'_self'`, `'_parent'`, or `'_top'`.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create an anchor element that links to p5js.org.
* let a = createA('http://p5js.org/', 'p5*js');
* a.position(25, 35);
*
* describe('The text "p5*js" written at the center of a gray square.');
* }
* </code>
* </div>
*
* <div>
* <code>
* function setup() {
* background(200);
*
* // Create an anchor tag that links to p5js.org.
* // Open the link in a new tab.
* let a = createA('http://p5js.org/', 'p5*js', '_blank');
* a.position(25, 35);
*
* describe('The text "p5*js" written at the center of a gray square.');
* }
* </code>
* </div>
*/
p5.prototype.createA = function (href, html, target) {
p5._validateParameters('createA', arguments);
const elt = document.createElement('a');
elt.href = href;
elt.innerHTML = html;
if (target) elt.target = target;
return addElement(elt, this);
};
/** INPUT **/
/**
* Creates a slider `<input></input>` element.
*
* Range sliders are useful for quickly selecting numbers from a given range.
*
* The first two parameters, `min` and `max`, are numbers that set the
* slider's minimum and maximum.
*
* The third parameter, `value`, is optional. It's a number that sets the
* slider's default value.
*
* The fourth parameter, `step`, is also optional. It's a number that sets the
* spacing between each value in the slider's range. Setting `step` to 0
* allows the slider to move smoothly from `min` to `max`.
*
* @method createSlider
* @param {Number} min minimum value of the slider.
* @param {Number} max maximum value of the slider.
* @param {Number} [value] default value of the slider.
* @param {Number} [step] size for each step in the slider's range.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create a slider and place it at the top of the canvas.
* slider = createSlider(0, 255);
* slider.position(10, 10);
* slider.size(80);
*
* describe('A dark gray square with a range slider at the top. The square changes color when the slider is moved.');
* }
*
* function draw() {
* // Use the slider as a grayscale value.
* let g = slider.value();
* background(g);
* }
* </code>
* </div>
*
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create a slider and place it at the top of the canvas.
* // Set its default value to 0.
* slider = createSlider(0, 255, 0);
* slider.position(10, 10);
* slider.size(80);
*
* describe('A black square with a range slider at the top. The square changes color when the slider is moved.');
* }
*
* function draw() {
* // Use the slider as a grayscale value.
* let g = slider.value();
* background(g);
* }
* </code>
* </div>
*
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create a slider and place it at the top of the canvas.
* // Set its default value to 0.
* // Set its step size to 50.
* slider = createSlider(0, 255, 0, 50);
* slider.position(10, 10);
* slider.size(80);
*
* describe('A black square with a range slider at the top. The square changes color when the slider is moved.');
* }
*
* function draw() {
* // Use the slider as a grayscale value.
* let g = slider.value();
* background(g);
* }
* </code>
* </div>
*
* <div>
* <code>
* let slider;
*
* function setup() {
* createCanvas(100, 100);
*
* // Create a slider and place it at the top of the canvas.
* // Set its default value to 0.
* // Set its step size to 0 so that it moves smoothly.
* slider = createSlider(0, 255, 0, 0);
* slider.position(10, 10);
* slider.size(80);
*
* describe('A black square with a range slider at the top. The square changes color when the slider is moved.');
* }
*
* function draw() {
* // Use the slider as a grayscale value.
* let g = slider.value();
* background(g);
* }
* </code>
* </div>
*/
p5.prototype.createSlider = function (min, max, value, step) {
p5._validateParameters('createSlider', arguments);
const elt = document.createElement('input');
elt.type = 'range';
elt.min = min;
elt.max = max;
if (step === 0) {
elt.step = 0.000000000000000001; // smallest valid step
} else if (step) {
elt.step = step;
}
if (typeof value === 'number') elt.value = value;
return addElement(elt, this);
};
/**
* Creates a `<button></button>` element.
*
* The first parameter, `label`, is a string that sets the label displayed on
* the button.
*
* The second parameter, `value`, is optional. It's a string that sets the
* button's value. See
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#value" target="_blank">MDN</a>
* for more details.
*
* @method createButton
* @param {String} label label displayed on the button.
* @param {String} [value] value of the button.
* @return {p5.Element} new <a href="#/p5.Element">p5.Element</a> object.
*
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a button and place it beneath the canvas.
* let button = createButton('click me');
* button.position(0, 100);
*
* // Call repaint() when the button is pressed.
* button.mousePressed(repaint);
*
* describe('A gray square with a button that says "click me" beneath it. The square changes color when the button is clicked.');
* }
*
* // Change the background color.
* function repaint() {
* let g = random(255);
* background(g);
* }
* </code>
* </div>
*
* <div>
* <code>
* let button;
*
* function setup() {
* createCanvas(100, 100);
*
* background(200);
*
* // Create a button and set its value to 0.
* // Place the button beneath the canvas.
* button = createButton('click me', 'red');
* button.position(0, 100);
*
* // Call randomColor() when the button is pressed.
* button.mousePressed(randomColor);
*
* describe('A red square with a button that says "click me" beneath it. The square changes color when the button is clicked.');
* }
*
* function draw() {
* // Use the button's value to set the background color.
* let c = button.value();
* background(c);
* }
*
* // Set the button's value to a random color.
* function randomColor() {
* let c = random(['red', 'green', 'blue', 'yellow']);
* button.value(c);
* }
* </code>
* </div>
*/
p5.prototype.createButton = function (label, value) {