views:

770

answers:

4

I'm exploring HTML5 and Processing.js.

When the user clicks on an image on my webpage, I'd like to call a function in the Processing.js code. Just calling the function in onclick, as illustrated in this simple example, isn't working:

<html>
  <head>
    <script language="javascript" src="processing.init.js"></script>
    <script language="javascript" src="processing.js"></script>
  </head>
  <body>
    <script type="application/processing">
    void restart()
    { /* does something */ }
    </script><canvas>
      You'll need a browser that supports HTML5 to see this content.
    </canvas><br/><br/>
    <img src="restart-image.png" onclick="restart()">
  </body>
</html>

Any thoughts on how I can call a method in the Processing.js script when an image is clicked? (Maybe I'm making a basic HTML mistake by using img onclick?)

A: 

I would start by pulling the javascript for the onclick out of the img tag.

In a separate javascript file you can have:

document.getElementById('restartimage').onclick = function() { Processing.setup(); }

Obviously I put an id attribute on your img tag.

The setup function I noticed on this page: http://processingjs.org/

The basic idea is that by pulling the onclick into a javascript file it will be easier to code what it should do.

James Black
I tried this, but in my error console, I see, "document.getElementById('restartimage') is null". Did I correctly add the ID attribute to my img tag? <img src="restart-image.png" id="restartimage">
David
Just make certain that your javascript runs after this page is rendered, but, you have the id tag listed properly. Hopefull you also have an end tag (</img>).
James Black
A: 

you need to change the script-type to text/javascript or application/javascript first.

then place 'function' before 'restart()' function reducing 'void'.

i have used firefox 3.0.14 on ubuntu 9.04.

Sadat
Actually, in HTML5, the default is text/javascript, so the script tags don't need a type or a language.
Rich Bradshaw
I tried this, but then the Processing.js code didn't run.
David
I have tested as belowHTML file:<html> <head> <script language="javascript" src="processing.js"></script> </head> <body><canvas> You'll need a browser that supports HTML5 to see this content. </canvas><br /><br /> <img src="restart.png" onclick="restart()"> </body></html>JS file:function restart() { /* does something */alert('hello'); }
Sadat
A: 

You can call processing functions using the Processing js object's getInstanceById function.

For example here is a function:

$(window).resize(function(){
    var p=Processing.getInstanceById('processing_canvas_id');
    p.resize($("#main").width(),$("#main").height());
  });

Where resize is my processing function inside the processing file associated with *processing_canvas_id*.

Magneon
A: 

This should work, enclose you image inside the anchor tag <a> image </a> Use href tag to call the function.

<a href="javascript:restart()"> image source </a>
Manivasagan