$ show jonobr1/two.js
Two.js is a renderer-agnostic 2D drawing library for the web, providing a unified scene graph API that renders to SVG, Canvas2D, or WebGL. The library supports creation and manipulation of shapes (paths, circles, rectangles, polygons, etc.), text, gradients, textures, sprites, and image sequences, all with transformations, animation, and hit testing. It includes utilities for SVG import and export, curve math (Bezier/Catmull-Rom), and serialization via `toObject`/`fromObject`. The architecture is modular: a core `Two` class manages the scene and animation loop, with a hierarchy of elements (Element → Shape → Path/Text/Points), a Group container for scene graph nesting, and separate renderer implementations for each target. Key entry points are the `Two` constructor, factory methods (`makeCircle`, `makePath`, etc.), `update()`, `play()`, and `pause()`. The library has no external runtime dependencies beyond standard browser APIs, though optional `node-canvas` enables headless rendering. Notably, the codebase demonstrates high craftsmanship with a flag-based dirty tracking system for efficient updates, an event system with cross-object observation, and a collection pattern with mutation events. However, it shows maintenance drift through some outdated development dependencies (e.g., `babel-eslint`, `underscore`) and manually maintained TypeScript declarations that may lag behind the source.
The LLM identified 4 files worth highlighting for reuse:
Provides a lightweight, self-contained event system (`addEventListener`, `removeEventListener`, `dispatchEvent`) with aliases (`on`, `off`, `bind`, `unbind`, `trigger`). Includes `listen`/`ignore` patterns for cross-object observation without tight coupling. Reusable as a standalone mixin for any object needing event capabilities.
Implements a 3×3 transformation matrix with operations for multiply, inverse, identity, scale, rotate, translate, skewX/Y, and decompose. Includes static helpers (`Multiply`, `Transpose`) and edge-case handling for degenerate matrices. Can be extracted for any project requiring affine 2D transformations.
Contains pure math functions for hit testing: `pointInPolygons` (ray casting), `distanceToSegments` (closest point on line), and utilities for building path bounding geometry. No internal dependencies; suitable for any graphics or interactive application needing containment or proximity checks.
Provides robust curve mathematics including Bezier subdivision (de Casteljau), arc length via Gauss-Legendre integration, bounding box computation, and Catmull-Rom spline estimation. Implements `Curve` constants and static methods that are reusable for any 2D vector graphics library or path manipulation tool.