@zumer/snapdom-plugins 4.4.1-dev.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/package.json +1 -1
- package/stream/README.md +1 -1
- package/stream/src/stream.js +71 -16
- package/stream.d.ts +3 -0
package/package.json
CHANGED
package/stream/README.md
CHANGED
|
@@ -46,7 +46,7 @@ All [capture options](https://snapdom.dev/) apply (`scale`, `dpr`, `embedFonts`,
|
|
|
46
46
|
|
|
47
47
|
| option | default | |
|
|
48
48
|
|---|---|---|
|
|
49
|
-
| `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 |
|
|
50
50
|
| `fps` | every animation frame | upper bound on repaints per second |
|
|
51
51
|
| `patches` | `true` | `false` repaints full frames only, for comparison |
|
|
52
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
|
@@ -170,6 +170,26 @@ function describe(node) {
|
|
|
170
170
|
return `<${el.tagName.toLowerCase()}${el.id ? '#' + el.id : ''}${el.classList?.length ? '.' + [...el.classList].join('.') : ''}>`
|
|
171
171
|
}
|
|
172
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
|
+
|
|
173
193
|
async function decode(url) {
|
|
174
194
|
const img = new Image()
|
|
175
195
|
img.decoding = 'sync'
|
|
@@ -182,7 +202,11 @@ async function decode(url) {
|
|
|
182
202
|
* Stream an element into a canvas, repainting only the boxes that changed.
|
|
183
203
|
* @param {Element} element
|
|
184
204
|
* @param {object} [options] - capture options (scale, dpr, embedFonts, ...), plus:
|
|
185
|
-
* @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.
|
|
186
210
|
* @param {number} [options.fps] - cap on frames per second; default every animation frame.
|
|
187
211
|
* @param {boolean} [options.patches=true] - false repaints full frames only (the baseline).
|
|
188
212
|
* @param {boolean} [options.animations=true] - false stops following running animations and
|
|
@@ -204,13 +228,14 @@ export function stream(element, options = {}) {
|
|
|
204
228
|
const backdrops = new Map() // this frame's hosts that draw over a solid colour
|
|
205
229
|
const movedFrom = new Map() // this frame's out-of-flow hosts that moved -> their old rect
|
|
206
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]
|
|
207
233
|
const stats = { full: 0, patch: 0, skipped: 0, lastMs: 0 }
|
|
208
234
|
let floaters = []
|
|
209
235
|
let glass = [] // backdrop-filter boxes: a change under one shows through its blur
|
|
210
236
|
let hoverList = null // the :hover selectors at the last full frame, null if unreadable
|
|
211
237
|
let hovered = new Set() // what they matched at the last look
|
|
212
238
|
let hoverMoved = false
|
|
213
|
-
let nodes = 0 // in the root at the last full frame, the root included
|
|
214
239
|
let forceFull = true
|
|
215
240
|
let lastKeyframe = 0
|
|
216
241
|
let keyframeDue = false // set from idle time once the keyframe interval has passed
|
|
@@ -270,9 +295,7 @@ export function stream(element, options = {}) {
|
|
|
270
295
|
rootRect = element.getBoundingClientRect()
|
|
271
296
|
floaters = []
|
|
272
297
|
glass = []
|
|
273
|
-
const
|
|
274
|
-
nodes = all.length + 1
|
|
275
|
-
for (const el of all) {
|
|
298
|
+
for (const el of element.querySelectorAll('*')) {
|
|
276
299
|
rects.set(el, rectOf(el))
|
|
277
300
|
const cs = getComputedStyle(el)
|
|
278
301
|
if (cs.position === 'absolute' || cs.position === 'fixed' || cs.position === 'sticky' ||
|
|
@@ -336,12 +359,20 @@ export function stream(element, options = {}) {
|
|
|
336
359
|
|
|
337
360
|
async function full() {
|
|
338
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 }
|
|
339
369
|
// A full frame is the one long task of a stream; 'auto' lets a long clone yield (#503).
|
|
340
370
|
// Lab, 1000 cards under constant edits: worst frame gap 67 -> 33 ms. Under edits on
|
|
341
371
|
// every frame a big tree tears and redoes in one task, which only the interval helps.
|
|
342
|
-
const res = await capture(element, { fast: 'auto', ...opts })
|
|
372
|
+
const res = await held(() => capture(element, { fast: 'auto', ...opts }), t0)
|
|
343
373
|
const { vbW, vbH, contentX = 0, contentY = 0 } = res.meta
|
|
344
374
|
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
375
|
+
const drawn = performance.now()
|
|
345
376
|
const w = Math.round(vbW * k), h = Math.round(vbH * k)
|
|
346
377
|
if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h }
|
|
347
378
|
ctx.clearRect(0, 0, w, h)
|
|
@@ -349,13 +380,10 @@ export function stream(element, options = {}) {
|
|
|
349
380
|
ox = contentX * k
|
|
350
381
|
oy = contentY * k
|
|
351
382
|
snapshotLayout()
|
|
352
|
-
// Read again at every full frame, so a sheet added later counts from the next keyframe.
|
|
353
|
-
const sels = hoverSelectors(element)
|
|
354
|
-
hoverList = sels && sels.length * nodes <= HOVER_BUDGET ? sels.join(',') : null
|
|
355
|
-
try { hovered = hoverMatches() } catch { hoverList = null }
|
|
356
383
|
lastKeyframe = performance.now()
|
|
357
384
|
keyframeDue = false
|
|
358
385
|
fullMs = lastKeyframe - t0
|
|
386
|
+
spans.push([drawn, lastKeyframe])
|
|
359
387
|
stats.full++
|
|
360
388
|
return [{ x: 0, y: 0, width: w, height: h }]
|
|
361
389
|
}
|
|
@@ -403,11 +431,12 @@ export function stream(element, options = {}) {
|
|
|
403
431
|
const x0 = Math.floor(left + 0.001), y0 = Math.floor(top + 0.001)
|
|
404
432
|
const phase = { x: (left - x0) / k, y: (top - y0) / k }
|
|
405
433
|
// Always passed, even at phase zero: it also lays the root out at its exact width.
|
|
406
|
-
const res = await capture(host, { ...opts, __subpixel: phase })
|
|
434
|
+
const res = await held(() => capture(host, { ...opts, __subpixel: phase }))
|
|
407
435
|
const { vbW, vbH, contentX = 0, contentY = 0 } = res.meta
|
|
408
436
|
const x = Math.round(x0 - contentX * k), y = Math.round(y0 - contentY * k)
|
|
409
437
|
const w = Math.round(vbW * k), h = Math.round(vbH * k)
|
|
410
438
|
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
439
|
+
const drawn = performance.now()
|
|
411
440
|
const layer = document.createElement('canvas')
|
|
412
441
|
layer.width = w
|
|
413
442
|
layer.height = h
|
|
@@ -420,6 +449,7 @@ export function stream(element, options = {}) {
|
|
|
420
449
|
lctx.fillRect(0, 0, w, h)
|
|
421
450
|
}
|
|
422
451
|
lctx.drawImage(src, 0, 0, w, h)
|
|
452
|
+
spans.push([drawn, performance.now()])
|
|
423
453
|
return { layer, x, y, w, h, replace: true, host, drawnAt: r }
|
|
424
454
|
}
|
|
425
455
|
// Otherwise keep only the pixels the box covers fully. Inside an opaque box that is
|
|
@@ -432,6 +462,7 @@ export function stream(element, options = {}) {
|
|
|
432
462
|
const px = img.data
|
|
433
463
|
for (let i = 3; i < px.length; i += 4) if (px[i] < 255) px[i] = 0
|
|
434
464
|
lctx.putImageData(img, 0, 0)
|
|
465
|
+
spans.push([drawn, performance.now()])
|
|
435
466
|
return { layer, x, y, w, h, host, drawnAt: r }
|
|
436
467
|
}
|
|
437
468
|
|
|
@@ -442,11 +473,24 @@ export function stream(element, options = {}) {
|
|
|
442
473
|
* found). Sixteen windows at once cost 101 ms; one window per row of them cost 149, since a
|
|
443
474
|
* row keeps every tile in it. */
|
|
444
475
|
async function renderRegion(host) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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
|
|
448
491
|
const { vbW, vbH, contentX = 0, contentY = 0, clip } = res.meta
|
|
449
492
|
const src = WEBKIT ? await res.toCanvas() : await decode(res.url)
|
|
493
|
+
const drawn = performance.now()
|
|
450
494
|
const x = Math.round(ox + r.left * k), y = Math.round(oy + r.top * k)
|
|
451
495
|
const w = Math.round(ox + r.right * k) - x, h = Math.round(oy + r.bottom * k) - y
|
|
452
496
|
// The image starts contentX before the window, over pixels the clip culled: keep the window.
|
|
@@ -454,14 +498,22 @@ export function stream(element, options = {}) {
|
|
|
454
498
|
layer.width = w
|
|
455
499
|
layer.height = h
|
|
456
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()])
|
|
457
502
|
return { layer, x, y, w, h, replace: true, host, drawnAt }
|
|
458
503
|
}
|
|
459
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
|
+
|
|
460
510
|
async function patch(hosts) {
|
|
461
511
|
// Captures and decodes overlap; the draws then land in one synchronous pass, so the
|
|
462
512
|
// canvas never shows half a frame. Three hosts a frame (clock, KPI, list row) in
|
|
463
513
|
// demos/stream-webgl.html: 11.9 ms one after another.
|
|
464
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()
|
|
465
517
|
const out = []
|
|
466
518
|
// The canvas clips draws past its edge; a reported rect must be clipped too, or a box
|
|
467
519
|
// sliding off the stage (the lower third hides to left:-720px) hands texSubImage2D a
|
|
@@ -491,6 +543,7 @@ export function stream(element, options = {}) {
|
|
|
491
543
|
rects.set(host, drawnAt)
|
|
492
544
|
for (const el of host.querySelectorAll('*')) rects.set(el, rectOf(el))
|
|
493
545
|
}
|
|
546
|
+
spans.push([drawn, performance.now()])
|
|
494
547
|
stats.patch++
|
|
495
548
|
return out
|
|
496
549
|
}
|
|
@@ -587,8 +640,10 @@ export function stream(element, options = {}) {
|
|
|
587
640
|
if (!dirty.size && !forceFull && !keyframeDue) { stats.skipped++; return }
|
|
588
641
|
busy = true
|
|
589
642
|
lastTick = now
|
|
643
|
+
spans = []
|
|
590
644
|
try {
|
|
591
645
|
const hosts = plan()
|
|
646
|
+
spans.push([now, performance.now()])
|
|
592
647
|
dirty.clear()
|
|
593
648
|
restyled.clear()
|
|
594
649
|
const reason = typeof hosts === 'string' ? hosts : null
|
|
@@ -597,7 +652,7 @@ export function stream(element, options = {}) {
|
|
|
597
652
|
// Records produced by our own capture are net-zero; anything else lands next frame.
|
|
598
653
|
netTargets(observer.takeRecords(), dirty, restyled)
|
|
599
654
|
stats.lastMs = performance.now() - now
|
|
600
|
-
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 })
|
|
601
656
|
} finally {
|
|
602
657
|
busy = false
|
|
603
658
|
}
|
|
@@ -612,7 +667,7 @@ export function stream(element, options = {}) {
|
|
|
612
667
|
// until a keyframe (a 1000-card lab under edits kept stale cards for seconds).
|
|
613
668
|
await full()
|
|
614
669
|
netTargets(observer.takeRecords(), dirty, restyled)
|
|
615
|
-
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 })
|
|
616
671
|
} finally { busy = false }
|
|
617
672
|
requestAnimationFrame(tick)
|
|
618
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
|
|