rive-app / rive-wasm

Wasm/JS runtime for Rive
MIT License
669 stars 46 forks source link

Rive animation is really slow (in Blazor WASM) after re-routing to the page with animation (even with dispose implemented) #283

Closed Laftek closed 1 year ago

Laftek commented 1 year ago

So I tried to display rive animation ("/" page) with Blazor WASM (basic template). When I click on Counter ("/counter" page) and back to index page ("/" page) lets say 20x animation is really freezing and maybe after 27x it takes 20s to even render animation. Also my CPU went 12% up and RAM 120MB after those 27x (checked in Task manager). I was trying to use dev tools but as newbie it was pretty worthless. I can see rive library is taking biggest memory cost. Also my code has Dispose implemented (see below).

My question is: is it something I am doing wrong or should I file new issue on rive repo or maybe asp.net?

Index.razor:

@page "/"
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable

<canvas id="canvas" width="500" height="500"></canvas>

@code {
    IJSObjectReference? rivWrapper;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            rivWrapper = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./Pages/Index.razor.js");
            await rivWrapper.InvokeVoidAsync("createRive");
        }
    }

    public async ValueTask DisposeAsync()
    {
        await rivWrapper!.DisposeAsync();
    }
}

Index.razor.js:

export function createRive() {
    const r = new rive.Rive({
        src: 'bear.riv',
        canvas: document.getElementById('canvas'),
        autoplay: true,
    });
}

index.html:

<script src="https://unpkg.com/@rive-app/canvas@1.0.97"></script>

and the web app: https://laftek.github.io/BlazorApp1/

Thank you.

zplata commented 1 year ago

Hi! I responded to what may be your Stack overflow post here: https://stackoverflow.com/questions/75128377/rive-animation-is-really-slow-in-blazor-wasm-after-re-routing-to-the-page-with/75153088#75153088

Basically, try updating to our most recent version 1.0.98 and make sure you're deleting the Rive instances you create (in this case, your variable r) using this JS runtime.

Laftek commented 1 year ago

Thanks Zplata! Its working now :)