Text watermark plugin sample
The afterDecode hook draws a text watermark onto every page after the viewer has decoded it. It hands back the image it drew, so the page reaches the canvas without the second decode and the lossy re-encode that transforming the fetched JPEG would cost.
A plugin changes how a page is fetched and transformed, never how it is styled, so the reader below is composed exactly as it is on the entry point.
Source code
1"use client";23import * as ComicViewer from "@publira/comic-viewer";4import type { DecodedPageImage, ViewerPage } from "@publira/comic-viewer";5import { ChevronLeft, ChevronRight } from "lucide-react";67/** Draws the watermark onto the decoded page and hands back the new image. */8const addWatermark = (image: DecodedPageImage): Promise<ImageBitmap> => {9 const height = "naturalHeight" in image ? image.naturalHeight : image.height;10 const width = "naturalWidth" in image ? image.naturalWidth : image.width;11 const canvas = document.createElement("canvas");12 canvas.height = height;13 canvas.width = width;1415 const context = canvas.getContext("2d");16 if (context === null) {17 throw new Error("Canvas 2D rendering is unavailable.");18 }1920 context.drawImage(image, 0, 0);21 context.fillStyle = "rgba(0, 0, 0, 0.45)";22 context.fillRect(0, height - 96, width, 96);23 context.fillStyle = "rgba(255, 255, 255, 0.88)";24 context.font = "600 32px system-ui, sans-serif";25 context.textAlign = "right";26 context.textBaseline = "middle";27 context.fillText("PUBLIRA DEMO", width - 40, height - 48);2829 // The viewer releases the image this one replaces, so the page it was drawn30 // from is not closed here.31 return createImageBitmap(canvas);32};3334/**35 * The page has already been decoded, so the watermark is drawn onto the image36 * itself instead of being re-encoded as a JPEG for the viewer to decode again.37 */38const watermarkPlugin = ComicViewer.definePlugin({39 afterDecode: ({ image }) => addWatermark(image),40 name: "text-watermark",41});4243const navigationButtonClassName =44 "pointer-events-auto absolute top-1/2 grid size-10 -translate-y-1/2 place-items-center rounded-full bg-black/60 p-0 text-slate-100 shadow-lg outline-offset-2 outline-slate-100 transition hover:bg-black/80 focus-visible:outline-2 disabled:cursor-not-allowed disabled:opacity-50";4546/**47 * The page-turn buttons over the page. The logical `start-3` and `end-3` place48 * them on the sides the reading direction gives them, and the icons point the49 * way a page turn goes, which the viewer context reports.50 */51const PageNavigation = () => {52 const { readingDirection } = ComicViewer.useViewerContext();53 const PreviousIcon = readingDirection === "rtl" ? ChevronRight : ChevronLeft;54 const NextIcon = readingDirection === "rtl" ? ChevronLeft : ChevronRight;5556 return (57 <ComicViewer.PageNavigation className="pointer-events-none absolute inset-0 z-10 transition duration-150 ease-out aria-hidden:translate-y-2 aria-hidden:opacity-0">58 <ComicViewer.PreviousPageButton59 className={`start-3 ${navigationButtonClassName}`}60 >61 <PreviousIcon aria-hidden="true" className="size-6" />62 </ComicViewer.PreviousPageButton>63 <ComicViewer.NextPageButton64 className={`end-3 ${navigationButtonClassName}`}65 >66 <NextIcon aria-hidden="true" className="size-6" />67 </ComicViewer.NextPageButton>68 </ComicViewer.PageNavigation>69 );70};7172interface WatermarkedReaderProps {73 pages: readonly ViewerPage[];74}7576/**77 * A plugin changes how a page is fetched and transformed, never how it is78 * styled, so the tree below is the one the entry point is built from.79 */80export const WatermarkedReader = ({ pages }: WatermarkedReaderProps) => (81 <ComicViewer.Root82 className="relative flex h-full min-h-0 w-full min-w-0 overflow-hidden rounded-xl bg-slate-950 text-slate-100 shadow-2xl shadow-slate-950/30"83 pages={pages}84 plugins={[watermarkPlugin]}85 >86 {/* The track is three viewports wide and turns pages by translating itself.87 The page set, the slot, and the page each align the half of the spread they88 hold through the `data-page-side` attribute the rail reports. */}89 <ComicViewer.Viewport className="group/viewport relative flex min-h-0 min-w-0 flex-1 touch-pan-y overflow-hidden data-[pannable]:cursor-grab data-[pannable]:touch-none data-[panning]:cursor-grabbing">90 <ComicViewer.ViewportTrack className="flex h-full w-[300%] shrink-0 basis-[300%] [transform:translateX(calc(-33.3333%_+_var(--pcv-drag-offset)))] data-[dragging]:transition-none data-[transition-state=active]:transition-transform data-[transition-state=active]:duration-[260ms] data-[transition-state=active]:ease-out data-[transition-state=active]:data-[slide-direction=left]:[transform:translateX(calc(-66.6667%_+_var(--pcv-drag-offset)))] data-[transition-state=active]:data-[slide-direction=right]:[transform:translateX(var(--pcv-drag-offset))]">91 {/* The pan and the zoom of a pinch gesture apply to the spread on screen. */}92 <ComicViewer.ViewportPageSet className="flex h-full min-w-0 shrink-0 basis-1/3 data-[page-side=left]:justify-start data-[page-side=right]:justify-end data-[rail-slot=current]:[transform:translate(var(--pcv-pan-x,0)_var(--pcv-pan-y,0))_scale(var(--pcv-zoom-scale,1))]">93 {/* A page that is a whole spread on its own keeps both halves of the set,94 so the double-page basis it would otherwise take is given back to it. */}95 <ComicViewer.ViewportPageSlot className="flex min-w-0 flex-1 items-center justify-center data-[page-side=left]:justify-end data-[page-side=right]:justify-start data-[view-mode=double]:max-w-1/2 data-[view-mode=double]:basis-1/2 data-[view-mode=double]:data-[page-layout=spread]:max-w-full data-[view-mode=double]:data-[page-layout=spread]:basis-full">96 <ComicViewer.ViewportPage className="flex h-full w-full min-w-0 items-center justify-center data-[page-side=left]:justify-end data-[page-side=right]:justify-start">97 {/* The page fit mode the viewport reports is what sizes the image. */}98 <ComicViewer.PageCanvas className="h-full max-w-full bg-slate-900 object-contain transition-[filter] duration-150 group-data-[page-fit-mode=actual]/viewport:h-auto group-data-[page-fit-mode=actual]/viewport:w-auto group-data-[page-fit-mode=actual]/viewport:max-w-none group-data-[page-fit-mode=width]/viewport:h-auto group-data-[page-fit-mode=width]/viewport:w-full group-data-[page-fit-mode=width]/viewport:max-w-none data-[placeholder]:brightness-75 data-[placeholder]:saturate-75" />99 </ComicViewer.ViewportPage>100 </ComicViewer.ViewportPageSlot>101 </ComicViewer.ViewportPageSet>102 </ComicViewer.ViewportTrack>103 </ComicViewer.Viewport>104 {/* The toolbar slides out of sight with the `aria-hidden` the viewer sets105 on it while the reader controls are at rest. */}106 <ComicViewer.Toolbar className="absolute inset-x-0 bottom-0 z-10 flex items-center gap-2 bg-linear-to-t from-black/80 via-black/55 to-transparent px-3 pt-8 pb-3 transition duration-150 ease-out aria-hidden:translate-y-2 aria-hidden:opacity-0">107 {/* The slider reports the share of the document its thumb rests at as108 `--pcv-page-progress-fill`, which paints the part of the track behind it,109 and the fill runs the way the reader turns pages. */}110 <ComicViewer.PageProgress className="mx-auto min-w-0 shrink basis-3/5">111 <ComicViewer.PageProgressSlider className="block h-3.5 w-full cursor-pointer appearance-none bg-transparent p-0 outline-offset-4 outline-slate-100 [--pcv-page-progress-fill-direction:to_right] focus-visible:outline-2 disabled:cursor-not-allowed disabled:opacity-50 rtl:[--pcv-page-progress-fill-direction:to_left] [&::-moz-range-thumb]:size-3.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-slate-100 [&::-moz-range-track]:h-1 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-black/65 [&::-moz-range-track]:[background-image:linear-gradient(var(--pcv-page-progress-fill-direction),var(--color-slate-100)_var(--pcv-page-progress-fill),transparent_var(--pcv-page-progress-fill))] [&::-webkit-slider-runnable-track]:h-1 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-black/65 [&::-webkit-slider-runnable-track]:[background-image:linear-gradient(var(--pcv-page-progress-fill-direction),var(--color-slate-100)_var(--pcv-page-progress-fill),transparent_var(--pcv-page-progress-fill))] [&::-webkit-slider-thumb]:-mt-[0.3125rem] [&::-webkit-slider-thumb]:size-3.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-slate-100" />112 <ComicViewer.PageStatus className="mt-1.5 block text-center text-sm text-slate-100" />113 </ComicViewer.PageProgress>114 </ComicViewer.Toolbar>115 <PageNavigation />116 </ComicViewer.Root>117);