-
Notifications
You must be signed in to change notification settings - Fork 2
/
p5.dom.js
727 lines (655 loc) · 20.9 KB
/
p5.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
/**
* This is the p5.dom library.
*
* @module p5.dom
* @submodule p5.dom
* @for p5.dom
* @main
*/
var p5DOM = (function(){
// =============================================================================
// p5 additions
// =============================================================================
p5.prototype._elements = [];
/**
* Searches the page for an element with given ID and returns it as
* a p5.Element. The DOM node itself can be accessed with .elt.
* Returns null if none found.
*
* @method getElement
* @param {String} id id of element to search for
* @return {Object/p5.Element|Null} p5.Element containing node found
*/
p5.prototype.getElement = function (e) {
var res = document.getElementById(e);
if (res) {
return new p5.Element(res);
} else {
return null;
}
};
/**
* Searches the page for elements with given class and returns an
* array of p5.Elements. The DOM nodes themselves can be accessed
* with .elt. Returns an empty array if none found.
*
* @method getElements
* @param {String} class class name of elements to search for
* @return {Array} array of p5.Element wrapped nodes found
*/
p5.prototype.getElements = function (e) {
var arr = [];
var res = document.getElementsByClassName(e);
if (res) {
for (var j = 0; j < res.length; j++) {
var obj = new p5.Element(res[j]);
arr.push(obj);
}
}
return arr;
};
/**
* Removes all elements created by p5, except the original canvas.
* Event handlers are removed, and element is removed from the DOM.
*
* @method removeElements
*/
p5.prototype.removeElements = function (e) {
for (var i=0; i<this._elements.length; i++) {
this._elements[i].remove();
}
};
/**
* Helpers for create methods.
*/
function addElement(elt, media) {
var node = this._userNode ? this._userNode : document.body;
node.appendChild(elt);
var c = media ? new p5.MediaElement(elt) : new p5.Element(elt);
this._elements.push(c);
return c;
}
/**
* Creates a <div></div> element in the DOM with given inner HTML.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createDiv
* @param {String} html inner HTML for element created
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
/**
* Creates a <p></p> element in the DOM with given inner HTML. Used
* for paragraph length text.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createP
* @param {String} html inner HTML for element created
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
/**
* Creates a <span></span> element in the DOM with given inner HTML.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createSpan
* @param {String} html inner HTML for element created
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
var tags = ['div', 'p', 'span'];
tags.forEach(function(tag) {
var method = 'create' + tag.charAt(0).toUpperCase() + tag.slice(1);
p5.prototype[method] = function(html) {
var elt = document.createElement(tag);
elt.innerHTML = html;
return addElement(elt);
}
});
/**
* Creates an <img /> element in the DOM with given src and
* alternate text.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createImg
* @param {String} src src path or url for image
* @param {String} alt alternate text to be used if image does not
* load
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createImg = function(src, alt) {
var elt = document.createElement('img');
elt.src = src;
if (typeof alt !== 'undefined') {
elt.alt = alt;
}
return addElement(elt);
};
/**
* Creates an <a></a> element in the DOM for including a hyperlink.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createA
* @param {String} href url of page to link to
* @param {String} html inner html of link element to display
* @param {String} [target] target where new link should open,
* could be _blank, _self, _parent, _top.
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createA = function(href, html, target) {
var elt = document.createElement('a');
elt.href = href;
elt.innerHTML = html;
if (target) elt.target = target;
return addElement(elt);
};
/** INPUT **/
/**
* Creates a slider <input></input> element in the DOM.
* Use .size() to set the display length of the slider.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @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
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createSlider = function(min, max, value) {
var elt = document.createElement('input');
elt.type = 'range';
elt.min = min;
elt.max = max;
if (value) elt.value = value;
return addElement(elt);
};
/**
* Creates a <button></button> element in the DOM.
* Use .size() to set the display size of the button.
* Use .mousePressed() to specify behavior on press.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createButton
* @param {String} label label displayed on the button
* @param {String} [value] value of the button
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createButton = function(label, value) {
var elt = document.createElement('button');
elt.innerHTML = label;
elt.value = value;
if (value) elt.value = value;
return addElement(elt);
};
/**
* Creates an <input></input> element in the DOM for text input.
* Use .size() to set the display length of the box.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createInput
* @param {Number} [value] default value of the input box
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createInput = function(value) {
var elt = document.createElement('input');
elt.type = 'text';
if (value) elt.value = value;
return addElement(elt);
};
/** VIDEO STUFF **/
/**
* Creates an HTML5 <video> element in the DOM for simple playback
* of audio/video. Shown by default, can be hidden with .hide()
* and drawn into canvas using video(). Appends to the container
* node if one is specified, otherwise appends to body.
*
* @method createVideo
* @param {String} src path to a video file
* @param {String} [src] path to another format of same file
* (optional, to ensure compatability)
* @return {Object/p5.Element} pointer to video p5.Element
*/
p5.prototype.createVideo = function(src1, src2) {
var elt = document.createElement('video');
var source= document.createElement('source');
source.src= src1;
elt.appendChild(source);
if (src2) {
var source2 = document.createElement('source');
source2.src = src2;
elt.appendChild(source2);
}
var c = addElement(elt, true);
return c;
};
p5.prototype.video = function(v, x, y, w, h) {
var frame = v.canvas ? v.canvas : v.elt;
this._curElement.context.drawImage(frame, x, y, w, h);
};
/** AUDIO STUFF **/
/**
* Creates a hidden HTML5 <audio> element in the DOM for simple audio
* playback. Appends to the container node if one is specified,
* otherwise appends to body.
*
* @method createAudio
* @param {String} src path to an audio file
* @param {String} [src] path to another format of same file
* (optional, to ensure compatability)
* @return {Object/p5.Element} pointer to audio p5.Element
*/
p5.prototype.createAudio = function(src1, src2) {
var elt = document.createElement('audio');
var source= document.createElement('source');
source.src= src1;
elt.appendChild(source);
if (src2) {
var source2 = document.createElement('source');
source2.src = src2;
elt.appendChild(source2);
}
var c = addElement(elt, true);
return c;
};
/** CAMERA STUFF **/
p5.prototype.VIDEO = 'video';
p5.prototype.AUDIO = 'audio';
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
/**
* Creates a new <video> element that contains the audio/video feed
* from a webcam. This can be drawn onto the canvas using video().
*
* @method createCapture
* @param {String/Constant} type type of capture, either VIDEO or
* AUDIO if none specified, default both
* @return {Object/p5.Element} capture video p5.Element
*/
p5.prototype.createCapture = function(type) {
console.log(type)
var useVideo, useAudio;
if (!type) {
useVideo = true;
useAudio = true;
} else if (type === p5.prototype.VIDEO) {
useVideo = true;
} else if (type === p5.prototype.AUDIO) {
useAudio = true;
}
if (navigator.getUserMedia) {
var elt = document.createElement('video');
navigator.getUserMedia({video: useVideo, audio: useAudio}, function(stream) {
elt.src = window.URL.createObjectURL(stream);
elt.play();
}, function(e) { console.log(e); });
} else {
throw 'getUserMedia not supported in this browser';
}
return addElement(elt);
};
/**
* Creates element with given tag in the DOM with given content.
* Appends to the container node if one is specified, otherwise
* appends to body.
*
* @method createElement
* @param {String} tag tag for the new element
* @param {String} [content] html content to be inserted into the element
* @return {Object/p5.Element} pointer to p5.Element holding created
* node
*/
p5.prototype.createElement = function(tag, content) {
var elt = document.createElement(tag);
if (typeof content !== 'undefined') {
elt.innerHTML = content;
}
return addElement(elt);
};
// =============================================================================
// p5.Element additions
// =============================================================================
/**
*
* Adds specified class to the element.
*
* @method addClass
* @param {String} class name of class to add
*/
p5.Element.prototype.addClass = function(c) {
if (this.elt.className) {
// PEND don't add class more than once
//var regex = new RegExp('[^a-zA-Z\d:]?'+c+'[^a-zA-Z\d:]?');
//if (this.elt.className.search(/[^a-zA-Z\d:]?hi[^a-zA-Z\d:]?/) === -1) {
this.elt.className = this.elt.className+' '+c;
//}
} else {
this.elt.className = c;
}
}
/**
*
* Removes specified class from the element.
*
* @method removeClass
* @param {String} class name of class to remove
*/
p5.Element.prototype.removeClass = function(c) {
var regex = new RegExp('(?:^|\\s)'+c+'(?!\\S)');
this.elt.className = this.elt.className.replace(regex, '');
this.elt.className = this.elt.className.replace(/^\s+|\s+$/g, ""); //prettify (optional)
}
/**
*
* Attaches the element to the parent specified. A way of setting
* the container for the element. Accepts either a string ID or
* DOM node.
*
* @method child
* @param {String|Object} child the ID or node to add to the current element
*/
p5.Element.prototype.child = function(c) {
if (typeof c === 'string') {
c = document.getElementById(c);
}
this.elt.appendChild(c);
};
/**
*
* Sets the inner HTML of the element. Replaces any existing html.
*
* @for p5.Element
* @method html
* @param {String} html the HTML to be placed inside the element
*/
p5.Element.prototype.html = function(html) {
this.elt.innerHTML = html;
};
/**
*
* Sets the position of the element relative to (0, 0) of the
* window. Essentially, sets position:absolute and left and top
* properties of style.
*
* @method position
* @param {Number} x x-position relative to upper left of window
* @param {Number} y y-position relative to upper left of window
*/
p5.Element.prototype.position = function(x, y) {
this.elt.style.position = 'absolute';
this.elt.style.left = x+'px';
this.elt.style.top = y+'px';
};
/**
*
* Sets the given style (css) property of the element with the given value.
* If no value is specified, returns the value of the given property,
* or undefined if the property is not.
*
* @method style
* @param {String} property property to be set
* @param {String} value value to assign to property
* @return {String} value of property, if no value is specified
*/
p5.Element.prototype.style = function(prop, val) {
if (typeof val === 'undefined') {
return this.elt.style[prop];
} else {
this.elt.style[prop] = val;
}
};
/**
*
* Adds a new attribute or changes the value of an existing attribute
* on the specified element. If no value is specified, returns the
* value of the given attribute, or null if attribute is not set.
*
* @method attribute
* @param {String} attr attribute to set
* @param {String} [value] value to assign to attribute
* @return {String} value of attribute, if no value is specified
*/
p5.Element.prototype.attribute = function(attr, value) {
if (typeof value === 'undefined') {
return this.elt.getAttribute(attr);
} else {
this.elt.setAttribute(attr, value);
}
};
/**
* Either returns the value of the element if no arguments
* given, or sets the value of the element.
*
* @method value
* @param {String|Number} [value]
* @return {String|Number}
*/
p5.Element.prototype.value = function() {
console.log("v")
if (arguments.length > 0) {
this.elt.value = arguments[0];
} else {
if (this.elt.type === 'range') {
return parseFloat(this.elt.value);
}
else return this.elt.value;
}
};
/**
*
* Shows the current element. Essentially, setting display:block for the style.
*
* @method show
*/
p5.Element.prototype.show = function() {
this.elt.style.display = 'block';
};
/**
* Hides the current element. Essentially, setting display:none for the style.
*
* @method hide
*/
p5.Element.prototype.hide = function() {
this.elt.style.display = 'none';
};
/**
*
* Sets the width and height of the element. AUTO can be used to
* only adjust one dimension.
*
* @method size
* @param {Number} w width of the element
* @param {Number} h height of the element
*/
p5.Element.prototype.size = function(w, h) {
var aW = w;
var aH = h;
var AUTO = p5.prototype.AUTO;
if (aW !== AUTO || aH !== AUTO) {
if (aW === AUTO) {
aW = h * this.elt.width / this.elt.height;
} else if (aH === AUTO) {
aH = w * this.elt.height / this.elt.width;
}
// set diff for cnv vs normal div
if (this.elt instanceof HTMLCanvasElement) {
this.elt.setAttribute('width', aW * this._pInst._pixelDensity);
this.elt.setAttribute('height', aH * this._pInst._pixelDensity);
this.elt.setAttribute('style', 'width:' + aW + 'px !important; height:' + aH + 'px !important;');
} else {
this.elt.style.width = aW;
this.elt.style.height = aH;
}
this.width = this.elt.offsetWidth;
this.height = this.elt.offsetHeight;
if (this._pInst) { // main canvas associated with p5 instance
if (this._pInst._curElement.elt === this.elt) {
this._pInst._setProperty('width', this.elt.offsetWidth);
this._pInst._setProperty('height', this.elt.offsetHeight);
}
}
}
};
/**
* Removes the element and deregisters all listeners.
*
* @method remove
*/
p5.Element.prototype.remove = function() {
// deregister events
for (var ev in this._events) {
this.elt.removeEventListener(ev, this._events[ev]);
}
this.elt.parentNode.removeChild(this.elt);
delete(this);
};
// =============================================================================
// p5.MediaElement additions
// =============================================================================
/**
* A class to describe...
*
* @class p5.MediaElement
* @constructor
* @param {String} elt DOM node that is wrapped
* @param {Object} [pInst] pointer to p5 instance
*/
p5.MediaElement = function(elt, pInst) {
p5.Element.call(this, elt, pInst);
};
p5.MediaElement.prototype = Object.create(p5.Element.prototype);
/**
* Play an HTML5 media element.
*
* @method play
*/
p5.MediaElement.prototype.play = function() {
if (this.elt.currentTime === this.elt.duration) {
this.elt.currentTime = 0;
}
this.elt.play();
};
/**
* Stops an HTML5 media element (sets current time to zero).
*
* @method stop
*/
p5.MediaElement.prototype.stop = function() {
this.elt.pause();
this.elt.currentTime = 0;
};
/**
* Pauses an HTML5 media element.
*
* @method pause
*/
p5.MediaElement.prototype.pause = function() {
this.elt.pause();
};
/**
* Set 'loop' to true for an HTML5 media element, and starts playing.
*
* @method loop
*/
p5.MediaElement.prototype.loop = function() {
this.elt.setAttribute('loop', true);
this.play();
};
/**
* Set 'loop' to false for an HTML5 media element. Element will stop
* when it reaches the end.
*
* @method noLoop
*/
p5.MediaElement.prototype.noLoop = function() {
this.elt.setAttribute('loop', false);
};
/**
* Set HTML5 media element to autoplay or not.
*
* @method autoplay
* @param {Boolean} autoplay whether the element should autoplay
*/
p5.MediaElement.prototype.autoplay = function(val) {
this.elt.setAttribute('autoplay', val);
};
/**
* Sets volume for this HTML5 media element. If no argument is given,
* returns the current volume;
*
* @param {Number} [val] volume between 0.0 and 1.0
* @return {Number} current volume
* @method volume
*/
p5.MediaElement.prototype.volume = function(val) {
if (typeof val === 'undefined') {
return this.elt.volume;
} else {
this.elt.volume = val;
}
};
/**
* If no arguments are given, returns the current time of the elmeent.
* If an argument is given the current time of the element is set to it.
*
* @method time
* @param {Number} [time] time to jump to (in seconds)
* @return {Number} current time (in seconds)
*/
p5.MediaElement.prototype.time = function(val) {
if (typeof val === 'undefined') {
return this.elt.currentTime;
} else {
this.elt.currentTime = val;
}
};
/**
* Returns the duration of the HTML5 media element.
*
* @method duration
* @return {Number} duration
*/
p5.MediaElement.prototype.duration = function() {
return this.elt.duration;
};
p5.MediaElement.prototype.stop = function() {
this.elt.pause();
this.elt.currentTime = 0;
};
p5.MediaElement.prototype.pixels = [];
p5.MediaElement.prototype.loadPixels = function() {
if (!this.canvas) {
this.canvas = document.createElement('canvas');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.getContext('2d').drawImage(this.elt, 0, 0);
p5.prototype.loadPixels.call(this);
} else {
c.canvas.getContext('2d').drawImage(this.elt, 0, 0);
p5.prototype.loadPixels.call(this);
}
}
p5.MediaElement.prototype.updatePixels = function(x, y, w, h){
p5.prototype.updatePixels.call(this, x, y, w, h);
}
p5.MediaElement.prototype.get = function(x, y, w, h){
return p5.prototype.get.call(this, x, y, w, h);
};
p5.MediaElement.prototype.set = function(x, y, imgOrCol){
p5.prototype.set.call(this, x, y, imgOrCol);
};
})();