For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt, and this page is available as Markdown at /next/api/lynx-native-api/lynx-extension-module/request-animation-frame.md.
Lynx
  • English
  • RequestAnimationFrame

    The RequestAnimationFrame method, part of the VSyncObserver class, allows you to schedule a function to be executed just before the next screen repaint, synchronized with the vertical synchronization (VSync) signal. This is the standard and most efficient way to run animations or perform visual updates in Lynx, as it ensures that your code executes at the optimal time, avoiding unnecessary rendering and improving performance.

    There are two overloads for this method, allowing you to use either a C++ std::function or a traditional C-style function pointer as a callback.

    Method Signature

    With std::function callback

    void RequestAnimationFrame(uintptr_t id, lynx::pub::VSyncObserverCallback callback);

    With C-function callback

    void RequestAnimationFrame(uintptr_t id,
                               vsync_observer_callback callback,
                               void* user_data);

    Parameters

    NameTypeDescriptionRequired
    iduintptr_tA unique identifier for the request instance. This helps in tracking or managing multiple animation requests.Yes
    callbacklynx::pub::VSyncObserverCallback / vsync_observer_callbackThe function to be called when the next VSync signal arrives. See the callback signatures below.Yes
    user_datavoid*(C-function overload only) A pass-through context pointer provided to the callback. Pass nullptr when no context is needed.Yes

    Callback Functions

    The C++ lynx::pub::VSyncObserverCallback signature is:

    void(int64_t frame_start_time, int64_t frame_end_time);

    The C vsync_observer_callback signature is:

    void(void* user_data, int64_t frame_start_time, int64_t frame_end_time);
    • frame_start_time: The timestamp indicating the start of the VSync frame.
    • frame_end_time: The timestamp indicating the end of the VSync frame.

    Example

    Here's an example of how to use RequestAnimationFrame with a C++ lambda function.

    // Assuming 'vsync_observer' is a valid std::unique_ptr<VSyncObserver>
    // obtained from the OnRuntimeAttach lifecycle method.
    
    uintptr_t request_id = 1; // A unique ID for this request
    
    vsync_observer->RequestAnimationFrame(request_id, [](int64_t start_time, int64_t end_time) {
        // Perform animation logic or visual updates here.
        // This code will run just before the next repaint.
        printf("Frame rendered between %lld and %lld\\n", start_time, end_time);
    });

    Compatibility

    LCD tables only load in the browser

    Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.