Documentation Index
Fetch the complete documentation index at: https://mintlify.com/johnfactotum/foliate-js/llms.txt
Use this file to discover all available pages before exploring further.
Overlayer is a class that places a full-size SVG element on top of a rendered page and draws annotation shapes — highlights, underlines, strikethroughs, squiggly lines, and outlines — from DOM Range objects. Renderers insert .element into the page and call .redraw() on resize; everything else is driven by your application code.
Import
Constructor
Overlayer instance. The internal SVG element is configured with position: absolute, width: 100%, height: 100%, and pointer-events: none so it sits over the page without blocking input.
Properties
.element
The SVG DOM element. Insert it into the page container so the renderer can position it automatically.
Methods
.add(key, range, draw, options)
Adds an annotation. If an annotation with the same key already exists it is replaced.
A unique key used to identify and later remove this annotation. Can be any value usable as a
Map key (string, number, object, etc.).A DOM
Range covering the text to annotate. Alternatively, pass a function (rootNode) => Range that will be called with the SVG’s root node to produce the range lazily.A draw function with signature
(rects: DOMRectList, options: object) => SVGElement. Use one of the built-in static methods or provide your own.Options forwarded directly to the
draw function (color, width, etc.)..remove(key)
Removes the annotation identified by key. Does nothing if key is not found.
The key passed to
.add()..redraw()
Redraws all annotations by recomputing client rects from their stored ranges. Called automatically by the renderer on resize. Call this yourself if the page layout changes without a resize event.
.hitTest({ x, y })
Returns the key and range of the topmost annotation at the given viewport coordinates. Iterates in reverse insertion order so more recently added annotations take priority.
Viewport x coordinate.
Viewport y coordinate.
[key, Range] when a hit is found, or [] when no annotation is at that position.
Static draw functions
Each static method receives theDOMRectList from range.getClientRects() and an options object, and returns an SVGElement (a <g> group containing one or more shapes).
Overlayer.highlight(rects, options)
Draws filled rectangles over the text. Opacity and blend mode are controlled by CSS custom properties.
Fill color for the rectangles.
0.3 via --overlayer-highlight-opacity and blend mode via --overlayer-highlight-blend-mode. Override these with CSS on a parent element.
Overlayer.underline(rects, options)
Draws thin rectangles along the baseline of each line of text.
Fill color.
Stroke width in pixels.
Set to
'vertical-rl' or 'vertical-lr' for vertical scripts; the underline is drawn on the right side of each rect instead of the bottom.Overlayer.strikethrough(rects, options)
Draws a line through the vertical midpoint of each line of text. Accepts the same options as underline.
Fill color.
Stroke width in pixels.
'vertical-rl' or 'vertical-lr' for vertical scripts.Overlayer.squiggly(rects, options)
Draws a wavy SVG path under each line of text. Accepts the same options as underline.
Stroke color.
Stroke width in pixels; also controls the amplitude of the wave.
'vertical-rl' or 'vertical-lr' for vertical scripts.Overlayer.outline(rects, options)
Draws rounded-rectangle outlines around each rect. Used by view.js for search result highlights.
Stroke color.
Stroke width in pixels.
Corner radius (
rx) of each rectangle.Custom draw functions
You can pass any function that matches the draw signature. It receives the rects fromrange.getClientRects() and your options object, and must return a single SVGElement.
Renderer interface
Renderer modules expect an overlayer object that exposes two members:| Member | Type | Description |
|---|---|---|
.element | SVGElement | Inserted into the page container and positioned automatically. |
.redraw() | function | Called by the renderer whenever the layout changes. |