Loading...
Logo
Processing Forum

I have a .pde file will detect user mouse click, and there are few object in it, if the user click on the first one, I will get '1' as output and so on. And I will use this value to decided a <div> content in the html file("targetDiv" in the demo code below), so I need to pass the value to javascript, and let javascript control the next step. The problem is I don't know how to pass the value("outcome" in the demo code below) to javascript(or jquery is also ok). 


Below is demo code, cannot run because it is simplify code. 

Please help me!!It is an important part has to be come across in my work!!!Thank you very much!


<!doctype html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script src="processing-1.3.6.js"></script>

<body>

<div id="targetDiv"></div>

<canvas data-processing-sources="testing.pde"></canvas>

<script type="text/javascript">

//below is something I want to do, but still don't know the details

//if (outcome == 1){

//load 1.txt in the targetDiv

//}else if(outcome == 2){

//load 2.txt in the targetDiv

//}and so on.....


</body>

</html>





and in the testing pde. ::



int outcome;

void setup() {

  //sth happend

}


void draw() {

  //draw some object

}


void mousePressed() {

  //when user click on some obj

  //output the number of the object

  for (int i = 0; i < num; i++) {

    //isOver is a processing function to check the mouse click on the object or not

    if (isOver(mouseX, mouseY, i)==true) {

      outcome = i;

    }

  }

}




Replies(4)

It is quite simple in fact:
first create a javascript function inside your html file:
<script type="text/javascript">
function getValue(v){
var value = v;
console.log(value);
}
</script>

then inside your processing code ("testing.pde") add a function that will call this previous function:
void mousePressed(){
getValue(mouseX);
}

here for exemple, i send mouseX value to a javascript variable via the mousePressed function


http://makio.free.fr/
Thank you so much!!It works!!!!! And your website is awesome!! 

A little further question, if I want to use the output value, to control the content of a div...and there are different var already declare as t1b, t2b and so on. (And the output value of the mousepress is 1-16). 

How can I concatenate the int and the string in order to call the var as the display content??

function getClick(v){

var value = v;

var docName = "t" + v + "b";

var con = docName.value;

//if the v  = 1, here will be $("#target").html(t1b); <--this line work, but doesn't work when I use the line below

$("#target").html(docName); 

}



Thank you!

why don't you do it in Processing via the str() function  http://processing.org/reference/str_.html and send this value to javascript

http://makio.free.fr/
Hey! it also worked for me :). but,  What about the other way around? passing a value from javaScript to the Processing pde file??? how can it be done??