mirror of
https://github.com/processing/processing4.git
synced 2026-01-29 19:31:16 +01:00
more js mode examples: user and file input
This commit is contained in:
23
javascript/examples/Environment/FileReader/FileReader.pde
Normal file
23
javascript/examples/Environment/FileReader/FileReader.pde
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* <form><input type="file" id="file-input"/></form>
|
||||
*
|
||||
* No Safari support.
|
||||
*/
|
||||
|
||||
PImage img = null;
|
||||
|
||||
void setup ()
|
||||
{
|
||||
size(300, 200);
|
||||
}
|
||||
|
||||
void draw ()
|
||||
{
|
||||
background(255);
|
||||
if ( img != null ) image(img, 0,0, width,height);
|
||||
}
|
||||
|
||||
void newImageAvailable ( Image i )
|
||||
{
|
||||
img = new PImage( i );
|
||||
}
|
||||
35
javascript/examples/Environment/FileReader/reader.js
Normal file
35
javascript/examples/Environment/FileReader/reader.js
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
window.onload = function () {
|
||||
tryFindSketch();
|
||||
}
|
||||
|
||||
function tryFindSketch() {
|
||||
var sketch = Processing.instances[0];
|
||||
if ( sketch == undefined )
|
||||
return setTimeout(tryFindSketch, 200); // retry soon
|
||||
|
||||
sketch.console = console;
|
||||
initUploader(sketch);
|
||||
}
|
||||
|
||||
function initUploader ( sketch ) {
|
||||
var uploadField = document.getElementById("file-input");
|
||||
|
||||
uploadField.onchange = function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
var file = uploadField.files[0];
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onload = function (event) {
|
||||
var img = new Image();
|
||||
img.onload = function (event2) {
|
||||
sketch.newImageAvailable(img);
|
||||
}
|
||||
img.src = event.target.result;
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
87
javascript/examples/Environment/Input/lockedIn/lockedIn.pde
Normal file
87
javascript/examples/Environment/Input/lockedIn/lockedIn.pde
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Promt for user input
|
||||
*/
|
||||
|
||||
String password = null;
|
||||
boolean locked = false;
|
||||
int nextLock = 0;
|
||||
|
||||
void setup ()
|
||||
{
|
||||
size( 300, 200 );
|
||||
|
||||
textFont(createFont("Arial", 22));
|
||||
textAlign(CENTER);
|
||||
}
|
||||
|
||||
void draw ()
|
||||
{
|
||||
if ( password == null )
|
||||
{
|
||||
background( 255 );
|
||||
|
||||
fill( 0 );
|
||||
text( "Please set a password \n(click here)", width/2, height/2);
|
||||
}
|
||||
else if ( locked )
|
||||
{
|
||||
background( 100 );
|
||||
|
||||
fill( 255 );
|
||||
text( "LOCKED, click to unlock", width/2, height/2);
|
||||
}
|
||||
else
|
||||
{
|
||||
background( 255 );
|
||||
|
||||
fill( 0 );
|
||||
text( "UNLOCKED\nwill lock in "+int(ceil((nextLock-millis())/1000))+" secs", width/2, height/2);
|
||||
|
||||
if ( nextLock-millis() < 0 ) locked = true;
|
||||
}
|
||||
}
|
||||
|
||||
/*void mouseMoved ()
|
||||
{
|
||||
if ( password != null && !locked )
|
||||
nextLock = millis() + 5000;
|
||||
}*/
|
||||
|
||||
void mousePressed ()
|
||||
{
|
||||
if ( js != null )
|
||||
{
|
||||
if ( password == null )
|
||||
{
|
||||
password = js.promtForInput( "Please set and remember a password", "***********" );
|
||||
nextLock = millis() + 5000;
|
||||
}
|
||||
else if ( locked )
|
||||
{
|
||||
String passTry = js.promtForInput( "Enter your password", "" );
|
||||
while ( passTry != null && !passTry.equals(password) )
|
||||
{
|
||||
passTry = js.promtForInput( "Nope, try again", "" );
|
||||
if ( passTry == null ) break;
|
||||
}
|
||||
locked = passTry != null && !passTry.equals(password);
|
||||
if ( !locked )
|
||||
nextLock = millis() + 5000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// this is needed to define a way for us to be able to call out
|
||||
|
||||
interface JavaScript
|
||||
{
|
||||
String promtForInput( String message, String defaultAnswer );
|
||||
}
|
||||
|
||||
JavaScript js;
|
||||
|
||||
void setJS ( JavaScript jsi )
|
||||
{
|
||||
js = jsi;
|
||||
}
|
||||
16
javascript/examples/Environment/Input/lockedIn/promt.js
Normal file
16
javascript/examples/Environment/Input/lockedIn/promt.js
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
window.onload = function () {
|
||||
tryFindSketch();
|
||||
}
|
||||
|
||||
function tryFindSketch () {
|
||||
var sketch = Processing.instances[0];
|
||||
if ( sketch == undefined )
|
||||
return setTimeout( tryFindSketch, 200 ); // try again in 0.2 secs
|
||||
|
||||
sketch.setJS( this );
|
||||
}
|
||||
|
||||
function promtForInput ( msg, def ) {
|
||||
return window.prompt( msg, def );
|
||||
}
|
||||
Reference in New Issue
Block a user