@niccolofanton/corkboard 0.0.0-stage → 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/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project follows [Semantic Versioning](https://semver.org/).
4
+
5
+ ## [0.1.0]
6
+
7
+ First release, published as `@niccolofanton/corkboard`.
8
+
9
+ ### Added
10
+
11
+ - Infinite WebGL canvas of image, video and embed cards on Pixi v8 (`pixi.js` ^8.22.0 as a peer dependency), with pan, zoom and an on-demand render loop (no `requestAnimationFrame` callbacks while nothing moves).
12
+ - Viewport culling through an rbush index, so cards outside the view cost nothing.
13
+ - Level-of-detail image pipeline: ThumbHash placeholder, shared atlas, per-card textures with baked rounded corners, decoded in a worker pool under an LRU memory budget.
14
+ - Video cards: a WebGL poster plus a `<video>` element synced to the camera, with a play pool (`always` / `hover` / `off`) that adapts to the frame rate.
15
+ - Embed cards for YouTube, Vimeo, Spotify, CodePen, Figma, any page or a snippet of HTML: a poster sprite that becomes a sandboxed iframe at a virtual 720p / 1080p screen, with live / parked / culled lifecycle, runtime posters and snapshots.
16
+ - Auto layout (masonry, tidy grid, align, distribute, equal width) with animated transitions.
17
+ - Interaction: selection and marquee, drag, resize and rotate, smart-guide and dot-grid snapping, keyboard shortcuts, hand tool.
18
+ - Typed events (`selection`, `hover`, `click`, `dblclick`, `transform`, `gesture`, `delete`, `change`, `camera`, `cameraSettled`, `embedInteract`, `embedLive`, `embedResolved`) and a `stats()` API.
19
+ - Extension points: custom element kinds (views and DOM overlays), light / dark theme, optional CSP-safe mode.
20
+ - Single bundled `dist/index.d.ts` that resolves under `node16`, `nodenext` and `bundler` module resolution.
21
+ - Decode worker inlined as a blob worker; also exported as `@niccolofanton/corkboard/decode.worker.js` for pages whose CSP forbids blob workers (`textures.workerUrl`).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Niccolò Fanton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,186 @@
1
- # Temporary Holding Version
1
+ # Corkboard
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ![Corkboard](docs/preview.gif)
4
+
5
+ Corkboard is a framework-agnostic TypeScript library that puts an infinite WebGL canvas of image, video and embed cards on screen, built on Pixi v8 and extracted from a moodboard app. It is meant for large boards on an ordinary laptop, so most of the work is in not doing work: the render loop is on demand and sits at 0 requestAnimationFrame callbacks while nothing moves, and an rbush index culls cards outside the view.
6
+
7
+ Pictures go through a level-of-detail pipeline that starts at a ThumbHash, moves to a shared atlas, and ends in per-card textures with baked rounded corners, all decoded in workers and kept under an LRU memory budget. A video card is its poster in WebGL plus a `<video>` element synced to the camera; an embed (YouTube, Vimeo, Spotify, CodePen, Figma, any page or a snippet of HTML) is a poster sprite that becomes a sandboxed iframe, laid out at a virtual 720p or 1080p screen and scaled down, only when it is large on screen or selected. Auto layout (masonry, tidy grid, align, distribute) animates cards to their new places, and dragging snaps to smart guides or to the dot grid.
8
+
9
+ **Live: [corkboard.niccolofanton.dev](https://corkboard.niccolofanton.dev)**
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm i @niccolofanton/corkboard pixi.js
15
+ ```
16
+
17
+ `pixi.js` (^8.22.0) is a peer dependency: one copy, shared with your app. The package is ESM-only (`import`, no
18
+ `require`), ships its own TypeScript declarations, and is tree-shakeable (`sideEffects: false`).
19
+
20
+ Browser support: any browser with WebGL2. Snapshots of HTML embeds (see [Embeds](#embeds)) are not available in Safari,
21
+ where the frame simply stays live.
22
+
23
+ ## Quick start
24
+
25
+ ```ts
26
+ import { Corkboard } from '@niccolofanton/corkboard';
27
+
28
+ async function main(): Promise<void> {
29
+ const board = await Corkboard.create(document.getElementById('board')!, { theme: 'dark' });
30
+ board.add([
31
+ { media: { src: 'https://picsum.photos/seed/1/480/600', aspect: 1.25 } },
32
+ { media: { src: 'https://picsum.photos/seed/2/480/270', video: 'https://example.com/clip.mp4' } },
33
+ ]);
34
+ board.layout.tidy();
35
+ board.camera.fit();
36
+ board.on('selection', ({ ids }) => console.log(ids));
37
+ }
38
+ void main();
39
+ ```
40
+
41
+ `Corkboard.create` is async (it starts the WebGL renderer). A top-level `await` works too where your build target allows it
42
+ (Vite's default target does not: set `build.target: 'es2022'`).
43
+
44
+ Pictures are read by a decode worker with `fetch`, so they need CORS headers. Videos only need to be playable by `<video>`.
45
+
46
+ ## Bundlers & CSP
47
+
48
+ The decode worker is inlined in the library and started from a blob URL, so it works under any bundler or dev server (no
49
+ asset URL to resolve, no `optimizeDeps` setting). A page whose CSP has no `worker-src blob:` (or `child-src blob:` /
50
+ `script-src blob:` as the fallback) can serve the worker itself instead: the package exports it as
51
+ `@niccolofanton/corkboard/decode.worker.js`. Pass its URL as `textures.workerUrl`:
52
+
53
+ ```ts
54
+ import workerUrl from '@niccolofanton/corkboard/decode.worker.js?url'; // Vite (types from vite/client); any bundler that gives you the file's URL
55
+ await Corkboard.create(el, { textures: { workerUrl } });
56
+ ```
57
+
58
+ For a CSP without `unsafe-eval`, also pass `cspSafe: true` (Pixi's `unsafe-eval` build).
59
+
60
+ ## API
61
+
62
+ - `Corkboard.create(host, options)`: `theme`, `grid`, `snapping`, `interaction`, `video`, `embeds`, `textures`, `overlays`, `camera`, `cspSafe`.
63
+ - Cards: `add(init | init[])`, `update(id, patch)`, `remove(ids)`, `clear()`, `get(id)`, `all()`, `size`, `inRect(rect)`, `bounds(ids?)`.
64
+ - Selection: `selection`, `select(ids)`, `selectAll()`, `clearSelection()`.
65
+ - `board.layout`: `tidy(ids?, opts)`, `masonry(ids?, opts)`, `align(mode, ids?)`, `distribute(axis, ids?)`, `equalWidth(ids?)`.
66
+ - `board.camera`: `get()`, `set(cam)`, `fit(ids?)`, `zoomTo(rect)`, `zoomBy(k)`, `actual()`, `center(x, y, scale?)`, `visible()`, `screenToWorld`, `worldToScreen`.
67
+ - Settings: `setTheme`, `setGrid`, `setSnapping({ guides, grid })`, `setVideo({ mode: 'always' | 'hover' | 'off', loop, muted, minPlayPx, maxPlaying, maxPlayingTouch })`, `setVideoPaused`, `setEmbeds({ maxLive, maxLiveTouch, maxMounted, maxOffscreen, cullMargin, minLivePx, minLiveScale, maxLoading, freezePx, maxAnimating, adaptive, resolution, arbitrarySameOrigin, autoplay, previewWorkers, previewCache })`.
68
+ - Events (`board.on(type, fn)` returns an unsubscribe): `selection`, `hover`, `click`, `dblclick`, `transform`, `gesture`, `delete`, `change`, `camera`, `cameraSettled`, `embedInteract`, `embedLive`, `embedResolved`.
69
+ - `stats()`, `dispose()`. The pure layout and snapping functions, `Store`, `Stage` and `Interaction` are exported too.
70
+
71
+ - `textures`: `budgetBytes`, `workers`, `workerUrl`, `uploadBudgetMs`, `overscan`, `softCanvas`.
72
+ - `camera`: `minScale`, `maxScale` (default 0.05..4). A board much wider than the screen that must fit whole needs a lower minimum.
73
+ - `interaction`: `select`, `emptyDrag`, `keyboard`, `commit`, and for a host that draws its own chrome `marquee: false` (a drag
74
+ on empty canvas always pans), `keepSelectionOnPan` (a pan keeps the selection, a click that does not move clears it) and
75
+ `selectionOutline: false` (the canvas draws no selection outline or handles). `board.interaction.setPanMode(true)` is a hand
76
+ tool: a left-button drag pans from anywhere, over a card included, like holding Space.
77
+ - `video` (`mode: 'always'`): a card plays on its own only if its long side is at least `minPlayPx` CSS px on screen
78
+ (default 160), and at most `maxPlaying` play at once (default 8; `maxPlayingTouch`, default 2, on touch), the biggest and
79
+ most central first. Smaller or surplus cards stay a thumbnail, so a zoomed-out board opens no streams. The pool shares the
80
+ frame budget of the embeds: a slow window (under 40 fps) cuts the cap to 60% of what plays (at least 1, the most visible
81
+ clips keep playing) and two smooth ones in a row give 50% back, up to `maxPlaying`. `board.videoBudget` is the cap now.
82
+ - `camera.fit()` on a host that has no size yet (hidden tab, pane not laid out) waits for the first real size and then
83
+ centres, unless the camera was moved meanwhile.
84
+ - Keyboard shortcuts act only while the board itself has focus, never for keys typed in an `<input>` inside the host.
85
+
86
+ A card is `{ media: { src, video?, sizes?, aspect?, thumbhash?, color? }, x?, y?, w?, h?, rot?, radius?, locked?, aspectLocked?, data? }`.
87
+ Cards with no `x` / `y` are placed on a free spot near what is on screen (a given `x` or `y` alone is kept, and an embed
88
+ without `w` is 400 wide).
89
+
90
+ ## Embeds
91
+
92
+ ```ts
93
+ const board = await Corkboard.create(el, {
94
+ embeds: { maxLive: 12, resolve: async (url) => (await fetch(`/api/embed?url=${url}`)).json() },
95
+ });
96
+ board.add({ media: { embed: { url: 'https://youtu.be/dQw4w9WgXcQ' } } }); // nothing else to set up
97
+ board.add({ media: { embed: { url: 'https://example.com/post', title: 'A post' }, aspect: 0.75 } });
98
+ board.add({ media: { embed: { html: '<button onclick="this.textContent=1">0</button>' } } }); // a snippet
99
+ ```
100
+
101
+ - `media.embed` is `{ url }` or `{ html }` (+ `title`). `media.aspect` is height / width, as everywhere; a known provider has its own.
102
+ `media.src` is the poster: leave it out and the board resolves one (the card is a textured sprite like any other).
103
+ What the board generates (tile, fetched thumbnail, snapshot, preview) is a runtime picture on the stage
104
+ (`stage.setPosterOverride`), never written to your store: no `change` event, nothing dead to save, and swapping one poster
105
+ for another keeps the old one on screen until the new one has decoded (no flash of the placeholder).
106
+ - Poster, first hit wins: your `src`; `embeds.resolve(url)` (`{ poster?, iframeSrc?, aspect?, title?, frameable? }`); YouTube
107
+ thumbnails; a generated tile (host, title, glyph, themed). oEmbed titles / thumbnails, `og:image`, screenshots and
108
+ `X-Frame-Options` probing need a server: do them behind `resolve` and cache the result. `frameable: false` makes a bookmark
109
+ card (never live, a double-click opens the URL in a new tab).
110
+ - Live iframe: any card in view (`minLivePx` and `minLiveScale` are opt-in floors, 0 by default) or selected, camera still, at
111
+ most `maxLive` at once (12, or 3 on touch), the biggest on screen first. Frames mount in chunks (`maxLoading`, default 6, at a
112
+ time, nearest the centre first). It is laid out at world size and moved with the camera as one CSS matrix. A frame that stops
113
+ qualifying is parked (inert, still showing its last frame) rather than destroyed; `maxMounted` frames (24, or 6 on touch) stay
114
+ in all, and the least valuable parked ones are destroyed past that.
115
+ - Culling: a mounted iframe whose card leaves the view (plus a `cullMargin` of 50 % of the viewport on each side) is hidden at
116
+ once (`display: none`, so it stops rendering but keeps its state) and shows again the moment the card returns. A loaded
117
+ snippet is snapshotted on the way out so its last frame is the poster. At most `maxOffscreen` hidden frames are kept (64, or
118
+ 12 on touch), the most recently seen first; the rest are destroyed when the camera settles.
119
+ - Freezing: a mounted frame whose card gets smaller than `freezePx` on screen (default 140 CSS px, short side; 0 turns it
120
+ off; raised up to 4x while the page stays slow with the animating budget at its floor, `board.embeds.freezeThreshold`) and is not selected or in interact mode stops costing anything. A snippet is asked (by a small script injected ahead
121
+ of its HTML) for a snapshot of its own document, current animation state included; that image becomes the card's poster
122
+ and the frame is hidden (`display: none`, alive, so it resumes where it was). A YouTube or Vimeo player is paused through
123
+ its postMessage API (it keeps showing the current video frame) and played again afterwards. Both come back above 1.4x
124
+ `freezePx`, when selected, or on interact. Parked and culled snippets are paused (animations and `requestAnimationFrame`),
125
+ parked and culled videos too. A snapshot that fails (a tainted canvas, Safari) leaves the frame live. `board.embeds.frozen`.
126
+ - Animating budget: of the mounted frames that are on screen, not parked, culled or frozen, the ones that can be paused
127
+ (snippets, YouTube / Vimeo players that may be playing) are ranked (interacting, selected, biggest on screen, nearest the
128
+ centre) and only the first N animate; the rest are paused and keep showing their last frame. Mounting is not affected.
129
+ `maxAnimating` is the ceiling (default `Infinity`); with `adaptive` (default `true`) the board's frame-rate monitor lowers N
130
+ to 70% of what animates (min 6) after a second under 40 fps and raises it by a quarter + 2 after two seconds in a row over
131
+ 55 fps, up to the ceiling. The monitor does not run a rAF loop all the time: it samples in short bursts (4 one-second
132
+ windows at most, or until two in a row agree) started by anything that can change the load (camera settled, frames mounted,
133
+ destroyed, frozen or thawed, a budget moved) and by a 10 s heartbeat while something renders in a visible tab, then stops so
134
+ the page can idle. Pages that cannot be paused (plain URLs, Spotify, CodePen, Figma) take no slot. `board.embeds.animating`,
135
+ `board.embeds.animatingBudget`.
136
+ - Previews: an HTML snippet card that has not been live shows a generated tile until `embeds.previewWorkers` hidden frames
137
+ (default 3, 0 turns it off) render it in the background (nearest the view first, camera still, 400 ms in) and its poster
138
+ becomes a real picture of the snippet. They take no live or loading slot. Pictures are cached by a hash of the snippet,
139
+ in memory and, with `embeds.previewCache: 'persistent'` (opt-in, default `'memory'`: it leaves pictures of the snippets on the
140
+ visitor's disk), in Cache Storage (at most 1000, the oldest dropped first) so a reload shows them at once.
141
+ `board.embeds.previewsPending`.
142
+ - Resolution: a live page is laid out at a virtual screen with the card's aspect, then scaled onto the card, so it shows
143
+ its desktop layout at any card size. `embeds.resolution` is `'720p'` (default: short side 720 CSS px, a 16:9 card is a
144
+ 1280 x 720 page), `'1080p'` (1920 x 1080), a number (the short side) or `'native'` (the card's world size: a 400 px card
145
+ is a 400 px wide page). `media.embed.resolution` overrides it per card; `setEmbeds({ resolution })` changes it live.
146
+ - It does not take pointer events: pan, zoom, select and drag work over it. Double-click (or Enter on a selected card) enters
147
+ interact mode; Done, Esc or a click outside ends it (`embedInteract`). Keys and wheel inside a cross-origin frame never reach
148
+ the page.
149
+ - Sandbox, computed at render time and never stored: allowlisted providers get `allow-scripts allow-same-origin allow-forms
150
+ allow-popups allow-presentation`; other URLs the same minus `allow-presentation`; snippets run in `srcdoc` with `allow-scripts`
151
+ only plus a CSP (no network). Only http(s) URLs load. A snippet has no storage and cannot reach the page.
152
+ The allowlist is the exact embed URLs the built-in parsers emit (`youtube-nocookie.com/embed/`, `player.vimeo.com/video/`,
153
+ `open.spotify.com/embed/`, `codepen.io/<user>/embed/`, `figma.com/embed`), not whole hosts.
154
+ - Arbitrary URLs keep `allow-same-origin` because many sites break without their own cookies and storage. The risk: the
155
+ sandbox flags survive navigation inside the frame, so a link followed in interact mode loads the next site with scripts
156
+ and its real origin (it still cannot reach your page; a frame on your own origin never gets the flag). To give those frames
157
+ an opaque origin instead, pass `embeds: { arbitrarySameOrigin: false }` (also `setEmbeds`).
158
+ - A `blob:` `media.src` on an embed card (an old saved board) is treated as absent, since it died with its page: a fresh poster
159
+ is made for it and `embedResolved.poster` never reports a blob. A host's https / data: `media.src` stays the poster of a
160
+ tile or fetched thumbnail; a snapshot or preview of a snippet, which is the real picture, goes over it. Neither is written
161
+ to `media.src`. Persist `embedResolved.poster` (an https URL), not blobs, when you save a board.
162
+ - Live state: `board.embeds.live`, `board.embeds.interacting`, `board.embeds.interact(id)`, `board.embeds.info(id)`.
163
+
164
+ ## Adding an element kind
165
+
166
+ `ElementKind` (`src/types.ts`) is a union. A new kind is one view in `src/render/views/registry.ts` and, if it needs live DOM,
167
+ one `Overlay` (`src/overlay.ts`) passed through `options.overlays`.
168
+
169
+ ## Docs
170
+
171
+ - [docs/HANDOFF.md](docs/HANDOFF.md): how the renderer works (stage, culling, LOD textures, atlas, video, layout, snapping, perf budget, lessons learned).
172
+ - [docs/HTML-EMBEDS.md](docs/HTML-EMBEDS.md): research on HTML / iframe cards in a WebGL canvas and the design chosen.
173
+
174
+ ## Development
175
+
176
+ ```sh
177
+ pnpm install # pnpm 10.11.1 (see packageManager); pixi.js is a devDependency here
178
+ pnpm dev # the demo (600 cards)
179
+ pnpm test
180
+ pnpm typecheck
181
+ pnpm build # dist/: library, bundled index.d.ts, decode.worker.js
182
+ ```
183
+
184
+ ## License
185
+
186
+ MIT