Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 135 additions & 18 deletions scripts/record-scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,113 @@ def overlay_script(badge: str, captions: bool = True) -> str:
{{ duration: {int(RIPPLE_SECONDS * 1000)}, easing: 'ease-out' }}).onfinish = () => ring.remove();
}};
// The box round what a line is about, following it if the page moves it, until cleared.
//
// Its label goes where it covers nothing of the page's: tried in turn above
// the box (at its left end, then its right), below it (the same), then
// beside its right edge and its left. A spot is taken only if it is wholly
// on the frame, below the page's sticky header, above the caption bar, out
// of the narrator's corner, off the box, and clear of every line of the
// page's own writing (a card's title above the box is the one it used to
// cover). The writing is looked for cheaply: the elements under a 5 by 3
// grid of points on the spot, and the rectangles of their own text. When no
// spot is clear, the same spots are tried again letting the label cover the
// boxed thing's own caption and nothing else: its `<label for>`, or the one
// short words (30 letters at most) beside it in a field of two ("Status" over a status pill in a
// grid of figures, "Characteristic" over its list). With no spot even then,
// the label is drawn inside the box's top left and the call says why, which
// refuses the take.
let callout = null;
const headerBand = {HEADER_BAND_JS.strip()};
const overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
const writingUnder = (spot, own) => {{
const seen = new Set();
for (let i = 0; i < 5; i++) for (let j = 0; j < 3; j++) {{
const x = spot.x + 1 + (spot.w - 2) * i / 4, y = spot.y + 1 + (spot.h - 2) * j / 2;
for (const node of document.elementsFromPoint(x, y)) {{
if (seen.has(node) || (node.id && node.id.startsWith('fs-scene'))) continue;
seen.add(node);
for (const text of node.childNodes) {{
if (text.nodeType !== Node.TEXT_NODE || !text.textContent.trim() || own.has(text)) continue;
const range = document.createRange();
range.selectNodeContents(text);
for (const r of range.getClientRects()) {{
const line = {{ x: r.left, y: r.top, w: r.width, h: r.height }};
if (overlaps(spot, line)) return text.textContent.trim().slice(0, 40);
}}
}}
}}
}}
return null;
}};
// The boxed thing's own caption, as text nodes: what labels it and nothing else.
const captionOf = (el) => {{
const words = new Set();
const take = (node) => {{
const walk = document.createTreeWalker(node, NodeFilter.SHOW_TEXT);
while (walk.nextNode()) words.add(walk.currentNode);
}};
for (const label of el.labels || []) take(label);
const field = el.parentElement;
if (field && field.children.length === 2) {{
const other = [...field.children].find((child) => child !== el);
if (!other.contains(el) && other.textContent.trim().length <= 30) take(other);
}}
return words;
}};
const labelSpot = (el, r, tab, pad) => {{
const w = tab.offsetWidth, h = tab.offsetHeight, gap = 4;
const box = {{ x: r.left - pad, y: r.top - pad, w: r.width + 2 * pad, h: r.height + 2 * pad }};
const band = headerBand(el);
const cap = document.getElementById('fs-scene-cap');
const capTop = cap && cap.offsetHeight ? cap.getBoundingClientRect().top : innerHeight;
const floor = Math.min(innerHeight - {CAPTION_HEIGHT}, capTop);
const corner = {{ x: innerWidth - {AVATAR_WIDTH}, y: innerHeight - {AVATAR_HEIGHT},
w: {AVATAR_WIDTH}, h: {AVATAR_HEIGHT} }};
const atLeft = Math.max(0, box.x), atRight = Math.min(innerWidth, box.x + box.w) - w;
const above = box.y - gap - h, below = box.y + box.h + gap, top = Math.max(box.y, band);
const tries = [
['above', atLeft, above], ['above, right', atRight, above],
['below', atLeft, below], ['below, right', atRight, below],
['right', box.x + box.w + gap, top], ['left', box.x - gap - w, top],
];
// Over its own caption the label sits on the box's edge, as a tab does:
// touching the thing's content, never on it.
const content = {{ x: r.left, y: r.top, w: r.width, h: r.height }};
const onEdge = [
['above', atLeft, r.top - h - 1], ['above, right', atRight, r.top - h - 1],
['below', atLeft, r.bottom + 1], ['below, right', atRight, r.bottom + 1],
];
const why = [];
for (const own of [new Set(), captionOf(el)]) {{
if (why.length && !own.size) break;
const off = own.size ? content : box;
for (const [side, x, y] of own.size ? onEdge : tries) {{
const spot = {{ x, y, w, h }};
const fault = x < 0 || y < 0 || x + w > innerWidth || y + h > innerHeight ? 'off the frame'
: y < band ? 'under the header'
: y + h > floor ? 'on the caption bar'
: overlaps(spot, corner) ? "in the narrator's corner"
: overlaps(spot, off) ? 'on the box'
: null;
const words = fault ? null : writingUnder(spot, own);
if (!fault && !words) return {{ side: own.size ? `${{side}}, over its own caption` : side,
dx: x - r.left, dy: y - r.top }};
if (!own.size) why.push(`${{side}}: ${{fault || `over "${{words}}"`}}`);
}}
}}
return {{ side: null, dx: Math.max(0, box.x) - r.left + 6, dy: Math.max(0, box.y) - r.top + 6,
why: why.join('; ') }};
}};
window.__sceneCallout = (el, label, style) => {{
window.__sceneCalloutClear();
const box = document.createElement('div');
box.id = 'fs-scene-callout';
if (style === 'spotlight') box.classList.add('fs-spot');
const tab = label ? document.createElement('div') : null;
if (tab) {{ tab.id = 'fs-scene-callout-label'; tab.textContent = label; }}
if (tab) {{ tab.id = 'fs-scene-callout-label'; tab.textContent = label; tab.style.left = '-9999px'; }}
document.documentElement.append(box, ...(tab ? [tab] : []));
const pad = {CALLOUT_PAD};
const spot = tab && el.isConnected ? labelSpot(el, el.getBoundingClientRect(), tab, pad) : null;
const place = () => {{
if (callout === null || callout.box !== box) return;
if (el.isConnected) {{
Expand All @@ -696,24 +793,23 @@ def overlay_script(badge: str, captions: bool = True) -> str:
box.style.top = (r.top - pad) + 'px';
box.style.width = (r.width + 2 * pad) + 'px';
box.style.height = (r.height + 2 * pad) + 'px';
if (tab) {{
// On the box's top left, outside it; below the box when the box is at
// the top; inside its top left when it fills the frame both ways.
const high = tab.offsetHeight;
const aboveAt = r.top - pad - high - 2;
const belowAt = r.bottom + pad + 2;
const floor = innerHeight - {CAPTION_HEIGHT};
tab.style.left = Math.max(0, r.left - pad) + 'px';
tab.style.top = (aboveAt >= 0 ? aboveAt
: belowAt + high <= floor ? belowAt : Math.max(0, r.top - pad) + 6) + 'px';
if (spot) {{
tab.style.left = (r.left + spot.dx) + 'px';
tab.style.top = (r.top + spot.dy) + 'px';
}}
}}
callout.frame = requestAnimationFrame(place);
}};
callout = {{ box, tab, frame: 0 }};
place();
const drawn = box.getBoundingClientRect();
return {{ x: drawn.left, y: drawn.top, w: drawn.width, h: drawn.height }};
const answer = {{ x: drawn.left, y: drawn.top, w: drawn.width, h: drawn.height }};
if (tab) {{
const t = tab.getBoundingClientRect();
answer.label = {{ x: t.left, y: t.top, w: t.width, h: t.height, side: spot ? spot.side : null,
why: spot && spot.why ? spot.why : null }};
}}
return answer;
}};
window.__sceneCalloutClear = () => {{
if (!callout) return;
Expand Down Expand Up @@ -1029,8 +1125,17 @@ def point_out(self, line: str, at: str, label: str, style: str) -> dict:
right = min(width, round(drawn["x"] + drawn["w"]))
bottom = min(height, round(drawn["y"] + drawn["h"]))
box = {"x": left, "y": top, "w": max(0, right - left), "h": max(0, bottom - top)}
print(f" box {box}" + (f" {label!r}" if label else ""), flush=True)
return {"box": box, "label": label, "selector": at, "style": style}
pointer = {"box": box, "label": label, "selector": at, "style": style}
if label:
tab = drawn["label"]
if tab["side"] is None:
raise SystemExit(f"step {self.step_number}: the label {label!r} on the box round `at = {at!r}` has"
f" no clear spot outside the box ({tab['why']}); the take is refused rather than"
" filmed with the label over the page's own writing")
pointer["label_box"] = {key: round(tab[key]) for key in ("x", "y", "w", "h")}
print(f" box {box}" + (f" {label!r} {pointer['label_box']} {drawn['label']['side']}"
if label else ""), flush=True)
return pointer

def hold(self, seconds: float) -> None:
self.note(f"hold {seconds}s", "hold")
Expand Down Expand Up @@ -1283,8 +1388,9 @@ def render_slide(browser, card: str, path: Path, width: int, height: int) -> Non
#: recorder's own layer); when the element's top is inside it, the element
#: is scrolled to sit `headroom` below the band. `scroll-margin-top` does the
#: scrolling so that a thing inside a scrolling panel is moved too.
CLEAR_OF_HEADER_SCRIPT = """
(node, headroom) => {
#: The band itself: how far down the window the stuck things reach over `node`'s width.
HEADER_BAND_JS = """
(node) => {
const box = node.getBoundingClientRect();
const stuck = [];
for (const el of document.body.querySelectorAll('*')) {
Expand All @@ -1304,6 +1410,15 @@ def render_slide(browser, card: str, path: Path, width: int, height: int) -> Non
if (r.top <= band + 2 && r.bottom > band) { band = r.bottom; grew = true; }
}
}
return band;
}
"""

#: Bring the element out from under the band.
CLEAR_OF_HEADER_SCRIPT = """
(node, headroom) => {
const box = node.getBoundingClientRect();
const band = (""" + HEADER_BAND_JS.strip() + """)(node);
if (!band || box.top >= band + headroom) return band;
const was = node.style.scrollMarginTop;
node.style.scrollMarginTop = (band + headroom) + 'px';
Expand Down Expand Up @@ -1599,7 +1714,8 @@ def readme(scene: dict, facts: dict) -> str:
"- `marks.json` — the seconds every step landed at; the frame's size and the GIF's; on every"
" click, `x` and `y`, the pixel of the mp4's frame the click landed on, and on every click, fill"
" and choose `cursor_from` and `ripple_at`; on a line that points at something, `box`"
" (`x`, `y`, `w`, `h` in the mp4's pixels), `label`, `selector` and `style`",
" (`x`, `y`, `w`, `h` in the mp4's pixels), `label`, `selector` and `style`, and with a label"
" `label_box`, where the label sat",
"",
]
return "\n".join(lines)
Expand Down Expand Up @@ -1674,7 +1790,8 @@ def marks_document(scene: dict, made: dict, gif: Path, gif_seconds_taken: float)
`ripple_at` (the second its ripple started, as the press was sent), and a
`fill` or `choose` also `x`, `y` (where it pointed) and a `fill`
`typed_seconds`; a `say` with `at` carries `box` (`{x, y, w, h}`, integers,
in frame pixels, clipped to the frame), `label`, `selector` and `style`.
in frame pixels, clipped to the frame), `label`, `selector` and `style`,
and, when it has a label, `label_box` (`{x, y, w, h}`), where the label sat.
"""
return {
"frame": {"width": int(scene["width"]), "height": int(scene["height"])},
Expand Down
13 changes: 10 additions & 3 deletions scripts/scenes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ cursor presses the list and the value changes.
A `say` with `at` (a selector, as `click` takes) draws a box round that thing
as the caption appears, for the line's hold, and through any `mark` straight
after it, so those stills show the box; `label` puts a few words on a tab
at the box's top left (below it when the box is at the top of the frame,
inside it when the box fills the frame); `style = "spotlight"` dims the rest
of the frame instead. A thing off the frame, or under the page's sticky
just outside the box, never over the page's own writing: above the box at
its left end, else above at its right end, else below it (left, then right),
else beside its right edge, else beside its left. A spot under the sticky
header, on the caption bar, in the narrator's corner, off the frame, on the
box, or over any text of the page (a card's title above the box) is passed
over. Only when no spot is clear may the label cover the boxed thing's own
caption (its `<label>`, or the short word over a value in a grid) and
nothing else; a label with no clear spot refuses the take, naming the step, so keep
labels short. `marks.json` says where it sat (`label_box`). `style =
"spotlight"` dims the rest of the frame instead. A thing off the frame, or under the page's sticky
header or the caption bar, or reaching into the narrator's corner at the
bottom right (240 by 200), is scrolled clear first; a thing not on the page
refuses the take, naming the step. Point `at` something smaller than the
Expand Down
Loading
Loading