# Resize Background Image when Resizing Window in p5.js?

**URL:** <https://discourse.processing.org/t/resize-background-image-when-resizing-window-in-p5-js/13671>\
**Category:** Coding Questions\
**Created:** [August 28, 2019, 4:31pm UTC](https://discourse.processing.org/t/resize-background-image-when-resizing-window-in-p5-js/13671 "2019-08-28T16:31:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyfruhling](https://avatars.discourse-cdn.com/v4/letter/z/db5fbb/32.png) [@zacharyfruhling](https://discourse.processing.org/u/zacharyfruhling)\
**Post date:** [August 28, 2019, 4:31pm UTC](https://discourse.processing.org/t/resize-background-image-when-resizing-window-in-p5-js/13671/1 "2019-08-28T16:31:53Z")

</div>

I’m having trouble getting the background image for a p5.js sketch to resize along with the window resize function. Basically I’m preloading the image in the preload function, resizing it to match the current window width and height in the setup function, and attempting to resize the image in the windowResized function. But the sketch is neither loading the correct background image size nor resizing the image correctly when the window is resized. Is resizing the background image on window resize even possible in p5.js? Thanks!

Zachary

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 28, 2019, 8:36pm UTC](https://discourse.processing.org/t/resize-background-image-when-resizing-window-in-p5-js/13671/2 "2019-08-28T20:36:10Z")

</div>

> [@Pls critique my code!](https://discourse.processing.org/t/pls-critique-my-code/12855/2):
>
> Not at all! Callback preload() is the single best place to load assets. star_struck BtW, you’ve got some missing indentation on your posted code. woozy_face We can use [Beautifier.io](http://Beautifier.io) in order to clean up our JS code. wink Here’s my own take on your sketch (Warning: Not fully tested!): sunglassesindex.html: \<!DOCTYPE html\> \<meta charset=utf-8\> \<meta name=viewport content=width=device-width,initial-scale=1\> \<script defer src=https://cdn.JsDelivr.net/npm/p5\>\</script\> \<script defer s…
