# Get resized window size?

**URL:** <https://discourse.processing.org/t/get-resized-window-size/30808>\
**Category:** Coding Questions\
**Created:** [June 19, 2021, 10:48pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808 "2021-06-19T22:48:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sayochi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sayochi/32/15126_2.png) [@Sayochi](https://discourse.processing.org/u/Sayochi)\
**Post date:** [June 19, 2021, 10:48pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/1 "2021-06-19T22:48:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 19, 2021, 11:39pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/2 "2021-06-19T23:39:01Z")

</div>

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](https://discourse.processing.org/t/resize-event-for-sketch-without-draw-loop/29462/5)

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

---

<div class="post-metadata">

**Author:** ![Sayochi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sayochi/32/15126_2.png) [@Sayochi](https://discourse.processing.org/u/Sayochi)\
**Post date:** [June 20, 2021, 10:23am UTC](https://discourse.processing.org/t/get-resized-window-size/30808/3 "2021-06-20T10:23:53Z")

</div>

@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

 ![Screenshot from 2021-06-20 13-09-44](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6f628fb6588e94446b90b4140eff61fdea79e7de.png)  
_^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.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 20, 2021, 10:47am UTC](https://discourse.processing.org/t/get-resized-window-size/30808/4 "2021-06-20T10:47:23Z")

</div>

perhaps I’m misunderstanding your problem, however

```auto

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

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

```

this print the correct width and height

---

<div class="post-metadata">

**Author:** ![Sayochi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sayochi/32/15126_2.png) [@Sayochi](https://discourse.processing.org/u/Sayochi)\
**Post date:** [June 20, 2021, 11:42am UTC](https://discourse.processing.org/t/get-resized-window-size/30808/5 "2021-06-20T11:42:15Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 20, 2021, 12:00pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/6 "2021-06-20T12:00:58Z")

</div>

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:

```auto
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");
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 20, 2021, 12:11pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/7 "2021-06-20T12:11:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sayochi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sayochi/32/15126_2.png) [@Sayochi](https://discourse.processing.org/u/Sayochi)\
**Post date:** [June 20, 2021, 6:44pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/8 "2021-06-20T18:44:07Z")

</div>

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:

```auto
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?

 ![Screenshot from 2021-06-20 21-43-15](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e68f0cc9f4e0d45806a4fc48eb01b209eb8c10f7.png)

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [June 20, 2021, 8:22pm UTC](https://discourse.processing.org/t/get-resized-window-size/30808/9 "2021-06-20T20:22:14Z")

</div>

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

in [setResizable](https://processing.org/reference/setResizable_.html) 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)
