• LOG IN
  • REGISTER
  • ISSUES

Make, Hack, Void

  • home
  • projects
  • news
  • community
Home › Getting started with ProcessingJS

Getting started with ProcessingJS

Adam — Tue, 31/08/2010 - 4:57pm

ProcessingJS, it's Processing but in Javascript & <canvas>.

The steps to get started with ProcessingJS in a stand alone environment are really straight forward but not documented anywhere. There is a really basic demo on the front page which basically implements these steps but it's not obvious that is what you need to download. Of course I only found that link after I wrote this up and figured this would be useful anyway.

Here's how you get started:

  1. Goto http://processingjs.org/download and download the latest
    processing-js-x.x.x-examples.zip file.
  2. Open the zip file and extract the following files into a working directory:
    • processing-js-x.x.x-examples/processing.js
    • processing-js-x.x.x-examples/examples/init.js
  3. Create a new html page called 'index.html' with the following:
        <!DOCTYPE html>
        <html>
          <head>
            <script src="processing.js"></script>
            <script src="init.js"></script>
            <title>Getting Started with ProcessingJS</title>
          </head>
          <body>
            <script type="application/processing" target="#canvas1">
        /* 
          PROCESSINGJS.COM - BASIC EXAMPLE  
          Delayed Mouse Tracking
          MIT License - Hyper-Metrix.com/F1LT3R
          Native Processing compatible 
        */ 
        // Global variables  
        float radius = 50.0;  
        int X, Y;  
        int nX, nY;  
        int delay = 16;  
          
        // Setup the Processing Canvas  
        void setup(){  
          size( 200, 200 );  
          strokeWeight( 10 );  
          frameRate( 15 );  
          X = width / 2;  
          Y = height / 2;  
          nX = X;  
          nY = Y;    
        }  
          
        // Main draw loop  
        void draw(){  
            
          radius = radius + sin( frameCount / 4 );  
            
          // Track circle to new destination  
          X+=(nX-X)/delay;  
          Y+=(nY-Y)/delay;  
            
          // Fill canvas grey  
          background( 100 );  
            
          // Set fill-color to blue  
          fill( 0, 121, 184 );  
            
          // Set stroke-color white  
          stroke(255);   
            
          // Draw circle  
          ellipse( X, Y, radius, radius );                    
        }  
          
          
        // Set circle's next destination  
        void mouseMoved(){  
          nX = mouseX;  
          nY = mouseY;    
        }          
            </script>
    
            <canvas width="200" height="200" id="canvas1"></canvas>
          </body>
        </html>
    
  4. Open the index.html file in your browser.

    You should see a grey box, 200x200 pixels in size, with a pulsating
    blue and white circle that follows your mouse around.

The processing.js file does all the work of implementing the Processing environment, the init.js file just searches your html page for script tags with the type set to 'application/processing'. It then loads the Processing object into canvas tags based on the target/id pairs or on a first come first served basis.

  • Projects
  • Javascript
  • Processing
  • ProcessingJS
  • Login or register to post comments
  • Printer-friendly version

Search

Recent Comments

Request
1 week 20 hours ago
more

Member Tweets

evildeece
1 hour 41 min ago — Spray on clothing! http://t.co/djQzAie7 #mhv
MakeHackVoid
1 hour 48 min ago — Space is closed (was open 7 hours)
evildeece
2 hours 5 min ago — 3D printed RC plane http://t.co/8LCfuR3C #mhv
evildeece
2 hours 8 min ago — 21 more MHVBoard #arduino clones packed up and ready to go to local school & uni #mhv
MakeHackVoid
8 hours 54 min ago — Space is open until 22:00 (estimate)
projectgus
12 hours 14 min ago — Club Mate induced madness MT @dev_dsp @MakeHackVoid It tastes like ground! and like tea! and like ground! Pretty much the best thing ever.
dev_dsp
12 hours 57 min ago — @MakeHackVoid It tastes like ground! and like tea! and like ground! It's pretty much the best thing ever.
MakeHackVoid
12 hours 58 min ago — Also there are rumours of @dev_dsp's "weird European hacker juice"
MakeHackVoid
13 hours 44 sec ago — Rumour has it that tonight's Maker Meetup will include an electric motorcycle, RepRap 3d printer, and SMD oven! Come! http://t.co/vmahKECz
evildeece
17 hours 30 min ago — OK Go's new music vid, using a car to play over 1000 instruments: http://t.co/Qca3G1Eu #mhv
  •  
  • 1 of 12
  • ››
more