Loading...
Logo
Processing Forum
Hi!
I've began to play with processin.js and I think it is quite nice for web, for me (and I play with processing from years) better then other 2d frameworks. I've tried to use it as java compiled into javscript and it is ok, and i've tried pure javascript, and it is a little bit verbose when you always has to call processing object (like _p.fill() instead of still fill()) but it is ok too.
I'm a little bit attracted to plain javascript because I don't know the language very well and it could be challenging to learn.

How do you use processing.js in production code?

Replies(10)

I don't use it directly. I just publish my codes @  http://studio.sketchpad.cc or  http://openprocessing.org/.   
Processing/Java becomes Processing/JavaScript then!
I felt the *exactly* same way about always having to call the processing object.  It was a tiny thorn in the side of perfect.  After trying it every which way I finally stumbled upon a solution.

Put this in yourSketch.html:
Copy code
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="../../libraries/processing-X.X.X.js"></script>
  4. </head>
  5. <body>
  6. <canvas datasrc="yourSketch.js" width="sketchWidth" height="sketchHeight"></canvas>
  7. </body>
  8. </html>

Put your JS in a file called yourSketch.js:

Copy code
  1. var x = 100;
  2. var y = 100;
  3. var w = 200;
  4. var h = 200;

  5. var bob = new Person("Bob");

  6. void setup(){
  7.       size(400,400);
  8.       background(150);
  9.       noLoop();
  10. }

  11. void draw(){
  12.       fill(255);
  13.       rect(x, y, w, h);
  14. }

  15. function Person(name){
  16.       this.name = name;
  17. };

  18. Person.prototype.speak = function(){
  19.       println("My name is " + this.name);
  20. };
And that's it!  I think it works because the PJS preprocessor ignores any JS and automatically converts any Processing. If there are no draw backs to the method, then I think it should be the advertised way to write PJS because it makes the transition a lot easier for people who are coming from a purely Processing background. [edit: I mean for using PJS outside of the IDE and writing in pure JS. at least in my IDE you cannot create prototypical objects, and functions that take arguments in JS mode.]

Full disclosure: I have only tried this method with native Processing API, core JS functions/classes, and functions/classes of my own creation.  I have not tried it with another library.  


For development, I use the Processing Development environment and write .pde files.

Testing is then done using either Java mode, or JavaScript mode/Web Export, and using the Developer Tools available in Safari/Chrome/Firefox/IE.

For production, I then pre-compile the code using the Processing.js helper page (you can also get an offline version from  https://github.com/processing-js/processing-js/tree/master/tools ) and load it with the API-only production version (processing-1.4.1-api.min.js). This results in much faster load times.
Do you have an HTML example, how to use the pre-compiled code?

I tried, but it don't work. Thank you
Some instructions on how to embed a .pde code into HTML:
http://processingjs.org/articles/p5QuickStart.html

And JavaScript mode generates such file when starting its server.

Also, this link transforms a P/Java into P/JS:
http://processingjs.org/tools/processing-helper.html

I also found useful this article, which shows how you can load XML data into your sketch from other websites (using your own php-proxy script). 



I have done some code, but it starts a bit slow on loading the page. So I try to use the precompiled code to speed up.

for example a small code:
test.pde
Copy code
  1. void setup()
  2. {
  3.   size(200, 100);
  4. }
  5. void draw()
  6. {
  7.   line(0, 0, width, height);
  8.   line(width, 0, 0, height);
  9. }

and then I use this simple HTML

Copy code
  1. <html>
  2.     <body>
  3.         <script src="processing.js" type="text/javascript"></script>
  4.         <canvas data-processing-sources="test.pde" width="200" height="100"></canvas>
  5.     </body>
  6. </html>

That works fine, but starts slow (not this code, but a bigger one)

I have compiled the test.pde to test.js:

Copy code
  1. // this code was autogenerated from PJS
  2. (function($p) {

  3. function setup() {
  4. $p.size(200, 100);
  5. }
  6. $p.setup = setup;

  7. function draw() {
  8. $p.line(0, 0, $p.width, $p.height);
  9.   $p.line($p.width, 0, 0, $p.height);
  10. }
  11. $p.draw = draw;

  12. })

And now my problem. How can I use it in my HTML? This one shows just a black canvas:

Copy code
  1. <html>
  2.     <body>
  3.         <script src="processing.js" type="text/javascript"></script>
  4.         <canvas datasrc="test.js" type="text/javascript" width="200" height="100"></canvas>
  5.     </body>
  6. </html>

There is something simple wrong?


Thank you for helping

Thomas





Wrap your compiled sketch into a variable (which will be the function name):
Copy code
  1. var myTest =  (function($p) {

  2. function setup() {
  3. $p.size(200, 100);
  4. }
  5. $p.setup = setup;

  6. function draw() {
  7. $p.line(0, 0, $p.width, $p.height);
  8.   $p.line($p.width, 0, 0, $p.height);
  9. }
  10. $p.draw = draw;

  11. })
Your HTML code:
Copy code
  1. <html>
    <head>
      <script src="processing.js" type="text/javascript"></script>
      <script src="test.js"  type="text/javascript"></script>
    </head>
    <body>
      <canvas id="mytwolines"   width="200" height="100"></canvas>
      <noscript>
        <p>JavaScript is required to view the contents of this page.</p>
      </noscript>
      <script type="text/javascript">
        var myCanvas = document.getElementById('mytwolines');
         new Processing(myCanvas, myTest);
      </script>
    </body>
    </html>
Not all browsers allow JS code and other plugins run on from offline HTML files:
http://forum.processing.org/topic/export-and-run-a-app-on-webspace
Thanks to mbraendle (Grüezi) That is the piece of code I had looking for.
And now I've put it in my page. And it starts a bit faster.

Here you can see:
http://www.motivationsbaum.de/baumbearbeiten.php?id=zfaaf&p=rcxfcodghe
sorry the page is in german, next time I will translate it.

Greetings
Thomas