How to communicate a Processing.js and a "normal" Processing sketch in the same host computer
in
Processing with Other Languages
•
9 months ago
Hi
Processing sketch
I have a Processing.js sketch where all the devices connected to the same local network can access it for choosing a number between 0 and 150. The numbers float in the browser's window and when someone touches one of them it dissapears from the sketch.
The "normal" Processing sketch must know what numbers are still available for making an animation.
I don't know much Javascript, so please share a comprehensive explanation.
Processing.js sketch
- String[] numbers_saved;
- PFont font;
- int numberQtty = 150;
- int gridWidth = 15, gridHeight = 10;
- int yOffset = 50, imageOffset = 10, textxOffset=21, textyOffset=25;
- float scale_factor = 0.6, drift_speed = 0.08, font_size = 14;
- int stage = 0;
- int button_position = 151;
- int program_counter = 0;
- int[] numbers = new int[numberQtty];
- boolean[] dissapear = new boolean[numberQtty];
- /* @pjs preload="fondo.png, plano2.png, plano1.png, circulo.png, sello.png, bienvenida.png, boton.png, rombo.png, sello2.png, medio.png, salida.png"; */
- PImage[] images = new PImage[11];
- PVector[] origins = new PVector[numberQtty];
- void setup()
- {
- size(1024, 768, P2D);
- frameRate(12);
- background(255);
- font = loadFont("SegoeScript-18.vlw");
- textFont(font, 10);
- textSize(font_size);
- fill(255, 255, 255, 35);
- imageMode(CENTER);
- textAlign(CENTER, CENTER);
- for (int i=0; i<numberQtty; i++) {
- dissapear[i] = false;
- }
- images[0] = loadImage("fondo.png");
- images[1] = loadImage("plano2.png");
- images[2] = loadImage("plano1.png");
- images[3] = loadImage("circulo.png");
- images[4] = loadImage("sello.png");
- images[5] = loadImage("bienvenida.png");
- images[6] = loadImage("boton.png");
- images[7] = loadImage("rombo.png");
- images[8] = loadImage("sello2.png");
- images[9] = loadImage("medio.png");
- images[10] = loadImage("salida.png");
- imageMode(CORNER);
- for (int i=0; i<gridHeight; i++) {
- for (int j=0; j<gridWidth; j++) {
- origins[gridWidth*i + j] = new PVector(width*j/gridWidth + random(width*(1-scale_factor)/gridWidth-imageOffset), (height-yOffset)*i/gridHeight + random(height*(1-scale_factor)/gridHeight-imageOffset));
- image(images[8], origins[gridWidth*i + j].x, origins[gridWidth*i + j].y, width*scale_factor/gridWidth, (height-yOffset)*scale_factor/gridHeight);
- text(gridWidth*i + j, origins[gridWidth*i + j].x + textxOffset, origins[gridWidth*i + j].y + textyOffset);
- }
- }
- }//setup
- void draw() {
- background(255);
- imageMode(CENTER);
- image(images[0], width/2, height/2);
- image(images[1], width/2, height/2);
- image(images[2], width/2, height/2);
- image(images[3], width/2, height/2);
- image(images[7], width/2, height/2, 250, 175);
- numbers_saved = loadStrings("saved_numbers.txt");
- //println(numbers_saved.length);
- if(numbers_saved.length > 0) {
- for(int i=0; i<numbers_saved.length; i++) {
- dissapear[numbers_saved[i]-1] = true;
- }
- }
- //println(program_counter);
- if (stage == 0) {
- imageMode(CORNER);
- for (int i=0; i<gridHeight; i++) {
- for (int j=0; j<gridWidth; j++) {
- //origins[gridWidth*i + j] = new PVector(width*j/gridWidth + random(width*(1-scale_factor)/gridWidth-imageOffset)*drift_speed, (height-yOffset)*i/gridHeight + random(height*(1-scale_factor)/gridHeight-imageOffset)*drift_speed);
- if (dissapear[gridWidth*i + j] == false) {
- image(images[8], origins[gridWidth*i + j].x, origins[gridWidth*i + j].y, width*scale_factor/gridWidth, (height-yOffset)*scale_factor/gridHeight);
- fill(255, 255, 255, 50);
- textSize(font_size);
- text(gridWidth*i + j + 1, origins[gridWidth*i + j].x + textxOffset, origins[gridWidth*i + j].y + textyOffset);
- }
- }
- }
- imageMode(CENTER);
- image(images[5], width/2, height/2);
- image(images[6], width/2, height/2);
- }
- else if (stage == 1) {
- imageMode(CORNER);
- for (int i=0; i<gridHeight; i++) {
- for (int j=0; j<gridWidth; j++) {
- if ((gridWidth*i + j) != button_position) {
- origins[gridWidth*i + j] = new PVector(width*j/gridWidth + random(width*(1-scale_factor)/gridWidth-imageOffset)*drift_speed, (height-yOffset)*i/gridHeight + random(height*(1-scale_factor)/gridHeight-imageOffset)*drift_speed);
- scale_factor = 0.6;
- font_size=14;
- textxOffset=21;
- textyOffset=25;
- }
- else if ((gridWidth*i + j) == button_position) {
- origins[gridWidth*i + j] = new PVector(mouseX, mouseY);
- scale_factor = 0.9;
- font_size=20;
- textxOffset=30;
- textyOffset=35;
- }
- if (dissapear[gridWidth*i + j] == false) {
- image(images[4], origins[gridWidth*i + j].x, origins[gridWidth*i + j].y, width*scale_factor/gridWidth, (height-yOffset)*scale_factor/gridHeight);
- fill(255, 255, 255, 255);
- textSize(font_size);
- text(gridWidth*i + j + 1, origins[gridWidth*i + j].x + textxOffset, origins[gridWidth*i + j].y + textyOffset);
- }
- }
- }
- imageMode(CENTER);
- image(images[9], width/2, height/2);
- }
- else if (stage==2) {
- imageMode(CORNER);
- for (int i=0; i<gridHeight; i++) {
- for (int j=0; j<gridWidth; j++) {
- //origins[gridWidth*i + j] = new PVector(width*j/gridWidth + random(width*(1-scale_factor)/gridWidth-imageOffset)*drift_speed, (height-yOffset)*i/gridHeight + random(height*(1-scale_factor)/gridHeight-imageOffset)*drift_speed);
- if (dissapear[gridWidth*i + j] == false) {
- image(images[8], origins[gridWidth*i + j].x, origins[gridWidth*i + j].y, width*scale_factor/gridWidth, (height-yOffset)*scale_factor/gridHeight);
- fill(255, 255, 255, 50);
- textSize(font_size);
- text(gridWidth*i + j + 1, origins[gridWidth*i + j].x + textxOffset, origins[gridWidth*i + j].y + textyOffset);
- }
- }
- }
- imageMode(CENTER);
- image(images[10], width/2, height/2);
- program_counter++;
- if(program_counter == 36) {
- program_counter = 0;
- stage=0;
- }
- }
- }//draw
- void mouseClicked() {
- if (stage == 0) {
- if (mouse_over_button(425, 510, 425+155, 510+35)) stage++;
- }
- }
- void mousePressed() {
- button_position = int(what_button());
- }
- void mouseReleased() {
- if (stage == 1) {
- if (mouse_over_button(435, 300, 435+150, 300+150)) {
- dissapear[button_position] = true;
- stage++;
- }
- }
- button_position = 151;
- }
- int what_button() {
- int posX = int(mouseX*gridWidth/width);
- int posY = int(mouseY*gridHeight/height);
- int button = gridWidth*posY + posX;
- return button;
- }
- boolean mouse_over_button(float x1, float y1, float x2, float y2) {
- if (mouseX>x1 && mouseX<x2 && mouseY>y1 && mouseY<y2) {
- return true;
- }
- else return false;
- }
- import controlP5.*;
- ControlP5 cp5;
- String NUMERO = "";
- PFont font;
- int numberQtty = 150;
- int gridWidth = 15, gridHeight = 10;
- PrintWriter output;
- int[] saved_numbers = new int[numberQtty];
- int new_number = 0, number_counter=0;
- PImage[] images = new PImage[2];
- void setup()
- {
- size(960, 960, P2D);
- background(255);
- font = loadFont("Arial-BoldMT-20.vlw");
- textFont(font, 20);
- textSize(20);
- fill(0, 20, 100, 255);
- for (int i=0; i<numberQtty; i++) {
- saved_numbers[i] = 0;
- }
- output = createWriter("E:/windows/Dropbox/Processing/number_server/web-export/saved_numbers.txt");
- cp5 = new ControlP5(this);
- cp5.addTextfield("NUMERO")
- .setPosition(220, height-45)
- .setSize(50, 40)
- .setFont(font)
- .setAutoClear(true)
- ;
- images[0] = loadImage("sello.png");
- images[1] = loadImage("fondo.jpg");
- }
- void draw() {
- background(images[1]);
- new_number = int(NUMERO);
- if (new_number>0 && new_number<=150) {
- if (new_number != saved_numbers[new_number-1]) {
- output.println(new_number);
- output.flush();
- saved_numbers[new_number-1] = new_number;
- number_counter++;
- }
- }
- for (int i=0; i<gridHeight; i++) {
- for (int j=0; j<gridWidth; j++) {
- if (saved_numbers[gridWidth*i + j] != 0) {
- tint(255, 0);
- image(images[0], width*j/gridWidth, height*i/gridHeight, width/gridWidth, height/gridHeight);
- }
- else {
- tint(255, 255);
- image(images[0], width*j/gridWidth, height*i/gridHeight, width/gridWidth, height/gridHeight);
- }
- }
- }
- text("Ingresa tu Número: ", 20, height-20);
- }
- void stop() {
- output.close();
- }
1