Issue with windowResize(windowWidth, windowHeight)

Hi, recently I was making my reactjs app and decided to use p5 canvas as background. Since I didn’t use p5 before, I found some code on internet and copy it to my app. Canvas (background) looks good on page first load, but I have some problems with windowResize event, which I added to code (as it should be added according to p5 documentation). I realize (by console.log()) that variables windowWidth and windowHeigth are changing on window resizing, event windowResize is triggered and even function draw() got called (by event). But this is where the problem arises. Although function draw() is called canvas is not redraw. Any suggestions?

Here is code:

let circles = [],
    circle = {},
    overlapping = false,
    NumCircles = 40,
    protection = 10000,
    counter = 0;

function windowResized() {
    resizeCanvas(windowWidth, windowHeight);
}

function setup() {
    let canvas = createCanvas(windowWidth, windowHeight);
    
    // populate circles array
    // brute force method continues until # of circles target is reached
    // or until the protection value is reached
    while (circles.length < NumCircles && counter < protection) {
        circle = {
            x: random(width),
            y: random(height),
            r: random(3, 15)
        };
        overlapping = false;

        // check that it is not overlapping with any existing circle
        // another brute force approach
        for (var i = 0; i < circles.length; i++) {
            var existing = circles[i];
            var d = dist(circle.x, circle.y, existing.x, existing.y);
            if (d < circle.r + existing.r) {
                // They are overlapping
                overlapping = true;
                // do not add to array
                break;
            }
        }

        // add valid circles to array
        if (!overlapping) {
            circles.push(circle);
        }

        counter++;
    }
}

function draw() {
    background('rgb(250, 251, 234)');
    noStroke();
    fill('rgba(23, 174, 238, 0.3)');
    for (let i = 0; i < circles.length; i++) {
        ellipse(circles[i].x, circles[i].y, circles[i].r * 2, circles[i].r * 2);
    }
}

And it is embedded in index.html:

<script src="https://cdn.jsdelivr.net/npm/p5@0.10.2/lib/p5.min.js"></script>
        <script src="./canvas.js"></script>
1 Like

The sketch from the link below uses windowResized() and it works alright: :pensive:
Bl.ocks.org/GoSubRoutine/raw/fa085945d45152786698f44a9523ccac/

1 Like

So what I did wrong?

Just by looking I can’t tell what’s wrong, sorry. :man_shrugging:
You can host your sketch online so others can see & test the problem: :tropical_fish:

Hello,

Add this to your draw and please report back:

    fill(255, 0, 0);
    for (let i = 0; i < 20; i++) 
      {
      ellipse(random(windowWidth), random(windowHeight), 20, 20);
      }

:slight_smile:

Here is demo of code

@glv that piece of code causes circles to infinitely redrawing (or moving), without me resizing window. I need canvas which will have random static circles (as it is in demo) which will move only on resizing window (or scrolling through page), so that I don’t have large parts of page without any circle. I have problems with second part.

Hello,

I got the impression that canvas was not resizing and provided the code above) to visualize that it was.

Consider:

  • An array of fixed x, y data was generated in setup().
  • Scale and plot that data to fit the new resized screen in draw().
  • Save the initial windowWidth and windowHeight in 2 variables such as iniX and iniY.
  • Use a factor of windowWidth/iX to scale the x of the ellipse and do similar for y

I got this working here.

:slight_smile:

2 Likes

Although it’s in Java Mode syntax, this sketch render faces w/ diff. sizes based on the coordinates & dimensions initial values: :angel:
Studio.ProcessingTogether.com/sp/pad/export/ro.9G5yFfYFFDXi1

This can easily be ported to p5js flavor w/ windowResized() to refresh each Face instance to be drawn based on new factor values. :mage:

1 Like

Hi,

sorry I was busy last two days so I didn’t have time to check your solution and reply you.

Just to inform you that I solved my issue. I didn’t solve it exactly as you wrote but you really help me to understand what is problem with my code. Thanks a lot!

2 Likes