如何在 QML 上使用 AnimeJS 库

How to use AnimeJS library on QML

在 Qt Creator 中,我创建了 QT Quick Application 并使用此 link github,但使用这种方式:

import "qrc:/anime-master/lib/anime.js" as Logic


Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("JS")

    Rectangle{
        id: rec1
        width: 100; height: width
        color: "orange"
        x: 200; y: 250
        MouseArea{
            anchors.fill: parent
            onClicked: {
                getAnime()
            }
        }
    }

    function getAnime(){
        Logic.anime({
                  targets: rec1,
                  translateX: 320
              });
    }
}

但是我有这个错误:

qrc:/anime-master/lib/anime.js:1283: ReferenceError: module is not defined 在其文件中:module.exports = anime; 而我对js的了解很少

简单回答:你应该使用QML's internal tools做动画。
更详细的回答

  1. 你不能简单地在 QML 中使用任何 JS 库。它们是为浏览器使用而编写的,其中 JS 是第一个 class 公民。但在 QML 中,它的功能非常有限。
  2. 我几乎可以肯定大多数 JS 动画库都是基于 setTimeout() 函数的。 QML中根本就没有这个功能
  3. 如果你以某种方式让它工作,它会非常缓慢且不稳定,因为它太离谱了。