Skip to content

analysis

Class info

Classes

Name Children Inherits
Element
framesheet.analysis
Element(index: int, tag: str, label: str, svg: bool, parent: int)
    Geometry
    framesheet.analysis
    Inputs shared by all render checks.

      🛈 DocStrings

      Render checks: geometry of every element across all sampled frames.

      Each check is a function (Geometry) -> list[Finding] registered in :data:CHECKS.

      CHECKS module-attribute

      Render checks run on every inspection, in order. Append to add a check.

      TOLERANCE module-attribute

      TOLERANCE = 1.0
      

      Pixels an element may poke out of its container/canvas before it is reported.

      Geometry dataclass

      Inputs shared by all render checks.

      Source code in src/framesheet/analysis.py
      @dataclass
      class Geometry:
          """Inputs shared by all render checks."""
      
          elements: dict[int, Element]
          times: list[float]
          client: dict[int, list[Rect | None]]
          """Client rect (CSS px) per element per frame; ``None`` = not rendered/invisible."""
          user: dict[int, list[Rect | None]]
          """SVG user-space rect (outermost ``<svg>``) per element per frame."""
          canvas: Rect
          viewbox: Rect | None
          targets: dict[int, list[str]]
          animations: list[AnimationTiming]
          is_svg_doc: bool
          html_overflow: list[list[Any]] = field(default_factory=list)
          """``[index, dx, dy]`` per frame (list of lists)."""
      
          @classmethod
          def build(
              cls,
              description: list[dict[str, Any]],
              snapshots: list[Snapshot],
              *,
              canvas: Rect,
              viewbox: Rect | None,
              targets: dict[str, list[str]],
              animations: list[AnimationTiming],
              is_svg_doc: bool,
          ) -> Geometry:
              elements = {
                  d["i"]: Element(d["i"], d["tag"], d["label"], d["svg"], d["parent"])
                  for d in description
              }
              n = len(snapshots)
              client: dict[int, list[Rect | None]] = {}
              user: dict[int, list[Rect | None]] = {}
              for k, snap in enumerate(snapshots):
                  for index, rect, user_rect, visible in snap.geometry:
                      if not visible:
                          continue
                      client.setdefault(index, [None] * n)[k] = as_rect(rect)
                      if user_rect is not None:
                          user.setdefault(index, [None] * n)[k] = as_rect(user_rect)
              return cls(
                  elements=elements,
                  times=[s.t for s in snapshots],
                  client=client,
                  user=user,
                  canvas=canvas,
                  viewbox=viewbox,
                  targets={int(k): v for k, v in targets.items()},
                  animations=animations,
                  is_svg_doc=is_svg_doc,
                  html_overflow=[s.overflow for s in snapshots],
              )
      
          def ancestors(self, index: int) -> list[int]:
              out = []
              parent = self.elements[index].parent if index in self.elements else -1
              while parent != -1 and parent in self.elements:
                  out.append(parent)
                  parent = self.elements[parent].parent
              return out
      
          def label(self, index: int) -> str:
              return self.elements[index].label if index in self.elements else f"element {index}"
      
          def moved(self) -> set[int]:
              out = set()
              for index, rects in self.client.items():
                  present = [r for r in rects if r is not None]
                  if len(present) != len(rects) and present:
                      out.add(index)  # appears / disappears
                      continue
                  if present and any(_differs(present[0], r) for r in present[1:]):
                      out.add(index)
              return out
      

      client instance-attribute

      client: dict[int, list[Rect | None]]
      

      Client rect (CSS px) per element per frame; None = not rendered/invisible.

      html_overflow class-attribute instance-attribute

      html_overflow: list[list[Any]] = field(default_factory=list)
      

      [index, dx, dy] per frame (list of lists).

      user instance-attribute

      user: dict[int, list[Rect | None]]
      

      SVG user-space rect (outermost <svg>) per element per frame.

      check_outside

      check_outside(geo: Geometry) -> list[Finding]
      

      Elements extending past the viewBox (SVG) or viewport (HTML).

      Source code in src/framesheet/analysis.py
      def check_outside(geo: Geometry) -> list[Finding]:
          """Elements extending past the viewBox (SVG) or viewport (HTML)."""
          cx, cy, cw, ch = geo.canvas
          hits: dict[int, list[int]] = {}
          amounts: dict[int, dict[str, float]] = {}
          for index, rects in geo.client.items():
              for k, rect in enumerate(rects):
                  if rect is None:
                      continue
                  x, y, w, h = rect
                  over = {
                      "left": cx - x,
                      "top": cy - y,
                      "right": x + w - (cx + cw),
                      "bottom": y + h - (cy + ch),
                  }
                  worst = {side: v for side, v in over.items() if v > TOLERANCE}
                  if worst:
                      hits.setdefault(index, []).append(k)
                      agg = amounts.setdefault(index, {})
                      for side, v in worst.items():
                          agg[side] = max(agg.get(side, 0), v)
          keep = _leaves(geo, set(hits))
          area = "viewBox" if geo.is_svg_doc else "viewport"
          out = []
          ordered = sorted(keep, key=lambda i: -max(amounts[i].values()))
          for index in ordered[:MAX_REPORTED]:
              ks = hits[index]
              sides = ", ".join(f"{v:.0f}px {side}" for side, v in amounts[index].items())
              when = (
                  "at every frame" if len(ks) == len(geo.times) else f"at {_fmt_times(_times(geo, ks))}"
              )
              out.append(
                  _finding(
                      "outside-bounds",
                      f"{geo.label(index)} extends outside the {area} {when} (up to {sides}).",
                      geo.label(index),
                      _times(geo, ks),
                  )
              )
          if len(ordered) > MAX_REPORTED:
              out.append(
                  _finding(
                      "outside-bounds",
                      f"... and {len(ordered) - MAX_REPORTED} more element(s) outside the {area}.",
                      None,
                      None,
                  )
              )
          return out
      

      check_parked

      check_parked(geo: Geometry) -> list[Finding]
      

      Small SVG elements sitting on the user-space origin (0,0), e.g. before begin.

      Source code in src/framesheet/analysis.py
      def check_parked(geo: Geometry) -> list[Finding]:
          """Small SVG elements sitting on the user-space origin (0,0), e.g. before ``begin``."""
          if geo.viewbox is None:
              return []
          _, _, vw, vh = geo.viewbox
          hits: dict[int, list[int]] = {}
          for index, rects in geo.user.items():
              element = geo.elements.get(index)
              if element is None or element.tag in CONTAINERS:
                  continue
              for k, rect in enumerate(rects):
                  if rect is None:
                      continue
                  x, y, w, h = rect
                  small = w < SMALL_FRACTION * vw and h < SMALL_FRACTION * vh
                  covers_origin = x <= TOLERANCE and y <= TOLERANCE and x + w >= 0 and y + h >= 0
                  if small and covers_origin:
                      hits.setdefault(index, []).append(k)
          out = []
          for index in sorted(_leaves(geo, set(hits))):
              ks = hits[index]
              times = _times(geo, ks)
              out.append(
                  _finding(
                      "parked-at-origin",
                      f"{geo.label(index)} sits at the SVG origin (0,0) at {_fmt_times(times)}; "
                      "typical cause: <animateMotion> with a positive begin, or a missing x/y/cx/cy.",
                      geo.label(index),
                      times,
                  )
              )
          return out
      

      check_text_overflow

      check_text_overflow(geo: Geometry) -> list[Finding]
      

      SVG <text> poking out of the <rect> painted under it; HTML text overflow.

      Source code in src/framesheet/analysis.py
      def check_text_overflow(geo: Geometry) -> list[Finding]:
          """SVG ``<text>`` poking out of the ``<rect>`` painted under it; HTML text overflow."""
          hits: dict[tuple[int, int], list[int]] = {}
          amounts: dict[tuple[int, int], float] = {}
          rects = [i for i, el in geo.elements.items() if el.tag == "rect"]
          for index, el in geo.elements.items():
              if el.tag != "text" or index not in geo.client:
                  continue
              for k, text_box in enumerate(geo.client[index]):
                  if text_box is None:
                      continue
                  container = _container(geo, index, text_box, rects, k)
                  if container is None:
                      continue
                  container_box = geo.client[container][k]
                  if container_box is None:
                      continue
                  excess = _excess(text_box, container_box)
                  if excess > TOLERANCE:
                      key = (index, container)
                      hits.setdefault(key, []).append(k)
                      amounts[key] = max(amounts.get(key, 0), excess)
          out = [
              _finding(
                  "text-overflow",
                  f"{geo.label(text)} overflows its container {geo.label(box)} by up to "
                  f"{amounts[(text, box)]:.0f}px at {_fmt_times(_times(geo, ks))}.",
                  geo.label(text),
                  _times(geo, ks),
              )
              for (text, box), ks in sorted(hits.items())
          ]
          html_hits: dict[int, list[int]] = {}
          html_amount: dict[int, tuple[float, float]] = {}
          for k, items in enumerate(geo.html_overflow):
              for index, dx, dy in items:
                  html_hits.setdefault(index, []).append(k)
                  ox, oy = html_amount.get(index, (0, 0))
                  html_amount[index] = (max(ox, dx), max(oy, dy))
          for index, ks in sorted(html_hits.items()):
              dx, dy = html_amount[index]
              out.append(
                  _finding(
                      "text-overflow",
                      f"{geo.label(index)} content is larger than its box ({dx:.0f}px wider, "
                      f"{dy:.0f}px taller) at {_fmt_times(_times(geo, ks))}.",
                      geo.label(index),
                      _times(geo, ks),
                  )
              )
          return out
      

      check_transform_box

      check_transform_box(geo: Geometry) -> list[Finding]
      

      CSS transform animations on SVG elements without transform-box: fill-box.

      Source code in src/framesheet/analysis.py
      def check_transform_box(geo: Geometry) -> list[Finding]:
          """CSS transform animations on SVG elements without ``transform-box: fill-box``."""
          out = []
          seen: set[str] = set()
          for anim in geo.animations:
              if anim.transform_box != "view-box" or not anim.target or anim.target in seen:
                  continue
              seen.add(anim.target)
              out.append(
                  _finding(
                      "svg-transform-box",
                      f"CSS animation '{anim.name}' on {anim.target} animates transform with "
                      "transform-box: view-box (the SVG default), so it rotates/scales around the "
                      "SVG origin, not the element. Add transform-box: fill-box; "
                      "transform-origin: center.",
                      anim.target,
                      None,
                  )
              )
          return out
      

      tracks

      tracks(geo: Geometry) -> list[ElementTrack]
      

      Per-frame boxes of animated elements (declared targets or anything that moved).

      Source code in src/framesheet/analysis.py
      def tracks(geo: Geometry) -> list[ElementTrack]:
          """Per-frame boxes of animated elements (declared targets or anything that moved)."""
          moved = geo.moved()
          # A moving group moves all its children: keep the outermost mover. A group whose box
          # only changes because an animated descendant moves is not reported on its own.
          explained = {a for i in geo.targets for a in geo.ancestors(i)} - set(geo.targets)
          indices = {
              i for i in moved - explained if not (set(geo.ancestors(i)) & (moved - explained))
          } | set(geo.targets)
          out = []
          for index in sorted(indices)[:MAX_TRACKS]:
              boxes = geo.user.get(index) or geo.client.get(index)
              if boxes is None:
                  continue
              kinds = list(geo.targets.get(index, []))
              if index in moved:
                  kinds.append("moved")
              out.append(
                  ElementTrack(
                      element=geo.label(index),
                      animated_by=kinds,
                      units="viewBox" if index in geo.user else "px",
                      boxes=boxes,
                  )
              )
          return out