@zumer/snapdom-plugins 4.4.0 → 4.4.1-dev.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/README.md +6 -3
- package/package.json +1 -1
- package/redact-inputs.d.ts +1 -1
- package/stream/README.md +6 -3
- package/stream/src/stream.js +208 -23
- package/stream.d.ts +3 -0
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# @zumer/snapdom-plugins
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@zumer/snapdom-plugins)
|
|
4
|
+
[](https://www.npmjs.com/package/@zumer/snapdom)
|
|
5
|
+
|
|
3
6
|
Official plugins for [SnapDOM](https://github.com/zumerlab/snapdom), a high-performance DOM engine
|
|
4
7
|
that exports a rendered element as PNG, SVG or canvas in the browser. Core exports images and
|
|
5
8
|
canvases; this package adds HTML, structured context, element maps, PDF, editable vectors, ASCII,
|
|
@@ -178,9 +181,9 @@ The built-in mask without custom selectors permits reuse of unchanged captures.
|
|
|
178
181
|
masks and nonempty `selector`, `blocks` or `attributes` rules run on every capture. Changed
|
|
179
182
|
content runs the full pipeline so no field skips redaction.
|
|
180
183
|
|
|
181
|
-
Nonempty `blocks` or `attributes` rules add a final `beforeRender` pass
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
Nonempty `blocks` or `attributes` rules add a final `beforeRender` pass, which makes a capture
|
|
185
|
+
requested with `engine: 'html-in-canvas'` use the SVG renderer. Field-only redaction does not
|
|
186
|
+
add this pass.
|
|
184
187
|
|
|
185
188
|
`agent-map` and `context-export` omit sensitive values and mask other typed values in their
|
|
186
189
|
structured state fields. Ordinary page text and labels remain. An attached image still
|
package/package.json
CHANGED
package/redact-inputs.d.ts
CHANGED
|
@@ -38,6 +38,6 @@ export interface RedactInputsOptions {
|
|
|
38
38
|
* Optional blocks exclude whole subtrees; attribute rules remove named attributes. The live DOM is unchanged.
|
|
39
39
|
* These rules do not search captured text or images for sensitive content.
|
|
40
40
|
* The default mask without selectors permits unchanged-repeat memoization; custom masks and selectors run every capture.
|
|
41
|
-
* Nonempty blocks/attribute rules add a beforeRender pass, so
|
|
41
|
+
* Nonempty blocks/attribute rules add a beforeRender pass, so html-in-canvas captures use SVG.
|
|
42
42
|
*/
|
|
43
43
|
export declare function redactInputs(options?: RedactInputsOptions): SnapdomPlugin;
|
package/stream/README.md
CHANGED
|
@@ -21,8 +21,11 @@ live.stop();
|
|
|
21
21
|
|
|
22
22
|
The first frame is a full capture. After that, every change (mutations, typing, focus, hover,
|
|
23
23
|
running animations) is traced to the nearest box that can be repainted on its own, and only
|
|
24
|
-
that subtree is captured and drawn into its rect. A
|
|
25
|
-
|
|
24
|
+
that subtree is captured and drawn into its rect. A change in a box that cannot (a value on a
|
|
25
|
+
translucent card, a row under a badge) repaints a window of the element instead: a `clip`
|
|
26
|
+
capture of that box, with everything that paints there. The pointer repaints only what a
|
|
27
|
+
`:hover` rule restyles. A frame that cannot be patched exactly is a full frame, and
|
|
28
|
+
`frame.reason` says why (`'root resized'`, `'no host for <div.x>'`, ...).
|
|
26
29
|
A keyframe in idle time heals anything the change tracking could not see, at most every 2 s
|
|
27
30
|
and never more than 2% of the main thread.
|
|
28
31
|
|
|
@@ -43,7 +46,7 @@ All [capture options](https://snapdom.dev/) apply (`scale`, `dpr`, `embedFonts`,
|
|
|
43
46
|
|
|
44
47
|
| option | default | |
|
|
45
48
|
|---|---|---|
|
|
46
|
-
| `onFrame(frame)` | | called after each repaint: `{ full, reason, rects, ms, canvas }` |
|
|
49
|
+
| `onFrame(frame)` | | called after each repaint: `{ full, reason, rects, ms, busyMs, canvas }`. `ms` is wall time; `busyMs` is the part the stream held the main thread, the one to budget on |
|
|
47
50
|
| `fps` | every animation frame | upper bound on repaints per second |
|
|
48
51
|
| `patches` | `true` | `false` repaints full frames only, for comparison |
|
|
49
52
|
| `animations` | `true` | `false` stops following running animations and transitions: an animated box keeps the pixels it was last drawn with until a patch over it, a full frame or the keyframe. For a still of the page, where a spinner's angle does not matter but its full frames would |
|
package/stream/src/stream.js
CHANGED
|
@@ -10,8 +10,11 @@
|
|
|
10
10
|
* was restyled hands the change to the enclosing box that did not; an absolutely positioned
|
|
11
11
|
* box over a solid or clear backdrop may move and resize itself, since it pushes nothing.
|
|
12
12
|
* Only that subtree is captured, laid out at its subpixel phase, and drawn over its backdrop
|
|
13
|
-
* or where it is fully opaque.
|
|
14
|
-
*
|
|
13
|
+
* or where it is fully opaque. A change with no such box (a value on a glass card, a row
|
|
14
|
+
* under a badge) repaints a window of the root: a `clip` capture of the box the change stayed
|
|
15
|
+
* inside, which has everything that paints there. Anything this cannot prove is a full
|
|
16
|
+
* frame, with the reason on the frame. The pointer counts only through `:hover` rules: a move
|
|
17
|
+
* repaints the elements whose match it changed.
|
|
15
18
|
*
|
|
16
19
|
* Measured 2026-10-23, 1000px grid, one card's text changed per frame, median ms per frame
|
|
17
20
|
* (full / patch): 400 cards Chromium 11.7 / 4.2, Firefox 28 / 4, WebKit 20 / 4; 2000 cards
|
|
@@ -34,6 +37,13 @@ const WEBKIT = typeof navigator !== 'undefined' && (/ipad|iphone|ipod/i.test(nav
|
|
|
34
37
|
(/applewebkit/i.test(navigator.userAgent) && !/chrome|chromium|crios|edg|opr|android/i.test(navigator.userAgent)))
|
|
35
38
|
|
|
36
39
|
const EVENTS = ['input', 'change', 'focusin', 'focusout', 'pointerover', 'pointerout', 'scroll', 'toggle']
|
|
40
|
+
const HOVER_EVENTS = new Set(['pointerover', 'pointerout'])
|
|
41
|
+
/** :hover selectors x nodes one hover query may cost. A query is linear in both: 200 x 1400
|
|
42
|
+
* took 1.5 to 1.75 ms on all three engines, 2000 x 1400 took 13 to 30. Past it the pointer
|
|
43
|
+
* repaints whatever it crosses, as it did before the query. */
|
|
44
|
+
const HOVER_BUDGET = 300_000
|
|
45
|
+
/** Cut from a :hover selector, so it matches the box the pseudo-element hangs from. */
|
|
46
|
+
const PSEUDO_ELEMENT = /::?(?:before|after|first-line|first-letter)\b|::[\w-]+(?:\([^)]*\))?/g
|
|
37
47
|
/** A patch larger than this share of the frame costs about what a full frame does. */
|
|
38
48
|
const MAX_PATCH_AREA = 0.5
|
|
39
49
|
/** Self-healing keyframe: anything the heuristics missed lasts at most this long, or 50
|
|
@@ -57,10 +67,48 @@ function hasEffect(cs) {
|
|
|
57
67
|
cs.clipPath !== 'none' || (cs.backdropFilter && cs.backdropFilter !== 'none')
|
|
58
68
|
}
|
|
59
69
|
|
|
70
|
+
/** A filter that carries a change past the box it happened in. */
|
|
71
|
+
function spreads(cs) {
|
|
72
|
+
return /blur|drop-shadow|url\(/.test(cs.filter)
|
|
73
|
+
}
|
|
74
|
+
|
|
60
75
|
function clips(cs) {
|
|
61
76
|
return cs.overflowX !== 'visible' || cs.overflowY !== 'visible'
|
|
62
77
|
}
|
|
63
78
|
|
|
79
|
+
/** Visible overflow paints outside the box. */
|
|
80
|
+
function overflows(el, cs) {
|
|
81
|
+
return !clips(cs) && (el.scrollWidth > el.clientWidth + 1 || el.scrollHeight > el.clientHeight + 1)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A shadow or outline paints outside the box. */
|
|
85
|
+
function outerInk(cs) {
|
|
86
|
+
return cs.boxShadow !== 'none' || (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Every selector that names :hover; null when a sheet cannot be read (cross origin) or a
|
|
90
|
+
* nested `&` rule cannot be queried, and then any hover may restyle. Pseudo-elements are
|
|
91
|
+
* cut: `.dot:hover::after` repaints with `.dot`. */
|
|
92
|
+
function hoverSelectors(root) {
|
|
93
|
+
const list = []
|
|
94
|
+
const walk = (rules) => {
|
|
95
|
+
for (const rule of rules) {
|
|
96
|
+
const sel = rule.selectorText
|
|
97
|
+
if (sel?.includes(':hover')) {
|
|
98
|
+
if (sel.includes('&')) throw new Error('nested')
|
|
99
|
+
list.push(sel.replace(PSEUDO_ELEMENT, ''))
|
|
100
|
+
}
|
|
101
|
+
if (rule.cssRules) walk(rule.cssRules)
|
|
102
|
+
if (rule.styleSheet) walk(rule.styleSheet.cssRules)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const scope = root.getRootNode()
|
|
106
|
+
try {
|
|
107
|
+
for (const sheet of [...scope.styleSheets, ...(scope.adoptedStyleSheets || [])]) walk(sheet.cssRules)
|
|
108
|
+
} catch { return null }
|
|
109
|
+
return list
|
|
110
|
+
}
|
|
111
|
+
|
|
64
112
|
function inside(a, b) {
|
|
65
113
|
return a.left >= b.left - 0.5 && a.top >= b.top - 0.5 && a.right <= b.right + 0.5 && a.bottom <= b.bottom + 0.5
|
|
66
114
|
}
|
|
@@ -122,6 +170,26 @@ function describe(node) {
|
|
|
122
170
|
return `<${el.tagName.toLowerCase()}${el.id ? '#' + el.id : ''}${el.classList?.length ? '.' + [...el.classList].join('.') : ''}>`
|
|
123
171
|
}
|
|
124
172
|
|
|
173
|
+
/** A task of its own, with rendering's turn before it: a message, as the core's time slicing
|
|
174
|
+
* yields (scheduler.yield() resumes ahead of rendering, a nested setTimeout clamps to 4 ms). */
|
|
175
|
+
function nextTask() {
|
|
176
|
+
return new Promise((resolve) => {
|
|
177
|
+
const channel = new MessageChannel()
|
|
178
|
+
channel.port1.onmessage = () => resolve()
|
|
179
|
+
channel.port2.postMessage(0)
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** How long the [start, end] spans cover, overlaps counted once. */
|
|
184
|
+
function covered(spans) {
|
|
185
|
+
let sum = 0
|
|
186
|
+
let end = -Infinity
|
|
187
|
+
for (const [s, e] of spans.sort((a, b) => a[0] - b[0])) {
|
|
188
|
+
if (e > end) { sum += e - Math.max(s, end); end = e }
|
|
189
|
+
}
|
|
190
|
+
return sum
|
|
191
|
+
}
|
|
192
|
+
|
|
125
193
|
async function decode(url) {
|
|
126
194
|
const img = new Image()
|
|
127
195
|
img.decoding = 'sync'
|
|
@@ -134,13 +202,18 @@ async function decode(url) {
|
|
|
134
202
|
* Stream an element into a canvas, repainting only the boxes that changed.
|
|
135
203
|
* @param {Element} element
|
|
136
204
|
* @param {object} [options] - capture options (scale, dpr, embedFonts, ...), plus:
|
|
137
|
-
* @param {(frame: {full: boolean, reason: string|null, rects: {x:number,y:number,width:number,height:number}[], ms: number, canvas: HTMLCanvasElement}) => void} [options.onFrame]
|
|
205
|
+
* @param {(frame: {full: boolean, reason: string|null, rects: {x:number,y:number,width:number,height:number}[], ms: number, busyMs: number, canvas: HTMLCanvasElement}) => void} [options.onFrame]
|
|
206
|
+
* `ms` is the frame's wall time; `busyMs` the part its own work held the main thread, without
|
|
207
|
+
* the turns between windows or the decodes (a full frame's capture counts whole, its sliced
|
|
208
|
+
* pauses included). Zumly's board, 17 windows a second at 4x CPU: 584 to 778 ms of wall
|
|
209
|
+
* time, which turned its stream off on a budget of blocked time.
|
|
138
210
|
* @param {number} [options.fps] - cap on frames per second; default every animation frame.
|
|
139
211
|
* @param {boolean} [options.patches=true] - false repaints full frames only (the baseline).
|
|
140
212
|
* @param {boolean} [options.animations=true] - false stops following running animations and
|
|
141
213
|
* transitions: an animated box keeps its last drawn pixels until a patch, a full frame or the
|
|
142
|
-
* keyframe repaints it. A transparent spinner has no host
|
|
143
|
-
* frame (Zumly views at dpr 2, fps 5: 109 ms a frame, against
|
|
214
|
+
* keyframe repaints it. A transparent spinner has no host and turns its own box, so by default
|
|
215
|
+
* nearly every tick is a full frame (Zumly views at dpr 2, fps 5: 109 ms a frame, against
|
|
216
|
+
* 9-13 ms patches without it).
|
|
144
217
|
* @returns {{canvas: HTMLCanvasElement, stop: () => void, ready: Promise<void>, stats: object}}
|
|
145
218
|
*/
|
|
146
219
|
export function stream(element, options = {}) {
|
|
@@ -154,8 +227,15 @@ export function stream(element, options = {}) {
|
|
|
154
227
|
const rects = new WeakMap() // element -> rect at the last paint that covered it
|
|
155
228
|
const backdrops = new Map() // this frame's hosts that draw over a solid colour
|
|
156
229
|
const movedFrom = new Map() // this frame's out-of-flow hosts that moved -> their old rect
|
|
230
|
+
const regions = new Set() // this frame's hosts repainted as a window of the root
|
|
231
|
+
let windows = Promise.resolve() // the turn of the next window capture
|
|
232
|
+
let spans = [] // this frame's own work on the main thread, [start, end]
|
|
157
233
|
const stats = { full: 0, patch: 0, skipped: 0, lastMs: 0 }
|
|
158
234
|
let floaters = []
|
|
235
|
+
let glass = [] // backdrop-filter boxes: a change under one shows through its blur
|
|
236
|
+
let hoverList = null // the :hover selectors at the last full frame, null if unreadable
|
|
237
|
+
let hovered = new Set() // what they matched at the last look
|
|
238
|
+
let hoverMoved = false
|
|
159
239
|
let forceFull = true
|
|
160
240
|
let lastKeyframe = 0
|
|
161
241
|
let keyframeDue = false // set from idle time once the keyframe interval has passed
|
|
@@ -171,6 +251,13 @@ export function stream(element, options = {}) {
|
|
|
171
251
|
const observer = new MutationObserver(records => netTargets(records, dirty, restyled))
|
|
172
252
|
observer.observe(element, { subtree: true, childList: true, attributes: true, characterData: true, attributeOldValue: true, characterDataOldValue: true })
|
|
173
253
|
const onEvent = (e) => {
|
|
254
|
+
// A pointer that crosses boxes no :hover rule matches changes no pixel. Moving over the
|
|
255
|
+
// gaps of a 48-tile board made its root dirty, and every move a 300 ms full frame (Zumly,
|
|
256
|
+
// dpr 2). A shadow tree's event arrives at its host, and its sheets were not read.
|
|
257
|
+
if (hoverList !== null && HOVER_EVENTS.has(e.type) && e.composedPath()[0] === e.target) {
|
|
258
|
+
hoverMoved = true
|
|
259
|
+
return
|
|
260
|
+
}
|
|
174
261
|
dirty.add(e.target)
|
|
175
262
|
if (e.relatedTarget && element.contains(e.relatedTarget)) dirty.add(e.relatedTarget)
|
|
176
263
|
}
|
|
@@ -207,14 +294,24 @@ export function stream(element, options = {}) {
|
|
|
207
294
|
function snapshotLayout() {
|
|
208
295
|
rootRect = element.getBoundingClientRect()
|
|
209
296
|
floaters = []
|
|
297
|
+
glass = []
|
|
210
298
|
for (const el of element.querySelectorAll('*')) {
|
|
211
299
|
rects.set(el, rectOf(el))
|
|
212
300
|
const cs = getComputedStyle(el)
|
|
213
301
|
if (cs.position === 'absolute' || cs.position === 'fixed' || cs.position === 'sticky' ||
|
|
214
302
|
(cs.position === 'relative' && cs.zIndex !== 'auto') || cs.transform !== 'none') floaters.push(el)
|
|
303
|
+
if (cs.backdropFilter && cs.backdropFilter !== 'none') glass.push(el)
|
|
215
304
|
}
|
|
216
305
|
}
|
|
217
306
|
|
|
307
|
+
/** The elements the :hover rules match now, the root included. */
|
|
308
|
+
function hoverMatches() {
|
|
309
|
+
if (!hoverList) return new Set()
|
|
310
|
+
const out = new Set(element.querySelectorAll(hoverList))
|
|
311
|
+
if (element.matches(hoverList)) out.add(element)
|
|
312
|
+
return out
|
|
313
|
+
}
|
|
314
|
+
|
|
218
315
|
/** The box a change to `node` can be repainted through, or null for a full frame. */
|
|
219
316
|
function hostFor(node) {
|
|
220
317
|
let el = node?.nodeType === 1 ? node : node?.parentElement
|
|
@@ -227,8 +324,8 @@ export function stream(element, options = {}) {
|
|
|
227
324
|
for (let i = start; i < chain.length; i++) {
|
|
228
325
|
const cs = styles[i]
|
|
229
326
|
if (!opaqueColor(cs.backgroundColor)) continue
|
|
230
|
-
//
|
|
231
|
-
if (
|
|
327
|
+
// Over pixels the patch does not repaint.
|
|
328
|
+
if (overflows(chain[i], cs)) continue
|
|
232
329
|
// A box a clipping ancestor cuts, past its edges or into a rounded corner, would be
|
|
233
330
|
// drawn whole: a row half out of a 40px scroller painted its hidden half (19200 px
|
|
234
331
|
// off). The host is the box itself, so the test is on its rect, not on the changed
|
|
@@ -242,14 +339,40 @@ export function stream(element, options = {}) {
|
|
|
242
339
|
return null
|
|
243
340
|
}
|
|
244
341
|
|
|
342
|
+
/** The box a change to `node` stayed inside, to repaint as a window of the root, or null.
|
|
343
|
+
* The window has everything that paints in it, so the box needs no backdrop and may sit
|
|
344
|
+
* under positioned boxes; it must keep its rect, paint nothing new outside itself, and
|
|
345
|
+
* have no blur spread the change past it: a blurring ancestor, or a glass box over it. */
|
|
346
|
+
function regionFor(node) {
|
|
347
|
+
let el = node?.nodeType === 1 ? node : node?.parentElement
|
|
348
|
+
for (let a = el; a; a = a === element ? null : a.parentElement) if (spreads(getComputedStyle(a))) return null
|
|
349
|
+
for (; el && el !== element; el = el.parentElement) {
|
|
350
|
+
const r = rectOf(el)
|
|
351
|
+
const cs = getComputedStyle(el)
|
|
352
|
+
if (!r.width || !r.height || !sameRect(rects.get(el) || {}, r) || overflows(el, cs)) continue
|
|
353
|
+
if (restyled.has(el) && outerInk(cs)) continue
|
|
354
|
+
for (const g of glass) if (!g.contains(el) && !el.contains(g) && intersects(rectOf(g), r)) return null
|
|
355
|
+
return el
|
|
356
|
+
}
|
|
357
|
+
return null
|
|
358
|
+
}
|
|
359
|
+
|
|
245
360
|
async function full() {
|
|
246
361
|
const t0 = performance.now()
|
|
362
|
+
// Read before the capture, at every full frame. A pointer that moves while it runs is then
|
|
363
|
+
// diffed next tick; read after it, the first capture left the events nothing to go by, and
|
|
364
|
+
// a pointer over the Zumly board's gaps made the root dirty (a 400 ms full frame, 4 of 4).
|
|
365
|
+
// A sheet added later counts from the next keyframe.
|
|
366
|
+
const sels = hoverSelectors(element)
|
|
367
|
+
hoverList = sels && sels.length * (element.getElementsByTagName('*').length + 1) <= HOVER_BUDGET ? sels.join(',') : null
|
|
368
|
+
try { hovered = hoverMatches() } catch { hoverList = null }
|
|
247
369
|
// A full frame is the one long task of a stream; 'auto' lets a long clone yield (#503).
|
|
248
370
|
// Lab, 1000 cards under constant edits: worst frame gap 67 -> 33 ms. Under edits on
|
|
249
371
|
// every frame a big tree tears and redoes in one task, which only the interval helps.
|
|
250
|
-
const res = await capture(element, { fast: 'auto', ...opts })
|
|
372
|
+
const res = await held(() => capture(element, { fast: 'auto', ...opts }), t0)
|
|
251
373
|
const { vbW, vbH, contentX = 0, contentY = 0 } = res.meta
|
|
252
374
|
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
375
|
+
const drawn = performance.now()
|
|
253
376
|
const w = Math.round(vbW * k), h = Math.round(vbH * k)
|
|
254
377
|
if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h }
|
|
255
378
|
ctx.clearRect(0, 0, w, h)
|
|
@@ -260,6 +383,7 @@ export function stream(element, options = {}) {
|
|
|
260
383
|
lastKeyframe = performance.now()
|
|
261
384
|
keyframeDue = false
|
|
262
385
|
fullMs = lastKeyframe - t0
|
|
386
|
+
spans.push([drawn, lastKeyframe])
|
|
263
387
|
stats.full++
|
|
264
388
|
return [{ x: 0, y: 0, width: w, height: h }]
|
|
265
389
|
}
|
|
@@ -271,8 +395,7 @@ export function stream(element, options = {}) {
|
|
|
271
395
|
* other box on the whole-pixel rect around the host, or around `old`, where a moved host
|
|
272
396
|
* was. */
|
|
273
397
|
function backdropOf(host, old) {
|
|
274
|
-
|
|
275
|
-
if (cs.boxShadow !== 'none' || (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0)) return null
|
|
398
|
+
if (outerInk(getComputedStyle(host))) return null
|
|
276
399
|
let color = null
|
|
277
400
|
for (let a = host.parentElement; a && !color; a = a === element ? null : a.parentElement) {
|
|
278
401
|
const s = getComputedStyle(a)
|
|
@@ -308,11 +431,12 @@ export function stream(element, options = {}) {
|
|
|
308
431
|
const x0 = Math.floor(left + 0.001), y0 = Math.floor(top + 0.001)
|
|
309
432
|
const phase = { x: (left - x0) / k, y: (top - y0) / k }
|
|
310
433
|
// Always passed, even at phase zero: it also lays the root out at its exact width.
|
|
311
|
-
const res = await capture(host, { ...opts, __subpixel: phase })
|
|
434
|
+
const res = await held(() => capture(host, { ...opts, __subpixel: phase }))
|
|
312
435
|
const { vbW, vbH, contentX = 0, contentY = 0 } = res.meta
|
|
313
436
|
const x = Math.round(x0 - contentX * k), y = Math.round(y0 - contentY * k)
|
|
314
437
|
const w = Math.round(vbW * k), h = Math.round(vbH * k)
|
|
315
438
|
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
439
|
+
const drawn = performance.now()
|
|
316
440
|
const layer = document.createElement('canvas')
|
|
317
441
|
layer.width = w
|
|
318
442
|
layer.height = h
|
|
@@ -325,7 +449,8 @@ export function stream(element, options = {}) {
|
|
|
325
449
|
lctx.fillRect(0, 0, w, h)
|
|
326
450
|
}
|
|
327
451
|
lctx.drawImage(src, 0, 0, w, h)
|
|
328
|
-
|
|
452
|
+
spans.push([drawn, performance.now()])
|
|
453
|
+
return { layer, x, y, w, h, replace: true, host, drawnAt: r }
|
|
329
454
|
}
|
|
330
455
|
// Otherwise keep only the pixels the box covers fully. Inside an opaque box that is
|
|
331
456
|
// everything; what is left are its antialiased edges and rounded corners, which did not
|
|
@@ -337,14 +462,58 @@ export function stream(element, options = {}) {
|
|
|
337
462
|
const px = img.data
|
|
338
463
|
for (let i = 3; i < px.length; i += 4) if (px[i] < 255) px[i] = 0
|
|
339
464
|
lctx.putImageData(img, 0, 0)
|
|
465
|
+
spans.push([drawn, performance.now()])
|
|
340
466
|
return { layer, x, y, w, h, host, drawnAt: r }
|
|
341
467
|
}
|
|
342
468
|
|
|
469
|
+
/** Capture the root clipped to `host`'s box on whole canvas pixels: the full frame's
|
|
470
|
+
* pixels for that window. A value on a glass tile of the Zumly board (48 tiles, dpr 2):
|
|
471
|
+
* 9 ms against 323 for the full frame, identical to it. A window over the tile's rounded
|
|
472
|
+
* corners differed on the antialiased arc, by up to 28 levels on 45 pixels (cause not
|
|
473
|
+
* found). Sixteen windows at once cost 101 ms; one window per row of them cost 149, since a
|
|
474
|
+
* row keeps every tile in it. */
|
|
475
|
+
async function renderRegion(host) {
|
|
476
|
+
// One window per task. Captures of one element run one after another in the core, and
|
|
477
|
+
// each began in the task the last one ended in: the board's 17 windows a second were one
|
|
478
|
+
// task of up to 95 ms, and 9.5% of the frames at rest were dropped.
|
|
479
|
+
const turn = windows.then(async () => {
|
|
480
|
+
// stop() between turns drops the rest: Zumly stops the stream as a zoom starts, and the
|
|
481
|
+
// queued windows went on capturing through the motion (2.7 to 5.3% frames dropped).
|
|
482
|
+
if (stopped) return null
|
|
483
|
+
const drawnAt = rectOf(host)
|
|
484
|
+
const r = pixelRect(drawnAt)
|
|
485
|
+
return { drawnAt, r, res: await held(() => capture(element, { ...opts, clip: { x: rootRect.left + scrollX + r.left, y: rootRect.top + scrollY + r.top, width: r.right - r.left, height: r.bottom - r.top } })) }
|
|
486
|
+
})
|
|
487
|
+
windows = turn.then(nextTask, nextTask)
|
|
488
|
+
const shot = await turn
|
|
489
|
+
if (!shot) return null
|
|
490
|
+
const { drawnAt, r, res } = shot
|
|
491
|
+
const { vbW, vbH, contentX = 0, contentY = 0, clip } = res.meta
|
|
492
|
+
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
493
|
+
const drawn = performance.now()
|
|
494
|
+
const x = Math.round(ox + r.left * k), y = Math.round(oy + r.top * k)
|
|
495
|
+
const w = Math.round(ox + r.right * k) - x, h = Math.round(oy + r.bottom * k) - y
|
|
496
|
+
// The image starts contentX before the window, over pixels the clip culled: keep the window.
|
|
497
|
+
const layer = document.createElement('canvas')
|
|
498
|
+
layer.width = w
|
|
499
|
+
layer.height = h
|
|
500
|
+
layer.getContext('2d').drawImage(src, Math.round(ox + (clip.x - contentX) * k) - x, Math.round(oy + (clip.y - contentY) * k) - y, Math.round(vbW * k), Math.round(vbH * k))
|
|
501
|
+
spans.push([drawn, performance.now()])
|
|
502
|
+
return { layer, x, y, w, h, replace: true, host, drawnAt }
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** `run()`'s result, the time from `t` to it counted as the frame's own. */
|
|
506
|
+
async function held(run, t = performance.now()) {
|
|
507
|
+
try { return await run() } finally { spans.push([t, performance.now()]) }
|
|
508
|
+
}
|
|
509
|
+
|
|
343
510
|
async function patch(hosts) {
|
|
344
511
|
// Captures and decodes overlap; the draws then land in one synchronous pass, so the
|
|
345
512
|
// canvas never shows half a frame. Three hosts a frame (clock, KPI, list row) in
|
|
346
513
|
// demos/stream-webgl.html: 11.9 ms one after another.
|
|
347
|
-
const layers = await Promise.all(hosts.map(renderPatch))
|
|
514
|
+
const layers = await Promise.all(hosts.map((h) => regions.has(h) ? renderRegion(h) : renderPatch(h)))
|
|
515
|
+
if (stopped) return []
|
|
516
|
+
const drawn = performance.now()
|
|
348
517
|
const out = []
|
|
349
518
|
// The canvas clips draws past its edge; a reported rect must be clipped too, or a box
|
|
350
519
|
// sliding off the stage (the lower third hides to left:-720px) hands texSubImage2D a
|
|
@@ -361,9 +530,9 @@ export function stream(element, options = {}) {
|
|
|
361
530
|
paintBackdrop(backdrops.get(host), x, y, w, h)
|
|
362
531
|
report(x, y, w, h)
|
|
363
532
|
}
|
|
364
|
-
for (const { layer, x, y, w, h,
|
|
365
|
-
// A backdrop layer is the full frame's pixels for its whole rect: replace, not blend.
|
|
366
|
-
if (
|
|
533
|
+
for (const { layer, x, y, w, h, replace } of layers) {
|
|
534
|
+
// A backdrop or window layer is the full frame's pixels for its whole rect: replace, not blend.
|
|
535
|
+
if (replace) ctx.clearRect(x, y, w, h)
|
|
367
536
|
ctx.drawImage(layer, x, y)
|
|
368
537
|
report(x, y, w, h)
|
|
369
538
|
}
|
|
@@ -374,6 +543,7 @@ export function stream(element, options = {}) {
|
|
|
374
543
|
rects.set(host, drawnAt)
|
|
375
544
|
for (const el of host.querySelectorAll('*')) rects.set(el, rectOf(el))
|
|
376
545
|
}
|
|
546
|
+
spans.push([drawn, performance.now()])
|
|
377
547
|
stats.patch++
|
|
378
548
|
return out
|
|
379
549
|
}
|
|
@@ -387,6 +557,7 @@ export function stream(element, options = {}) {
|
|
|
387
557
|
if (!sameRect({ left: 0, top: 0, width: now.width, height: now.height }, { left: 0, top: 0, width: rootRect.width, height: rootRect.height })) return 'root resized'
|
|
388
558
|
rootRect = now
|
|
389
559
|
const hosts = new Set()
|
|
560
|
+
regions.clear()
|
|
390
561
|
for (const node of dirty) {
|
|
391
562
|
if (!node?.isConnected) return 'node removed'
|
|
392
563
|
let host = hostFor(node)
|
|
@@ -406,6 +577,7 @@ export function stream(element, options = {}) {
|
|
|
406
577
|
movedFrom.set(p, old)
|
|
407
578
|
}
|
|
408
579
|
}
|
|
580
|
+
if (!host && (host = regionFor(node))) regions.add(host)
|
|
409
581
|
if (!host) return `no host for ${describe(node)}`
|
|
410
582
|
hosts.add(host)
|
|
411
583
|
}
|
|
@@ -417,14 +589,18 @@ export function stream(element, options = {}) {
|
|
|
417
589
|
backdrops.clear()
|
|
418
590
|
for (const h of movedFrom.keys()) if (!hosts.has(h)) movedFrom.delete(h)
|
|
419
591
|
for (const h of hosts) {
|
|
420
|
-
const backdrop = backdropOf(h)
|
|
421
|
-
if (backdrop) backdrops.set(h, backdrop)
|
|
422
592
|
const after = rectOf(h)
|
|
423
593
|
area += after.width * after.height
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
594
|
+
if (regions.has(h)) continue
|
|
595
|
+
const over = floaters.find((f) => f !== h && !h.contains(f) && !f.contains(h) && intersects(rectOf(f), after))
|
|
596
|
+
if (over) {
|
|
597
|
+
// The window has the positioned box in it too.
|
|
598
|
+
if (movedFrom.has(h) || regionFor(h) !== h) return `${describe(over)} overlaps ${describe(h)}`
|
|
599
|
+
regions.add(h)
|
|
600
|
+
continue
|
|
427
601
|
}
|
|
602
|
+
const backdrop = backdropOf(h)
|
|
603
|
+
if (backdrop) backdrops.set(h, backdrop)
|
|
428
604
|
}
|
|
429
605
|
if (area > MAX_PATCH_AREA * rootRect.width * rootRect.height) return 'patch area too large'
|
|
430
606
|
return [...hosts]
|
|
@@ -446,6 +622,13 @@ export function stream(element, options = {}) {
|
|
|
446
622
|
for (const target of animating) if (!running.has(target)) dirty.add(target)
|
|
447
623
|
animating = running
|
|
448
624
|
}
|
|
625
|
+
if (hoverMoved) {
|
|
626
|
+
hoverMoved = false
|
|
627
|
+
const now = hoverMatches()
|
|
628
|
+
for (const el of now) if (!hovered.has(el)) dirty.add(el)
|
|
629
|
+
for (const el of hovered) if (!now.has(el) && el.isConnected) dirty.add(el)
|
|
630
|
+
hovered = now
|
|
631
|
+
}
|
|
449
632
|
netTargets(observer.takeRecords(), dirty, restyled)
|
|
450
633
|
// The keyframe waits for idle time, so it never lands on top of a burst of edits.
|
|
451
634
|
if (!keyframeDue && !idleAsked && now - lastKeyframe > Math.max(KEYFRAME_MS, fullMs * KEYFRAME_COST)) {
|
|
@@ -457,8 +640,10 @@ export function stream(element, options = {}) {
|
|
|
457
640
|
if (!dirty.size && !forceFull && !keyframeDue) { stats.skipped++; return }
|
|
458
641
|
busy = true
|
|
459
642
|
lastTick = now
|
|
643
|
+
spans = []
|
|
460
644
|
try {
|
|
461
645
|
const hosts = plan()
|
|
646
|
+
spans.push([now, performance.now()])
|
|
462
647
|
dirty.clear()
|
|
463
648
|
restyled.clear()
|
|
464
649
|
const reason = typeof hosts === 'string' ? hosts : null
|
|
@@ -467,7 +652,7 @@ export function stream(element, options = {}) {
|
|
|
467
652
|
// Records produced by our own capture are net-zero; anything else lands next frame.
|
|
468
653
|
netTargets(observer.takeRecords(), dirty, restyled)
|
|
469
654
|
stats.lastMs = performance.now() - now
|
|
470
|
-
if (out.length) onFrame?.({ full: !!reason, reason, rects: out, ms: stats.lastMs, canvas })
|
|
655
|
+
if (out.length) onFrame?.({ full: !!reason, reason, rects: out, ms: stats.lastMs, busyMs: covered(spans), canvas })
|
|
471
656
|
} finally {
|
|
472
657
|
busy = false
|
|
473
658
|
}
|
|
@@ -482,7 +667,7 @@ export function stream(element, options = {}) {
|
|
|
482
667
|
// until a keyframe (a 1000-card lab under edits kept stale cards for seconds).
|
|
483
668
|
await full()
|
|
484
669
|
netTargets(observer.takeRecords(), dirty, restyled)
|
|
485
|
-
onFrame?.({ full: true, reason: 'start', rects: [{ x: 0, y: 0, width: canvas.width, height: canvas.height }], ms: 0, canvas })
|
|
670
|
+
onFrame?.({ full: true, reason: 'start', rects: [{ x: 0, y: 0, width: canvas.width, height: canvas.height }], ms: 0, busyMs: 0, canvas })
|
|
486
671
|
} finally { busy = false }
|
|
487
672
|
requestAnimationFrame(tick)
|
|
488
673
|
})()
|
package/stream.d.ts
CHANGED
|
@@ -6,7 +6,10 @@ export interface StreamFrame {
|
|
|
6
6
|
full: boolean
|
|
7
7
|
reason: string | null
|
|
8
8
|
rects: { x: number; y: number; width: number; height: number }[]
|
|
9
|
+
/** Wall time of the repaint. */
|
|
9
10
|
ms: number
|
|
11
|
+
/** The part of `ms` the stream's own work held the main thread: the turns between windows and the decodes are out, a full frame's capture counts whole. */
|
|
12
|
+
busyMs: number
|
|
10
13
|
canvas: HTMLCanvasElement
|
|
11
14
|
}
|
|
12
15
|
|