"""Render a supplied GSAP program with MoVer and the benchmark's GSAP runtime."""
from __future__ import annotations
import asyncio
import json
from importlib.metadata import version
from pathlib import Path
from typing import Any
from mover.converter.mover_converter import capture_frames_server_driven
from mover.converter.render_session import RenderSession
GSAP_VERSION = "3.14.1"
GSAP_ROOT = Path(__file__).resolve().parents[1] / "node_modules" / "gsap"
RUNTIME_FILES = (
"gsap.min.js",
"MotionPathPlugin.min.js",
"EasePack.min.js",
"MotionPathHelper.min.js",
"DrawSVGPlugin.min.js",
"MorphSVGPlugin.min.js",
"CustomEase.min.js",
"CustomBounce.min.js",
)
PLUGIN_GLOBALS = (
"MotionPathPlugin", "SlowMo", "RoughEase", "ExpoScaleEase",
"MotionPathHelper", "DrawSVGPlugin", "MorphSVGPlugin", "CustomEase", "CustomBounce",
)
def assemble_html(svg: str, program: str) -> str:
if not program.strip() or " tag")
manifest = GSAP_ROOT / "package.json"
installed = json.loads(manifest.read_text(encoding="utf-8"))["version"]
if installed != GSAP_VERSION:
raise RuntimeError(f"GSAP {GSAP_VERSION} is required, found {installed}; run npm ci")
scripts = []
for name in RUNTIME_FILES:
source = (GSAP_ROOT / "dist" / name).read_text(encoding="utf-8")
if "\n{source}\n')
return f"""
MotionGraphicsBench animation
{svg}
{"".join(scripts)}
"""
async def _capture(
html: Path, output: Path, fps: float, capture_duration: float | None,
) -> tuple[list[str], dict[str, Any]]:
async with RenderSession(html, output_dir=output, capture_duration=capture_duration) as session:
errors: list[str] = []
session.page.on("pageerror", lambda error: errors.append(str(error)))
initial_errors = await session.page.evaluate("window.__benchmark_errors")
if initial_errors:
raise RuntimeError("GSAP execution failed:\n" + "\n".join(initial_errors))
info = await session.get_animation_info(fps)
frames_dir = output / "svg_frames"
await capture_frames_server_driven(
session.page, str(frames_dir), fps=fps, output_format="svg",
capture_duration=capture_duration,
)
errors.extend(await session.page.evaluate("window.__benchmark_errors"))
if errors:
raise RuntimeError("GSAP execution failed:\n" + "\n".join(errors))
paths = sorted(frames_dir.glob("frame_*.svg"))
frames = [path.read_text(encoding="utf-8").strip() for path in paths]
browser = session.page.context.browser
assert browser is not None
return frames, {
"mover_version": version("mover"),
"gsap_version": GSAP_VERSION,
"chromium": browser.version,
"fps": fps,
"duration": float(info["animDuration"]),
"frame_count": len(frames),
}
def render_frames(
svg: str, program: str, output: Path, *, fps: float,
capture_duration: float | None = None, timeout: float = 900,
) -> tuple[list[str], dict[str, Any]]:
html_source = assemble_html(svg, program)
output.mkdir(parents=True, exist_ok=False)
html = output / "animation.html"
html.write_text(html_source, encoding="utf-8")
return asyncio.run(asyncio.wait_for(_capture(html, output, fps, capture_duration), timeout=timeout))