I have written generic Point classes, line/line intersection algorithms and Layer administration code more times than I can remember. I don't like menial programming, so this is effectively my code library. But you can use it too!
The language used is Processing, which is a mostly-Java-syntax language. I hardly ever use Java-specific ninja code, so rewriting any of these snippets to your favourite language is less than a minute's worth of work, typically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * enable JavaScript in sketches */ abstract class JavaScript { /** this is effectively an header file for all your sketch-accessible js variables and functions. While JavaScript doesn't care, Processing does, so just add sensible typing to your variables and function signatures here, even if you're going to ignore them on the JavaScript side. Examples: Type variable = defaultValue; void functor(Type arg1, Type arg2); **/ } // sketch-global javascript object JavaScript javascript; // sketch-global bind function void bindJavaScript(JavaScript js) { javascript = js; } /** call this from Javascript using: <script type="text/javascript"> var sketch; function bindJavaScriptTo(canvasId) { var instance = Processing.getInstanceById(canvasId); if(!sketch && instance && instance.bindJavaScript) { sketch = instance; sketch.bindJavaScript(this); return; } setTimeout(function(){bindJavaScript(canvasId);}, 250); } document.addEventListener("DOMContentLoaded", function() { bindJavaScript('[...id string for canvas element used...]'); }, false); </script> **/ // sketch-global flag that can tell use whether we're in Java or JavaScript. // Since Java considers floats and ints different numbers, and JavaScript does // not, Java will set this flag to "false", and JavaScript will set it to "true". boolean usingJavaScriptEngine = (2.0==2); |
Written and maintained by Mike "Pomax" Kamermans. All code snippets on this site are "do whatever you want with it" licensed.