React,在useeffect中使用requestanimationframe设置动画

eoigrqb6  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(299)

我一直在通过更新useeffect函数中setinterval内的状态变量来设置svg弧的动画,但这在我的前端似乎非常昂贵(在开始时会滞后几秒钟)。通过一些研究,我遇到了requestanimationframe,我发现了一些例子,但在我的案例中似乎没有一个是有效的,有人能帮忙吗?

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
        var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;

        return {
            x: centerX + (radius * Math.cos(angleInRadians)),
            y: centerY + (radius * Math.sin(angleInRadians))
        };
    }

    const describeArc = (x, y, radius, startAngle, endAngle, isSmall, time) => {

        var start = polarToCartesian(x, y, radius, endAngle);
        var end = polarToCartesian(x, y, radius, startAngle);

        var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";

        var d = [
            "M", start.x, start.y,
            "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
        ].join(" ");

        if (isSmall) {
            return (
                setSmallArcPath(d)
            )
        }

        setBigArcPath(d);
        setBigArcStart(startAngle + degreeIncrement);
        setSmallArcStart(startAngle + degreeDecrement);
    }

    useEffect(() => {
        let timerId;
        const f = () => {
            describeArc(4220, 3500, 3000, bigArcStart, bigArcStart + 90, false);
            describeArc(4220, 3500, 2800, smallArcStart, smallArcStart + 60, true);
            timerId = requestAnimationFrame(f);
        };
        timerId = requestAnimationFrame(f);
        return () => cancelAnimationFrame(timerId);

    }, [bigArcPath, smallArcPath, bigArcStart, smallArcStart]);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题