8#include <emscripten/val.h>
21QWasmAnimationDriver::QWasmAnimationDriver(QUnifiedTimer *)
22 : QAnimationDriver(
nullptr)
24 connect(
this, &QAnimationDriver::started,
this, &QWasmAnimationDriver::start);
25 connect(
this, &QAnimationDriver::stopped,
this, &QWasmAnimationDriver::stop);
28QWasmAnimationDriver::~QWasmAnimationDriver()
30 disconnect(
this, &QAnimationDriver::started,
this, &QWasmAnimationDriver::start);
31 disconnect(
this, &QAnimationDriver::stopped,
this, &QWasmAnimationDriver::stop);
33 if (m_animateCallbackHandle != 0)
34 QWasmAnimationFrameMultiHandler::instance()->unregisterAnimateCallback(m_animateCallbackHandle);
37qint64 QWasmAnimationDriver::elapsed()
const
39 return isRunning() ? qint64(m_currentTimestamp - m_startTimestamp) : 0;
42double QWasmAnimationDriver::getCurrentTimeFromTimeline()
const
47 emscripten::val document = emscripten::val::global(
"document");
48 emscripten::val timeline = document[
"timeline"];
49 if (!timeline.isNull() && !timeline.isUndefined()) {
50 emscripten::val currentTime = timeline[
"currentTime"];
51 if (!currentTime.isNull() && !currentTime.isUndefined())
52 return currentTime.as<
double>();
57void QWasmAnimationDriver::handleFallbackTimeout()
63 double currentTime = getCurrentTimeFromTimeline();
65 currentTime = m_currentTimestamp + FallbackTimerInterval;
66 const double timeSinceLastFrame = currentTime - m_currentTimestamp;
70 if (timeSinceLastFrame > FallbackTimerInterval * 0.8) {
71 m_currentTimestamp = currentTime;
76void QWasmAnimationDriver::start()
82 m_startTimestamp = getCurrentTimeFromTimeline();
83 m_currentTimestamp = m_startTimestamp;
86 m_animateCallbackHandle = QWasmAnimationFrameMultiHandler::instance()->registerAnimateCallback(
87 [
this](
double timestamp) { handleAnimationFrame(timestamp); });
90 fallbackTimer.setInterval(FallbackTimerInterval);
91 connect(&fallbackTimer, &QTimer::timeout,
this, &QWasmAnimationDriver::handleFallbackTimeout);
92 fallbackTimer.start();
94 QAnimationDriver::start();
97void QWasmAnimationDriver::stop()
100 m_currentTimestamp = 0;
103 fallbackTimer.stop();
104 disconnect(&fallbackTimer, &QTimer::timeout,
this, &QWasmAnimationDriver::handleFallbackTimeout);
107 if (m_animateCallbackHandle != 0) {
108 QWasmAnimationFrameMultiHandler::instance()->unregisterAnimateCallback(m_animateCallbackHandle);
109 m_animateCallbackHandle = 0;
112 QAnimationDriver::stop();
115void QWasmAnimationDriver::handleAnimationFrame(
double timestamp)
120 m_currentTimestamp = timestamp;
124 if (m_startTimestamp == 0)
125 m_startTimestamp = timestamp;
constexpr int FallbackTimerInterval