problem in javascript
in
Processing with Other Languages
•
2 months ago
I have made an image editor.
I have tried to make it work on the web through PHP. The code is working fine in JAVA. But when I convert it to JAVASCRIPT, there is some problem with tint and save functions. When i click tint or save, the screen just freezes.
My code is:
- /* @pjs preload="synthesizer.jpg";*/
- // the image that is being edited
- PImage originalImage;
- float imageWidth;
- // images to use for button logos
- PImage facebookLogo, cameraLogo, rotateLogo;
- // the overlay images that can be applied to the image
- PImage [] overlays;
- // images for the tint buttons
- PImage [] tintImages;
- // the tint colors we can apply
- color [] tints;
- // radio buttons to trigger tints and overlays
- RadioButtons tintButtons;
- RadioButtons overlayButtons;
- // button to load a file from the camera or file
- Button fileButton;
- // button to post to Facebook
- Button FBButton;
- Button tint;
- Button overlay;
- Button rotateButton;
- Button invert;
- Button threshhold;
- Button erode;
- Button dilate;
- Button save;
- Slider blur;
- Button poster;
- Button brush1;
- // whether the user interface is showing
- boolean displayGUI = false;
- // this stops you showing a file temporarily as
- // we load a new file
- boolean updateImage = false;
- boolean pos = false;
- boolean inv = false;
- boolean thr = false;
- boolean ero = false;
- boolean dil = false;
- boolean blu = false;
- boolean sav = false;
- boolean br1 = false;
- // post to facebook next frame
- boolean post = false;
- // the angle of rotation of the image
- float angle = 0;
- // the position of the center of the image,
- // this allows you to scroll it around
- int imageCenterX;
- // loads a new image when you get one from the camera
- void setImage(String url) {
- //console.log("loading: "+url);
- originalImage = loadImage(url);
- updateImage = true;
- loop();
- }
- void setup()
- {
- size(480, 640);
- imageCenterX = width/2;
- originalImage = loadImage("synthesizer.jpg");
- facebookLogo = loadImage("asset.f.logo.lg.png");
- cameraLogo = loadImage("cameraLogo.png");
- rotateLogo = loadImage("rotateLogo.png");
- tints = new color[4];
- tints[0] = color(0, 255, 100);
- tints[1]= color(120, 100, 0);
- tints[2] = color(0, 100, 160);
- tints[3] = color(160, 60, 10);
- /*tintImages = new PImage[4];
- tintImages[0] = loadImage("tintImage0.png");
- tintImages[1] = loadImage("tintImage1.png");
- tintImages[2] = loadImage("tintImage2.png");
- tintImages[3] = loadImage("red.png");*/
- overlays = new PImage[8];
- overlays[0] = loadImage("overlay1.png");
- overlays[1] = loadImage("overlay2.png");
- overlays[2] = loadImage("overlay1_old.png");
- overlays[3] = loadImage("fog.png");
- overlays[4] = loadImage("overlay3.png");
- overlays[5] = loadImage("overlay7.png");
- overlays[6] = loadImage("overlay11.png");
- overlays[7] = loadImage("o10.png");
- overlayButtons = new RadioButtons(overlays.length, width-70, 50, 64, 48, VERTICAL);
- String [] overlayn = {"overlay1","overlay2","overlay3","overlay4","overlay5","overlay6","overlay7","overlay8"};
- overlayButtons.setNames(overlayn);
- for (int i = 0; i < overlays.length; i++)
- {
- // sets the image for the button to be the same as the overlay itself
- overlayButtons.setImage(i, overlays[i]);
- // sets the colors to use for the outline
- // the inactive color is transparent so the border is invisible
- overlayButtons.buttons[i].setInactiveColor(color(255, 255, 255, 0));
- overlayButtons.buttons[i].setActiveColor(color(255));
- }
- tintButtons = new RadioButtons(4, 100, height-50, 64, 48, HORIZONTAL);
- String [] tintn = {"Green","Yellow","Blue","Red"};
- tintButtons.setNames(tintn);
- for (int i = 0; i < tints.length; i++)
- {
- // sets the image for the button to be the same as the main image
- //tintButtons.setImage(i, tintImages[i]);
- // set a tint for the button to be the same as tint i
- //tintButtons.buttons[i].setImageTint(tints[i]);
- // sets the colors to use for the outline
- // the inactive color is transparent so the border is invisible
- tintButtons.buttons[i].setInactiveColor(color(255, 0));
- tintButtons.buttons[i].setActiveColor(color(255));
- }
- tint = new Button("Tints",20,height-50,64,48);
- overlay = new Button("Overlays",width-70,0,64,48);
- // create buttons for file loading and facebook posting
- // load logos for each
- invert = new Button("Invert",20,100,100,30);
- threshhold = new Button("Black&White", 20,140,100,30);
- erode = new Button("Erode", 20,180,100,30);
- dilate = new Button("Dilate", 20,220,100,30);
- save = new Button("SAVE!!",width/2,20,100,30);
- rotateButton = new Button ("rotate", width-130, height-50, 36, 36);
- rotateButton.setImage(rotateLogo);
- fileButton = new Button ("file", width-90, height-50, 36, 36);
- fileButton.setImage(cameraLogo);
- FBButton = new Button ("FB", width-40, height-50, 36, 36);
- FBButton.setImage(facebookLogo);
- blur = new Slider("Blur",0,0,10,20,20,200,30,HORIZONTAL);
- poster = new Button("Posterize",20,60,100,30);
- //brush1 = new Button("Brush1",20,height-50,64,48);
- }
- void draw()
- {
- background(100);
- // don't display the image if you have just updated the image
- if (! updateImage)
- {
- pushStyle();
- // if we have selected a tint tintButtons.get will be positive
- // if so apply a tint
- if (tintButtons.get() >= 0)
- {
- tint(tints[tintButtons.get()]);
- }
- // rescale the image so that it is the full height of the screen
- float imageWidth = (height*originalImage.width)/originalImage.height;
- // draw the image
- imageMode(CENTER);
- pushMatrix();
- translate(imageCenterX, height/2);
- rotate(angle);
- image(originalImage, 0, 0, imageWidth, height);
- popMatrix();
- // turn the tint off for the overlay
- // if we have selected a tint tintButtons.get will be positive
- // if so draw the overlay
- if (overlayButtons.get() >= 0)
- {
- image(overlays[overlayButtons.get()], width/2, height/2, width, height);
- }
- if(inv)
- {filter(INVERT);}
- if(thr)
- {filter(THRESHOLD);}
- if(ero)
- {filter(ERODE);}
- if(dil)
- {filter(DILATE);}
- if(blu)
- {filter(BLUR, blur.get());}
- if(pos)
- {filter(POSTERIZE, 4);}
- if(sav)
- { save("mypic.png");
- sav = false;
- println("image saved");
- }
- popStyle();
- if (displayGUI)
- {
- // draw a transparent rectangle behind the UI to make it
- // more visible
- pushStyle();
- fill(0, 128);
- noStroke();
- rect(0, height - 60, width, 60);
- // draw all the UI elements
- tint.display();
- overlay.display();
- tintButtons.display();
- overlayButtons.display();
- FBButton.display();
- fileButton.display();
- rotateButton.display();
- invert.display();
- threshhold.display();
- erode.display();
- dilate.display();
- blur.display();
- poster.display();
- save.display();
- //brush1.display();
- noTint();
- popStyle();
- }
- // post is set to true
- // when the user hits the facebook button
- }
- else // if we are updating the image, just draw some text
- {
- textAlign(CENTER, CENTER);
- text("updating", width/2, width/2);
- // stop the update message once we have drawn it once
- float imageWidth = (height*originalImage.width)/originalImage.height;
- originalImage.resize((int)imageWidth, height);
- updateImage = false;
- }
- }
- // respond to user interface events
- void mouseReleased()
- {
- // first we need to check if the user interface is show
- // if it is we can trigger events. Otherwise the click
- // will show the interface
- if (displayGUI)
- {
- // if we are in the interface area at the bottom of the
- // screen we trigger the UI events
- if (mouseY > height - 60 || mouseX>0)
- {
- if(tintButtons.mouseReleased())
- { displayGUI = false;
- }
- if(overlayButtons.mouseReleased())
- { displayGUI = false;}
- if (rotateButton.mouseReleased())
- {
- angle += radians(90);
- displayGUI = false;
- }
- if(invert.mouseReleased())
- {inv = !inv;
- displayGUI = false;}
- if(threshhold.mouseReleased())
- {thr = !thr;
- displayGUI = false;}
- if(erode.mouseReleased())
- {ero = !ero;
- displayGUI = false;}
- if(dilate.mouseReleased())
- {dil = !dil;
- displayGUI = false;}
- if(poster.mouseReleased())
- {pos = !pos;
- displayGUI = false;}
- if(save.mouseReleased())
- {sav = true;
- displayGUI = false;
- }
- }
- else
- {
- // if we clicked in the middle of
- // the screen, hide the UI
- displayGUI = false;
- }
- }
- else
- {
- // if the UI was hidden, a click will show it
- displayGUI = true;
- }
- }
- // when we dragg we can move the image from left to
- // right (it is the full screen height so we don't want
- // to move it up and down)
- void mouseDragged()
- {
- imageCenterX += mouseX - pmouseX;
- blur.mouseDragged();
- if(blur.get()!=0)
- blu = true;
- }
Any clue on what the problem is?
thanx...
1