question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aspect ratio does not update when tsparticles' element changes size, but the window has not changed size

See original GitHub issue

In Vanilla JS version 1.26.0 if tsparticles’ DOM element resizes, the canvas render becomes distorted. I’ve enabled resize: true, and upon manually resizing the window, tsparticles adjusts as expected. This however does not solve when a page reflows happens and all elements in a flexbox update with new widths and heights, one of which children is tsparticles.

QUESTION: Is there a way to have tsparticles auto-detect if its element (or parent) has changed size as a result of a reflow without a window resize occurring?

I’ve temporarily injected a resize event as a result of page changes which corrects the aspect ratio, but this doesn’t seem very elegant.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
n8sabescommented, Apr 12, 2021

Hi @matteobit, Thank you for the response, and confirmation the workaround is currently best practice. It would be a nice if resize observed both the window and parent element to ensure aspect ratio is always correct in a responsive page.

tsparticles is amazing and I’m enjoying working with it!

0reactions
n8sabescommented, Apr 21, 2021

Thanks for the snippet to access the canvas resize function. You have a good point about particle density. In my use case, a slider adjusts the size of sibling containers which causes rapid-fire updates, so I think:

  1. Calling canvas.resizeWindow() will keep aspect ratios correct, and then
  2. Defer a full load on debounce to fix density (ONLY IF density or other significant attribute has changed to prevent an unnecessary jolting replacement of particles).

This should make it nearly indiscernible to the eye.

Even the simplest things have edge cases to manage!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make tsParticles full width and height of a container ...
I have been struggling trying to get particles.js to be the background of a given element. That library seems to position the canvas...
Read more >
Options | tsParticles - v2.6.0
Enables the retina detection, if disabled the ratio used by canvas will be always 1 and not the device setting. Implementation of IOptions.detectRetina....
Read more >
Image size not updating after toolbar resize - Stack Overflow
The replaced content is sized to maintain its aspect ratio while filling the element's entire content box. If the object's aspect ratio does...
Read more >
Keep it contained!. Enforcing an aspect ratio on an HTML…
Frequently, it's necessary to set an aspect ratio on an element (like a <div> element), so that it will maintain its shape while...
Read more >
Lightweight JS Library For Creating Interactive Particles
tsparticles is an upgraded and continued version of the ... react-particles-js, but I decided to not change the major version since all the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found