Can't figure out why Processing Java code not working with JavaScript

Hi,

I wrote code for a watch, and it works great within Processing compiling as Java, but when I try to host it with JavaScript, it doesn't work.

I read a bit on this issue, and I removed any non Processing API libraries/scripts, and tried formatting my code, but it still doesn't work.

Could you please take a look at my code (it's pretty short) and see if you can figure why it won't compile and host, or what I could do to edit it?

Thanks so much!

Dan

//import java.util.GregorianCalendar;
//import java.util.Calendar;
//import java.util.Date;
int xc,yc;
float angle, dayangle, hourangle, daytimeangle;
int radiusi, radiuso, radius;
float innerx, innery, outerx, outery, dayxi, dayyi, dayxo, dayyo;
float frequency;
String day;
int i;

int framesday;

int weekday;
int weekdayadd;


void setup() {
  size(200,200);
  xc=width/2;
  yc=height/2;
  radiusi=55;
  radiuso=90;
  radius=(radiusi+radiuso)/2;
  angle=3*PI/2;

  i=0;
  framesday = 2592000; //2592000
  frameRate(30);

  background(50);

}

void draw() {
  background(50);
  //GregorianCalendar gcal = new GregorianCalendar();  
  //weekday = gcal.get(GregorianCalendar.DAY_OF_WEEK);
  weekday=day()-1;
  weekdayadd=weekday + 7 % 14;



  float s;
  s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
  float m;
  m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
  float h;
  h = map(hour() + norm(minute(), 0, 60) + norm(second(), 0, 3600), 0, 24, 0, TWO_PI) - HALF_PI;
  angle = h;
  hourangle = map(hour() + norm(minute(), 0, 60) + norm(second(), 0, 3600), 0, 24, 0, TWO_PI/7) - HALF_PI;
  daytimeangle = dayangle*(weekday-1)+hourangle;
  innerx=xc+cos(angle)*(radiusi);
  innery=yc+sin(angle)*(radiusi);
  outerx=xc+cos(angle)*(radiuso);
  outery=yc+sin(angle)*(radiuso);
  dayxi=xc+cos(daytimeangle)*(radiusi);
  dayyi=yc+sin(daytimeangle)*(radiusi);
  dayxo=xc+cos(daytimeangle)*(radiuso);
  dayyo=yc+sin(daytimeangle)*(radiuso);

  fill(230);
  strokeWeight(3);
  ellipse(xc,yc,2*radiuso+2,2*radiuso+2);

  switch(weekday) {
    case 1:
      day = "N";
      break;
    case 2:
      day = "M";
      break;
    case 3:
      day = "T";
      break;
    case 4:
      day = "W";
      break;
    case 5:
      day = "H";
      break;
    case 6:
      day = "F";
      break;
    case 7:
      day = "S";
      break;
  }


  fill(50);
  stroke(50);
  textSize(84);
  textAlign(CENTER,CENTER);

  //text(day,xc,yc-7);

  dayangle=2*PI/7;
  float offset;
  offset = .265;
  stroke(50);
  strokeWeight(1);
  fill(240);


  line(xc,yc,xc,yc+sin(-PI/2)*radiuso);
  line(xc,yc,xc+cos(-PI/2+dayangle)*radiuso,yc+sin(-PI/2+dayangle)*radiuso);
  line(xc,yc,xc+cos(-PI/2+2*dayangle)*radiuso,yc+sin(-PI/2+2*dayangle)*radiuso);
  line(xc,yc,xc+cos(-PI/2+3*dayangle)*radiuso,yc+sin(-PI/2+3*dayangle)*radiuso);
  line(xc,yc,xc+cos(-PI/2+4*dayangle)*radiuso,yc+sin(-PI/2+4*dayangle)*radiuso);
  line(xc,yc,xc+cos(-PI/2+5*dayangle)*radiuso,yc+sin(-PI/2+5*dayangle)*radiuso);
  line(xc,yc,xc+cos(-PI/2+6*dayangle)*radiuso,yc+sin(-PI/2+6*dayangle)*radiuso);

  fill(50);
  textSize(24);

  text("N",xc+cos(-PI/2+offset)*radius,yc+sin(-PI/2+offset)*radius-2);
  text("M",xc+cos(-PI/2+offset+dayangle)*radius,yc+sin(-PI/2+offset+dayangle)*radius-2);
  text("T",xc+cos(-PI/2+offset+2*dayangle)*radius,yc+sin(-PI/2+offset+2*dayangle)*radius-2);
  text("W",xc+cos(-PI/2+offset+3*dayangle)*radius,yc+sin(-PI/2+offset+3*dayangle)*radius-2);
  text("H",xc+cos(-PI/2+offset+4*dayangle)*radius,yc+sin(-PI/2+offset+4*dayangle)*radius-2);
  text("F",xc+cos(-PI/2+offset+5*dayangle)*radius,yc+sin(-PI/2+offset+5*dayangle)*radius-2);
  text("S",xc+cos(-PI/2+offset+6*dayangle)*radius,yc+sin(-PI/2+offset+6*dayangle)*radius-2);

  stroke(50);
  strokeWeight(3.5);
  //line(xc-2,yc-2,innerx+2,innery+2);
  line(xc,yc,dayxo,dayyo);


}

Answers

  • edited February 2014 Answer ✓

    Golden rule for JS conversion:
    Don't have variables & functions sharing the same name!!! :-O

    In your code above you've got a String variable called day.
    Inside your switch block you lose track of function day() by assigning a String to variable day! >:P
    As you can see now, function references in JavaScript are stored in variables too! :-&

    In short, we invoke a function in JS by suffixing () to a variable.
    However, if the value stored in it isn't a function reference anymore, the script crashes!!! 8-}

  • Awesome! That fixed it! Thank you for pointing out my crazy coding haha!

Sign In or Register to comment.