how to get my sketches working under Processing JavaScript...

edited November 2014 in JavaScript Mode

I am really starting to have issues with porting some of my old style coding - Java applets called from HTML - to the new style of JavaScript Processing sources called from new HTML...

some of my code just won't work in JavaScript Mode of Processing, whilst working fine in Java Mode...

I would like to include a .ZIP with complete source code, but this post will not allow that...

I would really like some input...

Sincerely, Ron Vonk

Comments

  • here is a small portion, of what does work...

  • HM, there was supposed to be a link there... new go...

  • sorry, no link can be added... visit kilroywashere.nl

  • anyway, it is clear that JavaScript does not support a wide range of Java capabilities... how do I work around that?

  • Processing by itself is a framework atop some programming language. Some examples:

    • Java Mode = Java
    • JavaScript Mode (a.K.a. Processing.JS) & P5.JS = JavaScript
    • CoffeeScript Mode = CoffeeScript
    • Python Mode = Python
    • Android = Dalvik Java
    • etc.

    In particular, JavaScript Mode is meant to transpile a Java Mode sketch into JS atop Processing.JS framework.
    Therefore, such Java sketch should adhere to Processing.JS's API reference: http://processingjs.org/reference/
    The transpiler is very good, but it wasn't meant to convert the whole Java API into JavaScript!
    I've successfully got many sketches running in both Java & JS modes. As long they stick to Processing's API!

  • Then what should we do to get Java Mode sketches into the web w/o Java Applets?
    That depends on which libraries our sketches rely on. We gotta replace Java libraries w/ corresponding JS 1s!

    Of course, that would invalidate cross compatibility between Java & JS modes.
    In such cases, I recommend to learn 1 of these 2 modes: CoffeeScript or P5.JS!

    CoffeeScript language got both class & extends keywords, which eases transition from Java w/ classes:
    http://coffeescript.org/#classes
    Also CS mode can have both ".js" & ".coffee" tabs besides importing JS libs!

    While CoffeeScript uses Processing.JS framework just like JS mode, P5.JS is another framework focused on JS language rather than transpilation between Java to JS:
    http://p5js.org/reference/
    So if you choose P5.JS, you gotta convert Java classes into JS prototype style! :|

    Those 2 choices above are meant when JS mode alone proves too difficult to transpile Java mode, of course!

  • Dear GoToLoop,

    on behalf of all of us noobies, tx for your elaborate response... I am sure there is a lot to learn for us there... but I am not sure, if you are getting my point...

    I have a vast library of sketches that work fine under Java Mode in Processing, and I understand that the JavaScript Mode is a lot stricter than Java itself... in other words, not all Java code will work under JavaScript, via Processing, unless you use strict coding rules...

    all I really need is some useful hints, on how to port your Processing code to the JavaScript Mode in such a way that it will work...

    some of the hints I already discovered are:

    • put size() in your main setup(), and not elsewhere...
    • do drawing action via main draw(), and not 'hidden'...
    • do not overload the webpage/browser memory buffer...
    • when using PVector type, make it full 3D and do not leave out the Z-component...

    and such... but I need more hints...

    but thanks so far...

    1. Function size() should not only be in setup() but its 1st statement. In any Processing mode!
    2. I don't get 'hidden'! Callback draw() is continuously called by Processing's framework in any mode.
    3. Obvious, a web sketch got less memory available, just like Android mode.
    4. Some PVector methods got some glitches when not all 3 parameters are passed. Just use 0 for 3rd 1.
  • edited November 2014

    2 More online examples. This 1 uses extends for inheritance:
    http://studio.processingtogether.com/sp/pad/export/ro.91FXxn8BG2fPL/latest

    And next 1 got it all, PVector & ArrayList: $-)
    http://studio.processingtogether.com/sp/pad/export/ro.91kLmk61vAOZp/latest

  • GoToLoop,

    tx again... I will look into your input...

    about 'hidden' draws, I have noticed that under JavaScript Mode not all draws are recorded... I have not yet understood, why that is...

    I did notice, that even the straight draw() does not always show up... see my latest example above for that one...

    thanks for sticking with me...

  • short and simple, in order to let intermediate changes to your display matrix show up, you will have to render them in each cycle of draw()... translate(), pushMatrix(), and popMatrix() are usually good ways to do that... still learning that one ;)

  • edited November 2014

    ... that even the straight draw() does not always show up... see my latest example above for that one...

    I've noticed that up till now you hadn't mentioned anything that fails in JS Mode but works in Java Mode! ~:>
    Your sketch "Monte Carlo" got the same behavior both in Java & JS modes!

    It's basic knowledge in Processing in any of its flavors that the screen is updated only after draw() finishes!
    If we do everything in 1 draw() iteration, we won't see any "animation" happen! 8-X

  • GoToLoop, you seem like a funny person to me...

    trust me, I have lots of stuff that won't work under JavaSript Mode, but works fine under Java Mode... the reason you haven't seen any as of yet, is exactly my problem... if it won't work under JavaScript Mode, I cannot upload it... for obvious reasons...

    In the old days, I might have uploaded it to Open Processing as a working Java sketch... but that doesn't work no longer, because now you can only upload JavaScript Mode...

    of couse, I could find some way to post you a ZIP-file with the sketch(es)...

    thanks for the positive input so far...

    regards...

  • loopNoLoop,

    as a token of my appreciation, I did the unthinkable... I posted a non-working JavaScript on my webpage, just to supply you with some link to the source code...

    http://kilroywashere.nl/processing/tree_NR/

    and please, do not tell me it works fine in your browser ;)

  • here the link to the WordPress webpage...

    http://kilroywashere.nl/?p=50

  • downloaded the source code from the web, straight into Java Mode... works...

  • here is pretty much the same tree, only recursively drawn plus use of translate(), pushMatrix() and popMatrix()...

    http://kilroywashere.nl/processing/tree/

  • I downloaded your example from http://kilroywashere.nl/processing/tree_NR/ It works if you use ProcessingJS 1.4.8

  • Mbraendle, tx for the very useful hint...

    I was under the impression, that Processing would have prompted me for a new version of JavaScript, if and when available... but it obviously didn't ;)

    so I downloaded ProcessingJS 1.4.8 from Github... but where do I put it?

  • OK... I noticed I can pick up the Processing.JS file and put it on webpages... This seems to work fine...

    But how can I make Processing able to automatically attach it to my source code for web-export?

  • edited November 2014

    But how can I make Processing able to automatically attach it to my source code for web-export?

    "sketchbook"/modes/JavaScriptMode/template/processing.js

    Keep a copy of the original "processing.js" before replacing it, OK? ;-)
    And be aware that v1.4.8 is considered a development version and it's still too far from Java Mode 2 & 3!

  • edited November 2014

    I did the unthinkable... I posted a non-working JavaScript on my webpage...

    Well, you coulda posted it here, some PasteBin type site or even GitHub.com!
    It's a working Java Mode sketch after all, isn't it?! :-j

  • edited November 2014

    I guess I've found the reason why your sketch doesn't transpile into JS.
    Actually, your sketch can't be compiled w/ the old Processing v1.5.1 either! @-)

    Before Processing 2, class PVector didn't have method heading()! Take a look at this past forum thread:
    http://forum.processing.org/two/discussion/6368/project-drone-wing#Item_22

    Instead it exists by the @Deprecated name heading2D():
    http://forum.processing.org/two/discussion/6368/project-drone-wing#Item_30

    Take a look at this online example using heading2D() working w/ "processing.js" v1.4.1:
    http://studio.processingtogether.com/sp/pad/export/ro.9D4K1FIWJ7Kxc/latest

  • think I found it...

    put Processing.JS in folder My Documents\Processing\modes\JavaScriptMode\template... this is apparently were the modes and their templates are stored...

  • Loopster... you are late...

    but thanks anyway for all your input... and I like your collection of emoticons 3:)

  • in case you got worried: of course I did not keep a copy of the 1.4.1 JS-file... if it doesn't compile my code, screw it ;)

    anyway, it is still somewhere in my Processing 2.2.1 kit, isn't it? plus multiple copies of it reside on my website, in some web-export-folders, etcetera...

    I would like to keep this thread open, for future reference...

  • And be aware that v1.4.8 is considered a development version (...)

    Wrong. It's officially published on processingjs.org .

  • edited November 2014

    V1.4.8 is more like a snapshot of something that stopped in the middle of development.
    It's stable but still isn't too much diff. than v1.4.1 and far away to Java Mode v2 or v3 milestone! :-S

    Unfortunately, it's yet to be accepted by hosting sites like http://www.OpenProcessing.org & http://Studio.SketchPad.cc !
    And when I tested it under JS Mode, println() failed. And I like to use that for debugging too! *-:)

Sign In or Register to comment.