Get resized window size?

I’m making a 2d noise map generator in processing js. I’m doing this with pixels[] in my sketch and when I resize my window it doesn’t behave in the way i want. what I want is creating wider map if window width is increased. I’m using width and height values but they seem to being set only when size() is called. which is once in a run.

is there a way to get changed window width and height? so I can use them

hi,
can you paste some code here?
otherwise i checked, and after resize width and height values are correctly updated, but if you used it in setup() i guess you need to reinit some stuff
so may be you can store those values in variables and check if some change occurs if(width != pWidth) to resize your noise map?

or you can try to catch the resized event as Micuat explain in this post

it s a more elegant way to do that, ( above my understanding of java…)

@th75 I need to access window width and height when its changed. link you posted mostly focused on how to run some code when window resized. since my width and height are not changing… its not usefull to be able to do that tbh.

anyways the thing is… when I put println(width); in my code, it doesnt even update itself. it just stays same. but when I don’t use pixels[] which comes along with loadPixels() and updatePixels() it does work. it calculates my noise map respect to window size. but ofcourse its extremely slower

also it looks glitchy when I resize my window while working with pixels[]. im gonna put this here maybe its a bug or something


^resized towards right

I can’t interact with sketch via my mouse when I’m hovering that glitch area (map supposed to move towards mouse but it recognizes my mouse as its not hovering the sketch window at all). it crashes if i resize the window from bottom left corner. giving index out of array. expected because my width and height didn’t change at all so my loop of going over all pixels breaks.

perhaps I’m misunderstanding your problem, however



void setup(){
  surface.setResizable(true);
};

void draw(){
  println(width,height);
};

this print the correct width and height

i did some tests. yeah width and height really updates even tho I use pixels[]. i guess its because of my code. the one you sent is working fine.
but

void draw(){
  loadPixels();
  for(int y=0; y<height; y++){
    for(int x=0; x<width; x++){
      pixels[x+y*width] = color(map(x+y, 0, width+height, 0, 255));
    }
  }
  updatePixels();
};

tends to crash with index out of bounds. is it just some glitch dangerous way to render while resizing or its just crashing on my computer. can you run this code few times and resize the window please?

hi,

width and height are updated when you resize you screen , this occur when you release mouse button, there is no reason this event will be synchronised with draw loop, big chance it occur while you re manipulating pixels array while it is resized

i guess there is a better way to do that, but i will just add a try catch around your code

as resizing doesn t occur often, this frame will fail, but next will be fine:

void draw() {
  try {

    loadPixels();
    for (int y=0; y<height; y++) {
      for (int x=0; x<width; x++) {
        pixels[x+y*width] = color(map(x+y, 0, width+height, 0, 255));
      }
    }
    updatePixels();
  }
  catch(Exception e) {
    println(" frame failed on resize");
  }
}

Alternatively an if statement which checks that x+y*width is less than pixels.length before setting the pixel.

that’s a good approach and fixed the crash problem. but now I also realised that there is something else causing that weird glitchy area…

i did some tests with commenting out parts of my 2d terrain code and creating new sketches etc.
finally i tested on this:

void setup(){
  surface.setResizable(true);
  size(500,500);
};

void draw(){
  background(0,255,0);
  println(width,height);
};

randomly, sometimes, it does not work. it starts glitching.
I don’t know if its not recognizing my act of resizing because when it glitches, width and height values are also stays same in console.

otherwise it works. but this probability of “works” is very small in my 2d noise creator for some reason.
so I assume its less likely to work if project is slower?? i don’t know. any thoughts? your sketches always works as intended?

ok,
this code work well here (3.5.4, mac, mojave) so may be something else

in setResizable doc, it is set after the size(), may be it have some importance
i can t say, i will check other modes for size() P2D , FX2D
check videocard (opengl) drivers may be
or software that can interfer with the videocard like anydesk, vnc, teamviewer…

no other ideas so far…

(and no my sketches never works as intended, c’est la vie, but it is my fault not really on processing side)