improved error handling with preproc (empty status & server only runs when build successful); added 1 example: selectionFlower; added <IE9 alert to template

This commit is contained in:
fjenett
2011-06-04 10:38:43 +00:00
parent dbddec591a
commit c3dbecf291
5 changed files with 146 additions and 18 deletions
@@ -0,0 +1,75 @@
/**
* <p>This example shows you how to get the currently selected text
* from the HTML page that this sketch is running in.</p>
*
* <p>Just select some text somewhere on this page and see it be transformed
* into a ... graph.</p>
*
* <p>Heavily inspired by <a href="http://bit.ly/jHvvWX">Boris Müller</a>.</p>
*/
/* @pjs transparent=true; */
String selectedText = "";
void setup ()
{
size( 300, 300 );
}
void draw ()
{
background( 100,0 );
fill( 255 );
if ( !selectedText.equals("") )
{
translate( width/2, height/2 );
float tw = selectedText.length() * 10;
float[] x = new float[selectedText.length()];
float[] y = new float[selectedText.length()];
for ( int i = 0, n = 0; i < tw; n++ )
{
char cr = new Character(selectedText.charAt(n));
float c = cr;
float r = -HALF_PI + map( i, 0, tw, 0, TWO_PI );
x[n] = cos(r)*c;
y[n] = sin(r)*c;
i += 10;
}
fill( 100 );
if ( x.length > 0 )
{
beginShape();
for ( int i = 0; i < x.length; i++ )
{
vertex( x[i], y[i] );
}
endShape( CLOSE );
}
smooth();
fill( 255 );
stroke( 150 );
for ( int i = 0; i < x.length; i++ )
text( selectedText.charAt(i), x[i]*1.15, y[i]*1.15+6 );
}
else
{
fill( map( sin(frameCount/12.0),-1,1,100,200 ) );
text("Select some (other) text on this page to start.", 2, height/2);
}
}
void setSelectionText ( String txt )
{
selectedText = txt;
}
@@ -0,0 +1,50 @@
/**
* This code will be embedded into the HTML page as "normal"
* JavaScript code through a <script> tag. This allows one to
* interact with the page as any normal JavaScript code can.
*/
var mySketchInstance;
// called once the page has fully loaded
window.onload = function () {
getSketchInstance();
}
// this is called (repeatedly) to find the sketch
function getSketchInstance() {
var s = Processing.getInstanceById("selectionFlower");
if ( s == undefined )
return setTimeout(getSketchInstance, 200); // try again a bit later
mySketchInstance = s;
monitorSelection();
}
// this code gets called all the time (every 1/5 sec) to check
// if the selection changed
function monitorSelection ( ) {
var txt = undefined;
if (window.getSelection) {
txt = window.getSelection().toString();
} else if (document.getSelection) {
txt = document.getSelection().toString();
} else if (document.selection) {
txt = document.selection.createRange().text;
}
if ( txt !== undefined && txt != "" )
{
mySketchInstance.setSelectionText(txt); // set the text in the sketch
console.log( txt );
}
setTimeout(monitorSelection, 1000/5);
}