canvas-color-tracker is a utility library (current stable version 1.3.2) designed to facilitate interaction with dynamically rendered objects on an HTML5 canvas, where native object-specific mouse events are not available. It provides a system for tracking canvas elements by assigning each a unique, invisible color key on a 'shadow' or off-screen canvas. Developers render their objects on this shadow canvas with these unique colors, then use `mousemove` events on the main canvas to sample the pixel color under the mouse pointer. This color is then used to look up the associated object in the `canvas-color-tracker` registry. A key feature is its checksum encoding mechanism for color keys, which enhances lookup reliability by accounting for pixel anti-aliasing and color mutations at object boundaries. The library focuses solely on the registry aspect: generating keys, registering objects, and performing lookups. It enables effective object identification and interaction for complex canvas applications, supporting up to approximately 262,000 objects with default settings.
npm install canvas-color-trackerVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize ColorTracker, register an object with a unique color, draw it on both a main and a hidden shadow canvas, and then look up the object based on a sampled pixel color from the shadow canvas, simulating a hover event.
Adjust `new ColorTracker(checksum_bits)` based on your application's requirements for object count versus collision robustness. For example, `new ColorTracker(4)` allows ~1 million objects.
Always check the return value of `myTracker.register(obj)`. If `null` is returned, the object was not added, and you should consider increasing the registry capacity by reducing `checksum_bits` if feasible, or implementing a strategy to manage object registration.
Ensure your shadow canvas rendering is as consistent as possible. If issues persist, consider disabling anti-aliasing for the shadow canvas context if your canvas library supports it, or carefully choosing your `checksum_bits` value. The built-in checksum mechanism is designed to handle common anti-aliasing scenarios.
Ensure the color passed to `lookup` is either a hex string (e.g., `'#RRGGBB'`) or an `[r, g, b]` array (e.g., `ImageData.data` sliced to the first three values). Verify that your shadow canvas drawing precisely matches the object's registered color. The internal checksum usually handles minor anti-aliasing discrepancies.
Review the number of objects being tracked. If it exceeds approximately 262,000 (default `checksum_bits=6`), you may need to instantiate `ColorTracker` with a lower `checksum_bits` value (e.g., `new ColorTracker(4)` for ~1 million objects), accepting a slightly higher theoretical risk of checksum collisions.
No dependency data recorded yet.