跪拜 Guibai
← All articles
Frontend · JavaScript · GitHub

drawink: A Framework-Agnostic Canvas Library for Layering Annotations and Whiteboards onto Web Pages

By 黑土豆 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Web apps that need annotation or whiteboard features can adopt a maintained, MIT-licensed Canvas library with a shared core and Vue/React wrappers instead of reinventing coordinate mapping, undo stacks, and export. The trade-offs it documents, like object-level erasing and full-array rendering, give an honest baseline for deciding whether the approach fits your performance budget.

Summary

drawink turns a plain container into a drawing surface that sits over existing DOM content. A document mode overlays strokes on fixed pages for annotation and teaching, while an infinite mode provides a pannable, zoomable canvas that keeps world coordinates separate from screen transforms. Both modes share the same object-driven data model: brush strokes, rectangles, circles, 3D primitives like cylinders, and custom registered shapes, all backed by undo/redo with branching history.

Integration is minimal. Install @drawink/core, @drawink/vue, or @drawink/react, mount a Drawboard with a mode option, and receive serialized drawing data through onChange. Persistence uses versioned JSON for editing and PNG export for sharing; exported images contain only the annotation layer, not the underlying page content. The library handles coordinates, component lifecycle, and StrictMode cleanup, but leaves authentication, cloud storage, and collaboration to the host application.

The design splits model, renderer, and framework adapters, and the article warns that rendering iterates over all shapes and history is stored as serialized snapshots, so large workloads need real-browser testing. Custom shapes, toolbar placement, and API-driven export cover most business integration needs.

Takeaways
— drawink uses native Canvas 2D with no runtime dependencies, and shares one core among vanilla JS/TS, Vue 3.3+, and React 18+ adapters.
— Document mode overlays a transparent drawing layer on existing DOM content, while infinite mode provides a pannable and zoomable canvas with world coordinates.
— Built-in tools include brush, move, object erase, shapes (line, rectangle, circle, ellipse, triangle, diamond, trapezoid, parallelogram, cuboid, cylinder, cone), undo, redo, background, and PNG export.
— Erase deletes whole objects whose bounding boxes intersect the selection rectangle; it is not a pixel eraser and lacks rotation or multi-object grouping.
— Undo after drawing creates a new history branch that truncates the old redo path.
— Infinite mode supports 0.001%–800% zoom, autoPan when dragging near edges, and background textures with solid, dot, or grid styles.
— Adapters handle lifecycle (Vue mount/unmount, React effects and StrictMode cleanup) and sync controlled data without triggering onChange on load.
— Data persists as versioned JSON via getData() and loadData(); loading does not fire onChange to avoid feedback loops, and JSON excludes undo history and DOM content.
— PNG export filters to the drawing layer only, excludes page text/images/iframes, and fits output to 4096px per side by default.
— Custom shapes are registered per instance, stored by ID in JSON, and must be re-registered before loading a document.
— Modes: readOnly blocks editing but allows browsing/export, disabled makes the canvas ignore pointers, and penOnly accepts mouse/pen but ignores finger drawing.
Conclusions

Maintaining separate world and screen coordinates makes infinite zoom stable, but the enforced 1.5px minimum stroke width hints at a compromise to keep thin lines visible at high zoom-out.

The article's own caveat that rendering iterates all shapes and history uses serialized snapshots signals a practical ceiling: drawink suits annotation and teaching boards, not huge canvas apps with thousands of objects.

drawink's 'object eraser' trades pixel-level precision for semantic simplicity, which matches its target use of annotating lessons and documents rather than free-form image editing.

Registration of custom shapes by ID and the requirement to re-register before load indicates a deliberate design to keep serialized data small and framework-agnostic, at the cost of extra setup for host apps.

Concepts & terms
World coordinates vs screen coordinates
In infinite mode, the canvas maintains a world coordinate system for shapes and a separate camera transform for viewport pan/zoom. Input is converted from screen to world coordinates before updating objects, so zooming never alters the underlying geometry.
History branching
drawink's undo stack creates a new branch when you draw after undoing. The old redo path is discarded, so once you take a new action there is no way to redo the previously undone steps.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗