Confirmed users
656
edits
| Line 182: | Line 182: | ||
Here two buttons in the DOM are used to allow the user to start or stop a running Processing sketch. They control the Processing instance (you might have several in a page, or hidden in divs) directly from JavaScript, calling Processing functions: loop() and noLoop(). The Processing functions are well documented elsewhere. | Here two buttons in the DOM are used to allow the user to start or stop a running Processing sketch. They control the Processing instance (you might have several in a page, or hidden in divs) directly from JavaScript, calling Processing functions: loop() and noLoop(). The Processing functions are well documented elsewhere. | ||
==Things to Know as a | ==Things to Know as a JavaScript Developer using Processing.js== | ||
While Processing.js | While Processing.js tries to be fully compatible with Processing, there are some things which are different or require workarounds. We have also added some web-specific features to make Processing.js easier to use. Here are some tricks and tips as you start working on more complex sketches in Processing.js. | ||
===Processing.js | ===Processing.js provides access to various DOM/JavaScript objects via the externals property=== | ||
Processing | Each Processing instance (i.e., Processing.instances) has an '''externals''' property, which is an object containing references to various non-Processing DOM/JavaScript objects that can be useful. For example: | ||
* canvas - the canvas to which the sketch is bound | |||
* context - the canvas' context | |||
* onblur and onfocus - event handlers | |||
Processing | The Processing instance's externals property is a good place to stash various objects that you need to share between the instance and the global object. | ||
===Division which is expected to produce an integer might need explicit casting=== | ===Division which is expected to produce an integer might need explicit casting=== | ||
There are a class of bugs that arise when converting Processing code to Processing.js that involve integer vs. floating point division. What was straight-up integer division in Processing code, when converted to | There are a class of bugs that arise when converting Processing code to Processing.js that involve integer vs. floating point division. What was straight-up integer division in Processing code, when converted to JavaScript, can sometimes become problematic, as numbers become doubles, and introduce a fractional part. The fix is to explicitly cast any division to an integer that exhibits this behaviour: | ||
<pre> | <pre> | ||
| Line 212: | Line 214: | ||
Processing uses a synchronous I/O model, which means that functions like '''loadImage()''' take time to execute, and while they are running, nothing else happens: the program waits until '''loadImage()''' is done before moving on to the next statement. This means that you can count on the value returned by a function like '''loadImage()''' being usable in the next line of code. | Processing uses a synchronous I/O model, which means that functions like '''loadImage()''' take time to execute, and while they are running, nothing else happens: the program waits until '''loadImage()''' is done before moving on to the next statement. This means that you can count on the value returned by a function like '''loadImage()''' being usable in the next line of code. | ||
Obviously, JavaScript doesn't work like this. The web uses an asynchronous I/O model, which means that functions which load external resources can't make the program wait until they finish. In order to replicate Processing's load* functions, you have to use a special Processing.js Directive. | |||
The Processing.js Directives are hints to the browser that are written in comments rather than in the Processing code itself. Here's a typical Processing sketch that loads an image synchronously and then draws it: | The Processing.js Directives are hints to the browser that are written in comments rather than in the Processing code itself. Here's a typical Processing sketch that loads an image synchronously and then draws it: | ||
| Line 311: | Line 313: | ||
Portions of the code above are from the Processing.js project's '''init.js''' file, see http://github.com/annasob/processing-js/blob/0.9.8/examples/init.js. This file will likely be going away in the future, and happen automatically as part of Processing.js initialization. | Portions of the code above are from the Processing.js project's '''init.js''' file, see http://github.com/annasob/processing-js/blob/0.9.8/examples/init.js. This file will likely be going away in the future, and happen automatically as part of Processing.js initialization. | ||