Canviz
A souped-up, animated, dataviz oriented canvas for the browser, React,
and nodejs.
WIP.
Features
- Automatic and transparent DPI scaling (
devicePixelRatio
is handled for you) - Normalized, polyfilled cross-browser API
- Optional handy-dandy React component
Coming Soon
- A nifty animation framework
Installation
Via NPM:
npm install canviz
If you use Typescript, typings are
included automatically.
Usage
import { Canvas } from 'canviz';
import { AnimatedCanvas } from 'canviz';
import AnimatedCanvas from 'canviz';
import { CanvasComponent } from 'canviz/react';
import { scales } from 'canviz';
AnimatedCanvas
This is probably the class you want to use. It has a few API extensions
on top of the stock Canvas
you're used to. Details forthcoming, but for
now, take a gander:
Groups
let ac = new AnimatedCanvas(renderer);
function renderer(c) {
c.group('extra').draw(() => {
c.fillStyle = 'red';
c.fillRect(0, 0, c.width, c.height);
});
c.group('default').draw(myStuff);
c.fillStyle = 'blue';
c.fillRect(10, 10, c.width, c.height);
}
ac.render(['extra']);
Regions
This is like a lame version of the hopefully-soon-to-be-implemented "Hit Region"
family of APIs for canvas.
let ac = new AnimatedCanvas(renderer);
function renderer(c) {
c.region('a', 0, 0, 100, 100);
c.translate(50, 50);
c.scale(2, 2);
c.region('b', 0, 0, 100, 100);
}
ac.render();
ac.regions.forEach(r => {
{ name, x, y, w, h } = r;
});
let regions = ac.intersectingRegions(50, 50);
onMouseMoveSomehow((x, y) => {
ac.render(ac.intersectingRegions(x, y));
});
React
If you're using <CanvasComponent>
you get a convenient event handler
that will be called any time the mouse cursor moves within the canvas.
The regions will have been calculated for you as well, and passed through
to your handler, free of charge!
import { CanvasComponent } from 'canviz';
function interactHandler(c, regions) {
}
<CanvasComponent onInteract={interactHandler} />
Animation
NYI. Soon.