Given Pjs is a JS library inasmuch as p5js is, we can write sketches using JS syntax (or any other language transpilable to JS) for the Pjs library too!
The catch is, that a small part of Processingās API gives the same name for both a system variable and a function.
For example, the name frameRate
in Processing can either refer to a variable or a function:
- Variable frameRate: http://ProcessingJS.org/reference/frameRate/
- Function frameRate(): http://ProcessingJS.org/reference/frameRate_/
But alike Java, JS doesnāt offer diff. namespaces for each category.
Functions, classes, objects and any type of data are simply all stored in variables.
Therefore, the Pjs library gotta change the name of either the system variable or the function to something else.
Also, some Processingās function names which match Javaās keywords, like int
or float
, are also renamed, both in Processing & Pjs:
- Function int(): http://ProcessingJS.org/reference/int_/
- Keyword
int
: http://ProcessingJS.org/reference/int/
For example, if we copy & paste the sketch example below:
int a = int(PI);
frameRate(30.7);
println(frameRate);
In http://ProcessingJS.org/tools/processing-helper.html
And click at āConvert to JSā button; itās gonna output the following JS syntax:
// this code was autogenerated from PJS
(function($p) {
var a = $p.parseInt($p.PI);
$p.frameRate(30.7);
$p.println($p.__frameRate);
})
Notice that function int() became parseInt(), function frameRate() stayed the same, but its corresponding system variable became __frameRate!
If we take those small quirks into account, itās perfectly possible to write sketches in JS syntax targeting Pjs in place of p5js.
Although weād need to write our sketches for Pjs very similarly weād write to p5js using its āInstance Modeā approach:
For further details, read from these links: