Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Variable duration of animation
QtWS25 Last Chance

[Solved]Variable duration of animation

Scheduled Pinned Locked Moved QML and Qt Quick
13 Posts 3 Posters 5.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DRAX
    wrote on last edited by
    #2

    Try putting this condition on duration (you can modify time to match desired, I just give as example 1000ms):
    duration: (shown ? 1000 : 2000)

    Also, small tip - you can put it this way:
    when: shown (instead: when: shown == true)

    and

    when: !shown (instead: when: shown == false)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DRAX
      wrote on last edited by
      #3

      Here is another idea:
      You could define PropertyChange on PropertyAnimation to change duration depending on state.
      Example:

      @import QtQuick 1.1

      Rectangle {

      width: 360
      
      height: 360
      

      property bool shown: true

      Rectangle {
      
          id: rect
      
          width: parent.width
      
          height: parent.height
      
          color: "red"
      
      
      
          /*Behavior on y {
      
                   NumberAnimation { duration: 2000 }
      
               }*/
      
      }
      
      
      
      states: [
      
      State {
      
              name: "show"
      
              when: shown == true
      
              PropertyChanges { target: rect; y: 0}
      
              PropertyChanges { target: animation; duration: 2000}
      
          },
      
      State {
      
              name: "hide"
      
              when: shown == false
      
              PropertyChanges { target: rect; y: rect.height }
      
              PropertyChanges { target: animation; duration: 1000}
      
          }
      
      
      
      ]
      
      transitions: [
      
          Transition {
      
              from: "*"
      
              to: "*"
      
              reversible: true
      
              PropertyAnimation {
      
                  id: animation
      
                  target: rect
      
                  properties: "y"
      
                  duration: 2000
      
              }
      
          }
      
      
      
      ]
      
      MouseArea {
      
          anchors.fill: parent
      
          onClicked: {
      
              shown = !shown
      
              print(shown)
      
          }
      
      }
      

      }@

      1 Reply Last reply
      0
      • R Offline
        R Offline
        riddle
        wrote on last edited by
        #4

        You misunderstood me. Maybe I wrote it unclearly.

        duration: (shown ? 1000 : 2000) does not solve anything.
        The issue is that when you click once (animation starts now), and you click once again. Then the movement is short, but the duration is still 2000s. No matter how long the distance is.
        Please run and check how it works..

        Simply, I want to have constant speed no matter how many pixels are left to finish the animation.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #5

          [quote author="riddle" date="1355145150"]Simply, I want to have constant speed no matter how many pixels are left to finish the animation.
          [/quote]

          Use physics, then. time = distance/velocity. In your case, y should point to the current value when you invoke it in duration. Or you can temporarily stop the animation, get y and then use it to calculate duration.

          (Z(:^

          1 Reply Last reply
          0
          • R Offline
            R Offline
            riddle
            wrote on last edited by
            #6

            bq. Use physics

            Sure, but I have problem with using property y in binding calulcations. I get binding loop.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DRAX
              wrote on last edited by
              #7

              I agree with sierdzio, you must use Y then.
              No need for bounding.
              You can do it on this way:

              1. when you click on button, grab current position (A) and position where it should go (B)
              2. subtract those two and take absolute value (abs(A-B))
                And you are ready to go.
              1 Reply Last reply
              0
              • R Offline
                R Offline
                riddle
                wrote on last edited by
                #8

                Maths says
                When going from show to hide:
                @duration: (rect.height-rect.y)/rect.height2000@
                and when going from hide to show:
                @duration: rect.y/rect.height
                2000@

                But it doesn't work... we have QML PropertyAnimation: Binding loop detected for property "duration".
                What's going on?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DRAX
                  wrote on last edited by
                  #9

                  Well, problem is that you are using binding.
                  Don't use it!
                  As I said above, when you click on button (rectangle) set formula (calculate, not bind).

                  Like this:
                  @
                  MouseArea {
                  anchors.fill: parent

                      onClicked: {
                          shown = !shown
                          print(shown)
                          animation.duration = (shown ? rect.y/rect.height*2000 : (rect.height-rect.y)/rect.height*2000)
                      }
                  }
                  

                  @

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    riddle
                    wrote on last edited by
                    #10

                    So there is no declarative way to do so?
                    Maybe it's a good idea to add constVelocity boolean property to animations. It could ensure that when transition is reversible, no matter when you reverse the animation, it will play with constant speed.

                    Oh, and one more thing. Could someone explain me why it does not work with properties (duration: (rect.height-rect.y)/rect.height*2000) ?

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DRAX
                      wrote on last edited by
                      #11

                      In declarative way, it is not possible, only via JavaScript.

                      Duration cannot be bound to Y since it is one which changes Y.
                      Code that you posted should work like this:

                      1. animation started and changes Y
                      2. animation duration depends on Y so it updates
                      3. animation instance either resets or creates another one which changes Y (and from here you can see the loop)

                      In other words:
                      -change Y (animate)
                      -Y is changed, update duration (bound to Y)
                      -duration is changed, change Y (animate)
                      -Y is changed, update duration
                      -duration is changed, change Y
                      -Y is changed, update duration
                      -duration is changed, change Y
                      -Y is changed, update duration
                      ...

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        riddle
                        wrote on last edited by
                        #12

                        Thank you for explanation.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DRAX
                          wrote on last edited by
                          #13

                          Anytime :)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved