diff --git a/Readme.md b/Readme.md index dbb17ee..e653a4c 100644 --- a/Readme.md +++ b/Readme.md @@ -27,6 +27,45 @@ fs.readFile(sketch, function(err, data) { }); ``` +## Usage + +```javascript +// Compile processing code into javascript, so it can be used in a LAMP webapp more effectively (no p5 lang parsing step by client needed) +// Intended to use with Grunt when developing and deploying web applications +// contribution by kroko.me + +var fs = require('fs'); +var p5 = require('processing'); +if (process.argv.length != 5) { + console.log("Usage: node compile.js "); + process.exit(code = 1); +} +else { + console.log("Reading file: " + process.argv[2]); + console.log("Output will be: " + process.argv[3]); + console.log("Variable will be: " + process.argv[4]); +} +console.log("Compiling sketch..."); + +fs.readFile(process.argv[2], function(err, data) { + var compiled = p5.Processing.compile(data.toString('utf-8')); + compiled = "var " + process.argv[4] + " = " + compiled + ";"; + fs.writeFile(process.argv[3], compiled, function(err) { + if (err) { + console.log(err); + } else { + console.log("...done!"); + } + }); +}); + +// Usage in web +// var domCanvas = document.getElementById('id-of-canvas-dom-element'); +// pjsPtr = new Processing(domCanvas, valiable-name-as-passed-to-this-script); +// if (pjsPtr) { } +``` + + ## Authors - Seiya Konno <seiya@uniba.jp> ([nulltask](https://github.com/nulltask)) diff --git a/deps/processing-js b/deps/processing-js index 308591d..2c88a18 160000 --- a/deps/processing-js +++ b/deps/processing-js @@ -1 +1 @@ -Subproject commit 308591d05db8be7e9a6ea0ea1f9ca1fb304e4e89 +Subproject commit 2c88a18fcd10951bd84008a5c2ebd996fe873042 diff --git a/examples/compile/compile.js b/examples/compile/compile.js new file mode 100644 index 0000000..ce1c83a --- /dev/null +++ b/examples/compile/compile.js @@ -0,0 +1,33 @@ +// Compile processing code into javascript, so that can be used in webpage more effectively (no p5 lang parsing step by client needed) +// Intended to use with Grunt when developing and deploying web applications +// contribution by kroko.me + +var fs = require('fs'); +var p5 = require('processing'); +if (process.argv.length != 5) { + console.log("Usage: node compile.js "); + process.exit(code = 1); +} +else { + console.log("Reading file: " + process.argv[2]); + console.log("Output will be: " + process.argv[3]); + console.log("Variable will be: " + process.argv[4]); +} +console.log("Compiling sketch..."); + +fs.readFile(process.argv[2], function(err, data) { + var compiled = p5.Processing.compile(data.toString('utf-8')); + compiled = "var " + process.argv[4] + " = " + compiled + ";"; + fs.writeFile(process.argv[3], compiled, function(err) { + if (err) { + console.log(err); + } else { + console.log("...done!"); + } + }); +}); + +// Usage in web +// var domCanvas = document.getElementById('id_of_canvas_dom_element'); +// pjsPtr = new Processing(domCanvas, valiable_name_as_passed_to_this_script); +// if (pjsPtr) { } \ No newline at end of file diff --git a/lib/processing.js b/lib/processing.js index bad7a5f..c30e22b 100644 --- a/lib/processing.js +++ b/lib/processing.js @@ -16,7 +16,7 @@ var fs = require('fs') , navigator = window.navigator , HTMLImageElement = window.HTMLImageElement , noop = function() {} - , processing = fs.readFileSync('./deps/processing-js/processing.js'); + , processing = fs.readFileSync(__dirname+'/../deps/processing-js/processing.js'); /** * Expose `version`. @@ -57,7 +57,7 @@ eval('(function(window, document) {' * Expose `Processing`. */ -exports.Processing = Processing; +exports.Processing = window.Processing; /** * Return processing instance.