Difference between revisions of "Processing.js/HowTo"

From CDOT Wiki
Jump to: navigation, search
Line 16: Line 16:
 
** Simple drawing tutorial
 
** Simple drawing tutorial
  
==PJS Preloading directive==
+
==P.JS Preloading directive==
  
Because of asynchronous loading in the browser, images used in sketches for processing.js should be preloaded using the @PJS directive at the top line of the sketch.  The PJS preloading directive forces the sketch to load specified images ahead of time before the sketch begins to run.
+
Because of asynchronous loading in the browser, images used in sketches for processing.js should be preloaded using the @pjs directive at the top line of the sketch.  The P.JS preloading directive forces the sketch to load specified images ahead of time before the sketch begins to run.
  
 
Not using the preloading functionality will cause the sketch to continue running after a loadImage() or requestImage() call and if you specify actions to happen on the images you will get unpredictable behavior if the image data isn’t fully loaded yet.
 
Not using the preloading functionality will cause the sketch to continue running after a loadImage() or requestImage() call and if you specify actions to happen on the images you will get unpredictable behavior if the image data isn’t fully loaded yet.
Line 28: Line 28:
 
Sample code
 
Sample code
 
loading one image
 
loading one image
     /* @PJS preload=”image.jpg”; */
+
     /* @pjs preload=”image.jpg”; */
  
 
loading multiple images
 
loading multiple images
     /* @PJS preload=”sun.jpg,moon.jpg,stars.jpg”; */
+
     /* @pjs preload=”sun.jpg,moon.jpg,stars.jpg”; */
  
 
loading an image from a remote location
 
loading an image from a remote location
     /* @PJS preload=”http://www.urltoimage.com/image.jpg”; */
+
     /* @pjs preload=”http://www.urltoimage.com/image.jpg”; */
  
 
loadImage() example
 
loadImage() example

Revision as of 15:12, 27 August 2010

Getting Started With Processing

P.JS Preloading directive

Because of asynchronous loading in the browser, images used in sketches for processing.js should be preloaded using the @pjs directive at the top line of the sketch. The P.JS preloading directive forces the sketch to load specified images ahead of time before the sketch begins to run.

Not using the preloading functionality will cause the sketch to continue running after a loadImage() or requestImage() call and if you specify actions to happen on the images you will get unpredictable behavior if the image data isn’t fully loaded yet.

The alternative to preload caching is to put any image manipulation code inside the draw() loop and use checks to make sure the width and height are larger than 0 before manipulating pixels. This will cause frames to skip and pixel manipulation to happen only after an image has fully loaded.

NOTE: Because of the way preloading is being used in P.JS and the fact that you cant block the execution of a sketch while waiting for an image to load in the browser, both loadImage() and requestImage() function the same way in P.JS. Processing's JAVA implementation allows loadImage() to block sketch execution while an image loads up and requestImage() is used to preload images ahead of time.

Sample code loading one image

   /* @pjs preload=”image.jpg”; */

loading multiple images

   /* @pjs preload=”sun.jpg,moon.jpg,stars.jpg”; */

loading an image from a remote location

   /* @pjs preload=”http://www.urltoimage.com/image.jpg”; */

loadImage() example

   Pimage x = loadImage(“sun.jpg”);

x will contain the preloaded image: sun.jpg.

requestImage() example

   Pimage y = requestImage(“moon.jpg”);

y will contain the preloaded image: moon.jpg.