@rcarls/rc-virtual-canvas 0.1.0

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 ADDED
@@ -0,0 +1,171 @@
1
+ # `@rcarls/rc-virtual-canvas`
2
+
3
+ A virtualized scrollable canvas component. Overlays a transparent scrollable div on top of a slotted `<canvas>` element, dispatching `rc-virtual-canvas-render` events with the current viewport and content rectangles. Consumers draw only what is visible. Built with [Lit 3](https://lit.dev).
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @rcarls/rc-virtual-canvas
11
+ ```
12
+
13
+ ## Import
14
+
15
+ ```js
16
+ import '@rcarls/rc-virtual-canvas'; // side-effect: registers <rc-virtual-canvas>
17
+ import { RCVirtualCanvas } from '@rcarls/rc-virtual-canvas'; // named class export
18
+ ```
19
+
20
+ ---
21
+
22
+ ## Basic usage
23
+
24
+ Place a `<canvas>` inside the component. Set `contentWidth` and `contentHeight` to the total size of your virtual content. On each `rc-virtual-canvas-render` event, use `detail.viewRect` to determine which portion of that content is visible and draw accordingly.
25
+
26
+ ```html
27
+ <rc-virtual-canvas
28
+ id="vc"
29
+ contentWidth="4000"
30
+ contentHeight="3000"
31
+ style="width: 800px; height: 600px;"
32
+ >
33
+ <canvas id="canvas" width="800" height="600"></canvas>
34
+ </rc-virtual-canvas>
35
+
36
+ <script>
37
+ const ctx = document.querySelector('#canvas').getContext('2d');
38
+
39
+ document.querySelector('#vc').addEventListener('rc-virtual-canvas-render', (e) => {
40
+ const { time, viewRect } = e.detail;
41
+
42
+ ctx.clearRect(0, 0, viewRect.width, viewRect.height);
43
+
44
+ // Draw only content within viewRect.x / viewRect.y offset
45
+ drawScene(ctx, viewRect);
46
+ });
47
+ </script>
48
+ ```
49
+
50
+ The canvas element should match the component's rendered size (set via CSS). The virtual content is larger — scrolling is handled by an absolutely-positioned transparent `<div>` that triggers native scroll events.
51
+
52
+ ---
53
+
54
+ ## API
55
+
56
+ ### Properties / attributes
57
+
58
+ | Property | Attribute | Type | Default | Description |
59
+ |---|---|---|---|---|
60
+ | `contentWidth` | `contentWidth` | `number` | `0` | Total pixel width of the virtual content |
61
+ | `contentHeight` | `contentHeight` | `number` | `0` | Total pixel height of the virtual content |
62
+ | `autoResizeCanvas` | `auto-resize-canvas` | `boolean` | `true` | Keeps the slotted canvas backing store aligned to the measured viewport |
63
+ | `renderMode` | `render-mode` | `'continuous' \| 'viewport-change' \| 'manual'` | `'continuous'` | Controls when render events are dispatched |
64
+ | `imageRendering` | `image-rendering` | `'auto' \| 'crisp-edges' \| 'pixelated'` | `'auto'` | Convenience value applied to the slotted canvas via `image-rendering` |
65
+
66
+ ### Methods
67
+
68
+ | Method | Description |
69
+ |---|---|
70
+ | `getViewRect()` | Returns an immutable snapshot of the current viewport rectangle |
71
+ | `scrollToContent(x, y, options?)` | Scrolls so the content coordinate is at the viewport origin |
72
+ | `centerOnContent(x, y, options?)` | Scrolls so the content coordinate is centered in the viewport |
73
+ | `clientToContent(clientX, clientY)` | Converts browser client coordinates to content coordinates using the current backing-store scale |
74
+ | `contentToClient(x, y)` | Converts content coordinates back to browser client coordinates |
75
+ | `requestRender(reason?)` | Queues a render event; required when `renderMode` is `'manual'` |
76
+
77
+ ### CSS custom properties
78
+
79
+ | Property | Default | Description |
80
+ |---|---|---|
81
+ | `--rc-virtual-canvas-image-rendering` | `auto` | Fallback styling hook for the slotted canvas `image-rendering` value |
82
+
83
+ ### CSS parts
84
+
85
+ | Part | Description |
86
+ |---|---|
87
+ | `scroller` | Internal scroll container that owns the virtual content scroll range |
88
+ | `overlay` | Viewport-positioned overlay container rendered inside the scroll container |
89
+
90
+ ### Slots
91
+
92
+ | Slot | Description |
93
+ |---|---|
94
+ | *(default)* | A single `<canvas>` element. Non-canvas slotted elements are ignored by the component but rendered. |
95
+ | `overlay` | Optional viewport-positioned content rendered over the canvas inside the scroll container. |
96
+
97
+ ### Events
98
+
99
+ | Event | Bubbles | Cancelable | Detail | When |
100
+ |---|---|---|---|---|
101
+ | `rc-virtual-canvas-render` | Yes (composed) | No | `{ time: DOMHighResTimeStamp, reason: RenderReason, viewRect: ViewRect, contentRect: ViewRect }` | When the active render mode schedules a frame and a canvas is slotted |
102
+
103
+ **`ViewRect` shape:**
104
+
105
+ ```ts
106
+ type ViewRect = {
107
+ x: number; // Scroll offset — the x origin of the visible window in content space
108
+ y: number; // Scroll offset — the y origin of the visible window in content space
109
+ width: number; // Visible width in device pixels (from ResizeObserver devicePixelContentBoxSize)
110
+ height: number; // Visible height in device pixels
111
+ };
112
+ ```
113
+
114
+ `viewRect` is the visible window into the virtual content. `contentRect` is the full content bounds (`x: 0, y: 0, width: contentWidth, height: contentHeight`).
115
+
116
+ `viewRect` and `contentRect` are frozen snapshots for the dispatched frame. Keep the object if you need it later; it will not be mutated by future scroll or resize work.
117
+
118
+ **Render modes:**
119
+
120
+ | Mode | Behavior |
121
+ |---|---|
122
+ | `'continuous'` | Dispatches on every animation frame while connected and a canvas is slotted |
123
+ | `'viewport-change'` | Dispatches after scroll, resize, content-size changes, and canvas replacement |
124
+ | `'manual'` | Dispatches only after `requestRender()` |
125
+
126
+ ---
127
+
128
+ ## Coordinate system
129
+
130
+ The component maps scroll position directly to content coordinates:
131
+
132
+ - `viewRect.x` = `scrollLeft` of the internal scroll container
133
+ - `viewRect.y` = `scrollTop` of the internal scroll container
134
+ - `viewRect.width` / `viewRect.height` = canvas dimensions in device pixels (high-DPI aware)
135
+
136
+ When drawing, offset your scene by `viewRect.x` and `viewRect.y` to align the visible portion with the canvas origin:
137
+
138
+ ```js
139
+ ctx.translate(-viewRect.x, -viewRect.y);
140
+ drawEntireScene(ctx);
141
+ ctx.resetTransform();
142
+ ```
143
+
144
+ Pointer input can be mapped through the same coordinate system:
145
+
146
+ ```js
147
+ vc.addEventListener('pointerdown', (event) => {
148
+ const point = vc.clientToContent(event.clientX, event.clientY);
149
+
150
+ selectThingAt(point.x, point.y);
151
+ });
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Accessibility
157
+
158
+ `rc-virtual-canvas` is a rendering surface. A `<canvas>` element has no inherent semantics — it is a bitmap, not a document. If your canvas renders interactive or informational content, you are responsible for providing supplemental accessibility:
159
+
160
+ - Add an accessible `<div>` or `<table>` alternative outside the canvas with the same information (hidden visually with `clip-path` or similar, not `display: none`).
161
+ - Use `aria-label` or `aria-labelledby` on the `<canvas>` to describe what it shows.
162
+ - For interactive canvas content (hit-testing, drag-and-drop), implement keyboard equivalents on a separate focusable element.
163
+
164
+ ---
165
+
166
+ ## Browser support
167
+
168
+ | Feature | Requirement |
169
+ |---|---|
170
+ | Core | Chrome 67+, Firefox 63+, Safari 12.1+ (Web Components) |
171
+ | Device-pixel-accurate sizing | Chrome 84+, Firefox 93+, Safari 15.4+ (ResizeObserver `devicePixelContentBoxSize`) |
@@ -0,0 +1,531 @@
1
+ {
2
+ "schemaVersion": "1.0.0",
3
+ "readme": "",
4
+ "modules": [
5
+ {
6
+ "kind": "javascript-module",
7
+ "path": "src/define.ts",
8
+ "declarations": [],
9
+ "exports": [
10
+ {
11
+ "kind": "custom-element-definition",
12
+ "name": "rc-virtual-canvas",
13
+ "declaration": {
14
+ "name": "RCVirtualCanvas",
15
+ "module": "/src/index.js"
16
+ }
17
+ },
18
+ {
19
+ "kind": "js",
20
+ "name": "*",
21
+ "declaration": {
22
+ "name": "*",
23
+ "module": "src/index.js"
24
+ }
25
+ }
26
+ ]
27
+ },
28
+ {
29
+ "kind": "javascript-module",
30
+ "path": "src/index.ts",
31
+ "declarations": [],
32
+ "exports": [
33
+ {
34
+ "kind": "js",
35
+ "name": "*",
36
+ "declaration": {
37
+ "name": "*",
38
+ "module": "src/rc-virtual-canvas"
39
+ }
40
+ }
41
+ ]
42
+ },
43
+ {
44
+ "kind": "javascript-module",
45
+ "path": "src/rc-virtual-canvas.ts",
46
+ "declarations": [
47
+ {
48
+ "kind": "class",
49
+ "description": "An accessible virtual scrollable canvas component.",
50
+ "name": "RCVirtualCanvas",
51
+ "slots": [
52
+ {
53
+ "description": "The HTMLCanvasElement",
54
+ "name": ""
55
+ },
56
+ {
57
+ "description": "Optional viewport-positioned content rendered inside the scroll container above the canvas.",
58
+ "name": "overlay"
59
+ }
60
+ ],
61
+ "members": [
62
+ {
63
+ "kind": "field",
64
+ "name": "_rafHandle",
65
+ "type": {
66
+ "text": "number"
67
+ },
68
+ "privacy": "private",
69
+ "default": "0"
70
+ },
71
+ {
72
+ "kind": "field",
73
+ "name": "_pendingRenderReason",
74
+ "type": {
75
+ "text": "RCVirtualCanvasRenderReason | undefined"
76
+ },
77
+ "privacy": "private"
78
+ },
79
+ {
80
+ "kind": "field",
81
+ "name": "_handledPointerEvents",
82
+ "privacy": "private",
83
+ "readonly": true,
84
+ "default": "new WeakSet<Event>()"
85
+ },
86
+ {
87
+ "kind": "field",
88
+ "name": "_boundUpdate",
89
+ "privacy": "private",
90
+ "readonly": true
91
+ },
92
+ {
93
+ "kind": "field",
94
+ "name": "_boundPointerEvent",
95
+ "privacy": "private",
96
+ "readonly": true
97
+ },
98
+ {
99
+ "kind": "field",
100
+ "name": "contentWidth",
101
+ "description": "Pixel width of the virtual content",
102
+ "attribute": "contentWidth"
103
+ },
104
+ {
105
+ "kind": "field",
106
+ "name": "_contentWidth",
107
+ "type": {
108
+ "text": "number"
109
+ },
110
+ "privacy": "protected",
111
+ "default": "0"
112
+ },
113
+ {
114
+ "kind": "field",
115
+ "name": "contentHeight",
116
+ "description": "Pixel height of the virtual content",
117
+ "attribute": "contentHeight"
118
+ },
119
+ {
120
+ "kind": "field",
121
+ "name": "_contentHeight",
122
+ "type": {
123
+ "text": "number"
124
+ },
125
+ "privacy": "protected",
126
+ "default": "0"
127
+ },
128
+ {
129
+ "kind": "field",
130
+ "name": "autoResizeCanvas",
131
+ "type": {
132
+ "text": "boolean"
133
+ },
134
+ "default": "true",
135
+ "description": "When true, keep the slotted canvas backing store aligned to the viewport.",
136
+ "attribute": "auto-resize-canvas"
137
+ },
138
+ {
139
+ "kind": "field",
140
+ "name": "renderMode",
141
+ "type": {
142
+ "text": "RCVirtualCanvasRenderMode"
143
+ },
144
+ "default": "'continuous'",
145
+ "description": "Controls when render events are dispatched.",
146
+ "attribute": "render-mode"
147
+ },
148
+ {
149
+ "kind": "field",
150
+ "name": "imageRendering",
151
+ "type": {
152
+ "text": "RCVirtualCanvasImageRendering"
153
+ },
154
+ "default": "'auto'",
155
+ "description": "Convenience image-rendering value applied to the slotted canvas.",
156
+ "attribute": "image-rendering"
157
+ },
158
+ {
159
+ "kind": "field",
160
+ "name": "_$root",
161
+ "type": {
162
+ "text": "HTMLDivElement"
163
+ },
164
+ "privacy": "protected"
165
+ },
166
+ {
167
+ "kind": "field",
168
+ "name": "_$placeholder",
169
+ "type": {
170
+ "text": "HTMLDivElement"
171
+ },
172
+ "privacy": "protected"
173
+ },
174
+ {
175
+ "kind": "field",
176
+ "name": "_$canvas",
177
+ "type": {
178
+ "text": "HTMLCanvasElement | null"
179
+ },
180
+ "privacy": "protected",
181
+ "default": "null"
182
+ },
183
+ {
184
+ "kind": "field",
185
+ "name": "_viewRect",
186
+ "type": {
187
+ "text": "object"
188
+ },
189
+ "privacy": "protected",
190
+ "default": "{ x: 0, y: 0, width: 0, height: 0, }"
191
+ },
192
+ {
193
+ "kind": "field",
194
+ "name": "_contentRect",
195
+ "type": {
196
+ "text": "object"
197
+ },
198
+ "privacy": "protected",
199
+ "default": "{ x: 0, y: 0, width: 0, height: 0, }"
200
+ },
201
+ {
202
+ "kind": "field",
203
+ "name": "_resizeObserver",
204
+ "privacy": "protected",
205
+ "default": "new ResizeObserver( (entries: ResizeObserverEntry[]) => { for (const entry of entries) { const devicePixelBoxSize = Array.isArray( entry.devicePixelContentBoxSize, ) ? entry.devicePixelContentBoxSize[0] : entry.devicePixelContentBoxSize; this._viewRect.width = this._getMeasuredCanvasWidth( devicePixelBoxSize?.inlineSize ?? Math.round(entry.contentRect.width * window.devicePixelRatio), ); this._viewRect.height = this._getMeasuredCanvasHeight( devicePixelBoxSize?.blockSize ?? Math.round(entry.contentRect.height * window.devicePixelRatio), ); this._syncCanvasBackingStore(); this._scheduleRender('viewport-change'); } }, )"
206
+ },
207
+ {
208
+ "kind": "method",
209
+ "name": "_onScroll",
210
+ "privacy": "protected"
211
+ },
212
+ {
213
+ "kind": "method",
214
+ "name": "_onSlotChange",
215
+ "privacy": "protected",
216
+ "parameters": [
217
+ {
218
+ "name": "e",
219
+ "type": {
220
+ "text": "Event"
221
+ }
222
+ }
223
+ ]
224
+ },
225
+ {
226
+ "kind": "method",
227
+ "name": "getViewRect"
228
+ },
229
+ {
230
+ "kind": "method",
231
+ "name": "scrollToContent",
232
+ "parameters": [
233
+ {
234
+ "name": "x",
235
+ "type": {
236
+ "text": "number"
237
+ }
238
+ },
239
+ {
240
+ "name": "y",
241
+ "type": {
242
+ "text": "number"
243
+ }
244
+ },
245
+ {
246
+ "name": "options",
247
+ "default": "{}",
248
+ "type": {
249
+ "text": "ScrollOptions"
250
+ }
251
+ }
252
+ ]
253
+ },
254
+ {
255
+ "kind": "method",
256
+ "name": "centerOnContent",
257
+ "parameters": [
258
+ {
259
+ "name": "x",
260
+ "type": {
261
+ "text": "number"
262
+ }
263
+ },
264
+ {
265
+ "name": "y",
266
+ "type": {
267
+ "text": "number"
268
+ }
269
+ },
270
+ {
271
+ "name": "options",
272
+ "default": "{}",
273
+ "type": {
274
+ "text": "ScrollOptions"
275
+ }
276
+ }
277
+ ]
278
+ },
279
+ {
280
+ "kind": "method",
281
+ "name": "clientToContent",
282
+ "parameters": [
283
+ {
284
+ "name": "clientX",
285
+ "type": {
286
+ "text": "number"
287
+ }
288
+ },
289
+ {
290
+ "name": "clientY",
291
+ "type": {
292
+ "text": "number"
293
+ }
294
+ }
295
+ ]
296
+ },
297
+ {
298
+ "kind": "method",
299
+ "name": "contentToClient",
300
+ "parameters": [
301
+ {
302
+ "name": "x",
303
+ "type": {
304
+ "text": "number"
305
+ }
306
+ },
307
+ {
308
+ "name": "y",
309
+ "type": {
310
+ "text": "number"
311
+ }
312
+ }
313
+ ]
314
+ },
315
+ {
316
+ "kind": "method",
317
+ "name": "requestRender",
318
+ "parameters": [
319
+ {
320
+ "name": "reason",
321
+ "default": "'manual'",
322
+ "type": {
323
+ "text": "RCVirtualCanvasRenderReason"
324
+ }
325
+ }
326
+ ]
327
+ },
328
+ {
329
+ "kind": "method",
330
+ "name": "_onPointerEvent",
331
+ "privacy": "protected",
332
+ "parameters": [
333
+ {
334
+ "name": "event",
335
+ "type": {
336
+ "text": "PointerEvent | MouseEvent"
337
+ }
338
+ }
339
+ ]
340
+ },
341
+ {
342
+ "kind": "method",
343
+ "name": "_isOverlayEvent",
344
+ "privacy": "private",
345
+ "return": {
346
+ "type": {
347
+ "text": "boolean"
348
+ }
349
+ },
350
+ "parameters": [
351
+ {
352
+ "name": "event",
353
+ "type": {
354
+ "text": "Event"
355
+ }
356
+ }
357
+ ]
358
+ },
359
+ {
360
+ "kind": "method",
361
+ "name": "_update",
362
+ "privacy": "protected",
363
+ "parameters": [
364
+ {
365
+ "name": "time",
366
+ "type": {
367
+ "text": "DOMHighResTimeStamp"
368
+ }
369
+ }
370
+ ]
371
+ },
372
+ {
373
+ "kind": "method",
374
+ "name": "_scheduleRender",
375
+ "privacy": "private",
376
+ "parameters": [
377
+ {
378
+ "name": "reason",
379
+ "type": {
380
+ "text": "RCVirtualCanvasRenderReason"
381
+ }
382
+ }
383
+ ]
384
+ },
385
+ {
386
+ "kind": "method",
387
+ "name": "_syncCanvasBackingStore",
388
+ "privacy": "private"
389
+ },
390
+ {
391
+ "kind": "method",
392
+ "name": "_getCanvasClientRect",
393
+ "privacy": "private"
394
+ },
395
+ {
396
+ "kind": "method",
397
+ "name": "_getMeasuredCanvasWidth",
398
+ "privacy": "private",
399
+ "parameters": [
400
+ {
401
+ "name": "fallbackWidth",
402
+ "type": {
403
+ "text": "number"
404
+ }
405
+ }
406
+ ]
407
+ },
408
+ {
409
+ "kind": "method",
410
+ "name": "_getMeasuredCanvasHeight",
411
+ "privacy": "private",
412
+ "parameters": [
413
+ {
414
+ "name": "fallbackHeight",
415
+ "type": {
416
+ "text": "number"
417
+ }
418
+ }
419
+ ]
420
+ },
421
+ {
422
+ "kind": "method",
423
+ "name": "_getViewportScaleX",
424
+ "privacy": "private",
425
+ "parameters": [
426
+ {
427
+ "name": "canvasRect",
428
+ "type": {
429
+ "text": "DOMRect"
430
+ }
431
+ }
432
+ ]
433
+ },
434
+ {
435
+ "kind": "method",
436
+ "name": "_getViewportScaleY",
437
+ "privacy": "private",
438
+ "parameters": [
439
+ {
440
+ "name": "canvasRect",
441
+ "type": {
442
+ "text": "DOMRect"
443
+ }
444
+ }
445
+ ]
446
+ }
447
+ ],
448
+ "events": [
449
+ {
450
+ "name": "rc-virtual-canvas-pointer",
451
+ "type": {
452
+ "text": "CustomEvent"
453
+ },
454
+ "description": "Fires pointer/mouse input mapped to virtual content coordinates."
455
+ },
456
+ {
457
+ "name": "rc-virtual-canvas-render",
458
+ "type": {
459
+ "text": "CustomEvent"
460
+ },
461
+ "description": "Fires with viewport data when the canvas should redraw."
462
+ }
463
+ ],
464
+ "attributes": [
465
+ {
466
+ "name": "contentWidth",
467
+ "description": "Pixel width of the virtual content",
468
+ "fieldName": "contentWidth"
469
+ },
470
+ {
471
+ "name": "contentHeight",
472
+ "description": "Pixel height of the virtual content",
473
+ "fieldName": "contentHeight"
474
+ },
475
+ {
476
+ "name": "auto-resize-canvas",
477
+ "type": {
478
+ "text": "boolean"
479
+ },
480
+ "default": "true",
481
+ "description": "When true, keep the slotted canvas backing store aligned to the viewport.",
482
+ "fieldName": "autoResizeCanvas"
483
+ },
484
+ {
485
+ "name": "render-mode",
486
+ "type": {
487
+ "text": "RCVirtualCanvasRenderMode"
488
+ },
489
+ "default": "'continuous'",
490
+ "description": "Controls when render events are dispatched.",
491
+ "fieldName": "renderMode"
492
+ },
493
+ {
494
+ "name": "image-rendering",
495
+ "type": {
496
+ "text": "RCVirtualCanvasImageRendering"
497
+ },
498
+ "default": "'auto'",
499
+ "description": "Convenience image-rendering value applied to the slotted canvas.",
500
+ "fieldName": "imageRendering"
501
+ }
502
+ ],
503
+ "superclass": {
504
+ "name": "LitElement",
505
+ "package": "lit"
506
+ },
507
+ "tagName": "rc-virtual-canvas",
508
+ "customElement": true
509
+ }
510
+ ],
511
+ "exports": [
512
+ {
513
+ "kind": "js",
514
+ "name": "RCVirtualCanvas",
515
+ "declaration": {
516
+ "name": "RCVirtualCanvas",
517
+ "module": "src/rc-virtual-canvas.ts"
518
+ }
519
+ },
520
+ {
521
+ "kind": "js",
522
+ "name": "default",
523
+ "declaration": {
524
+ "name": "RCVirtualCanvas",
525
+ "module": "src/rc-virtual-canvas.ts"
526
+ }
527
+ }
528
+ ]
529
+ }
530
+ ]
531
+ }
Binary file