Processing.js

Compiling Java code to be used as a library for the web page sketches.

Let's say we have the following library:

public class TheLib {
  public String greet() {
    return "Hello, world";
  }
}

And the sketch to use it with:

background(0);
TheLib lib = new TheLib();
String s = lib.greet();
text(s,10,40);

Load the library code as a string and call the Processing.compile method. The returned object is a Processing.Sketch object. Its sourceCode property returns the code for the "attach" function.

var attachCode = Processing.compile(libraryCode).sourceCode;

This code can be used to call in the form attachFunction(Processing.prototype). That will let all library classes and methods be registered with the default scope.

So the library source code will look like:


  

The code above that can be used as a stanalone js-file and be loaded in the <head> after the processing.js.

The sketch execution results: