I'm evaluating Fabric.js as an alternative to Raphael.js for creating interactive visualisations that are compatible with IE8, which doesn't support SVG or canvas (IE8 support is non-negotiable unfortunately).
Raphael can be made to work with the visualisation library D3.js - which outputs SVG and is incompatible with IE8 - via the bridging library D34Raphael, a fork of D3 adapted for use with Raphael. D34Raphael adapts some (but not all) D3 features to output into Raphael's abstracted objects instead of the DOM, so that, on IE8, Raphael can interpret D3's output as VML.
(edit Feb 2014 - D34Raphael seems to be dead now, but there's an awesomely named alternative R2D3 that seems to be under active development)
Fabric can output Canvas on IE8 (using excanvas to convert to VML), and can convert SVG to interactive Canvas elements. So, it seems theoretically possible that Fabric could replace Raphael in bridging D3 and IE8. Doing so would bring the added flexibility of also supporting Canvas functionality as well as SVG.
I've not found any Fabric equivalent to D34Raphael - the closest I can find is this demo page which doesn't work in IE8.
From what I've seen of the Fabric docs, it looks like something like D34Raphael could be attempted for Fabric: it supports converting SVG path, circle, polygon, polyline, ellipse, rect, line, and image elements, and works with abstracted objects that allow for ongoing interactivity. The benchmarks comparing performance of Fabric handling vector paths compared to Raphael handling the same are impressive (although no comparison benchmarks are given that involve interaction or animation).
A couple of typical D3 projects as examples:
I'm sure I'm not the first to have looked into this. I don't much like the idea of launching into trying to implement something like this, only to find there's some unavoidable problem that anyone more experienced with Fabric, Canvas and/or D3 could have pointed out from the start.
What I've tried: Some issues I've already looked into:
Disclaimer: I'm the author of Fabric.js
Very interesting question. To address your points:
Are there any existing projects that allow D3 output to be rendered using fabric.js, similar to D34Raphael?
Not that I know of. But from what I can see, D3.js has SVG output. And Fabric has SVG parser, so it seems pretty straightforward to feed D3's markup to Fabric for rendering.
Is there anything about how D3 works with SVG that simply couldn't be piped through Fabric's SVG to Canvas conversion and object model?
I'm not really familiar with D3, but looking at one of the examples you linked to, I see some compatibility issues indeed. I copied entire SVG markup of "force directed graphs" and loaded it in kitchensink
Circles are rendered properly-ish, but something is off with lines. Curiously, all the lines are parsed and loaded onto canvas correctly. But they're not visible. Why? Because their styles in D3.js are defined via ".line" class and we don't support stylesheet parsing in Fabric.
.link {
stroke: #999;
stroke-opacity: .6;
}
If we were to "unroll" those styles unto each line (either via "stroke" and "stroke-opacity" attributes or style="stroke: ...; stroke-opacity: ...;") it would work as expected.
I imagine the problem with white outlines around circles has the same roots.
Is there any simpler way to push D3 output through Fabric than the D34Raphael approach of forking the D3 project and adapting its output?
Can't think of anything really.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With