formkit / auto-animate

A zero-config, drop-in animation utility that adds smooth transitions to your web app. You can use it with React, Vue, or any other JavaScript application.

Home Page:https://auto-animate.formkit.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect rounding of element sizes while animating

sippeangelo opened this issue · comments

https://github.com/formkit/auto-animate/blame/b62aa87b0ce6b90c92fb7ebb06a3da49f74b7f77/src/index.ts#L428

Due to this Math.round, element sizes are changed from their actual size while animating, causing flex elements with text to wrap.

Screen.Recording.2024-01-19.at.17.35.30.mov

For example, here I have a flex item with three elements. The first and third element are fixed in width, while the middle text element's width is dynamic. When rendered, it measures up to 245.17px. Once the element is removed and auto-animate starts fading it out, it sets width: 245px for whatever reason, causing the text inside to wrap to the next line. Removing the round in the code above fixes this.

Are you sure that’s due to rounding? I would be thrilled if it was because we could fix it, but up till now my working hypothesis is this: some "paints" take 2 paints and some take 1. Text is particularly tricky because the browser itself doesnt know how big/wide the text will be until it has painted it. So it paints, measures it, then paints again. Unfortunately autoanimate is only informed of the first paint (which wrapped) and thus sets the size to that instead of the second paint. Ive looked for a way around this but been unable to find one.

This tends to happen with things were the width of the item is determined by its context, like width-less flex items, table cells (those have other issues too), floats, etc.

If you can demonstrate that it is indeed a rounding issue I would be thrilled to accept a PR or look into it further!

It looks to be. If I break just as the element is fading out and check its size in the inspector, its width has been rounded down, and if I remove the rounding from the line I mentioned it works fine. I don't know why the rounding was added originally so I'm hesitant to create a PR, but it works a lot better without it for my specific case!

image

image