Skip to content

feat: crisp canvas vector rendering + reader UX - #28

Open
pgebheim wants to merge 8 commits into
raaneem:mainfrom
lilco-dev:pr/rendering-overhaul
Open

pgebheim wants to merge 8 commits into
raaneem:mainfrom
lilco-dev:pr/rendering-overhaul

Conversation

@pgebheim

@pgebheim pgebheim commented Nov 30, 2026 •

Copy link
Copy Markdown

Crisp canvas vector rendering + reader UX

Renders notebook ink as real anti-aliased vector strokes drawn on a canvas,
instead of the device's 1-bit RLE ink raster — which reads as dotted /
un-antialiased when scaled. Pages are noticeably crisper, especially when
zoomed.

Also includes reader improvements that pair with it:

  • Resolution-independent page SVGs (viewBox only)
  • Fit-width reading mode + wheel page-turn
  • Open notes at page 1 instead of the device's last-viewed page

Depends on #24 (the supernote-typescript 0.7.3 upgrade) — this
uses 0.7.3's vector-ink API. Opened with that branch as the base; will retarget
to main once it merges.

The canvas-vector technique is credited to
supernote-obsidian-plugin
by Brandon Philips (MIT) — reimplemented here on supernote-typescript's
vector-ink, no code copied.

Test plan: pnpm test — page-svg / rasterizer / vector-render tests pass
(464 passed). Rendering verified manually in Obsidian.


Extracted from the
Supernote Sync & Transcribe
fork.

The parser is two major lines newer (0.3.0 -> 0.7.3), bringing the
stroke-decoding improvements the newer renderer is known for (eraser
gestures no longer drawn, recognition/textbox improvements, and the
deliberate gray-level swap so darkGray < gray in lightness, matching the
device). The plugin's parser surface is just two files, and the vendored
Ratta RLE compositor in notebook-rasterizer.ts is byte-compatible with
0.7.3's _walkRuns — the only sync needed was the palette swap.

- package.json + lockfile: 0.7.3; the image-js exclusion override is
  re-pinned to supernote-typescript@0.7.3 (the parser itself stays
  image-js-free, verified — the worker-ownership lockfile test passes).
- notebook-rasterizer: vendored palette updated to 0.7.3's gray levels
  with a comment pointing at the upstream change.
- tests: two synthetic fixtures updated to the new gray values.

NOTE: real-note rendering changes intentionally (grays shift to the
0.7.x levels). The skipped real-note RGBA-hash test (private fixture)
will need its recorded hash regenerated by someone with the fixture.

pnpm format/lint/typecheck/460 tests/build green.
Foundation for crisp-at-any-zoom page display. The worker can now render
a page as a standalone SVG: the raster background (templates/images,
ink layers removed via buildVectorInkBackgroundNote) with the pen
strokes drawn as real vector elements from supernote-typescript 0.7.x's
vector-ink machinery (prepareVectorInkPages + buildVectorInkPrimitives).
Pages whose strokes don't decode trustworthily return null so the
caller falls back to the raster bitmap.

- New render output "svg" in the worker protocol; renderSvg() in the
  worker runtime.
- page-svg.ts: vendored SVG document builder (background data URI +
  rect/filledPath/strokedPath elements). Vendored rather than imported
  from supernote-typescript's svg.ts, which pulls pdf.js -> pdf-lib -
  banned from the notebook worker bundle (esbuild assertAbsent).
- image-js stub + esbuild/vitest aliases: vector-ink/conversion import
  image-js at module scope, but the used code paths never call it, and
  the lockfile stays image-js-free (worker-ownership test). The stub
  throws on any real invocation.
- mobile-boundary test: assert on the image-js dependency rather than
  the word (supernote-typescript comments mention it), alias its own
  esbuild build, and raise the unminified worker cap 100KB -> 160KB
  (vector machinery costs ~50KB unminified, ~40KB minified; production
  main.js stays at 3.43MB, well under its 4MB cap).
- 5 unit tests for the SVG builder.

Reader/service integration (displaying the SVG) lands separately.
Adds a display-path SVG render alongside renderPng: PendingSvg follows
the uncached PendingPng pattern (promise-based, no bitmap cache), at
currentPage priority since it's for the visible page, with a two-page-
buffer memory reservation. The session lease gains renderSvg(), and
the worker-response dispatch resolves svg outputs.

Also removes retainedBytes(), pre-existing dead code (nothing called
it; snapshot() computes the same total inline).
The reader now displays vector-ink pages as an SVG <img> (crisp at any
zoom) instead of a CSS-scaled raster canvas. Each PageSurface gains an
<img> alongside its canvas - exactly one is visible at a time:

- renderSurface tries session.renderSvg() first; pages without
  trustworthy vector ink (null) fall back to the raster bitmap path.
- drawSurfaceSvg shows the img (SVG blob URL), releases the canvas
  backing so vector pages hold no canvas memory; drawSurfaceBitmap
  resets to the canvas for raster pages.
- The zoom/pan transform (translate + scale) and its animation classes
  now apply to both display elements, so whichever is visible zooms
  identically - and the SVG stays sharp because it's resolution-
  independent, not a magnified bitmap.
- setTransformAnimation widened to HTMLElement (canvas or img).
- Blob URLs are revoked on page swap and surface disposal.

This is the payoff of the parser upgrade + worker/service SVG plumbing:
handwritten strokes now render as real vector paths, smooth at any zoom.
Fit-width is a new default reading mode: pages render at viewport width
and scroll vertically with native scroll, instead of fit-page
letterboxing. A toolbar toggle switches between fit-width and fit-page.

- Two clean modes: fit-width (native scroll, transform zoom reset) and
  fit-page (whole page + transform zoom). Zooming in fit-width switches
  to fit-page first, so the two interaction models never fight.
- Desktop is crisp for free (the canvas backing is already native-res);
  the SVG vector-ink pages are resolution-independent either way.
- Wheel page-turn in fit-page at zoom=1: vertical wheel turns pages,
  with an accumulator + cooldown so a trackpad fling turns once.
- Also fixes two pre-existing lint issues in notebook-worker-runtime.ts
  (bare 'object' param, uncommented 'as unknown as' cast).

Stacked on feat/vector-ink. NEEDS VISUAL VERIFICATION - the mode toggle,
scroll feel, and page-turn gestures should be tried in a real vault.
The page SVG root carried fixed width/height (native page px), so the
browser rasterized the <img> once at that intrinsic size and magnified
the resulting bitmap whenever the page was shown larger - fit-width on a
wide viewport, or fit-page transform-zoom - which read as strokes going
'dotted'. Dropping the fixed width/height (keeping the viewBox, which
still provides the intrinsic aspect ratio) makes the browser rasterize
the SVG at the display size instead, so the vector strokes stay crisp at
any size.

Confirmed via the vector-ink pipeline that the strokes themselves are
smooth (225 filledPath primitives with 4-269-point rings on the test
note) - the generation was never the problem, only the fixed-size
rasterization.
…sed)

Replaces the SVG <img> approach with philips' rendering technique: draw
the vector strokes into the page raster via canvas in the worker, so the
strokes are anti-aliased at render time instead of depending on the
browser's SVG rasterizer (which came out aliased under Obsidian's GPU
compositing - the 'dotted / no anti-aliasing' the SVG approach produced).

- notebook-vector-render.ts: renderVectorPage rasterizes the ink-less
  background, then draws the vector-ink primitives (rect/filledPath/
  strokedPath) onto an OffscreenCanvas via Path2D. The canvas
  anti-aliases the strokes. Returns null for non-vector pages so the
  caller falls back to the plain raster. Verified anti-aliased by
  rendering a real note in chromium.
- The worker's decode() uses renderVectorPage for vector-eligible pages,
  so both the display bitmap and the export PNG get crisp strokes.
- Reverts the reader's SVG <img> display back to the plain bitmap path,
  and removes the now-dead SVG plumbing (renderSvg in the service,
  the svg worker output + protocol types, page-svg.ts).
- drawPrimitives is unit-tested with a mock canvas context.

Keeps fit-width, wheel page-turn, and the parser upgrade. The image-js
stub stays (vector-ink still imports it via conversion.js).
A fresh reader open fell back to session.descriptor.devicePage
(FINALOPERATION_PAGE - the page the note was last on when used on the
device), so any note read to the end on the device reopened on its last
page. Fresh opens now start at page 1; reloads of the same notebook
still keep the reader's current page (previous.currentPage), and
explicit page navigation (pendingPage) is unaffected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant