7#include <emscripten/val.h>
20QWasmAnimationDriver::QWasmAnimationDriver(QUnifiedTimer *)
21 : QAnimationDriver(
nullptr)
23 connect(
this, &QAnimationDriver::started,
this, &QWasmAnimationDriver::start);
24 connect(
this, &QAnimationDriver::stopped,
this, &QWasmAnimationDriver::stop);
27QWasmAnimationDriver::~QWasmAnimationDriver()
29 disconnect(
this, &QAnimationDriver::started,
this, &QWasmAnimationDriver::start);
30 disconnect(
this, &QAnimationDriver::stopped,
this, &QWasmAnimationDriver::stop);
32 if (m_animateCallbackHandle != 0)
33 QWasmAnimationFrameMultiHandler::instance()->unregisterAnimateCallback(m_animateCallbackHandle);
36qint64 QWasmAnimationDriver::elapsed()
const
38 return isRunning() ? qint64(m_currentTimestamp - m_startTimestamp) : 0;
41double QWasmAnimationDriver::getCurrentTimeFromTimeline()
const
46 emscripten::val document = emscripten::val::global(
"document");
47 emscripten::val timeline = document[
"timeline"];
48 if (!timeline.isNull() && !timeline.isUndefined()) {
49 emscripten::val currentTime = timeline[
"currentTime"];
50 if (!currentTime.isNull() && !currentTime.isUndefined())
51 return currentTime.as<
double>();
56void QWasmAnimationDriver::handleFallbackTimeout()
62 double currentTime = getCurrentTimeFromTimeline();
64 currentTime = m_currentTimestamp + FallbackTimerInterval;
65 const double timeSinceLastFrame = currentTime - m_currentTimestamp;
69 if (timeSinceLastFrame > FallbackTimerInterval * 0.8) {
70 m_currentTimestamp = currentTime;
75void QWasmAnimationDriver::start()
81 m_startTimestamp = getCurrentTimeFromTimeline();
82 m_currentTimestamp = m_startTimestamp;
85 m_animateCallbackHandle = QWasmAnimationFrameMultiHandler::instance()->registerAnimateCallback(
86 [
this](
double timestamp) { handleAnimationFrame(timestamp); });
89 fallbackTimer.setInterval(FallbackTimerInterval);
90 connect(&fallbackTimer, &QTimer::timeout,
this, &QWasmAnimationDriver::handleFallbackTimeout);
91 fallbackTimer.start();
93 QAnimationDriver::start();
96void QWasmAnimationDriver::stop()
99 m_currentTimestamp = 0;
102 fallbackTimer.stop();
103 disconnect(&fallbackTimer, &QTimer::timeout,
this, &QWasmAnimationDriver::handleFallbackTimeout);
106 if (m_animateCallbackHandle != 0) {
107 QWasmAnimationFrameMultiHandler::instance()->unregisterAnimateCallback(m_animateCallbackHandle);
108 m_animateCallbackHandle = 0;
111 QAnimationDriver::stop();
114void QWasmAnimationDriver::handleAnimationFrame(
double timestamp)
119 m_currentTimestamp = timestamp;
123 if (m_startTimestamp == 0)
124 m_startTimestamp = timestamp;
constexpr int FallbackTimerInterval