Open main menu

CDOT Wiki β

Changes

Processing.js/HowTo

145 bytes added, 19:43, 24 October 2010
Review changes from humph
** Simple drawing tutorial
==PProcessing.JS 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 Pprocessing.JS 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 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 Pprocessing.JS js, and the fact that you cant 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 Pprocessing.JSjs. 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
/* @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”; */