GeezerVison is showing github.com — more js mode examples: user and file input · Variablefrog/processing@a0130d6
File treeExpand file treeCollapse file tree Open diff view settings javascript/examples/Environment
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * <form><input type="file" id="file-input"/></form> | | 3 | + * | | 4 | + * No Safari support. | | 5 | + */ | | 6 | + | | 7 | + PImage img = null; | | 8 | + | | 9 | + void setup () | | 10 | + { | | 11 | + size(300, 200); | | 12 | + } | | 13 | + | | 14 | + void draw () | | 15 | + { | | 16 | + background(255); | | 17 | + if ( img != null ) image(img, 0,0, width,height); | | 18 | + } | | 19 | + | | 20 | + void newImageAvailable ( Image i ) | | 21 | + { | | 22 | + img = new PImage( i ); | | 23 | + } |
Collapse file Copy file name to clipboard +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | + | | 2 | +window.onload = function () { | | 3 | + tryFindSketch(); | | 4 | +} | | 5 | + | | 6 | +function tryFindSketch() { | | 7 | + var sketch = Processing.instances[0]; | | 8 | + if ( sketch == undefined ) | | 9 | + return setTimeout(tryFindSketch, 200); // retry soon | | 10 | + | | 11 | + sketch.console = console; | | 12 | + initUploader(sketch); | | 13 | +} | | 14 | + | | 15 | +function initUploader ( sketch ) { | | 16 | + var uploadField = document.getElementById("file-input"); | | 17 | + | | 18 | + uploadField.onchange = function (e) { | | 19 | + e.preventDefault(); | | 20 | + | | 21 | + var file = uploadField.files[0]; | | 22 | + var reader = new FileReader(); | | 23 | + | | 24 | + reader.onload = function (event) { | | 25 | + var img = new Image(); | | 26 | + img.onload = function (event2) { | | 27 | + sketch.newImageAvailable(img); | | 28 | + } | | 29 | + img.src = event.target.result; | | 30 | + }; | | 31 | + reader.readAsDataURL(file); | | 32 | + | | 33 | + return false; | | 34 | + } | | 35 | +} |
Collapse file Copy file name to clipboard +87Lines changed: 87 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Promt for user input | | 3 | + */ | | 4 | + | | 5 | +String password = null; | | 6 | +boolean locked = false; | | 7 | +int nextLock = 0; | | 8 | + | | 9 | +void setup () | | 10 | +{ | | 11 | + size( 300, 200 ); | | 12 | + | | 13 | + textFont(createFont("Arial", 22)); | | 14 | + textAlign(CENTER); | | 15 | +} | | 16 | + | | 17 | +void draw () | | 18 | +{ | | 19 | + if ( password == null ) | | 20 | + { | | 21 | + background( 255 ); | | 22 | + | | 23 | + fill( 0 ); | | 24 | + text( "Please set a password \n(click here)", width/2, height/2); | | 25 | + } | | 26 | + else if ( locked ) | | 27 | + { | | 28 | + background( 100 ); | | 29 | + | | 30 | + fill( 255 ); | | 31 | + text( "LOCKED, click to unlock", width/2, height/2); | | 32 | + } | | 33 | + else | | 34 | + { | | 35 | + background( 255 ); | | 36 | + | | 37 | + fill( 0 ); | | 38 | + text( "UNLOCKED\nwill lock in "+int(ceil((nextLock-millis())/1000))+" secs", width/2, height/2); | | 39 | + | | 40 | + if ( nextLock-millis() < 0 ) locked = true; | | 41 | + } | | 42 | +} | | 43 | + | | 44 | +/*void mouseMoved () | | 45 | +{ | | 46 | + if ( password != null && !locked ) | | 47 | + nextLock = millis() + 5000; | | 48 | +}*/ | | 49 | + | | 50 | +void mousePressed () | | 51 | +{ | | 52 | + if ( js != null ) | | 53 | + { | | 54 | + if ( password == null ) | | 55 | + { | | 56 | + password = js.promtForInput( "Please set and remember a password", "***********" ); | | 57 | + nextLock = millis() + 5000; | | 58 | + } | | 59 | + else if ( locked ) | | 60 | + { | | 61 | + String passTry = js.promtForInput( "Enter your password", "" ); | | 62 | + while ( passTry != null && !passTry.equals(password) ) | | 63 | + { | | 64 | + passTry = js.promtForInput( "Nope, try again", "" ); | | 65 | + if ( passTry == null ) break; | | 66 | + } | | 67 | + locked = passTry != null && !passTry.equals(password); | | 68 | + if ( !locked ) | | 69 | + nextLock = millis() + 5000; | | 70 | + } | | 71 | + } | | 72 | +} | | 73 | + | | 74 | + | | 75 | +// this is needed to define a way for us to be able to call out | | 76 | + | | 77 | +interface JavaScript | | 78 | +{ | | 79 | + String promtForInput( String message, String defaultAnswer ); | | 80 | +} | | 81 | + | | 82 | +JavaScript js; | | 83 | + | | 84 | +void setJS ( JavaScript jsi ) | | 85 | +{ | | 86 | + js = jsi; | | 87 | +} |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | + | | 2 | +window.onload = function () { | | 3 | + tryFindSketch(); | | 4 | +} | | 5 | + | | 6 | +function tryFindSketch () { | | 7 | + var sketch = Processing.instances[0]; | | 8 | + if ( sketch == undefined ) | | 9 | + return setTimeout( tryFindSketch, 200 ); // try again in 0.2 secs | | 10 | + | | 11 | + sketch.setJS( this ); | | 12 | +} | | 13 | + | | 14 | +function promtForInput ( msg, def ) { | | 15 | + return window.prompt( msg, def ); | | 16 | +} |
|