airbnb / lottie-web

Render After Effects animations natively on Web, Android and iOS, and React Native. http://airbnb.io/lottie/
MIT License
29.86k stars 2.85k forks source link

updateDocumentData not triggering re-renders other attributes expect for text. #2902

Closed orenlwsc closed 6 months ago

orenlwsc commented 1 year ago

Hello,

I'm trying to use the updateDocumentData API to update text elements. Currently, passing t in the payload updates the text correctly and properly triggers the re-rendering. However, when passing other attributes like s for font size and j for justifying, the values are updated correctly, but the animation does not update.

I made sure that:

Currently, my workaround is to work directly on the JSON. When I manually update the JSON object by finding the relevant elements, it correctly updates both values and renders. However, the amination flicker and its behavior I prefer to avoid.

Is there another way to get the values updated without flickering the animation? Am I missing something when using the updateDocumentData?

Thanks!

bodymovin commented 1 year ago

hi, is the animation playing when the updateDocumentData is called? If it is paused it will not update until it renders again

orenlwsc commented 1 year ago

@bodymovin actually, no, I'm stopping it, updating, then playing it again. check out the following snippet:

const reRender = () => {
    // This is a workaround since in Lottie, while updating text layers on paused animations,
    // rendering isn't occur, and the updated parameters will not be shown until seek to other frame
    // https://github.com/airbnb/lottie-web/issues/2297
    if (lottieInstance.lottie) {
      lottieInstance.lottie.stop()
      const { currentFrame, totalFrames } = lottieInstance.lottie
      const frameToJumpTo = totalFrames > currentFrame ? currentFrame + 1 : currentFrame - 1

      lottieInstance.lottie.goToAndStop(frameToJumpTo, true)
      lottieInstance.lottie.goToAndStop(currentFrame, true)
      lottieInstance.lottie.play()
    }
  }

also, I tried it without playing again (aka the last line in the snippet) - when I'm not playing it, nothing gets updated at all, which is expected behavior.

bodymovin commented 1 year ago

so using that snippet it is not working?

orenlwsc commented 1 year ago

so using that snippet it is not working?

@bodymovin nope. I'm suspecting it might be related to the animation file itself. if you have any insights or ideas to share with me, would love to hear any possible direction.

Edit Turns out I was right. When a text gets an attribute with an expression, theupdateDocumentData not works perfectly. Here the problem:

        "t":{
             "d":{
                "k":[
                   {
                      "s":{
                         "sz":[
                            1182,
                            1075
                         ],
                         "ps":[
                            -525.5,
                            -171.398193359375
                         ],
                         "s":150,
                         "f":"Arial-BoldMT",
                         "t":"TEXT 2",
                         "ca":1,
                         "j":2,
                         "tr":20,
                         "lh":180,
                         "ls":0,
                         "fc":[
                            0.13,
                            0.142,
                            0.188
                         ]
                      },
                      "t":0
                   }
                ],
                "x":"var $bm_rt;\n$bm_rt = text.sourceText;" <==== when removed, the update works properly
             },
             ...,
             ...,

Is that a bug? expected behavior? any proper workaround for such cases?

Edit 2: Is it possible to apply something similar to updateDocumentData on general objects and attributes such as position, rotation, etc?