@phil65/demoreel 0.1.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/scene.js CHANGED
@@ -1,13 +1,96 @@
1
1
  import { easeAimed, makePath, mulberry32, sleep, travelDuration } from "./motion.js";
2
+ function isLocator(target) {
3
+ return typeof target === "object" && target !== null && typeof target.filter === "function";
4
+ }
5
+ function isPoint(target) {
6
+ return (typeof target === "object" &&
7
+ target !== null &&
8
+ !isLocator(target) &&
9
+ "x" in target &&
10
+ "y" in target &&
11
+ typeof target.x === "number" &&
12
+ typeof target.y === "number");
13
+ }
14
+ function isScrollDelta(target) {
15
+ return typeof target === "object" && target !== null && !isLocator(target) && "by" in target && typeof target.by === "number";
16
+ }
17
+ function formatMatch(v) {
18
+ return typeof v === "string" ? JSON.stringify(v) : String(v);
19
+ }
20
+ /**
21
+ * Resolves a Target to a Playwright Locator filtered to visible matches.
22
+ * Uses `.nth(nth)` when `nth` is specified on a TargetSpec, otherwise `.first()`.
23
+ */
24
+ export function resolveTarget(page, target) {
25
+ if (typeof target === "string") {
26
+ return page.locator(target).filter({ visible: true }).first();
27
+ }
28
+ if (isLocator(target)) {
29
+ return target.filter({ visible: true }).first();
30
+ }
31
+ let loc;
32
+ if ("role" in target) {
33
+ const opts = {};
34
+ if (target.name !== undefined)
35
+ opts.name = target.name;
36
+ if (target.exact !== undefined)
37
+ opts.exact = target.exact;
38
+ loc = Object.keys(opts).length > 0 ? page.getByRole(target.role, opts) : page.getByRole(target.role);
39
+ }
40
+ else if ("label" in target) {
41
+ loc = target.exact !== undefined ? page.getByLabel(target.label, { exact: target.exact }) : page.getByLabel(target.label);
42
+ }
43
+ else if ("placeholder" in target) {
44
+ loc = target.exact !== undefined ? page.getByPlaceholder(target.placeholder, { exact: target.exact }) : page.getByPlaceholder(target.placeholder);
45
+ }
46
+ else if ("text" in target) {
47
+ loc = target.exact !== undefined ? page.getByText(target.text, { exact: target.exact }) : page.getByText(target.text);
48
+ }
49
+ else if ("testId" in target) {
50
+ loc = page.getByTestId(target.testId);
51
+ }
52
+ else {
53
+ loc = page.locator(target.css);
54
+ }
55
+ const visible = loc.filter({ visible: true });
56
+ return target.nth !== undefined ? visible.nth(target.nth) : visible.first();
57
+ }
58
+ /** Readable default label for a Target, used in timeline.json, marks.json and error messages. */
59
+ export function describeTarget(target) {
60
+ if (typeof target === "string")
61
+ return target;
62
+ if (isLocator(target))
63
+ return String(target);
64
+ let base;
65
+ if ("role" in target) {
66
+ base = target.name !== undefined ? `${target.role} ${formatMatch(target.name)}` : target.role;
67
+ }
68
+ else if ("label" in target) {
69
+ base = `label ${formatMatch(target.label)}`;
70
+ }
71
+ else if ("placeholder" in target) {
72
+ base = `placeholder ${formatMatch(target.placeholder)}`;
73
+ }
74
+ else if ("text" in target) {
75
+ base = `text ${formatMatch(target.text)}`;
76
+ }
77
+ else if ("testId" in target) {
78
+ base = `testId ${formatMatch(target.testId)}`;
79
+ }
80
+ else {
81
+ base = `css ${target.css}`;
82
+ }
83
+ return target.nth !== undefined ? `${base} [${target.nth}]` : base;
84
+ }
2
85
  /** Attribute that marks a zoom anchor: actions inside it zoom onto the anchor's box. */
3
86
  export const ZOOM_ATTRIBUTE = "data-demoreel-zoom";
4
87
  export const DEFAULT_PACING = {
5
88
  speed: 1,
6
- beforeAction: 250,
7
- afterClick: 550,
8
- afterType: 700,
9
- typeDelay: 70,
10
- aimDwell: 140,
89
+ beforeAction: 160,
90
+ afterClick: 360,
91
+ afterType: 450,
92
+ typeDelay: 42,
93
+ aimDwell: 95,
11
94
  };
12
95
  /** Applies a `data-demoreel-zoom` value to an action's zoom setting. */
13
96
  export function applyZoomAnchor(zoom, anchor) {
@@ -33,6 +116,7 @@ export class Scene {
33
116
  rand;
34
117
  recorder = null;
35
118
  startRecording;
119
+ fast;
36
120
  pacing;
37
121
  /** The underlying Playwright page, for anything the Scene API does not cover. */
38
122
  page;
@@ -41,6 +125,7 @@ export class Scene {
41
125
  this.page = page;
42
126
  this.viewport = viewport;
43
127
  this.startRecording = startRecording;
128
+ this.fast = Boolean(opts.fast);
44
129
  this.rand = mulberry32(opts.seed ?? 42);
45
130
  this.pacing = { ...DEFAULT_PACING, ...opts.pacing };
46
131
  this.cursor = { x: viewport.width * 0.62, y: viewport.height * 0.72 };
@@ -69,20 +154,20 @@ export class Scene {
69
154
  });
70
155
  }
71
156
  ms(v) {
72
- return v / this.pacing.speed;
157
+ return this.fast ? 0 : v / this.pacing.speed;
73
158
  }
74
159
  // ---- helpers ------------------------------------------------------------------------------
75
160
  locate(target) {
76
161
  // Prefer the first *visible* match (docs sites often duplicate nav for mobile drawers).
77
- const loc = typeof target === "string" ? this.page.locator(target) : target;
78
- return loc.filter({ visible: true }).first();
162
+ return resolveTarget(this.page, target);
79
163
  }
80
- async inspect(loc) {
164
+ async inspect(loc, target) {
81
165
  try {
82
166
  await loc.waitFor({ state: "visible", timeout: 10_000 });
83
167
  }
84
168
  catch (e) {
85
- throw new Error(`target not visible on ${this.page.url()}: ${loc} (${e.message.split("\n")[0]})`);
169
+ const name = target !== undefined ? describeTarget(target) : String(loc);
170
+ throw new Error(`target not visible on ${this.page.url()}: ${name} (${e.message.split("\n")[0]})`);
86
171
  }
87
172
  const info = await loc.evaluate((el, attr) => {
88
173
  const r = el.getBoundingClientRect();
@@ -108,10 +193,10 @@ export class Scene {
108
193
  /** Scrolls the target into view (animated, recorded) if needed and returns its box. */
109
194
  async prepare(target) {
110
195
  const loc = this.locate(target);
111
- let info = await this.inspect(loc);
196
+ let info = await this.inspect(loc, target);
112
197
  if (!this.inViewport(info.box)) {
113
198
  await this.scrollTo(loc);
114
- info = await this.inspect(loc);
199
+ info = await this.inspect(loc, target);
115
200
  }
116
201
  return { loc, ...info };
117
202
  }
@@ -134,7 +219,7 @@ export class Scene {
134
219
  await this.ensureRecording();
135
220
  const from = { ...this.cursor };
136
221
  const dist = Math.hypot(to.x - from.x, to.y - from.y);
137
- const duration = travelDuration(dist, target ? Math.min(target.width, target.height) : 40, this.pacing.speed);
222
+ const duration = this.fast ? 0 : travelDuration(dist, target ? Math.min(target.width, target.height) : 40, this.pacing.speed);
138
223
  if (duration > 0) {
139
224
  const path = makePath(from, to, this.rand);
140
225
  const t0 = performance.now();
@@ -206,7 +291,7 @@ export class Scene {
206
291
  zoomBox: anchor?.box,
207
292
  zoom: applyZoomAnchor(opts.zoom ?? true, anchor),
208
293
  point: { ...this.cursor },
209
- label: opts.label ?? labelOf(target),
294
+ label: opts.label ?? describeTarget(target),
210
295
  });
211
296
  await sleep(this.ms(this.pacing.afterClick));
212
297
  if (opts.navigates || this.page.url() !== urlBefore) {
@@ -214,6 +299,107 @@ export class Scene {
214
299
  this.cursorBox = null;
215
300
  }
216
301
  }
302
+ /**
303
+ * Moves to a `<select>` element and picks an option via Playwright's `selectOption`.
304
+ */
305
+ async select(target, value, opts = {}) {
306
+ await this.ensureRecording();
307
+ await sleep(this.ms(this.pacing.beforeAction));
308
+ const { loc, box, kind, anchor } = await this.prepare(target);
309
+ const startMs = this.now();
310
+ await this.travel(this.aimPoint(box), box, kind);
311
+ const arriveMs = this.now();
312
+ await sleep(this.ms(this.pacing.aimDwell));
313
+ this.sample("click");
314
+ await loc.selectOption(value);
315
+ await sleep(this.ms(70));
316
+ this.sample("mouseup");
317
+ this.record({
318
+ kind: "select",
319
+ startMs,
320
+ arriveMs,
321
+ box,
322
+ zoomBox: anchor?.box,
323
+ zoom: applyZoomAnchor(opts.zoom ?? true, anchor),
324
+ point: { ...this.cursor },
325
+ label: opts.label ?? describeTarget(target),
326
+ text: typeof value === "string" ? value : (value.label ?? value.value ?? String(value.index ?? "")),
327
+ });
328
+ await sleep(this.ms(this.pacing.afterClick));
329
+ }
330
+ /**
331
+ * Drags from a target or viewport point to another target, point, or relative offset `{ dx, dy }`.
332
+ * Records continuous cursor telemetry so OpenScreen renders the cursor tracking the drag
333
+ * (useful for 3D canvases, sliders, and drag-and-drop).
334
+ */
335
+ async drag(from, to, opts = {}) {
336
+ await this.ensureRecording();
337
+ await sleep(this.ms(this.pacing.beforeAction));
338
+ let fromPoint;
339
+ let box;
340
+ let kind = "arrow";
341
+ let anchor;
342
+ if (isPoint(from)) {
343
+ fromPoint = from;
344
+ }
345
+ else {
346
+ const info = await this.prepare(from);
347
+ box = info.box;
348
+ kind = info.kind;
349
+ anchor = info.anchor;
350
+ fromPoint = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
351
+ }
352
+ let toPoint;
353
+ if (typeof to === "object" && to !== null && "dx" in to && "dy" in to) {
354
+ toPoint = { x: fromPoint.x + to.dx, y: fromPoint.y + to.dy };
355
+ }
356
+ else if (isPoint(to)) {
357
+ toPoint = to;
358
+ }
359
+ else {
360
+ const info = await this.prepare(to);
361
+ toPoint = this.aimPoint(info.box);
362
+ }
363
+ const startMs = this.now();
364
+ await this.travel(fromPoint, box ?? null, kind);
365
+ const arriveMs = this.now();
366
+ await sleep(this.ms(this.pacing.aimDwell));
367
+ await this.page.mouse.down();
368
+ this.sample("click");
369
+ const dist = Math.hypot(toPoint.x - fromPoint.x, toPoint.y - fromPoint.y);
370
+ const dur = this.ms(opts.duration ?? Math.min(1200, Math.max(350, dist * 2.5)));
371
+ if (dur > 0) {
372
+ const ease = (t) => (t < 0.5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2);
373
+ const t0 = performance.now();
374
+ for (;;) {
375
+ const t = Math.min(1, (performance.now() - t0) / dur);
376
+ const k = ease(t);
377
+ const p = { x: fromPoint.x + (toPoint.x - fromPoint.x) * k, y: fromPoint.y + (toPoint.y - fromPoint.y) * k };
378
+ this.cursor = p;
379
+ await this.page.mouse.move(p.x, p.y);
380
+ this.sample("move");
381
+ if (t >= 1)
382
+ break;
383
+ await sleep(16 - ((performance.now() - t0) % 16));
384
+ }
385
+ }
386
+ this.cursor = toPoint;
387
+ await this.page.mouse.move(toPoint.x, toPoint.y);
388
+ this.sample("move");
389
+ await this.page.mouse.up();
390
+ this.sample("mouseup");
391
+ this.record({
392
+ kind: "drag",
393
+ startMs,
394
+ arriveMs,
395
+ box,
396
+ zoomBox: anchor?.box,
397
+ zoom: applyZoomAnchor(opts.zoom ?? false, anchor),
398
+ point: fromPoint,
399
+ label: opts.label ?? (isPoint(from) ? undefined : describeTarget(from)),
400
+ });
401
+ await sleep(this.ms(this.pacing.afterClick));
402
+ }
217
403
  async type(target, text, opts = {}) {
218
404
  await this.ensureRecording();
219
405
  await sleep(this.ms(this.pacing.beforeAction));
@@ -227,10 +413,15 @@ export class Scene {
227
413
  if (opts.clear)
228
414
  await loc.fill("");
229
415
  await sleep(this.ms(180));
230
- for (const ch of text) {
231
- await this.page.keyboard.type(ch);
232
- const base = this.pacing.typeDelay * (0.55 + this.rand() * 0.9);
233
- await sleep(this.ms(ch === " " ? base * 1.6 : base));
416
+ if (this.fast) {
417
+ await this.page.keyboard.type(text);
418
+ }
419
+ else {
420
+ for (const ch of text) {
421
+ await this.page.keyboard.type(ch);
422
+ const base = this.pacing.typeDelay * (0.55 + this.rand() * 0.9);
423
+ await sleep(this.ms(ch === " " ? base * 1.6 : base));
424
+ }
234
425
  }
235
426
  this.record({
236
427
  kind: "type",
@@ -240,7 +431,7 @@ export class Scene {
240
431
  zoomBox: anchor?.box,
241
432
  zoom: applyZoomAnchor(opts.zoom ?? true, anchor),
242
433
  point,
243
- label: opts.label ?? labelOf(target),
434
+ label: opts.label ?? describeTarget(target),
244
435
  text,
245
436
  });
246
437
  await sleep(this.ms(this.pacing.afterType));
@@ -268,7 +459,7 @@ export class Scene {
268
459
  box,
269
460
  zoomBox: anchor?.box,
270
461
  zoom: applyZoomAnchor(opts.zoom ?? false, anchor),
271
- label: opts.label ?? labelOf(target),
462
+ label: opts.label ?? describeTarget(target),
272
463
  });
273
464
  }
274
465
  /**
@@ -278,7 +469,7 @@ export class Scene {
278
469
  async scroll(target, opts = {}) {
279
470
  await this.ensureRecording();
280
471
  await sleep(this.ms(this.pacing.beforeAction));
281
- if (typeof target === "object" && "by" in target) {
472
+ if (isScrollDelta(target)) {
282
473
  await this.animateScroll(null, target.by, opts.duration);
283
474
  }
284
475
  else {
@@ -313,18 +504,23 @@ export class Scene {
313
504
  const max = scroller.scrollHeight - scroller.clientHeight;
314
505
  const to = Math.max(0, Math.min(max, from + args.delta));
315
506
  const ease = (t) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2);
316
- await new Promise((resolve) => {
317
- const t0 = performance.now();
318
- const step = () => {
319
- const t = Math.min(1, (performance.now() - t0) / args.dur);
320
- scroller.scrollTop = from + (to - from) * ease(t);
321
- if (t < 1)
322
- requestAnimationFrame(step);
323
- else
324
- resolve();
325
- };
326
- requestAnimationFrame(step);
327
- });
507
+ if (args.dur <= 0) {
508
+ scroller.scrollTop = to;
509
+ }
510
+ else {
511
+ await new Promise((resolve) => {
512
+ const t0 = performance.now();
513
+ const step = () => {
514
+ const t = Math.min(1, (performance.now() - t0) / args.dur);
515
+ scroller.scrollTop = from + (to - from) * ease(t);
516
+ if (t < 1)
517
+ requestAnimationFrame(step);
518
+ else
519
+ resolve();
520
+ };
521
+ requestAnimationFrame(step);
522
+ });
523
+ }
328
524
  html.style.scrollBehavior = prevBehavior;
329
525
  };
330
526
  // <html> has no scrollable parent, so it resolves to the document scroller.
@@ -339,14 +535,14 @@ export class Scene {
339
535
  async moveTo(target, opts = {}) {
340
536
  await this.ensureRecording();
341
537
  const startMs = this.now();
342
- if (typeof target === "object" && "x" in target && "y" in target && !("locator" in target)) {
538
+ if (isPoint(target)) {
343
539
  await this.travel({ x: target.x, y: target.y });
344
540
  this.record({ kind: "move", startMs, arriveMs: this.now(), label: opts.label });
345
541
  return;
346
542
  }
347
543
  const { box, kind } = await this.prepare(target);
348
544
  await this.travel(this.aimPoint(box), box, kind);
349
- this.record({ kind: "move", startMs, arriveMs: this.now(), box, zoom: false, label: opts.label ?? labelOf(target) });
545
+ this.record({ kind: "move", startMs, arriveMs: this.now(), box, zoom: false, label: opts.label ?? describeTarget(target) });
350
546
  }
351
547
  /**
352
548
  * Explicit zoom onto an element for `duration` ms, overriding automatic zooms in that window.
@@ -365,9 +561,9 @@ export class Scene {
365
561
  endMs: startMs + duration,
366
562
  box,
367
563
  zoom: Object.keys(hint).length ? hint : true,
368
- label: opts.label ?? labelOf(target),
564
+ label: opts.label ?? describeTarget(target),
369
565
  });
370
- if (opts.wait)
566
+ if (opts.wait && !this.fast)
371
567
  await sleep(duration);
372
568
  }
373
569
  /** Draws an outline around an element in the page (recorded, so it zooms with the page). */
@@ -411,9 +607,9 @@ export class Scene {
411
607
  box,
412
608
  zoomBox: anchor?.box,
413
609
  zoom: applyZoomAnchor(opts.zoom ?? false, anchor),
414
- label: opts.label ?? labelOf(target),
610
+ label: opts.label ?? describeTarget(target),
415
611
  });
416
- if (opts.wait)
612
+ if (opts.wait && !this.fast)
417
613
  await sleep(duration);
418
614
  }
419
615
  async pause(ms) {
@@ -432,10 +628,7 @@ export class Scene {
432
628
  const duration = this.ms(opts.duration ?? 2600);
433
629
  const startMs = this.now();
434
630
  this.captions.push({ text, startMs, endMs: startMs + duration });
435
- if (opts.wait)
631
+ if (opts.wait && !this.fast)
436
632
  await sleep(duration);
437
633
  }
438
634
  }
439
- function labelOf(target) {
440
- return typeof target === "string" ? target : undefined;
441
- }
package/dist/types.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface Box {
9
9
  width: number;
10
10
  height: number;
11
11
  }
12
+ /** OpenScreen zoom depth tier: 1=1.25x, 2=1.5x, 3=1.8x, 4=2.2x, 5=3x, 6=5x. */
12
13
  export type Depth = 1 | 2 | 3 | 4 | 5 | 6;
13
14
  /** Mirrors OpenScreen's `CursorRecordingSample` (src/native/contracts.ts). */
14
15
  export interface CursorSample {
@@ -20,7 +21,7 @@ export interface CursorSample {
20
21
  interactionType?: "move" | "click" | "mouseup";
21
22
  assetId?: string | null;
22
23
  }
23
- export type ActionKind = "goto" | "click" | "type" | "hover" | "scroll" | "pause" | "caption" | "press" | "move" | "zoom" | "highlight";
24
+ export type ActionKind = "goto" | "click" | "select" | "drag" | "type" | "hover" | "scroll" | "pause" | "caption" | "press" | "move" | "zoom" | "highlight";
24
25
  /** One scripted step, as it actually happened during recording. */
25
26
  export interface ActionRecord {
26
27
  kind: ActionKind;
package/dist/zoom.d.ts CHANGED
@@ -31,6 +31,8 @@ export interface ZoomOptions {
31
31
  clampToVideo: boolean;
32
32
  }
33
33
  export declare const DEFAULT_ZOOM_OPTIONS: ZoomOptions;
34
+ /** Clamps and rounds any numeric depth to a valid OpenScreen integer tier (1..6). */
35
+ export declare function normalizeDepth(depth: number): Depth;
34
36
  /**
35
37
  * Moves a stage-normalised focus so that the view at `depth` stays inside the recording's
36
38
  * rectangle (OpenScreen itself only keeps it inside the stage, which shows wallpaper).
package/dist/zoom.js CHANGED
@@ -22,12 +22,18 @@ export const DEFAULT_ZOOM_OPTIONS = {
22
22
  auto: true,
23
23
  clampToVideo: true,
24
24
  };
25
+ /** Clamps and rounds any numeric depth to a valid OpenScreen integer tier (1..6). */
26
+ export function normalizeDepth(depth) {
27
+ if (!Number.isFinite(depth))
28
+ return 2;
29
+ return Math.min(6, Math.max(1, Math.round(depth)));
30
+ }
25
31
  /**
26
32
  * Moves a stage-normalised focus so that the view at `depth` stays inside the recording's
27
33
  * rectangle (OpenScreen itself only keeps it inside the stage, which shows wallpaper).
28
34
  */
29
35
  export function clampFocusToVideo(focus, depth, layout) {
30
- const half = 0.5 / DEPTH_SCALES[depth];
36
+ const half = 0.5 / DEPTH_SCALES[normalizeDepth(depth)];
31
37
  const v = layout.video;
32
38
  const c = (val, start, size) => size <= 2 * half ? start + size / 2 : Math.min(start + size - half, Math.max(start + half, val));
33
39
  return { cx: c(focus.cx, v.x, v.w), cy: c(focus.cy, v.y, v.h) };
@@ -38,12 +44,12 @@ export function clampFocusToVideo(focus, depth, layout) {
38
44
  * `videoFraction` is the share of the stage the recording covers (padding shrinks it).
39
45
  */
40
46
  export function depthForBox(box, vp, opts = DEFAULT_ZOOM_OPTIONS, hint, videoFraction = { w: 1, h: 1 }) {
41
- if (hint?.depth)
42
- return hint.depth;
47
+ if (hint?.depth !== undefined)
48
+ return normalizeDepth(hint.depth);
43
49
  const ctx = hint?.context ?? 1;
44
50
  const fitW = (vp.width * opts.fillWidth) / Math.max(1, box.width * ctx * videoFraction.w);
45
51
  const fitH = (vp.height * opts.fillHeight) / Math.max(1, box.height * ctx * videoFraction.h);
46
- const maxScale = Math.min(fitW, fitH, DEPTH_SCALES[opts.maxDepth]);
52
+ const maxScale = Math.min(fitW, fitH, DEPTH_SCALES[normalizeDepth(opts.maxDepth)]);
47
53
  let best = null;
48
54
  for (const d of [1, 2, 3, 4, 5, 6]) {
49
55
  if (DEPTH_SCALES[d] <= maxScale + 1e-6)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phil65/demoreel",
3
- "version": "0.1.0",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "description": "Scripted product demo videos (videos as code): Playwright scenarios with auto cursor + auto zoom, rendered headless by OpenScreen's exporter.",
6
6
  "keywords": [
@@ -57,7 +57,7 @@
57
57
  "prepack": "npm run build"
58
58
  },
59
59
  "peerDependencies": {
60
- "playwright": ">=1.45.0"
60
+ "playwright": ">=1.49.0"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "^26.6.6",