Skip to main content
w
1y ago

Getting RunTime Error

RuntimeError: memory access out of bounds
here is the code

import {
    useRive,
    useStateMachineInput,
    Fit,
    Layout as RiveLayout,
    Alignment
} from "@rive-app/react-canvas";
import { useEffect } from "react";
import s from './HeroScrollRive.module.scss'

import { useScroll } from '@/hooks/use-scroll'

export default function HeroScrollingRive(props) {
    const { rive, RiveComponent } = useRive({
        src: "/eyenai.riv",
        artboard: "HeroAnimation",
        stateMachines: "ScrollingBehaviour",
        layout: new RiveLayout({
            fit: Fit.FitHeight,
            alignment: Alignment.CenterRight,
        }),
        autoplay: true,
    })

    const scrollPos = useStateMachineInput(
        rive,
        "ScrollingBehaviour",
        "ScrollPos",
        0
    )

    useEffect(() => {
        let scrollListener;
        if (scrollPos) {
            const body = document.querySelector("body");
            const bodyParent = body.parentNode;
            const pageLength = bodyParent.scrollHeight;

            const scrollCallback = () => {
                const scrollOffset = bodyParent.scrollTop;
                const scrollPosValue =
                    (450 * scrollOffset) / (pageLength - bodyParent.clientHeight);
                scrollPos.value = scrollPosValue;
            };

            scrollListener = document.addEventListener("scroll", scrollCallback);
        }

        return () => {
            if (scrollListener) {
                document.removeEventListener("scroll", scrollListener);
                console.log(scrollListener);
            }
        };
    }, [scrollPos]);

    return (
        <RiveComponent className={s.hero__rive} />
    );
}
4 replies
P
w
w
1y ago

Getting these type of errors

w
1y ago

I followed the tutorial from the Rive Animation on Scroll things suppose to be working but not the way I want, I'm exporting it as a component and idk how i can trigger this aniamtion at certain level of scroll to let it animate according to that point to the end of the animation on scroll

P
(edited) 1y ago

Hi I did a quick test using your code and it seems to work fine without error (I removed the use-scroll dependency). You can give it a try at this CodeSandbox link. Just scroll up and down the preview window and you'll see the rectangle color change. I also uploaded the .rev file to the CodeSandbox so you can look at the setup. If you're still getting errors and are comfortable sharing your .rev file, I can take a closer look.

One thing I would take a look at is to make sure the name of your state machine and input in your .rev file matches what you have in your code (ie, "ScrollingBehavior" as the state machine name and "ScrollPos" as the input name).

w
(edited) 1y ago

Hello Thanks for your response, so what i was trying to achieve with this animation was that it is basically in a section, so when scroll down to that section i want this animation to play that time only like it should start incrementing the ScrollPos after reaching to that section top or something!

here is my riv file

https://drive.google.com/file/d/1jVuMy41f_h8ZIVEsiskApe5Hk8LhLLF4/view?usp=sharing