All Projects → vasturiano → canvas-color-tracker

vasturiano / canvas-color-tracker

Licence: MIT License
A utility to track objects on a canvas by unique px color

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to canvas-color-tracker

Omg
🎨 一个让你跳过canvas,直接绘图的 2d 绘图库,上手简单,接口简洁,功能丰富.
Stars: ✭ 107 (+268.97%)
Mutual labels:  drawing, canvas
Pencil.js
✏️ Nice modular interactive 2D drawing library
Stars: ✭ 204 (+603.45%)
Mutual labels:  drawing, canvas
Clumsy
A library written on node.js for creating math figures on HTMLCanvas in XKCD style.
Stars: ✭ 107 (+268.97%)
Mutual labels:  drawing, canvas
Notation
✏️ A simple web app to make drawings that are easy to embed, particularly for Notion.so.
Stars: ✭ 63 (+117.24%)
Mutual labels:  drawing, canvas
VIAN
No description or website provided.
Stars: ✭ 18 (-37.93%)
Mutual labels:  color, drawing
Sketchpad
Sketchpad is fully customisable collaborative whiteboard plugin written in pure JavaScript.
Stars: ✭ 85 (+193.1%)
Mutual labels:  drawing, canvas
Drawingboard.js
A canvas based drawing app that you can integrate easily on your website.
Stars: ✭ 2,072 (+7044.83%)
Mutual labels:  drawing, canvas
Angular Canvas Area Draw
Simple library to draw polygons over image with canvas
Stars: ✭ 21 (-27.59%)
Mutual labels:  drawing, canvas
Sketch
Sketch have a lot of basic functions to develop a drawing app for iPhone. Anyone can easily create drawing iOS Application.
Stars: ✭ 229 (+689.66%)
Mutual labels:  color, drawing
Color.js
Extract colors from an image (0.75 KB) 🎨
Stars: ✭ 42 (+44.83%)
Mutual labels:  color, canvas
Mopaint
🎨💪 Modern, modular paint and more! (pre-alpha, not much done yet)
Stars: ✭ 50 (+72.41%)
Mutual labels:  drawing, canvas
Drawing
Drawing and fill color
Stars: ✭ 37 (+27.59%)
Mutual labels:  color, drawing
Literallycanvas
A canvas in your browser. Literally.
Stars: ✭ 1,043 (+3496.55%)
Mutual labels:  drawing, canvas
Pretty Painter
Graphics editor for Android.
Stars: ✭ 105 (+262.07%)
Mutual labels:  drawing, canvas
Minipaint
online image editor
Stars: ✭ 1,014 (+3396.55%)
Mutual labels:  drawing, canvas
Pixelfarm
From Vectors to (sub) Pixels, C# 2D Rendering Library
Stars: ✭ 120 (+313.79%)
Mutual labels:  drawing, canvas
Jspaint
🎨 Classic MS Paint, REVIVED + ✨Extras
Stars: ✭ 5,972 (+20493.1%)
Mutual labels:  drawing, canvas
Signature pad
HTML5 canvas based smooth signature drawing
Stars: ✭ 7,623 (+26186.21%)
Mutual labels:  drawing, canvas
Fast Average Color
🍏🍊🍅 Fast Average Color
Stars: ✭ 531 (+1731.03%)
Mutual labels:  color, canvas
imageman
Image manipulation library. Use Pixie instead.
Stars: ✭ 58 (+100%)
Mutual labels:  color, drawing

canvas-color-tracker

NPM package Build Size NPM Downloads

A utility to track objects on a canvas by unique px color.

When using HTML5 canvas to render elements, we don't have the convenience of readily available mouseover events per object, which makes interaction difficult. canvas-color-tracker provides a system for keeping track of objects in your canvas by indexing them by a unique color, which can be retrieved by determining the 1px color that is directly under the mouse pointer.

This is generally done using a spare/shadow canvas which is not attached to the DOM, but is synchronyzed in terms of object positions with the main canvas. On this shadow canvas we render the objects filled with artificial unique colors that are keys to the object's data, so that by attaching mousemove events to the whole canvas we can determine which objects are being hovered on.

canvas-color-tracker is just the registry part of this process, which generates unique color keys per object and supports addition and retrieval of objects. It also includes a mechanism for validating the color keys using checksum encoding. This is necessary because of pixel antialiasing/smoothing on the boundary of canvas objects, leading into new color mutations which invalidate the object color key lookup.

Check out the canvas examples:

Quick start

import ColorTracker from 'canvas-color-tracker';

or

const ColorTracker = require('canvas-color-tracker');

or even

<script src="//unpkg.com/canvas-color-tracker"></script>

then

const myTracker = new ColorTracker();

const myObject = { ... };
const myObjectColor = myTracker.register(myObject);

// ...

const hoverColor = context.getImageData(x, y, 1, 1).data;
const hoverObject = myTracker.lookup(hoverColor);

API reference

Instantiation

new ColorTracker([checksum_bits])

Creates a new object registry.

The parameter checkum_bits defines how many bits should be used for storing the checksum of the colors. Higher values produce less chance of collisions introduced by anti-aliasing of pixels on object boundaries, which yield artificial erroneous colors. Each bit used for checksum eats away from the maximum size of the registry, as less bits are available for indexing objects. The maximum number of objects that can be stored in the registry is equal to 2^(24-checksum_bits) - 1 (one position is reserved for background). If not provided, checksum_bits takes the default of 6 bits, generating a registry of max size ~262k objects. Normally, you'll only need to override checksum_bits if you wish to store more than this amount of objects.

Methods

register(object)

Adds an object to the registry, and returns a unique color (hex string) that can be used to retrieve the object in the future. Object can be of any type, even primitive values. The color returned encodes the checksum, and will be checked for validity at retrieval time. In case the registry is full and has reached its limit of objects, a value of null is returned, indicating that the object was not stored.

lookup(string or [r, g, b])

Retrieve an object from the registry by its unique color key. The color should be passed either as a plain string such as #23a69c, or an array of 3 octet numbers indicating the color's r, g, b encoding. This array is the same format as returned by the canvas context getImageData method. If the color passes the checksum verification and has a registered object in the registry, it is returned. Otherwise the method returns null.

Giving Back

paypal If this project has helped you and you'd like to contribute back, you can always buy me a !

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].