Skip to content

Commit 42be883

Browse files
committed
Move input event listener to p5.Element.js
1 parent 9081ef1 commit 42be883

File tree

2 files changed

+74
-75
lines changed

2 files changed

+74
-75
lines changed

src/dom/dom.js

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -330,79 +330,6 @@ function dom(p5, fn){
330330
removeableElements.map(el => el.remove());
331331
};
332332

333-
/**
334-
* Calls a function when the element receives input.
335-
*
336-
* `myElement.input()` is often used to with text inputs and sliders. Calling
337-
* `myElement.input(false)` disables the function.
338-
*
339-
* @method input
340-
* @param {Function|Boolean} fxn function to call when input is detected within
341-
* the element.
342-
* `false` disables the function.
343-
* @chainable
344-
*
345-
* @example
346-
* <div>
347-
* <code>
348-
* let slider;
349-
*
350-
* function setup() {
351-
* createCanvas(100, 100);
352-
*
353-
* background(200);
354-
*
355-
* // Create a slider and place it beneath the canvas.
356-
* slider = createSlider(0, 255, 200);
357-
* slider.position(0, 100);
358-
*
359-
* // Call repaint() when the slider changes.
360-
* slider.input(repaint);
361-
*
362-
* describe('A gray square with a range slider underneath it. The background changes shades of gray when the slider is moved.');
363-
* }
364-
*
365-
* // Paint the background using slider's value.
366-
* function repaint() {
367-
* let g = slider.value();
368-
* background(g);
369-
* }
370-
* </code>
371-
* </div>
372-
*
373-
* <div>
374-
* <code>
375-
* let input;
376-
*
377-
* function setup() {
378-
* createCanvas(100, 100);
379-
*
380-
* background(200);
381-
*
382-
* // Create an input and place it beneath the canvas.
383-
* input = createInput('');
384-
* input.position(0, 100);
385-
*
386-
* // Call repaint() when input is detected.
387-
* input.input(repaint);
388-
*
389-
* describe('A gray square with a text input bar beneath it. Any text written in the input appears in the middle of the square.');
390-
* }
391-
*
392-
* // Paint the background gray and display the input's value.
393-
* function repaint() {
394-
* background(200);
395-
* let msg = input.value();
396-
* text(msg, 5, 50);
397-
* }
398-
* </code>
399-
* </div>
400-
*/
401-
Element.prototype.input = function (fxn) {
402-
Element._adjustListener('input', fxn, this);
403-
return this;
404-
};
405-
406333
/**
407334
* Helpers for create methods.
408335
*/
@@ -712,8 +639,7 @@ function dom(p5, fn){
712639
return addElement(elt, this);
713640
};
714641

715-
/** INPUT **/
716-
642+
/* INPUT */
717643
/**
718644
* Creates a slider `&lt;input&gt;&lt;/input&gt;` element.
719645
*

src/dom/p5.Element.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,79 @@ class Element {
22112211
return this;
22122212
}
22132213

2214+
/**
2215+
* Calls a function when the element receives input.
2216+
*
2217+
* `myElement.input()` is often used to with text inputs and sliders. Calling
2218+
* `myElement.input(false)` disables the function.
2219+
*
2220+
* @method input
2221+
* @param {Function|Boolean} fxn function to call when input is detected within
2222+
* the element.
2223+
* `false` disables the function.
2224+
* @chainable
2225+
*
2226+
* @example
2227+
* <div>
2228+
* <code>
2229+
* let slider;
2230+
*
2231+
* function setup() {
2232+
* createCanvas(100, 100);
2233+
*
2234+
* background(200);
2235+
*
2236+
* // Create a slider and place it beneath the canvas.
2237+
* slider = createSlider(0, 255, 200);
2238+
* slider.position(0, 100);
2239+
*
2240+
* // Call repaint() when the slider changes.
2241+
* slider.input(repaint);
2242+
*
2243+
* describe('A gray square with a range slider underneath it. The background changes shades of gray when the slider is moved.');
2244+
* }
2245+
*
2246+
* // Paint the background using slider's value.
2247+
* function repaint() {
2248+
* let g = slider.value();
2249+
* background(g);
2250+
* }
2251+
* </code>
2252+
* </div>
2253+
*
2254+
* <div>
2255+
* <code>
2256+
* let input;
2257+
*
2258+
* function setup() {
2259+
* createCanvas(100, 100);
2260+
*
2261+
* background(200);
2262+
*
2263+
* // Create an input and place it beneath the canvas.
2264+
* input = createInput('');
2265+
* input.position(0, 100);
2266+
*
2267+
* // Call repaint() when input is detected.
2268+
* input.input(repaint);
2269+
*
2270+
* describe('A gray square with a text input bar beneath it. Any text written in the input appears in the middle of the square.');
2271+
* }
2272+
*
2273+
* // Paint the background gray and display the input's value.
2274+
* function repaint() {
2275+
* background(200);
2276+
* let msg = input.value();
2277+
* text(msg, 5, 50);
2278+
* }
2279+
* </code>
2280+
* </div>
2281+
*/
2282+
input(fxn) {
2283+
Element._adjustListener('input', fxn, this);
2284+
return this;
2285+
}
2286+
22142287
/**
22152288
* Calls a function when the user drops a file on the element.
22162289
*

0 commit comments

Comments
 (0)