@rcarls/rc-virtual-canvas 0.0.0-next-20260921045401

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/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ # @rcarls/rc-virtual-canvas
2
+
3
+ ## 0.0.0-next-20260921045401
4
+
5
+ ## 0.6.0
6
+
7
+ ### Patch Changes
8
+
9
+ - 57370e4: Align component metadata, aggregate framework typings, development-time native-child
10
+ validation, controlled menu state, package declarations, and public documentation before the
11
+ next release.
12
+
13
+ ## 0.5.0
14
+
15
+ ## 0.4.2
16
+
17
+ ## 0.4.1
18
+
19
+ ## 0.4.0
20
+
21
+ ### Patch Changes
22
+
23
+ - 037b1b3: Add try/catch for ResizeObserver.observe() for WebKit TypeError
24
+ - d49cde9: Fix `contentWidth`/`contentHeight` so their HTML attributes are `content-width`/`content-height`,
25
+ matching every other multi-word attribute in the library. They previously had no kebab-case
26
+ mapping, so `<rc-virtual-canvas content-width="...">` was silently ignored.
27
+
28
+ ## 0.3.2
29
+
30
+ ## 0.3.1
31
+
32
+ ## 0.3.0
33
+
34
+ ### Added
35
+
36
+ - Add read-only `canvasScaleX` and `canvasScaleY` getters.
37
+
38
+ ### Changed
39
+
40
+ - Update package metadata, README intro, and docs links.
41
+
42
+ ## 0.2.0
package/README.md ADDED
@@ -0,0 +1,190 @@
1
+ # `@rcarls/rc-virtual-canvas`
2
+
3
+ <!-- markdownlint-disable MD013 -->
4
+
5
+ Scrollable virtual canvas for rendering large coordinate-space content.
6
+
7
+ Docs: [https://richardcarls.github.io/rc-webcomponents/components/rc-virtual-canvas](https://richardcarls.github.io/rc-webcomponents/components/rc-virtual-canvas).
8
+
9
+ Overlays a transparent scrollable div on top of a slotted `<canvas>` element, dispatching
10
+ `rc-virtual-canvas-render` events with the current viewport and content rectangles.
11
+ Consumers draw only what is visible.
12
+
13
+ ---
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @rcarls/rc-virtual-canvas
19
+ ```
20
+
21
+ ## Import
22
+
23
+ ```js
24
+ import '@rcarls/rc-virtual-canvas'; // side-effect: registers <rc-virtual-canvas>
25
+ import { RCVirtualCanvas } from '@rcarls/rc-virtual-canvas'; // named class export
26
+ ```
27
+
28
+ ---
29
+
30
+ ## Basic usage
31
+
32
+ Place a `<canvas>` inside the component. Set `content-width` and `content-height` to the
33
+ total size of your virtual content. On each `rc-virtual-canvas-render` event, use
34
+ `detail.viewRect` to determine which portion of that content is visible and draw accordingly.
35
+
36
+ ```html
37
+ <rc-virtual-canvas
38
+ id="vc"
39
+ content-width="4000"
40
+ content-height="3000"
41
+ style="width: 800px; height: 600px;"
42
+ >
43
+ <canvas id="canvas" width="800" height="600"></canvas>
44
+ </rc-virtual-canvas>
45
+
46
+ <script>
47
+ const ctx = document.querySelector('#canvas').getContext('2d');
48
+
49
+ document.querySelector('#vc').addEventListener('rc-virtual-canvas-render', (e) => {
50
+ const { time, viewRect } = e.detail;
51
+
52
+ ctx.clearRect(0, 0, viewRect.width, viewRect.height);
53
+
54
+ // Draw only content within viewRect.x / viewRect.y offset
55
+ drawScene(ctx, viewRect);
56
+ });
57
+ </script>
58
+ ```
59
+
60
+ The canvas element should match the component's rendered size (set via CSS). The virtual
61
+ content is larger; scrolling is handled by an absolutely positioned transparent `<div>`
62
+ that triggers native scroll events.
63
+
64
+ ---
65
+
66
+ ## API
67
+
68
+ ### Properties / attributes
69
+
70
+ | Property | Attribute | Type | Default | Description |
71
+ | ------------------ | -------------------- | ----------------------------------------------- | -------------- | ----------------------------------------------------------------------- |
72
+ | `contentWidth` | `content-width` | `number` | `0` | Total pixel width of the virtual content |
73
+ | `contentHeight` | `content-height` | `number` | `0` | Total pixel height of the virtual content |
74
+ | `autoResizeCanvas` | `auto-resize-canvas` | `boolean` | `true` | Keeps the slotted canvas backing store aligned to the measured viewport |
75
+ | `renderMode` | `render-mode` | `'continuous' \| 'viewport-change' \| 'manual'` | `'continuous'` | Controls when render events are dispatched |
76
+ | `imageRendering` | `image-rendering` | `'auto' \| 'crisp-edges' \| 'pixelated'` | `'auto'` | Convenience value applied to the slotted canvas via `image-rendering` |
77
+
78
+ ### Read-only properties
79
+
80
+ | Property | Type | Description |
81
+ | -------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
82
+ | `canvasScaleX` | `number` | Ratio of canvas backing-store pixels to CSS pixels along the x-axis (≈ `devicePixelRatio` when `autoResizeCanvas` is `true`) |
83
+ | `canvasScaleY` | `number` | Ratio of canvas backing-store pixels to CSS pixels along the y-axis |
84
+
85
+ ### Methods
86
+
87
+ | Method | Description |
88
+ | ----------------------------------- | ------------------------------------------------------------------------------------------------ |
89
+ | `getViewRect()` | Returns an immutable snapshot of the current viewport rectangle |
90
+ | `scrollToContent(x, y, options?)` | Scrolls so the content coordinate is at the viewport origin |
91
+ | `centerOnContent(x, y, options?)` | Scrolls so the content coordinate is centered in the viewport |
92
+ | `clientToContent(clientX, clientY)` | Converts browser client coordinates to content coordinates using the current backing-store scale |
93
+ | `contentToClient(x, y)` | Converts content coordinates back to browser client coordinates |
94
+ | `requestRender(reason?)` | Queues a render event; required when `renderMode` is `'manual'` |
95
+
96
+ ### CSS custom properties
97
+
98
+ | Property | Default | Description |
99
+ | ------------------------------------- | ------- | -------------------------------------------------------------------- |
100
+ | `--rc-virtual-canvas-image-rendering` | `auto` | Fallback styling hook for the slotted canvas `image-rendering` value |
101
+
102
+ ### CSS parts
103
+
104
+ | Part | Description |
105
+ | ---------- | -------------------------------------------------------------------------- |
106
+ | `scroller` | Internal scroll container that owns the virtual content scroll range |
107
+ | `overlay` | Viewport-positioned overlay container rendered inside the scroll container |
108
+
109
+ ### Slots
110
+
111
+ | Slot | Description |
112
+ | ----------- | --------------------------------------------------------------------------------------------------- |
113
+ | _(default)_ | A single `<canvas>` element. Non-canvas slotted elements are ignored by the component but rendered. |
114
+ | `overlay` | Optional viewport-positioned content rendered over the canvas inside the scroll container. |
115
+
116
+ ### Events
117
+
118
+ | Event | Bubbles | Cancelable | Detail | When |
119
+ | -------------------------- | -------------- | ---------- | ------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------- |
120
+ | `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 |
121
+
122
+ **`ViewRect` shape:**
123
+
124
+ ```ts
125
+ type ViewRect = {
126
+ x: number; // Scroll offset — the x origin of the visible window in content space
127
+ y: number; // Scroll offset — the y origin of the visible window in content space
128
+ width: number; // Visible width in device pixels (from ResizeObserver devicePixelContentBoxSize)
129
+ height: number; // Visible height in device pixels
130
+ };
131
+ ```
132
+
133
+ `viewRect` is the visible window into the virtual content. `contentRect` is the full content bounds (`x: 0, y: 0, width: contentWidth, height: contentHeight`).
134
+
135
+ `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.
136
+
137
+ **Render modes:**
138
+
139
+ | Mode | Behavior |
140
+ | ------------------- | ----------------------------------------------------------------------------- |
141
+ | `'continuous'` | Dispatches on every animation frame while connected and a canvas is slotted |
142
+ | `'viewport-change'` | Dispatches after scroll, resize, content-size changes, and canvas replacement |
143
+ | `'manual'` | Dispatches only after `requestRender()` |
144
+
145
+ ---
146
+
147
+ ## Coordinate system
148
+
149
+ The component maps scroll position directly to content coordinates:
150
+
151
+ - `viewRect.x` = `scrollLeft` of the internal scroll container
152
+ - `viewRect.y` = `scrollTop` of the internal scroll container
153
+ - `viewRect.width` / `viewRect.height` = canvas dimensions in device pixels (high-DPI aware)
154
+
155
+ When drawing, offset your scene by `viewRect.x` and `viewRect.y` to align the visible portion with the canvas origin:
156
+
157
+ ```js
158
+ ctx.translate(-viewRect.x, -viewRect.y);
159
+ drawEntireScene(ctx);
160
+ ctx.resetTransform();
161
+ ```
162
+
163
+ Pointer input can be mapped through the same coordinate system:
164
+
165
+ ```js
166
+ vc.addEventListener('pointerdown', (event) => {
167
+ const point = vc.clientToContent(event.clientX, event.clientY);
168
+
169
+ selectThingAt(point.x, point.y);
170
+ });
171
+ ```
172
+
173
+ ---
174
+
175
+ ## Accessibility
176
+
177
+ `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:
178
+
179
+ - Add an accessible `<div>` or `<table>` alternative outside the canvas with the same information (hidden visually with `clip-path` or similar, not `display: none`).
180
+ - Use `aria-label` or `aria-labelledby` on the `<canvas>` to describe what it shows.
181
+ - For interactive canvas content (hit-testing, drag-and-drop), implement keyboard equivalents on a separate focusable element.
182
+
183
+ ---
184
+
185
+ ## Browser support
186
+
187
+ | Feature | Requirement |
188
+ | ---------------------------- | ---------------------------------------------------------------------------------- |
189
+ | Core | Chrome 67+, Firefox 63+, Safari 12.1+ (Web Components) |
190
+ | Device-pixel-accurate sizing | Chrome 84+, Firefox 93+, Safari 15.4+ (ResizeObserver `devicePixelContentBoxSize`) |