开发者

Processing animation extremely choppy when getting data from a remote host

开发者 https://www.devze.com 2023-01-23 21:54 出处:网络
I am connecting to a remote web server to get mouse movements stored in a database. The processing program I\'ve written to animate these movements has been INCREDIBLY choppy since putting the code on

I am connecting to a remote web server to get mouse movements stored in a database. The processing program I've written to animate these movements has been INCREDIBLY choppy since putting the code on the server. I realize this is because rather than running locally it's got to fetch the information, but is it possible to speed things up a bit? Here's the code I'm using

String get_users = "http://example.com/get_users.php";
String get_data = "http://example.com/get_data.php?user=";
ArrayList arrows;
PImage mouse;
int[] user_ids;
int num_users;

void setup() {
  size(1024, 768);
  frameRate(24);
  smooth();
  noStroke();
  mouse = loadImage("arrow-clear.png");
  arrows = new ArrayList();
  getUsers();

  for (int i = 0; i < num_users; i++){
    arrows.add(new Arrow(user_ids[i], i*400, 2*i*100));
  }
}

void getUsers(){
  user_ids = int(loadStrings(get_users));
  num_users = user_ids.length;
  println(num_users);
}

void draw() {
  background(0);

  if (frameCount % 600 == 0){
    getUsers();
    for (int i = 0; i < num_users; i++){
      arrows.add(new Arrow(user_ids[i], i*400, 2*i*100));
    }
  }

  for (int i = arrows.size()-1; i >= 0; i--) { 
    Arrow arrow = (Arrow) arrows.get(i);
    arrow.move();
    if (arrow.finished()) {
      arrows.remove(i);
    }
  }

}

class Arrow {
  String[] all_moves, move_pairs, new_moves;
  int[] moves;
  float x;
  float y;
  int id;
  int i = 0;
  Boolean is_done = false;

  Arrow(int tempID, float tempX, float tempY) {
    all_moves = loadStrings(get_data + tempID);
    id = tempID;
    x = tempX;
    y = tempY;
    if  (all_moves.length > 0){
      move_pairs = shorten(split(all_moves[0], "|"));
    }
  }

  void move() {
    if (move_pairs != null){
      if (i < move_pairs.length){
        moves = int(split(move_pairs[i], ","));
        image(mouse, moves[0], moves[1]);
        ++i;
      } else {
        all_moves = loadStrings(get_data + id);
        if  (all_moves.length > 0){
          new_moves = shorten(split(all_moves[0], "|"));
          for (int j = 0; j < new_moves.length; j++){          
            move_pairs = append(move_pairs, new_moves[j]);
          }
          println(move_开发者_Python百科pairs);
        } else {
          is_done = true;
        }
      }
    } else {
        is_done = true;
    }
  }

  boolean finished() {
    if (is_done) {
      return true;
    } else {
      return false;
    }
  }
}

EDIT: To clarify: the Processing application doing all the animation is running locally. The X and Y points for the mouse is the only thing getting downloaded from the server.


You want to get all of the movement data (or large chunks of it) down to the client, and let the client do the work of animating everything.


I doubt it is a good idea to download the movement data on every single frame. If you don't need such detailed responsiveness, fetch a batch of movements from the server periodically and queue them for the draw method. Otherwise make sure the server sends only the data needed. I realized that you only use the first line of the data fetched from the server - all_moves[0]. If there is indeed only one line at all - fine.

You should consider using createInput(URL) and read from that stream, this way you wouldn't need to open a new input stream for every movement requested, but your server side code must be able to maintain the streams and write to them continuously.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号