Difference between revisions of "Processing.js/HowTo"

From CDOT Wiki
Jump to: navigation, search
(Review changes from humph)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
==Getting Started With Processing==
 
==Getting Started With Processing==
  
==PJS Preloading directive==
+
* [[Introduction | Introduction]]
 +
** [[What_is_Processing.js | What is Processing.js?]]
 +
** [[What can you do with Processing.js | What can you do with Processing.js?]]
 +
** [[Technology behind Processing.js]]
  
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.
+
* [[Processing.js/Basic_Requirements | Basic Requirements]]
 +
** [[Processing.js/Browsers_Supported | Browsers supported]]
 +
** [[Processing.js/Operating_Systems_Supported | Operating Systems supported]]
 +
** [[Processing.js/Network_Requirements | Network requirements]]
 +
* Usage Basics
 +
** Download, Installation and Configuration
 +
** What is a sketch?
 +
*** Loading a sketch from external file
 +
** Simple drawing tutorial
  
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.
+
==Processing.js Preloading directive==
  
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.
+
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 processing.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 preloading 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 processing.js, and the fact that you can't 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 processing.js. Processing's JAVA implementation allows loadImage() to block sketch execution while an image loads and requestImage() is used to preload images ahead of time.
  
 
Sample code
 
Sample code
 +
loading one image
 +
    /* @pjs preload=”image.jpg”; */
 +
 +
loading multiple images
 +
    /* @pjs preload=”sun.jpg,moon.jpg,stars.jpg”; */
  
    /* @PJS preload=”image.jpg”; */
+
loading an image from a remote location. NOTE: remote images can be drawn, but their pixels cannot be manipulated due to browser security restrictions.
    /* @PJS preload=”sun.jpg,moon.jpg,stars.jpg”; */
+
     /* @pjs preload=”http://www.urltoimage.com/image.jpg”; */
     /* @PJS preload=”http://www.urltoimage.com/image.jpg”; */
 
  
 
loadImage() example
 
loadImage() example

Latest revision as of 20:43, 24 October 2010

Getting Started With Processing

Processing.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 processing.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 preloading 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 processing.js, and the fact that you can't 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 processing.js. Processing's JAVA implementation allows loadImage() to block sketch execution while an image loads 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. NOTE: remote images can be drawn, but their pixels cannot be manipulated due to browser security restrictions.

   /* @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.